-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from davidrunger/add-method-shape
Add a `Method` shape
- Loading branch information
Showing
7 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ Validate the "shape" of Ruby objects! | |
* [Shaped::Shapes::Array](#shapedshapesarray) | ||
* [Shaped::Shapes::Class](#shapedshapesclass) | ||
* [ActiveModel validations](#activemodel-validations) | ||
* [Shaped::Shapes::Method](#shapedshapesmethod) | ||
* [Shaped::Shapes::Callable](#shapedshapescallable) | ||
* [Shaped::Shapes::Equality](#shapedshapesequality) | ||
* [Shaped::Shapes::Any](#shapedshapesany) | ||
|
@@ -30,7 +31,7 @@ Validate the "shape" of Ruby objects! | |
* [For maintainers](#for-maintainers) | ||
* [License](#license) | ||
|
||
<!-- Added by: david, at: Wed Jun 24 13:23:10 PDT 2020 --> | ||
<!-- Added by: david, at: Wed Jun 24 13:56:49 PDT 2020 --> | ||
|
||
<!--te--> | ||
|
||
|
@@ -205,6 +206,21 @@ shape.matched_by?('[email protected]') # too short | |
# => false | ||
``` | ||
|
||
## Shaped::Shapes::Method | ||
|
||
This shape allows specifying a method name that, when called upon a test object, must return a | ||
truthy value in order for `matched_by?` to be true. | ||
|
||
```rb | ||
shape = Shaped::Shape(:odd?) | ||
|
||
shape.matched_by?(55) | ||
# => true | ||
|
||
shape.matched_by?(60) | ||
# => false | ||
``` | ||
|
||
## Shaped::Shapes::Callable | ||
|
||
This shape is very powerful if you need a very customized shape definition; you can define any | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class Shaped::Shapes::Method < Shaped::Shape | ||
def initialize(method_name) | ||
@method_name = method_name | ||
end | ||
|
||
def matched_by?(object) | ||
!!object.public_send(@method_name) | ||
end | ||
|
||
def to_s | ||
"object returning truthy for ##{@method_name}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Shaped::Shapes::Method do | ||
subject(:method_shape) { Shaped::Shapes::Method.new(method_shape_description) } | ||
|
||
let(:method_shape_description) { :even? } | ||
let(:test_object) { 64 } | ||
|
||
describe '#initialize' do | ||
it 'does not raise an error' do | ||
expect { method_shape }.not_to raise_error | ||
end | ||
end | ||
|
||
describe '#matched_by?' do | ||
subject(:matched_by?) { method_shape.matched_by?(test_object) } | ||
|
||
context 'when the test object returns a truthy value when called with the specified method' do | ||
before { expect(test_object).to be_even } | ||
|
||
it 'returns true' do | ||
expect(matched_by?).to eq(true) | ||
end | ||
end | ||
|
||
context 'when the test object returns a falsy value when called with the specified method' do | ||
before { expect(test_object).not_to be_even } | ||
|
||
let(:test_object) { 71 } | ||
|
||
it 'returns false' do | ||
expect(matched_by?).to eq(false) | ||
end | ||
end | ||
end | ||
|
||
describe '#to_s' do | ||
subject(:to_s) { method_shape.to_s } | ||
|
||
it 'returns a string naming the method that must be matched' do | ||
expect(to_s).to eq('object returning truthy for #even?') | ||
end | ||
end | ||
end |