File: start_and_end_with_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (415 lines) | stat: -rw-r--r-- 13,532 bytes parent folder | download | duplicates (2)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
RSpec.describe "expect(...).to start_with" do
  it_behaves_like "an RSpec value matcher", :valid_value => "ab", :invalid_value => "bc" do
    let(:matcher) { start_with("a") }
  end

  context "with a string" do
    it "passes if it matches the start of the actual string" do
      expect("this string").to start_with "this str"
    end

    it "fails if it does not match the start of the actual string" do
      expect {
        expect("this string").to start_with "that str"
      }.to fail_with("expected \"this string\" to start with \"that str\"")
    end
  end

  context "with an array" do
    it "passes if it is the first element of the array" do
      expect([0, 1, 2]).to start_with 0
    end

    it "passes if the first elements of the array match" do
      expect([0, 1, 2]).to start_with 0, 1
    end

    it "fails if it does not match the first element of the array" do
      expect {
        expect([0, 1, 2]).to start_with 2
      }.to fail_with("expected [0, 1, 2] to start with 2")
    end

    it "fails if it the first elements of the array do not match" do
      expect {
        expect([0, 1, 2]).to start_with 1, 2
      }.to fail_with("expected [0, 1, 2] to start with 1 and 2")
    end
  end

  context "with an array of strings" do
    it "passes if given the first element of the array" do
      expect(%w[ a b c ]).to start_with 'a'
    end

    it "passes if given the first n of the array" do
      expect(%w[ a b c ]).to start_with('a', 'b')
    end

    it 'fails if given the wrong first element of the array' do
      expect {
        expect(%w[ a b c ]).to start_with 'z'
      }.to fail_with('expected ["a", "b", "c"] to start with "z"')
    end
  end

  context "with an array of uncustomized structs" do
    struct = Struct.new(:foo)

    it 'passes if the array ends with a struct equal to the provided struct' do
      s1 = struct.new(5)
      s2 = struct.new(5)
      expect(s1).to eq(s2)

      expect([s1, 10]).to start_with(s2)
    end

    it 'fails if the array ends with a struct not equal to the provided struct' do
      s1 = struct.new(5)
      s2 = struct.new(6)
      expect(s1).not_to eq(s2)

      expect {
        expect([s1, 10]).to start_with(s2)
      }.to fail_including("expected [#{s1.inspect}, 10] to start with #{s2.inspect}")
    end
  end

  context "with an array of structs that have a custom `==` definition" do
    my_struct = Struct.new(:id, :fluff) do
      def ==(other)
        other.is_a?(self.class) && other.id == id
      end
    end

    it 'passes if the array ends with a struct equal to the provided struct' do
      s1 = my_struct.new(1, "foo")
      s2 = my_struct.new(1, "bar")
      expect(s1).to eq(s2)

      expect([s1, 10]).to start_with(s2)
    end

    it 'fails if the array ends with a struct not equal to the provided struct' do
      s1 = my_struct.new(1, "foo")
      s2 = my_struct.new(2, "bar")
      expect(s1).not_to eq(s2)

      expect {
        expect([s1, 10]).to start_with(s2)
      }.to fail_including("expected [#{s1.inspect}, 10] to start with #{s2.inspect}")
    end
  end

  context "with an object that does not respond to :[]" do
    it "fails with a useful message" do
      actual = Object.new
      expect {
        expect(actual).to start_with 0
      }.to fail_with("expected #{actual.inspect} to start with 0, but it cannot be indexed using #[]")
    end
  end

  context "with a hash" do
    it "fails with a useful error if trying to match more than one element" do
      actual   = { :a => 'b', :b => 'b', :c => 'c' }
      expected = { :a => 'b', :b => 'b' }
      expect {
        expect(actual).to start_with(expected)
      }.to fail_with(/\Aexpected #{hash_inspect(actual).gsub(" => ", "=>")} to start with #{hash_inspect(expected).gsub(" => ", "=>")}, but it does not have ordered elements\z/)
    end
  end

  describe "composing with other matchers" do
    it 'passes if the start of an array matches two given matchers' do
      expect([1.01, "food", 3]).to start_with(a_value_within(0.2).of(1), a_string_matching(/foo/))
    end

    it 'passes if the start of an array matches one given matcher' do
      expect([1.01, "food", 3]).to start_with(a_value_within(0.2).of(1))
    end

    it 'provides a description' do
      description = start_with(a_value_within(0.1).of(1), a_string_matching(/abc/)).description
      expect(description).to eq("start with a value within 0.1 of 1 and a string matching /abc/")
    end

    it 'fails with a clear error message when the matchers do not match' do
      expect {
        expect([2.01, "food", 3]).to start_with(a_value_within(0.2).of(1), a_string_matching(/foo/))
      }.to fail_with('expected [2.01, "food", 3] to start with a value within 0.2 of 1 and a string matching /foo/')
    end
  end
end

RSpec.describe "expect(...).not_to start_with" do
  context "with a string" do
    it "passes if it does not match the start of the actual string" do
      expect("this string").not_to start_with "that str"
    end

    it "fails if it does match the start of the actual string" do
      expect {
        expect("this string").not_to start_with "this str"
      }.to fail_with("expected \"this string\" not to start with \"this str\"")
    end
  end

  context "with an array" do
    it "passes if it is not the first element of the array" do
      expect([0, 1, 2]).not_to start_with 2
    end

    it "passes if the first elements of the array do not match" do
      expect([0, 1, 2]).not_to start_with 1, 2
    end

    it "fails if it matches the first element of the array" do
      expect {
        expect([0, 1, 2]).not_to start_with 0
      }.to fail_with("expected [0, 1, 2] not to start with 0")
    end

    it "fails if it the first elements of the array match" do
      expect {
        expect([0, 1, 2]).not_to start_with 0, 1
      }.to fail_with("expected [0, 1, 2] not to start with 0 and 1")
    end
  end

  context "with an array of strings" do
    it "fails if given the first element of the array" do
      expect {
        expect(%w[ a b c ]).not_to start_with 'a'
      }.to fail_with('expected ["a", "b", "c"] not to start with "a"')
    end

    it "fails if given the first n of the array" do
      expect {
        expect(%w[ a b c ]).not_to start_with('a', 'b')
      }.to fail_with('expected ["a", "b", "c"] not to start with "a" and "b"')
    end

    it 'passes if given the wrong first element of the array' do
      expect(%w[ a b c ]).not_to start_with 'z'
    end
  end

  it 'can pass when composed with another matcher' do
    expect(["a"]).not_to start_with(a_string_matching(/bar/))
  end

  it 'can fail when composed with another matcher' do
    expect {
      expect(["a"]).not_to start_with(a_string_matching(/a/))
    }.to fail_with('expected ["a"] not to start with a string matching /a/')
  end
end

RSpec.describe "expect(...).to end_with" do
  it_behaves_like "an RSpec value matcher", :valid_value => "ab", :invalid_value => "bc" do
    let(:matcher) { end_with("b") }
  end

  context "with a string" do
    it "passes if it matches the end of the actual string" do
      expect("this string").to end_with "is string"
    end

    it "fails if it does not match the end of the actual string" do
      expect {
        expect("this string").to end_with "is stringy"
      }.to fail_with("expected \"this string\" to end with \"is stringy\"")
    end
  end

  context "with an array" do
    it "passes if it is the last element of the array" do
      expect([0, 1, 2]).to end_with 2
    end

    it "passes if the last elements of the array match" do
      expect([0, 1, 2]).to end_with [1, 2]
    end

    it "fails if it does not match the last element of the array" do
      expect {
        expect([0, 1, 2]).to end_with 1
      }.to fail_with("expected [0, 1, 2] to end with 1")
    end

    it "fails if it the last elements of the array do not match" do
      expect {
        expect([0, 1, 2]).to end_with [0, 1]
      }.to fail_with("expected [0, 1, 2] to end with 0 and 1")
    end
  end

  context "with an array of strings" do
    it "passes if given the last element of the array" do
      expect(%w[ a b c ]).to end_with 'c'
    end

    it "passes if given the last n of the array" do
      expect(%w[ a b c ]).to end_with('b', 'c')
    end

    it 'fails if given the wrong last element of the array' do
      expect {
        expect(%w[ a b c ]).to end_with 'z'
      }.to fail_with('expected ["a", "b", "c"] to end with "z"')
    end
  end

  context "with an array of uncustomized structs" do
    struct = Struct.new(:foo)

    it 'passes if the array ends with a struct equal to the provided struct' do
      s1 = struct.new(5)
      s2 = struct.new(5)
      expect(s1).to eq(s2)

      expect([10, s1]).to end_with(s2)
    end

    it 'fails if the array ends with a struct not equal to the provided struct' do
      s1 = struct.new(5)
      s2 = struct.new(6)
      expect(s1).not_to eq(s2)

      expect {
        expect([10, s1]).to end_with(s2)
      }.to fail_including("expected [10, #{s1.inspect}] to end with #{s2.inspect}")
    end
  end

  context "with an array of structs that have a custom `==` definition" do
    my_struct = Struct.new(:id, :fluff) do
      def ==(other)
        other.is_a?(self.class) && other.id == id
      end
    end

    it 'passes if the array ends with a struct equal to the provided struct' do
      s1 = my_struct.new(1, "foo")
      s2 = my_struct.new(1, "bar")
      expect(s1).to eq(s2)

      expect([10, s1]).to end_with(s2)
    end

    it 'fails if the array ends with a struct not equal to the provided struct' do
      s1 = my_struct.new(1, "foo")
      s2 = my_struct.new(2, "bar")
      expect(s1).not_to eq(s2)

      expect {
        expect([10, s1]).to end_with(s2)
      }.to fail_including("expected [10, #{s1.inspect}] to end with #{s2.inspect}")
    end
  end

  context "with an object that does not respond to :[]" do
    it "fails with a useful message" do
      actual = Object.new
      expect {
        expect(actual).to end_with 0
      }.to fail_with("expected #{actual.inspect} to end with 0, but it cannot be indexed using #[]")
    end
  end

  context "with a hash" do
    it "raises an ArgumentError if trying to match more than one element" do
      actual   = { :a => 'b', :b => 'b', :c => 'c' }
      expected = { :a => 'b', :b => 'b' }
      expect {
        expect(actual).to end_with(expected)
      }.to fail_with(/\Aexpected #{hash_inspect(actual).gsub(" => ", "=>")} to end with #{hash_inspect(expected).gsub(" => ", "=>")}, but it does not have ordered elements\z/)
    end
  end

  describe "composing with other matchers" do
    it 'passes if the end of an array matches two given matchers' do
      expect([3, "food", 1.1]).to end_with(a_string_matching(/foo/), a_value_within(0.2).of(1))
    end

    it 'passes if the end of an array matches one given matcher' do
      expect([3, "food", 1.1]).to end_with(a_value_within(0.2).of(1))
    end

    it 'provides a description' do
      description = end_with(a_value_within(0.1).of(1), a_string_matching(/abc/)).description
      expect(description).to eq("end with a value within 0.1 of 1 and a string matching /abc/")
    end

    it 'fails with a clear error message when the matchers do not match' do
      expect {
        expect([2.01, 3, "food"]).to end_with(a_value_within(0.2).of(1), a_string_matching(/foo/))
      }.to fail_with('expected [2.01, 3, "food"] to end with a value within 0.2 of 1 and a string matching /foo/')
    end
  end
end

RSpec.describe "expect(...).not_to end_with" do
  context "with a sting" do
    it "passes if it does not match the end of the actual string" do
      expect("this string").not_to end_with "stringy"
    end

    it "fails if it matches the end of the actual string" do
      expect {
        expect("this string").not_to end_with "string"
      }.to fail_with("expected \"this string\" not to end with \"string\"")
    end
  end

  context "an array" do
    it "passes if it is not the last element of the array" do
      expect([0, 1, 2]).not_to end_with 1
    end

    it "passes if the last elements of the array do not match" do
      expect([0, 1, 2]).not_to end_with [0, 1]
    end

    it "fails if it matches the last element of the array" do
      expect {
        expect([0, 1, 2]).not_to end_with 2
      }.to fail_with("expected [0, 1, 2] not to end with 2")
    end

    it "fails if it the last elements of the array match" do
      expect {
        expect([0, 1, 2]).not_to end_with [1, 2]
      }.to fail_with("expected [0, 1, 2] not to end with 1 and 2")
    end
  end

  context "with an array of strings" do
    it "fails if given the last element of the array" do
      expect {
        expect(%w[ a b c ]).not_to end_with 'c'
      }.to fail_with('expected ["a", "b", "c"] not to end with "c"')
    end

    it "fails if given the last n of the array" do
      expect {
        expect(%w[ a b c ]).not_to end_with('b', 'c')
      }.to fail_with('expected ["a", "b", "c"] not to end with "b" and "c"')
    end

    it 'passes if given the wrong last element of the array' do
      expect(%w[ a b c ]).not_to end_with 'z'
    end
  end

  it 'can pass when composed with another matcher' do
    expect(["a"]).not_to end_with(a_string_matching(/bar/))
  end

  it 'can fail when composed with another matcher' do
    expect {
      expect(["a"]).not_to end_with(a_string_matching(/a/))
    }.to fail_with('expected ["a"] not to end with a string matching /a/')
  end
end