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
|
Feature: KeywordArgs when used with optional positional arguments
Checks that the argument is an options hash, and all required keyword arguments are present, and all values pass their respective contracts
```ruby
Contract Any, KeywordArgs[:number => Num, :description => Optional[String]] => Any
```
Background:
Given a file named "keyword_args_with_optional_positional_args_usage.rb" with:
"""ruby
require "contracts"
C = Contracts
class Example
include Contracts::Core
Contract C::Any, String, C::KeywordArgs[b: C::Optional[String]] => Symbol
def foo(output, a = 'a', b: 'b')
p [a, b]
output
end
end
"""
Scenario: Accepts arguments when only require arguments filled and valid
Given a file named "accepts_all_filled_valid_args.rb" with:
"""ruby
require "./keyword_args_with_optional_positional_args_usage"
puts Example.new.foo(:output)
"""
When I run `ruby accepts_all_filled_valid_args.rb`
Then output should contain:
"""
["a", "b"]
output
"""
Scenario: Accepts arguments when all filled and valid
Given a file named "accepts_all_filled_valid_args.rb" with:
"""ruby
require "./keyword_args_with_optional_positional_args_usage"
puts Example.new.foo(:output, 'c', b: 'd')
"""
When I run `ruby accepts_all_filled_valid_args.rb`
Then output should contain:
"""
["c", "d"]
output
"""
Scenario: Accepts arguments when only require arguments & optional keyword arguments filled and valid
Given a file named "accepts_all_filled_valid_args.rb" with:
"""ruby
require "./keyword_args_with_optional_positional_args_usage"
puts Example.new.foo(:output, b: 'd')
"""
When I run `ruby accepts_all_filled_valid_args.rb`
Then output should contain:
"""
["a", "d"]
output
"""
Scenario: Accepts arguments when only require arguments & optional positional arguments filled and valid
Given a file named "accepts_all_filled_valid_args.rb" with:
"""ruby
require "./keyword_args_with_optional_positional_args_usage"
puts Example.new.foo(:output, 'c')
"""
When I run `ruby accepts_all_filled_valid_args.rb`
Then output should contain:
"""
["c", "b"]
output
"""
|