-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
Contracts::Attrs
module containing attribute w/ contracts uti…
…lities. (#255) * Add `Attrs` module containing attribute w/ contracts utilities. * Add documentation for new `Attrs` module. * Add tests for new `Attrs` module.
- Loading branch information
1 parent
c408bcc
commit 56c2075
Showing
4 changed files
with
122 additions
and
0 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
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 |