1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
Feature: Nat
Checks that an argument is a natural number.
```ruby
Contract C::Nat => C::Nat
```
Background:
Given a file named "nat_usage.rb" with:
"""ruby
require "contracts"
C = Contracts
class Natural
include Contracts::Core
Contract C::Nat => C::Nat
def prev(number)
number - 1
end
end
"""
Scenario: Accepts positive integers
Given a file named "accepts_positive_integers.rb" with:
"""ruby
require "./nat_usage"
puts Natural.new.prev(7)
"""
When I run `ruby accepts_positive_integers.rb`
Then output should contain:
"""
6
"""
Scenario: Accepts zero
Given a file named "accepts_zero.rb" with:
"""ruby
require "./nat_usage"
puts Natural.new.prev(1)
"""
When I run `ruby accepts_zero.rb`
Then output should contain:
"""
0
"""
Scenario: Rejects negative integers
Given a file named "rejects_negative_integers.rb" with:
"""ruby
require "./nat_usage"
puts Natural.new.prev(-1)
"""
When I run `ruby rejects_negative_integers.rb`
Then output should contain:
"""
: Contract violation for argument 1 of 1: (ParamContractError)
Expected: Nat,
Actual: -1
Value guarded in: Natural::prev
With Contract: Nat => Nat
"""
And output should contain "nat_usage.rb:8"
Scenario: Rejects negative integers as a return value
Given a file named "rejects_negative_integers.rb" with:
"""ruby
require "./nat_usage"
puts Natural.new.prev(0)
"""
When I run `ruby rejects_negative_integers.rb`
Then output should contain:
"""
: Contract violation for return value: (ReturnContractError)
Expected: Nat,
Actual: -1
Value guarded in: Natural::prev
With Contract: Nat => Nat
"""
And output should contain "nat_usage.rb:8"
Scenario: Rejects floats
Given a file named "rejects_floats.rb" with:
"""ruby
require "./nat_usage"
puts Natural.new.prev(3.43)
"""
When I run `ruby rejects_floats.rb`
Then output should contain:
"""
: Contract violation for argument 1 of 1: (ParamContractError)
Expected: Nat,
Actual: 3.43
Value guarded in: Natural::prev
With Contract: Nat => Nat
"""
And output should contain "nat_usage.rb:8"
Scenario: Rejects other values
Given a file named "rejects_others.rb" with:
"""ruby
require "./nat_usage"
puts Natural.new.prev("foo")
"""
When I run `ruby rejects_others.rb`
Then output should contain:
"""
: Contract violation for argument 1 of 1: (ParamContractError)
Expected: Nat,
Actual: "foo"
Value guarded in: Natural::prev
With Contract: Nat => Nat
"""
And output should contain "nat_usage.rb:8"
|