-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash_helper_test.rb
67 lines (60 loc) · 1.8 KB
/
hash_helper_test.rb
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
require_relative '../common_requires'
require_relative '../../lib/helpers/hash_helper'
# Class to test class +HashHelper+.
class HashHelperTest < MiniTest::Test
def test_compare
# Contract violation for expected hash arg.
assert_raises ParamContractError do
HashHelper.compare(nil, {})
end
# Contract violation for actual hash arg.
assert_raises ParamContractError do
HashHelper.compare({}, nil)
end
# General case.
expected_missing = {
:a => 0,
:b => 1,
}
expected_unexpected = {
:c => 2,
:d => 3,
}
expected_changed = {
:e => {:expected => 5, :actual => 6},
:f => {:expected => 6, :actual => 5},
}
expected_ok = {
:g => 7,
:h => 8,
}
# Build the arguments.\\ hashes, expected and actual.
expected = {}
actual = {}
expected_missing.each_pair do |key, value|
expected[key] = value
end
expected_unexpected.each_pair do |key, value|
actual[key] = value
end
expected_ok.each_pair do |key, value|
expected[key] = value
actual[key] = value
end
expected_changed.each_pair do |key, value|
expected[key] = value[:expected]
actual[key] = value[:actual]
end
# Call and assert.
actual = HashHelper.compare(expected, actual)
# Use fetch, not [], so that key errors would be raised.
actual_missing = actual.fetch(:missing)
actual_unexpected = actual.fetch(:unexpected)
actual_changed = actual.fetch(:changed)
actual_ok = actual.fetch(:ok)
assert_equal(expected_missing, actual_missing, 'expected only')
assert_equal(expected_unexpected, actual_unexpected, 'actual only')
assert_equal(expected_changed, actual_changed, 'changed')
assert_equal(expected_ok, actual_ok, 'same')
end
end