File: args.feature

package info (click to toggle)
ruby-contracts 0.17-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 624 kB
  • sloc: ruby: 3,805; makefile: 4; sh: 2
file content (80 lines) | stat: -rw-r--r-- 1,999 bytes parent folder | download | duplicates (4)
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
Feature: Args

  Used for `*args` (variadic functions). Takes contract and uses it to validate
  every element passed in through `*args`.

  ```ruby
  Contract C::Args[C::Num] => C::Bool
  def example(*args)
  ```

  This example contract will validate all arguments passed through `*args` to
  accept only numbers.

  Background:
    Given a file named "args_usage.rb" with:
    """ruby
    require "contracts"
    C = Contracts

    class Example
      include Contracts::Core

      Contract C::Args[C::Num] => C::Bool
      def only_nums(*args)
        args.inspect
      end
    end
    """

  Scenario: Accepts no arguments
    Given a file named "accepts_no_arguments.rb" with:
    """ruby
    require "./args_usage"
    puts Example.new.only_nums
    """
    When I run `ruby accepts_no_arguments.rb`
    Then the output should contain:
    """
    []
    """

  Scenario: Accepts one valid argument
    Given a file named "accepts_one_argument.rb" with:
    """ruby
    require "./args_usage"
    puts Example.new.only_nums(42)
    """
    When I run `ruby accepts_one_argument.rb`
    Then the output should contain:
    """
    [42]
    """

  Scenario: Accepts many valid arguments
    Given a file named "accepts_many_arguments.rb" with:
    """ruby
    require "./args_usage"
    puts Example.new.only_nums(42, 45, 17, 24)
    """
    When I run `ruby accepts_many_arguments.rb`
    Then the output should contain:
    """
    [42, 45, 17, 24]
    """

  Scenario: Rejects invalid argument
    Given a file named "rejects_invalid_argument.rb" with:
    """ruby
    require "./args_usage"
    puts Example.new.only_nums(42, "foo", 17, 24)
    """
    When I run `ruby rejects_invalid_argument.rb`
    Then the output should contain:
    """
    : Contract violation for argument 1 of 4: (ParamContractError)
            Expected: (Args[Contracts::Builtin::Num]),
            Actual: "foo"
            Value guarded in: Example::only_nums
            With Contract: Args => Bool
    """