Skip to content

RangeOf[...] implemented #171

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

Merged
merged 5 commits into from
Jul 4, 2015
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 @@ -81,6 +81,7 @@ contracts.ruby comes with a lot of built-in contracts, including the following:
* [`ArrayOf`](http://www.rubydoc.info/gems/contracts/Contracts/ArrayOf) – checks that the argument is an array, and all elements pass the given contract, e.g. `ArrayOf[Num]`
* [`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]`
* [`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]`
* [`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]]`
* [`Optional`](http://www.rubydoc.info/gems/contracts/Contracts/Optional) – checks that the keyword argument is either not present or pass the given contract, can not be used outside of `KeywordArgs` contract, e.g. `Optional[Num]`
* [`Maybe`](http://www.rubydoc.info/gems/contracts/Contracts/Maybe) – passes if the argument is `nil`, or if the given contract passes
Expand Down
18 changes: 18 additions & 0 deletions lib/contracts/builtin_contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,24 @@ def self.valid? val
end
end

# Use this to specify a Range object of a particular datatype.
# Example: <tt>RangeOf[Nat]</tt>, <tt>RangeOf[Date]</tt>, ...
class RangeOf < CallableClass
def initialize(contract)
@contract = contract
end

def valid?(val)
val.is_a?(Range) &&
Contract.valid?(val.first, @contract) &&
Contract.valid?(val.last, @contract)
end

def to_s
"a range of #{@contract}"
end
end

# Use this to specify the Hash characteristics. Takes two contracts,
# one for hash keys and one for hash values.
# Example: <tt>HashOf[Symbol, String]</tt>
Expand Down
39 changes: 39 additions & 0 deletions spec/builtin_contracts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,45 @@
end
end

describe "RangeOf:" do
require "date"
it "should pass for a range of nums" do
expect { @o.first_in_range_num(3..10) }.to_not raise_error
end

it "should pass for a range of dates" do
d1 = Date.today
d2 = d1 + 18
expect { @o.first_in_range_date(d1..d2) }.to_not raise_error
end

it "should fail for a non-range" do
expect { @o.first_in_range_num("foo") }.to raise_error(ContractError)
expect { @o.first_in_range_num(:foo) }.to raise_error(ContractError)
expect { @o.first_in_range_num(5) }.to raise_error(ContractError)
expect { @o.first_in_range_num(nil) }.to raise_error(ContractError)
end

it "should fail for a range with incorrect data type" do
expect { @o.first_in_range_num("a".."z") }.to raise_error(ContractError)
end

it "should fail for a badly-defined range" do
# For some reason, Ruby 2.0.0 allows (date .. number) as a range.
# Perhaps other Ruby versions do too.
# Note that (date .. string) gives ArgumentError.
# This test guards against ranges with inconsistent data types.
begin
d1 = Date.today
expect { @o.first_in_range_date(d1..10).to raise_error(ContractError) }
expect { @o.first_in_range_num(d1..10).to raise_error(ContractError) }
rescue ArgumentError
# If Ruby doesn't like the range, we ignore the test.
:nop
end
end
end

describe "SetOf:" do
it "should pass for a set of nums" do
expect { @o.product_from_set(Set.new([1, 2, 3])) }.to_not raise_error
Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/fixtures.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "date"

class A
include Contracts

Expand Down Expand Up @@ -224,6 +226,16 @@ def product_from_set(vals)
end
end

Contract RangeOf[Num] => Num
def first_in_range_num(r)
r.first
end

Contract RangeOf[Date] => Date
def first_in_range_date(r)
r.first
end

Contract Bool => nil
def bool_test(x)
end
Expand Down