Debian Codegraph @

rspec-expectations/spec/rspec/matchers/built_in/contain_exactly_spec.rb

raw symbols SCIP
1 class UnsortableObject
2 def initialize(id)
3 @id = id
4 end
5
6 def inspect
7 @id.to_s
8 end
9
10 def ==(_other)
11 false
12 end
13 end
14
15 # AR::Relation is meant to act like a collection, but does
16 # not include `Enumerable`. It does implement `to_ary`.
17 class FakeActiveRecordRelation
18 def initialize(records)
19 @records = records
20 end
21
22 def to_ary
23 @records
24 end
25 end
26
27 RSpec.describe "should =~ array", :uses_should do
28 it "passes a valid positive expectation" do
29 [1, 2].should =~ [2, 1]
30 end
31
32 it "fails an invalid positive expectation" do
33 expect {
34 [1, 2, 3].should =~ [2, 1]
35 }.to fail_with(/expected collection contained/)
36 end
37
38 context "when the array defines a `=~` method" do
39 it 'delegates to that method rather than using the contain_exactly matcher' do
40 array = []
41 def array.=~(other)
42 other == :foo
43 end
44
45 array.should =~ :foo
46 expect {
47 array.should =~ :bar
48 }.to fail_with(/expected: :bar/)
49 end
50 end
51
52 context 'when the array defines a `send` method' do
53 it 'still works' do
54 array = [1, 2]
55 def array.send; :sent; end
56
57 array.should =~ array
58 end
59 end
60
61 context "when the array undefines `=~`" do
62 it 'still works' do
63 array_klass = Class.new(Array) { undef =~ if respond_to?(:=~) }
64 array = array_klass.new([1, 2])
65
66 array.should =~ [1, 2]
67
68 expect {
69 array.should =~ [0, 1, 2]
70 }.to fail_with(/expected collection contained/)
71 end
72 end
73 end
74
75 RSpec.describe "should_not =~ [:with, :multiple, :args]", :uses_should do
76 it "fails when the arrays match" do
77 expect {
78 [1, 2, 3].should_not =~ [1, 2, 3]
79 }.to fail_with "expected [1, 2, 3] not to contain exactly 1, 2, and 3"
80 end
81
82 it "fails when the arrays match in a different order" do
83 expect {
84 [1, 3, 2].should_not =~ [1, 2, 3]
85 }.to fail_with "expected [1, 3, 2] not to contain exactly 1, 2, and 3"
86 end
87
88 it "passes when there are extra elements in the array" do
89 [1, 3].should_not =~ [1, 2, 3]
90 end
91
92 it "passes when there are elements missing from the array" do
93 [1, 2, 3, 4].should_not =~ [1, 2, 3]
94 end
95 end
96
97 RSpec.describe "using contain_exactly with expect" do
98 it "passes a valid positive expectation" do
99 expect([1, 2]).to contain_exactly(2, 1)
100 end
101
102 it "fails an invalid positive expectation" do
103 expect {
104 expect([1, 2, 3]).to contain_exactly(2, 1)
105 }.to fail_with(/expected collection contained/)
106 end
107
108 it "passes for an out of order valid positive expectation with hashes" do
109 expect([
110 { :a => 10 },
111 { :a => -10 }
112 ]).to contain_exactly(
113 { :a => (a_value < 0) },
114 { :a => (a_value > 0) }
115 )
116 end
117
118 it "passes for an in order valid positive expectation with hashes" do
119 expect([
120 { :a => 10 },
121 { :a => -10 }
122 ]).to contain_exactly(
123 { :a => (a_value > 0) },
124 { :a => (a_value < 0) }
125 )
126 end
127
128 it 'works with strict test doubles (which have not defined `<=>`)' do
129 dbl_1 = double("1")
130 dbl_2 = double("2")
131
132 expect([dbl_1, dbl_2]).to contain_exactly(dbl_2, dbl_1)
133
134 expect {
135 expect([dbl_1, dbl_2]).to contain_exactly(dbl_1)
136 }.to fail
137 end
138
139 it "does not support strings (which have no standard way to be enumerated: bytes, chars or lines)" do
140 expect {
141 expect("abcd").to contain_exactly("d", "c", "a", "b")
142 }.to fail_with('expected a collection that can be converted to an array with `#to_ary` or `#to_a`, but got "abcd"')
143 end
144
145 it "supports ranges" do
146 expect(1..3).to contain_exactly(1, 2, 3)
147 end
148 end
149
150 RSpec.describe "expect(array).to contain_exactly(*other_array)" do
151 it_behaves_like "an RSpec value matcher", :valid_value => [1, 2], :invalid_value => [1] do
152 let(:matcher) { contain_exactly(2, 1) }
153 end
154
155 it 'is also exposed as `match_array` (with unsplatted args)' do
156 expect([1, 2, 3]).to match_array([3, 2, 1])
157 end
158
159 it "passes if target contains all items" do
160 expect([1, 2, 3]).to contain_exactly(1, 2, 3)
161 end
162
163 it "passes if target contains all items out of order" do
164 expect([1, 3, 2]).to contain_exactly(1, 2, 3)
165 end
166
167 it 'fails if the expected array is empty and the actual array is non-empty' do
168 expect {
169 expect([1]).to contain_exactly # no arguments passed to the matcher
170 }.to fail_with(<<-MESSAGE)
171 expected collection contained: []
172 actual collection contained: [1]
173 the extra elements were: [1]
174 MESSAGE
175 end
176
177 it 'fails if the actual array is empty and the expected array is non-empty' do
178 expect {
179 expect([]).to contain_exactly(1)
180 }.to fail_with(<<-MESSAGE)
181 expected collection contained: [1]
182 actual collection contained: []
183 the missing elements were: [1]
184 MESSAGE
185 end
186
187 def timeout_if_not_debugging(time)
188 in_sub_process_if_possible do
189 require 'timeout'
190 return yield if defined?(::Debugger)
191 Timeout.timeout(time) { yield }
192 end
193 end
194
195 it 'fails a match of 11 items with duplicates in a reasonable amount of time' do
196 timeout_if_not_debugging(0.1) do
197 expected = [0, 1, 1, 3, 3, 3, 4, 4, 8, 8, 9 ]
198 actual = [ 1, 2, 3, 3, 3, 3, 7, 8, 8, 9, 9]
199
200 expect {
201 expect(actual).to contain_exactly(*expected)
202 }.to fail_including("the missing elements were: [0, 1, 4, 4]")
203 end
204 end
205
206 it "fails if target includes extra items" do
207 expect {
208 expect([1, 2, 3, 4]).to contain_exactly(1, 2, 3)
209 }.to fail_with(<<-MESSAGE)
210 expected collection contained: [1, 2, 3]
211 actual collection contained: [1, 2, 3, 4]
212 the extra elements were: [4]
213 MESSAGE
214 end
215
216 it "fails if target is missing items" do
217 expect {
218 expect([1, 2]).to contain_exactly(1, 2, 3)
219 }.to fail_with(<<-MESSAGE)
220 expected collection contained: [1, 2, 3]
221 actual collection contained: [1, 2]
222 the missing elements were: [3]
223 MESSAGE
224 end
225
226 it "fails if target is missing items and has extra items" do
227 expect {
228 expect([1, 2, 4]).to contain_exactly(1, 2, 3)
229 }.to fail_with(<<-MESSAGE)
230 expected collection contained: [1, 2, 3]
231 actual collection contained: [1, 2, 4]
232 the missing elements were: [3]
233 the extra elements were: [4]
234 MESSAGE
235 end
236
237 it "sorts items in the error message if they all respond to <=>" do
238 expect {
239 expect([6, 2, 1, 5]).to contain_exactly(4, 1, 2, 3)
240 }.to fail_with(<<-MESSAGE)
241 expected collection contained: [1, 2, 3, 4]
242 actual collection contained: [1, 2, 5, 6]
243 the missing elements were: [3, 4]
244 the extra elements were: [5, 6]
245 MESSAGE
246 end
247
248 it "does not sort items in the error message if they don't all respond to <=>" do
249 expect {
250 expect([UnsortableObject.new(2), UnsortableObject.new(1)]).to contain_exactly(UnsortableObject.new(4), UnsortableObject.new(3))
251 }.to fail_with(<<-MESSAGE)
252 expected collection contained: [4, 3]
253 actual collection contained: [2, 1]
254 the missing elements were: [4, 3]
255 the extra elements were: [2, 1]
256 MESSAGE
257 end
258
259 it "accurately reports extra elements when there are duplicates" do
260 expect {
261 expect([1, 1, 1, 5]).to contain_exactly(1, 5)
262 }.to fail_with(<<-MESSAGE)
263 expected collection contained: [1, 5]
264 actual collection contained: [1, 1, 1, 5]
265 the extra elements were: [1, 1]
266 MESSAGE
267 end
268
269 it "accurately reports missing elements when there are duplicates" do
270 expect {
271 expect([1, 5]).to contain_exactly(1, 1, 5)
272 }.to fail_with(<<-MESSAGE)
273 expected collection contained: [1, 1, 5]
274 actual collection contained: [1, 5]
275 the missing elements were: [1]
276 MESSAGE
277 end
278 end
279
280 RSpec.describe "expect(...).not_to contain_exactly(:with, :multiple, :args)" do
281 it "fails when the arrays match" do
282 expect {
283 expect([1, 2, 3]).not_to contain_exactly(1, 2, 3)
284 }.to fail_with "expected [1, 2, 3] not to contain exactly 1, 2, and 3"
285 end
286
287 it "fails when the arrays match in a different order" do
288 expect {
289 expect([1, 3, 2]).not_to contain_exactly(1, 2, 3)
290 }.to fail_with "expected [1, 3, 2] not to contain exactly 1, 2, and 3"
291 end
292
293 it "passes when there are extra elements in the array" do
294 expect([1, 3]).not_to contain_exactly(1, 2, 3)
295 end
296
297 it "passes when there are elements missing from the array" do
298 expect([1, 2, 3, 4]).not_to contain_exactly(1, 2, 3)
299 end
300 end
301
302 RSpec.describe "matching against things that aren't arrays" do
303 it "fails with nil and the expected error message is given" do
304 expect {
305 expect(nil).to contain_exactly(1, 2, 3)
306 }.to fail_with(/expected a collection/)
307 end
308
309 it "fails with a float and the expected error message is given" do
310 expect {
311 expect(3.7).to contain_exactly(1, 2, 3)
312 }.to fail_with(/expected a collection/)
313 end
314
315 it "fails with a string and the expected error message is given" do
316 expect {
317 expect("I like turtles").to contain_exactly(1, 2, 3)
318 }.to fail_with(/expected a collection/)
319 end
320
321 it 'works with other collection objects' do
322 in_sub_process_if_possible do
323 require 'set'
324 expect(Set.new([3, 2, 1])).to contain_exactly(1, 2, 3)
325 expect {
326 expect(Set.new([3, 2, 1])).to contain_exactly(1, 2)
327 }.to fail_including("expected collection contained: [1, 2]")
328 end
329 end
330
331 it 'works with non-enumerables that implement `to_ary`' do
332 relation = FakeActiveRecordRelation.new([1, 2, 3])
333 expect(relation).to contain_exactly(2, 1, 3)
334 expect {
335 expect(relation).to contain_exactly(1, 2)
336 }.to fail_including("expected collection contained: [1, 2]")
337 end
338 end
339
340 RSpec.describe "Composing `contain_exactly` with other matchers" do
341 context "when it is compared to multiple possible matches" do
342 it 'works properly when passed as an argument in its aliased form' do
343 expect([[1, 3], ["food", "barn"]]).to include(
344 a_collection_containing_exactly(/foo/, /bar/)
345 )
346 end
347 end
348
349 describe "expect(...).to contain_exactly(matcher, matcher)" do
350 it 'passes when the array matches the matchers in the same order' do
351 expect(["food", "barn"]).to contain_exactly(
352 a_string_matching(/foo/),
353 a_string_matching(/bar/)
354 )
355 end
356
357 it 'passes when the array matches the matchers in a different order' do
358 expect(["food", "barn"]).to contain_exactly(
359 a_string_matching(/bar/),
360 a_string_matching(/foo/)
361 )
362 end
363
364 it 'fails with a useful message when there is an extra element' do
365 expect {
366 expect(["food", "barn", "goo"]).to contain_exactly(
367 a_string_matching(/bar/),
368 a_string_matching(/foo/)
369 )
370 }.to fail_with(dedent <<-EOS)
371 |expected collection contained: [(a string matching /bar/), (a string matching /foo/)]
372 |actual collection contained: ["barn", "food", "goo"]
373 |the extra elements were: ["goo"]
374 |
375 EOS
376 end
377
378 it 'fails with a useful message when there is a missing element' do
379 expect {
380 expect(["food", "barn"]).to contain_exactly(
381 a_string_matching(/bar/),
382 a_string_matching(/foo/),
383 a_string_matching(/goo/)
384 )
385 }.to fail_with(dedent <<-EOS)
386 |expected collection contained: [(a string matching /bar/), (a string matching /foo/), (a string matching /goo/)]
387 |actual collection contained: ["barn", "food"]
388 |the missing elements were: [(a string matching /goo/)]
389 |
390 EOS
391 end
392
393 it 'pairs up the items in order to minimize the number of unpaired items' do
394 expect {
395 expect(["fool", "food", "good"]).to contain_exactly(/foo/, /fool/, /poo/)
396 }.to fail_with(dedent <<-EOS)
397 |expected collection contained: [/foo/, /fool/, /poo/]
398 |actual collection contained: ["food", "fool", "good"]
399 |the missing elements were: [/poo/]
400 |the extra elements were: ["good"]
401 |
402 EOS
403 end
404
405 it 'provides a description' do
406 description = contain_exactly(a_string_matching(/bar/), a_string_matching(/foo/)).description
407 expect(description).to eq("contain exactly (a string matching /bar/) and (a string matching /foo/)")
408 end
409
410 context 'when an earlier matcher matches more strictly than a later matcher' do
411 it 'works when the actual items match in the same order' do
412 expect(["food", "fool"]).to contain_exactly(a_string_matching(/foo/), a_string_matching(/fool/))
413 end
414
415 it 'works when the actual items match in reverse order' do
416 expect(["fool", "food"]).to contain_exactly(a_string_matching(/foo/), a_string_matching(/fool/))
417 end
418
419 it 'can handle multiple sets of overlapping matches' do
420 expect(["fool", "barn", "bare", "food"]).to contain_exactly(
421 a_string_matching(/bar/),
422 a_string_matching(/barn/),
423 a_string_matching(/foo/),
424 a_string_matching(/fool/)
425 )
426 end
427 end
428
429 it "can use `a_value_within` and `a_string_starting_with` against multiple types of values" do
430 expect(["barn", 2.45]).to contain_exactly(
431 a_value_within(0.1).of(2.5),
432 a_string_starting_with("bar")
433 )
434 end
435
436 context 'when a later matcher matches more strictly than an earlier matcher' do
437 it 'works when the actual items match in the same order' do
438 expect(["fool", "food"]).to contain_exactly(a_string_matching(/fool/), a_string_matching(/foo/))
439 end
440
441 it 'works when the actual items match in reverse order' do
442 expect(["food", "fool"]).to contain_exactly(a_string_matching(/fool/), a_string_matching(/foo/))
443 end
444 end
445 end
446
447 describe "expect(...).to_not contain_exactly(matcher, matcher)" do
448 it 'fails when the array matches the matchers' do
449 expect {
450 expect(["food", "barn"]).to_not contain_exactly(
451 a_string_matching(/bar/),
452 a_string_matching(/foo/)
453 )
454 }.to fail_with 'expected ["food", "barn"] not to contain exactly '\
455 "(a string matching /bar/) and (a string matching /foo/)"
456 end
457
458 it 'passes when there is an extra element' do
459 expect(["food", "barn", "goo"]).to_not contain_exactly(
460 a_string_matching(/bar/),
461 a_string_matching(/foo/)
462 )
463 end
464
465 it 'passes when there is a missing element' do
466 expect(["food", "barn"]).to_not contain_exactly(
467 a_string_matching(/bar/),
468 a_string_matching(/foo/),
469 a_string_matching(/goo/)
470 )
471 end
472
473 end
474 end
475
476 RSpec.describe "Reusing a matcher that memoizes state" do
477 require "rspec/matchers/fail_matchers"
478
479 it "works properly in spite of the memoization" do
480 matcher = contain_exactly(eq(1))
481
482 expect {
483 expect([2]).to matcher
484 }.to fail_including(<<-MESSAGE)
485 expected collection contained: [(eq 1)]
486 actual collection contained: [2]
487 the missing elements were: [(eq 1)]
488 the extra elements were: [2]
489 MESSAGE
490
491 expect {
492 expect([3]).to matcher
493 }.to fail_including(<<-MESSAGE)
494 expected collection contained: [(eq 1)]
495 actual collection contained: [3]
496 the missing elements were: [(eq 1)]
497 the extra elements were: [3]
498 MESSAGE
499 end
500 end
501
502 module RSpec
503 module Matchers
504 module BuiltIn
505 class ContainExactly
506 RSpec.describe PairingsMaximizer do
507 it 'finds unmatched expected indexes' do
508 maximizer = PairingsMaximizer.new({ 0 => [], 1 => [0] }, { 0 => [1] })
509 expect(maximizer.solution.unmatched_expected_indexes).to eq([0])
510 end
511
512 it 'finds unmatched actual indexes' do
513 maximizer = PairingsMaximizer.new({ 0 => [0] }, { 0 => [0], 1 => [] })
514 expect(maximizer.solution.unmatched_actual_indexes).to eq([1])
515 end
516
517 describe "finding indeterminate indexes" do
518 it 'does not include unmatched indexes' do
519 maximizer = PairingsMaximizer.new({ 0 => [], 1 => [0] }, { 0 => [1], 1 => [] })
520
521 expect(maximizer.solution.indeterminate_expected_indexes).not_to include(0)
522 expect(maximizer.solution.indeterminate_actual_indexes).not_to include(1)
523 end
524
525 it 'does not include indexes that are reciprocally to exactly one index' do
526 maximizer = PairingsMaximizer.new({ 0 => [], 1 => [0] }, { 0 => [1], 1 => [0] })
527
528 expect(maximizer.solution.indeterminate_expected_indexes).not_to include(1)
529 expect(maximizer.solution.indeterminate_actual_indexes).not_to include(0)
530 end
531
532 it 'includes indexes that have multiple matches' do
533 maximizer = PairingsMaximizer.new({ 0 => [0, 2], 1 => [0, 2], 2 => [] },
534 { 0 => [0, 1], 1 => [], 2 => [0, 1] })
535
536 expect(maximizer.solution.indeterminate_expected_indexes).to include(0, 1)
537 expect(maximizer.solution.indeterminate_actual_indexes).to include(0, 2)
538 end
539
540 it 'includes indexes that have one match which has multiple matches' do
541 maximizer = PairingsMaximizer.new({ 0 => [0], 1 => [0], 2 => [1, 2] },
542 { 0 => [0, 1], 1 => [2], 2 => [2] })
543
544 expect(maximizer.solution.indeterminate_expected_indexes).to include(0, 1)
545 expect(maximizer.solution.indeterminate_actual_indexes).to include(1, 2)
546 end
547 end
548
549 describe "#unmatched_item_count" do
550 it 'returns the count of unmatched items' do
551 maximizer = PairingsMaximizer.new({ 0 => [1], 1 => [0] },
552 { 0 => [1], 1 => [0] })
553 expect(maximizer.solution.unmatched_item_count).to eq(0)
554
555 maximizer = PairingsMaximizer.new({ 0 => [1], 1 => [0] },
556 { 0 => [1], 1 => [0], 2 => [] })
557 expect(maximizer.solution.unmatched_item_count).to eq(1)
558 end
559 end
560
561 describe "#find_best_solution" do
562 matcher :produce_result do |unmatched_expected, unmatched_actual|
563 match do |result|
564 result.candidate? &&
565 result.unmatched_expected_indexes == unmatched_expected &&
566 result.unmatched_actual_indexes == unmatched_actual
567 end
568
569 failure_message do |result|
570 if result.candidate_result?
571 "expected a complete solution, but still had indeterminate indexes: " \
572 "expected: #{result.indeterminate_expected_indexes.inspect}; " \
573 "actual: #{result.indeterminate_actual_indexes.inspect}"
574 elsif result.unmatched_expected_indexes != unmatched_expected
575 "expected unmatched_expected_indexes: #{unmatched_expected.inspect} " \
576 "but got: #{result.unmatched_expected_indexes.inspect}"
577 elsif result.unmatched_actual_indexes != unmatched_actual
578 "expected unmatched_actual_indexes: #{unmatched_actual.inspect} " \
579 "but got: #{result.unmatched_actual_indexes.inspect}"
580 end
581 end
582 end
583
584 it 'returns no unmatched indexes when everything reciprocally matches one item' do
585 maximizer = PairingsMaximizer.new({ 0 => [1], 1 => [0] },
586 { 0 => [1], 1 => [0] })
587 expect(maximizer.find_best_solution).to produce_result([], [])
588 end
589
590 it 'returns unmatched indexes for everything that has no matches' do
591 maximizer = PairingsMaximizer.new({ 0 => [], 1 => [0] },
592 { 0 => [1], 1 => [] })
593 expect(maximizer.find_best_solution).to produce_result([0], [1])
594 end
595
596 it 'searches the solution space for a perfectly matching solution' do
597 maximizer = PairingsMaximizer.new({ 0 => [0, 1], 1 => [0] },
598 { 0 => [0, 1], 1 => [0] })
599 expect(maximizer.find_best_solution).to produce_result([], [])
600 end
601 end
602 end
603 end
604 end
605 end
606 end