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 116
|
Feature: Pos
Checks that an argument is positive `Numeric`.
```ruby
Contract C::Pos => C::Pos
```
Background:
Given a file named "pos_usage.rb" with:
"""ruby
require "contracts"
C = Contracts
class Example
include Contracts::Core
Contract C::Pos, C::Pos => C::Pos
def power(number, power)
return number if power <= 1
number * self.power(number, power - 1)
end
end
"""
Scenario: Accepts positive integers
Given a file named "accepts_positive_integers.rb" with:
"""ruby
require "./pos_usage"
puts Example.new.power(3, 4)
"""
When I run `ruby accepts_positive_integers.rb`
Then output should contain:
"""
81
"""
Scenario: Accepts positive floats
Given a file named "accepts_positive_floats.rb" with:
"""ruby
require "./pos_usage"
puts Example.new.power(3.7, 4.5)
"""
When I run `ruby accepts_positive_floats.rb`
Then output should contain:
"""
693.4395
"""
Scenario: Rejects negative integers
Given a file named "rejects_negative_integers.rb" with:
"""ruby
require "./pos_usage"
puts Example.new.power(3, -4)
"""
When I run `ruby rejects_negative_integers.rb`
Then output should contain:
"""
: Contract violation for argument 2 of 2: (ParamContractError)
Expected: Pos,
Actual: -4
Value guarded in: Example::power
With Contract: Pos, Pos => Pos
"""
And output should contain "pos_usage.rb:8"
Scenario: Rejects negative floats
Given a file named "rejects_negative_floats.rb" with:
"""ruby
require "./pos_usage"
puts Example.new.power(3.7, -4.4)
"""
When I run `ruby rejects_negative_floats.rb`
Then output should contain:
"""
: Contract violation for argument 2 of 2: (ParamContractError)
Expected: Pos,
Actual: -4.4
Value guarded in: Example::power
With Contract: Pos, Pos => Pos
"""
And output should contain "pos_usage.rb:8"
Scenario: Rejects zero
Given a file named "rejects_zero.rb" with:
"""ruby
require "./pos_usage"
puts Example.new.power(3, 0)
"""
When I run `ruby rejects_zero.rb`
Then output should contain:
"""
: Contract violation for argument 2 of 2: (ParamContractError)
Expected: Pos,
Actual: 0
Value guarded in: Example::power
With Contract: Pos, Pos => Pos
"""
And output should contain "pos_usage.rb:8"
Scenario: Rejects other values
Given a file named "rejects_others.rb" with:
"""ruby
require "./pos_usage"
puts Example.new.power("foo", 2)
"""
When I run `ruby rejects_others.rb`
Then output should contain:
"""
: Contract violation for argument 1 of 2: (ParamContractError)
Expected: Pos,
Actual: "foo"
Value guarded in: Example::power
With Contract: Pos, Pos => Pos
"""
And output should contain "pos_usage.rb:8"
|