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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
Feature: Method Overloading
You can use contracts for method overloading! This is commonly called "pattern matching" in functional programming languages.
```ruby
Contract 1 => 1
def fact x
x
end
Contract C::Num => C::Num
def fact x
x * fact(x - 1)
end
```
Background:
Given a file named "method_overloading_with_positional_args_usage.rb" with:
"""ruby
require "contracts"
C = Contracts
class Example
include Contracts::Core
Contract 1 => 1
def fact(x)
x
end
Contract C::Num => C::Num
def fact(x)
x * fact(x - 1)
end
end
"""
Given a file named "method_overloading_with_keyword_args_usage.rb" with:
"""ruby
require "contracts"
C = Contracts
class Example
include Contracts::Core
Contract C::KeywordArgs[age: Integer, size: Symbol] => String
def speak(age:, size:)
"age: #{age} size: #{size}"
end
Contract C::KeywordArgs[sound: String] => String
def speak(sound:)
"sound: #{sound}"
end
end
"""
Scenario: Positional Args Method 1
Given a file named "positional_args_method_1.rb" with:
"""ruby
require "./method_overloading_with_positional_args_usage"
puts Example.new.fact(1)
"""
When I run `ruby positional_args_method_1.rb`
Then the output should contain:
"""
1
"""
Scenario: Positional Args Method 2
Given a file named "positional_args_method_2.rb" with:
"""ruby
require "./method_overloading_with_positional_args_usage"
puts Example.new.fact(4)
"""
When I run `ruby positional_args_method_2.rb`
Then the output should contain:
"""
24
"""
Scenario: Keyword Args Method 1
Given a file named "keyword_args_method_1.rb" with:
"""ruby
require "./method_overloading_with_keyword_args_usage"
puts Example.new.speak(age: 5, size: :large)
"""
When I run `ruby keyword_args_method_1.rb`
Then the output should contain:
"""
age: 5 size: large
"""
Scenario: Keyword Args Method 2
Given a file named "keyword_args_method_2.rb" with:
"""ruby
require "./method_overloading_with_keyword_args_usage"
puts Example.new.speak(sound: "woof")
"""
When I run `ruby keyword_args_method_2.rb`
Then the output should contain:
"""
sound: woof
"""
Scenario: Incorrect Positional Args Method
Given a file named "incorrect_positional_args_method.rb" with:
"""ruby
require "contracts"
C = Contracts
class Example
include Contracts::Core
# Notice that this method's contract is wider than the one below
# This would cause this method to be called every time but never the one below
Contract C::Num => C::Num
def fact(x)
x * fact(x - 1)
end
Contract 1 => 1
def fact(x)
x
end
end
puts Example.new.fact(4)
"""
When I run `ruby incorrect_positional_args_method.rb`
Then the output should contain:
"""
stack level too deep (SystemStackError)
"""
|