summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/matchers/base_spec.rb
blob: 6b5a3fbd7248446ce698b8b885a4b0fc4bad35e0 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require 'spec_helper'
require 'mspec/expectations/expectations'
require 'mspec/matchers'
require 'time'

RSpec.describe SpecPositiveOperatorMatcher, "== operator" do
  it "provides a failure message that 'Expected x to equal y'" do
    expect {
      SpecPositiveOperatorMatcher.new(1) == 2
    }.to raise_error(SpecExpectationNotMetError, "Expected 1 == 2\nto be truthy but was false")
  end

  it "does not raise an exception when == returns true" do
    SpecPositiveOperatorMatcher.new(1) == 1
  end
end

RSpec.describe SpecPositiveOperatorMatcher, "=~ operator" do
  it "provides a failure message that 'Expected \"x\" to match y'" do
    expect {
      SpecPositiveOperatorMatcher.new('real') =~ /fake/
    }.to raise_error(SpecExpectationNotMetError, "Expected \"real\" =~ /fake/\nto be truthy but was nil")
  end

  it "does not raise an exception when =~ returns true" do
    SpecPositiveOperatorMatcher.new('real') =~ /real/
  end
end

RSpec.describe SpecPositiveOperatorMatcher, "> operator" do
  it "provides a failure message that 'Expected x to be greater than y'" do
    expect {
      SpecPositiveOperatorMatcher.new(4) > 5
    }.to raise_error(SpecExpectationNotMetError, "Expected 4 > 5\nto be truthy but was false")
  end

  it "does not raise an exception when > returns true" do
    SpecPositiveOperatorMatcher.new(5) > 4
  end
end

RSpec.describe SpecPositiveOperatorMatcher, ">= operator" do
  it "provides a failure message that 'Expected x to be greater than or equal to y'" do
    expect {
      SpecPositiveOperatorMatcher.new(4) >= 5
    }.to raise_error(SpecExpectationNotMetError, "Expected 4 >= 5\nto be truthy but was false")
  end

  it "does not raise an exception when > returns true" do
    SpecPositiveOperatorMatcher.new(5) >= 4
    SpecPositiveOperatorMatcher.new(5) >= 5
  end
end

RSpec.describe SpecPositiveOperatorMatcher, "< operator" do
  it "provides a failure message that 'Expected x to be less than y'" do
    expect {
      SpecPositiveOperatorMatcher.new(5) < 4
    }.to raise_error(SpecExpectationNotMetError, "Expected 5 < 4\nto be truthy but was false")
  end

  it "does not raise an exception when < returns true" do
    SpecPositiveOperatorMatcher.new(4) < 5
  end
end

RSpec.describe SpecPositiveOperatorMatcher, "<= operator" do
  it "provides a failure message that 'Expected x to be less than or equal to y'" do
    expect {
      SpecPositiveOperatorMatcher.new(5) <= 4
    }.to raise_error(SpecExpectationNotMetError, "Expected 5 <= 4\nto be truthy but was false")
  end

  it "does not raise an exception when < returns true" do
    SpecPositiveOperatorMatcher.new(4) <= 5
    SpecPositiveOperatorMatcher.new(4) <= 4
  end
end

RSpec.describe SpecPositiveOperatorMatcher, "arbitrary predicates" do
  it "do not raise an exception when the predicate is truthy" do
    SpecPositiveOperatorMatcher.new(2).eql?(2)
    SpecPositiveOperatorMatcher.new(2).equal?(2)
    SpecPositiveOperatorMatcher.new([1, 2, 3]).include?(2)
    SpecPositiveOperatorMatcher.new("abc").start_with?("ab")
    SpecPositiveOperatorMatcher.new("abc").start_with?("d", "a")
    SpecPositiveOperatorMatcher.new(3).odd?
    SpecPositiveOperatorMatcher.new([1, 2]).any? { |e| e.even? }
  end

  it "provide a failure message when the predicate returns a falsy value" do
    expect {
      SpecPositiveOperatorMatcher.new(2).eql?(3)
    }.to raise_error(SpecExpectationNotMetError, "Expected 2.eql? 3\nto be truthy but was false")
    expect {
      SpecPositiveOperatorMatcher.new(2).equal?(3)
    }.to raise_error(SpecExpectationNotMetError, "Expected 2.equal? 3\nto be truthy but was false")
    expect {
      SpecPositiveOperatorMatcher.new([1, 2, 3]).include?(4)
    }.to raise_error(SpecExpectationNotMetError, "Expected [1, 2, 3].include? 4\nto be truthy but was false")
    expect {
      SpecPositiveOperatorMatcher.new("abc").start_with?("de")
    }.to raise_error(SpecExpectationNotMetError, "Expected \"abc\".start_with? \"de\"\nto be truthy but was false")
    expect {
      SpecPositiveOperatorMatcher.new("abc").start_with?("d", "e")
    }.to raise_error(SpecExpectationNotMetError, "Expected \"abc\".start_with? \"d\", \"e\"\nto be truthy but was false")
    expect {
      SpecPositiveOperatorMatcher.new(2).odd?
    }.to raise_error(SpecExpectationNotMetError, "Expected 2.odd?\nto be truthy but was false")
    expect {
      SpecPositiveOperatorMatcher.new([1, 3]).any? { |e| e.even? }
    }.to raise_error(SpecExpectationNotMetError, "Expected [1, 3].any? { ... }\nto be truthy but was false")
  end
end

RSpec.describe SpecNegativeOperatorMatcher, "arbitrary predicates" do
  it "do not raise an exception when the predicate returns a falsy value" do
    SpecNegativeOperatorMatcher.new(2).eql?(3)
    SpecNegativeOperatorMatcher.new(2).equal?(3)
    SpecNegativeOperatorMatcher.new([1, 2, 3]).include?(4)
    SpecNegativeOperatorMatcher.new("abc").start_with?("de")
    SpecNegativeOperatorMatcher.new("abc").start_with?("d", "e")
    SpecNegativeOperatorMatcher.new(2).odd?
    SpecNegativeOperatorMatcher.new([1, 3]).any? { |e| e.even? }
  end

  it "provide a failure message when the predicate returns a truthy value" do
    expect {
      SpecNegativeOperatorMatcher.new(2).eql?(2)
    }.to raise_error(SpecExpectationNotMetError, "Expected 2.eql? 2\nto be falsy but was true")
    expect {
      SpecNegativeOperatorMatcher.new(2).equal?(2)
    }.to raise_error(SpecExpectationNotMetError, "Expected 2.equal? 2\nto be falsy but was true")
    expect {
      SpecNegativeOperatorMatcher.new([1, 2, 3]).include?(2)
    }.to raise_error(SpecExpectationNotMetError, "Expected [1, 2, 3].include? 2\nto be falsy but was true")
    expect {
      SpecNegativeOperatorMatcher.new("abc").start_with?("ab")
    }.to raise_error(SpecExpectationNotMetError, "Expected \"abc\".start_with? \"ab\"\nto be falsy but was true")
    expect {
      SpecNegativeOperatorMatcher.new("abc").start_with?("d", "a")
    }.to raise_error(SpecExpectationNotMetError, "Expected \"abc\".start_with? \"d\", \"a\"\nto be falsy but was true")
    expect {
      SpecNegativeOperatorMatcher.new(3).odd?
    }.to raise_error(SpecExpectationNotMetError, "Expected 3.odd?\nto be falsy but was true")
    expect {
      SpecNegativeOperatorMatcher.new([1, 2]).any? { |e| e.even? }
    }.to raise_error(SpecExpectationNotMetError, "Expected [1, 2].any? { ... }\nto be falsy but was true")
  end
end

RSpec.describe SpecNegativeOperatorMatcher, "== operator" do
  it "provides a failure message that 'Expected x not to equal y'" do
    expect {
      SpecNegativeOperatorMatcher.new(1) == 1
    }.to raise_error(SpecExpectationNotMetError, "Expected 1 == 1\nto be falsy but was true")
  end

  it "does not raise an exception when == returns false" do
    SpecNegativeOperatorMatcher.new(1) == 2
  end
end

RSpec.describe SpecNegativeOperatorMatcher, "=~ operator" do
  it "provides a failure message that 'Expected \"x\" not to match /y/'" do
    expect {
      SpecNegativeOperatorMatcher.new('real') =~ /real/
    }.to raise_error(SpecExpectationNotMetError, "Expected \"real\" =~ /real/\nto be falsy but was 0")
  end

  it "does not raise an exception when =~ returns false" do
    SpecNegativeOperatorMatcher.new('real') =~ /fake/
  end
end

RSpec.describe SpecNegativeOperatorMatcher, "< operator" do
  it "provides a failure message that 'Expected x not to be less than y'" do
    expect {
      SpecNegativeOperatorMatcher.new(4) < 5
    }.to raise_error(SpecExpectationNotMetError, "Expected 4 < 5\nto be falsy but was true")
  end

  it "does not raise an exception when < returns false" do
    SpecNegativeOperatorMatcher.new(5) < 4
  end
end

RSpec.describe SpecNegativeOperatorMatcher, "<= operator" do
  it "provides a failure message that 'Expected x not to be less than or equal to y'" do
    expect {
      SpecNegativeOperatorMatcher.new(4) <= 5
    }.to raise_error(SpecExpectationNotMetError, "Expected 4 <= 5\nto be falsy but was true")
    expect {
      SpecNegativeOperatorMatcher.new(5) <= 5
    }.to raise_error(SpecExpectationNotMetError, "Expected 5 <= 5\nto be falsy but was true")
  end

  it "does not raise an exception when <= returns false" do
    SpecNegativeOperatorMatcher.new(5) <= 4
  end
end

RSpec.describe SpecNegativeOperatorMatcher, "> operator" do
  it "provides a failure message that 'Expected x not to be greater than y'" do
    expect {
      SpecNegativeOperatorMatcher.new(5) > 4
    }.to raise_error(SpecExpectationNotMetError, "Expected 5 > 4\nto be falsy but was true")
  end

  it "does not raise an exception when > returns false" do
    SpecNegativeOperatorMatcher.new(4) > 5
  end
end

RSpec.describe SpecNegativeOperatorMatcher, ">= operator" do
  it "provides a failure message that 'Expected x not to be greater than or equal to y'" do
    expect {
      SpecNegativeOperatorMatcher.new(5) >= 4
    }.to raise_error(SpecExpectationNotMetError, "Expected 5 >= 4\nto be falsy but was true")
    expect {
      SpecNegativeOperatorMatcher.new(5) >= 5
    }.to raise_error(SpecExpectationNotMetError, "Expected 5 >= 5\nto be falsy but was true")
  end

  it "does not raise an exception when >= returns false" do
    SpecNegativeOperatorMatcher.new(4) >= 5
  end
end