Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ contracts.ruby comes with a lot of built-in contracts, including the following:
* [`SetOf`](http://www.rubydoc.info/gems/contracts/Contracts/SetOf) – checks that the argument is a set, and all elements pass the given contract, e.g. `SetOf[Num]`
* [`HashOf`](http://www.rubydoc.info/gems/contracts/Contracts/HashOf) – checks that the argument is a hash, and all keys and values pass the given contract, e.g. `HashOf[Symbol => String]` or `HashOf[Symbol,String]`
* [`RangeOf`](http://www.rubydoc.info/gems/contracts/Contracts/RangeOf) – checks that the argument is a range whose elements (#first and #last) pass the given contract, e.g. `RangeOf[Date]`
* [`Enum`](http://www.rubydoc.info/gems/contracts/Contracts/Enum) – checks that the argument is part of a given collection of objects, e.g. `Enum[:a, :b, :c]`

* Keyword arguments
* [`KeywordArgs`](http://www.rubydoc.info/gems/contracts/Contracts/KeywordArgs) – checks that the argument is an options hash, and all required keyword arguments are present, and all values pass their respective contracts, e.g. `KeywordArgs[:number => Num, :description => Optional[String]]`
Expand Down
14 changes: 14 additions & 0 deletions lib/contracts/builtin_contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ def to_s
end
end

# Takes a list of values, e.g. +[:a, :b, :c]+. If argument is included in
# the list, the contract passes.
#
# Example: <tt>Enum[:a, :b, :c]</tt>?
class Enum < CallableClass
def initialize(*vals)
@vals = vals
end

def valid?(val)
@vals.include? val
end
end

# Takes a value +v+. If the argument is +.equal+ to +v+, the contract passes,
# otherwise the contract fails.
# Example: <tt>Eq[Class]</tt>
Expand Down
10 changes: 10 additions & 0 deletions spec/builtin_contracts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@
end
end

describe "Enum:" do
it "should pass for an object that is included" do
expect { @o.enum_test(:a) }.to_not raise_error
end

it "should fail for an object that is not included" do
expect { @o.enum_test(:z) }.to raise_error(ContractError)
end
end

describe "RespondTo:" do
it "should pass for an object that responds to :good" do
expect { @o.responds_test(A.new) }.to_not raise_error
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ def xor_test(x)
def and_test(x)
end

Contract Enum[:a, :b, :c] => nil
def enum_test(x)
end

Contract RespondTo[:good] => nil
def responds_test(x)
end
Expand Down