-
Notifications
You must be signed in to change notification settings - Fork 0
/
structural_matcher_test.rb
130 lines (123 loc) · 3.46 KB
/
structural_matcher_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
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
require 'rubygems'
require 'unindent'
require './structural_matcher.rb'
class StructuralMatcherTest
def assert_equals(expected, actual, context)
raise Exception.new("#{context}\nexpecting #{expected}, got #{actual}") unless expected == actual
end
def expect(expected_data, actual_data, expected_result, expected_description=nil)
actual_result = StructuralMatcher.match(expected_data, actual_data)
context = "when matching #{expected_data} with #{actual_data},"
assert_equals(expected_result, actual_result[:matches?], context)
assert_equals(expected_description.rstrip.unindent, actual_result[:description], context) unless expected_description.nil?
end
def examples
[
[:hello, :goodbye, false,
<<-EOS
-:hello
+:goodbye
EOS
],
[:hello, :hello, true],
["hello", "goodbye", false,
<<-EOS
-"hello"
+"goodbye"
EOS
],
[1, 2, false,
<<-EOS
-1
+2
EOS
],
[/xyz/, "abc", false,
<<-EOS
-/xyz/
+"abc"
EOS
],
[{}, {}, true, nil],
[{:exactmatchstring => "exactmatch"}, {:exactmatchstring => "exactmatch"}, true],
[{:exactmatchstring => ""}, {:exactmatchstring => "exactmatch"}, false,
<<-EOS
{
-:exactmatchstring => ""
+:exactmatchstring => "exactmatch"
}
EOS
],
[{:exactmatchinteger => 1}, {:exactmatchinteger => 1}, true],
[{:exactmatchinteger => 1}, {:exactmatchinteger => 2}, false,
<<-EOS
{
-:exactmatchinteger => 1
+:exactmatchinteger => 2
}
EOS
],
[{}, {:anything => 'anything'}, true],
[{:firstpart => 'something'},
{:firstpart => 'something', :secondpart => 'anything'},
true],
[{:patternmatchstring => /starts with/},
{:patternmatchstring => 'starts with something'},
true],
[{:nestedmap => {:exactvalue => 'matchmeexactly'}},
{:nestedmap => {:exactvalue => 'matchmeexactly'}},
true],
[{:nestedmap => {:patternmatch => /.*theend/}},
{:nestedmap => {:patternmatch => 'blah blah blah theend'}},
true],
[{:nestedmap => {:exactmatch => 'exact',
:patternmatch => /.*theend/}},
{:nestedmap => {:anythingelse => 'anything',
:exactmatch => 'exact',
:patternmatch => 'blah blah blah theend'}},
true],
[{:nestedmap => {:exactmatch => 'wrongvalue'}},
{:nestedmap => {:anythingelse => 'anything'}},
false,
<<-EOS
{
:nestedmap => {
-:exactmatch => "wrongvalue"
}
}
EOS
],
[{:equalarray => [1,2]},
{:equalarray => [1,2]},
true],
[{:containsitemsarray => [1]},
{:containsitemsarray => [1,2]},
true],
[{:mismatchingarray => [1,2]},
{:mismatchingarray => [1]},
false,
<<-EOS
{
:mismatchingarray => [
1
-2
+nil
]
}
EOS
],
[{:arraywithpatternstring => [/hasthisinit/]},
{:arraywithpatternstring => ["xxxxx hasthisinit xxxx"]},
true],
["exact","exact", true],
["exactmismatch","notexact", false],
[/pattern/,"blah blah pattern blah blah", true]
]
end
def test
examples.each {|example|
expect(example[0], example[1], example[2], example[3])
}
end
end
StructuralMatcherTest.new.test