File: none.feature

package info (click to toggle)
ruby-contracts 0.17.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: ruby: 3,807; makefile: 4; sh: 2
file content (151 lines) | stat: -rw-r--r-- 3,773 bytes parent folder | download
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Feature: None

  Fails for any argument.

  Often used to explicitly specify that method does not accept any arguments:

  ```ruby
  Contract C::None => Symbol
  def a_symbol
    :a_symbol
  end
  ```

  The same behavior can be achieved when argument list omitted:

  ```ruby
  Contract Symbol
  def a_symbol
    :a_symbol
  end
  ```

  Background:
    Given a file named "helper.rb" with:
    """ruby
    def autorescue
      yield
    rescue => e
      # Since ruby 3.2 the `#inspect` output becomes a bit different
      puts e.inspect.gsub(/^#</, "").gsub(/Error:\"/, "Error: ")
    end
    """
    Given a file named "none_usage.rb" with:
    """ruby
    require "contracts"
    require "./helper"
    C = Contracts

    class Example
      include Contracts::Core

      Contract C::None => Symbol
      def self.a_symbol(*args)
        :a_symbol
      end

      Contract C::None => Symbol
      def a_symbol(*args)
        :a_symbol
      end
    end
    """

  Scenario: Accepts nothing
    Given a file named "nothing.rb" with:
    """ruby
    require "./none_usage"
    puts Example.a_symbol
    """
    When I run `ruby nothing.rb`
    Then output should contain:
    """
    a_symbol
    """

  Scenario: Rejects any argument
    Given a file named "anything.rb" with:
    """ruby
    require "./none_usage"
    autorescue { Example.new.a_symbol(nil) }
    autorescue { Example.new.a_symbol(12) }
    autorescue { Example.new.a_symbol(37.5) }
    autorescue { Example.new.a_symbol("foo") }
    autorescue { Example.new.a_symbol(:foo) }
    autorescue { Example.new.a_symbol({}) }
    autorescue { Example.new.a_symbol([]) }
    autorescue { Example.new.a_symbol(Object) }
    """
    When I run `ruby anything.rb`

    Then output should contain:
    """
    ParamContractError: Contract violation for argument 1 of 1:
            Expected: None,
            Actual: nil
            Value guarded in: Example::a_symbol
            With Contract: None => Symbol
    """

    Then output should contain:
    """
    ParamContractError: Contract violation for argument 1 of 1:
            Expected: None,
            Actual: 12
            Value guarded in: Example::a_symbol
            With Contract: None => Symbol
    """

    Then output should contain:
    """
    ParamContractError: Contract violation for argument 1 of 1:
            Expected: None,
            Actual: 37.5
            Value guarded in: Example::a_symbol
            With Contract: None => Symbol
    """

    Then output should contain:
    """
    ParamContractError: Contract violation for argument 1 of 1:
            Expected: None,
            Actual: "foo"
            Value guarded in: Example::a_symbol
            With Contract: None => Symbol
    """

    Then output should contain:
    """
    ParamContractError: Contract violation for argument 1 of 1:
            Expected: None,
            Actual: :foo
            Value guarded in: Example::a_symbol
            With Contract: None => Symbol
    """

    Then output should contain:
    """
    ParamContractError: Contract violation for argument 1 of 1:
            Expected: None,
            Actual: {}
            Value guarded in: Example::a_symbol
            With Contract: None => Symbol
    """

    Then output should contain:
    """
    ParamContractError: Contract violation for argument 1 of 1:
            Expected: None,
            Actual: []
            Value guarded in: Example::a_symbol
            With Contract: None => Symbol
    """

    Then output should contain:
    """
    ParamContractError: Contract violation for argument 1 of 1:
            Expected: None,
            Actual: Object
            Value guarded in: Example::a_symbol
            With Contract: None => Symbol
    """