Repository navigation
Expand file tree
/
Copy pathnode_diff_spec.rb
More file actions
168 lines (138 loc) · 4.86 KB
/
Copy pathnode_diff_spec.rb
File metadata and controls
168 lines (138 loc) · 4.86 KB
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
require_relative '../helpers'
java_import org.jrubyparser.util.diff.SequenceMatcher
java_import org.jrubyparser.util.diff.Change
java_import org.jrubyparser.util.diff.NodeDiff
describe org.jrubyparser.util.diff.SequenceMatcher do
before(:each) do
SequenceMatcher.__persistent__ = true
end
it 'should take two nodes at creation' do
nodeA = parse('a = "a"')
nodeB = parse('b = "b"')
sm = SequenceMatcher.new(nodeA, nodeB)
sm.new_node.should == nodeA
sm.old_node.should == nodeB
end
it 'should calculate the complexity of a node' do
nodeA = parse("def foo(bar)\n bar\n end\n foo('astring')")
nodeB = parse('b = "b"')
sm = SequenceMatcher.new(nodeA, nodeB)
sm.calc_complexity(nodeA).should == 14
end
it 'should call a callback passed in' do
nodeA = parse('a = "a"')
nodeB = parse('b = "b"')
check = false
sm = SequenceMatcher.new(nodeA, nodeB) do |node|
check = true
end
sm.diff_nodes
check.should == true
end
it 'should diff two Strings of different value' do
nodeA = parse("'The rain in Spain falls mainly on the plain. -- My Fair Lady'")
nodeB = parse("'The life of the wife is ended by the knife. -- Stewie, Family Guy'")
seqm = SequenceMatcher.new(nodeA, nodeB)
seqm.diff_nodes.size.should >= 1
end
it 'should diff fcalls' do
nodeA = parse('a()')
nodeB = parse('b()')
seqm = SequenceMatcher.new(nodeA, nodeB)
seqm.diff_nodes.size.should >= 1
end
it 'should diff massgnnodes' do
nodeA = parse('ninjas.each {|x,y,z| puts "#{x}, #{y}, #{z}"}')
nodeB = parse('ninjas.each {|m,n,o| puts "#{x}, #{y}, #{z}"}')
seqm = SequenceMatcher.new(nodeA, nodeB)
seqm.diff_nodes.size.should >= 1
nodeC = parse('ninjas.each {|m,n,o| puts "#{x}, #{y}, #{z}"}')
nodeD = parse('ninjas.each {|m| puts "#{x}, #{y}, #{z}"}')
seqmtoo = SequenceMatcher.new(nodeC, nodeD)
seqmtoo.diff_nodes.size.should >= 1
end
it 'should not diff matching if statements' do
nodeA = parse("if 1 == 1\nputs 1\nelse\n1\nend")
nodeB = parse("if 1 == 1\nputs 1\nelse\n1\nend")
seqm = SequenceMatcher.new(nodeA, nodeB)
seqm.diff_nodes.size.should == 0
end
it 'should diff a changed if statement' do
nodeA = parse("if 1 == 1\nputs 1\nelse\n1\nend")
nodeB = parse("if 2 == 2\nputs 1\nelse\n1\nend")
nodeC = parse("if 2 == 2\nputs 3\nelse\n1\nend")
nodeD = parse("if 2 == 2\nputs 1\nelse\n2\nend")
seqm = SequenceMatcher.new(nodeA, nodeB)
seqm2 = SequenceMatcher.new(nodeB, nodeC)
seqm3 = SequenceMatcher.new(nodeB, nodeD)
seqm.diff_nodes.size.should >= 1
seqm2.diff_nodes.size.should >= 1
seqm3.diff_nodes.size.should >= 1
end
it 'should diff an OpElementAsgnNode' do
nodeA = parse("a = [1, 2, 3]\na[1] += 2")
nodeB = parse("b = [1, 2, 3]\nb[1] += 2")
nodeC = parse("a = [1, 2, 3]\na[2] += 2")
nodeD = parse("a = [1, 2, 3]\na[1] += 3")
seqm = SequenceMatcher.new(nodeA, nodeB)
seqm2 = SequenceMatcher.new(nodeA, nodeC)
seqm3 = SequenceMatcher.new(nodeA, nodeD)
seqm.diff_nodes[3].old_node.receiver.name.should == 'b'
seqm.diff_nodes[2].new_node.receiver.name.should == 'a'
seqm2.diff_nodes[1].old_node.args.get(0).value.should == 2
seqm2.diff_nodes[0].new_node.args.get(0).value.should == 1
seqm3.diff_nodes[1].old_node.value.value.should == 3
seqm3.diff_nodes[0].new_node.value.value.should == 2
end
it 'should diff Regexps' do
nodeA = parse("/abc/")
nodeB = parse("/abc/")
nodeC = parse("/abd/")
nodeD = parse("/abc/i")
seqm = SequenceMatcher.new(nodeA, nodeB)
seqm2 = SequenceMatcher.new(nodeA, nodeC)
seqm3 = SequenceMatcher.new(nodeA, nodeD)
seqm.diff_nodes.size.should == 0
seqm2.diff_nodes.size.should >= 1
seqm3.diff_nodes.size.should >= 1
end
end
describe "Change" do
nodeA = parse("b = 'b'")
nodeB = parse("b = 'a'")
change = Change.new(nodeA, 4, nodeB, 4)
it 'should hold the old node' do
change.old_node.should == nodeB
end
it 'should hold the new node' do
change.new_node.should == nodeA
end
it 'should hold the complexity of the nodes' do
change.old_cost.should == 4
change.new_cost.should == 4
change.total_cost.should == 8
end
end
describe org.jrubyparser.util.diff.NodeDiff do
before(:each) do
NodeDiff.__persistent__ = true
end
it 'should create a diff' do
stringA = "b = 'b'"
stringB = "b = 'a'"
nodeA = parse(stringA)
nodeB = parse(stringB)
nd = NodeDiff.new(nodeA, stringA, nodeB, stringB)
diff = nd.diff
diff.size.should >= 1
end
it 'should create a deepdiff (diff of subnodes)' do
stringA = "'astring'\ndef foo(bar)\n bar\n end\n"
stringB = "def foo(bar)\n puts bar\n end\n"
nodeA = parse(stringA)
nodeB = parse(stringB)
nd = NodeDiff.new(nodeA, stringA, nodeB, stringB)
ddiff = nd.deep_diff
ddiff[1].subdiff.size.should >= 1
end
end