Commit cf372b23a2d085a50ed807ee46f06ea806984ae2
1 parent
f88ca472
Exists in
master
and in
18 other branches
fix logic of equivalent assertion for arrays with same size and elements duplicated
Showing
2 changed files
with
64 additions
and
2 deletions
Show diff stats
test/test_helper.rb
@@ -87,8 +87,8 @@ class ActiveSupport::TestCase | @@ -87,8 +87,8 @@ class ActiveSupport::TestCase | ||
87 | alias :ok :assert_block | 87 | alias :ok :assert_block |
88 | 88 | ||
89 | def assert_equivalent(enum1, enum2) | 89 | def assert_equivalent(enum1, enum2) |
90 | - norm1 = enum1.to_a | ||
91 | - norm2 = enum2.to_a | 90 | + norm1 = enum1.group_by{|e|e}.values |
91 | + norm2 = enum2.group_by{|e|e}.values | ||
92 | assert_equal norm1.size, norm2.size, "Size mismatch: #{enum1.inspect} vs #{enum2.inspect}" | 92 | assert_equal norm1.size, norm2.size, "Size mismatch: #{enum1.inspect} vs #{enum2.inspect}" |
93 | assert_equal [], norm1 - norm2 | 93 | assert_equal [], norm1 - norm2 |
94 | assert_equal [], norm2 - norm1 | 94 | assert_equal [], norm2 - norm1 |
@@ -0,0 +1,62 @@ | @@ -0,0 +1,62 @@ | ||
1 | +require_relative "../test_helper" | ||
2 | + | ||
3 | +class HelperTest < ActiveSupport::TestCase | ||
4 | + | ||
5 | + should 'assert_equivalent be true for the same arrays' do | ||
6 | + a1 = [1,2,3] | ||
7 | + a2 = [1,2,3] | ||
8 | + assert_equivalent a1, a2 | ||
9 | + end | ||
10 | + | ||
11 | + should 'assert_equivalent be true for equivalent arrays' do | ||
12 | + a1 = [1,2,3] | ||
13 | + a2 = [3,2,1] | ||
14 | + assert_equivalent a1, a2 | ||
15 | + end | ||
16 | + | ||
17 | + should 'assert_equivalent be true for equivalent arrays independent of parameter order' do | ||
18 | + a1 = [1,2,3] | ||
19 | + a2 = [3,2,1] | ||
20 | + assert_equivalent a2, a1 | ||
21 | + end | ||
22 | + | ||
23 | + should 'assert_equivalent be false for different arrays' do | ||
24 | + a1 = [1,2,3] | ||
25 | + a2 = [4,2,1] | ||
26 | + assert_raise Minitest::Assertion do | ||
27 | + assert_equivalent(a1, a2) | ||
28 | + end | ||
29 | + end | ||
30 | + | ||
31 | + should 'assert_equivalent be false for different arrays independent of parameter order' do | ||
32 | + a1 = [1,2,3] | ||
33 | + a2 = [4,2,1] | ||
34 | + assert_raise Minitest::Assertion do | ||
35 | + assert_equivalent(a2, a1) | ||
36 | + end | ||
37 | + end | ||
38 | + | ||
39 | + should 'assert_equivalent be false for arrays with different sizes' do | ||
40 | + a1 = [1,2,3] | ||
41 | + a2 = [1,2,3,4] | ||
42 | + assert_raise Minitest::Assertion do | ||
43 | + assert_equivalent(a1, a2) | ||
44 | + end | ||
45 | + end | ||
46 | + | ||
47 | + should 'assert_equivalent be false for arrays with same elements duplicated' do | ||
48 | + a1 = [2,2,3] | ||
49 | + a2 = [2,3,3] | ||
50 | + assert_raise Minitest::Assertion do | ||
51 | + assert_equivalent(a1, a2) | ||
52 | + end | ||
53 | + end | ||
54 | + | ||
55 | + should 'assert_equivalent be false for arrays with same elements duplicated of different sizes' do | ||
56 | + a1 = [2,2,3] | ||
57 | + a2 = [2,3,3,3] | ||
58 | + assert_raise Minitest::Assertion do | ||
59 | + assert_equivalent(a1, a2) | ||
60 | + end | ||
61 | + end | ||
62 | +end |