-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Contracts::Attrs
module containing attribute w/ contracts utilities.
#255
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,20 @@ | ||
module Contracts | ||
module Attrs | ||
def attr_reader_with_contract(*names, contract) | ||
Contract Contracts::None => contract | ||
attr_reader(*names) | ||
end | ||
|
||
def attr_writer_with_contract(*names, contract) | ||
Contract contract => contract | ||
attr_writer(*names) | ||
end | ||
|
||
def attr_accessor_with_contract(*names, contract) | ||
attr_reader_with_contract(*names, contract) | ||
attr_writer_with_contract(*names, contract) | ||
end | ||
end | ||
|
||
include Attrs | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
RSpec.describe "Contracts:" do | ||
describe "Attrs:" do | ||
class Person | ||
include Contracts::Core | ||
include Contracts::Attrs | ||
include Contracts::Builtin | ||
|
||
def initialize(name) | ||
@name_r = name | ||
@name_w = name | ||
@name_rw = name | ||
end | ||
|
||
attr_reader_with_contract :name_r, String | ||
attr_writer_with_contract :name_w, String | ||
attr_accessor_with_contract :name_rw, String | ||
end | ||
|
||
context "attr_reader_with_contract" do | ||
it "getting valid type" do | ||
expect(Person.new("bob").name_r) | ||
.to(eq("bob")) | ||
end | ||
|
||
it "getting invalid type" do | ||
expect { Person.new(1.3).name_r } | ||
.to(raise_error(ReturnContractError)) | ||
end | ||
|
||
it "setting" do | ||
expect { Person.new("bob").name_r = "alice" } | ||
.to(raise_error(NoMethodError)) | ||
end | ||
end | ||
|
||
context "attr_writer_with_contract" do | ||
it "getting" do | ||
expect { Person.new("bob").name_w } | ||
.to(raise_error(NoMethodError)) | ||
end | ||
|
||
it "setting valid type" do | ||
expect(Person.new("bob").name_w = "alice") | ||
.to(eq("alice")) | ||
end | ||
|
||
it "setting invalid type" do | ||
expect { Person.new("bob").name_w = 1.2 } | ||
.to(raise_error(ParamContractError)) | ||
end | ||
end | ||
|
||
context "attr_accessor_with_contract" do | ||
it "getting valid type" do | ||
expect(Person.new("bob").name_rw) | ||
.to(eq("bob")) | ||
end | ||
|
||
it "getting invalid type" do | ||
expect { Person.new(1.2).name_rw } | ||
.to(raise_error(ReturnContractError)) | ||
end | ||
|
||
it "setting valid type" do | ||
expect(Person.new("bob").name_rw = "alice") | ||
.to(eq("alice")) | ||
end | ||
|
||
it "setting invalid type" do | ||
expect { Person.new("bob").name_rw = 1.2 } | ||
.to(raise_error(ParamContractError)) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this is included in place?
Allow people to just include ::Contract & use all methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't remember why i added that line; feel free to remove it