tdiff 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.md +8 -0
- data/gemspec.yml +1 -1
- data/lib/tdiff/tdiff.rb +27 -3
- data/lib/tdiff/unordered.rb +32 -4
- data/spec/tdiff_examples.rb +2 -2
- data/spec/tdiff_spec.rb +14 -10
- metadata +2 -2
data/ChangeLog.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
### 0.3.2 / 2010-11-28
|
2
|
+
|
3
|
+
* Added {TDiff#tdiff_recursive} to only handle recursively traversing
|
4
|
+
and diffing the children nodes.
|
5
|
+
* Added {TDiff::Unordered#tdiff_recursive_unordered} to only handle
|
6
|
+
recursively traversing and diffing the children nodes, without respecting
|
7
|
+
the order of the nodes.
|
8
|
+
|
1
9
|
### 0.3.1 / 2010-11-28
|
2
10
|
|
3
11
|
* Fixed a typo in {TDiff::Unordered#tdiff_unordered}, which was causing
|
data/gemspec.yml
CHANGED
data/lib/tdiff/tdiff.rb
CHANGED
@@ -58,6 +58,32 @@ module TDiff
|
|
58
58
|
return self
|
59
59
|
end
|
60
60
|
|
61
|
+
yield ' ', self
|
62
|
+
|
63
|
+
tdiff_recursive(tree,&block)
|
64
|
+
return self
|
65
|
+
end
|
66
|
+
|
67
|
+
protected
|
68
|
+
|
69
|
+
#
|
70
|
+
# Recursively compares the differences between the children nodes.
|
71
|
+
#
|
72
|
+
# @param [#tdiff_each_child] tree
|
73
|
+
# The other tree.
|
74
|
+
#
|
75
|
+
# @yield [change, node]
|
76
|
+
# The given block will be passed the added or removed nodes.
|
77
|
+
#
|
78
|
+
# @yieldparam [' ', '+', '-'] change
|
79
|
+
# The state-change of the node.
|
80
|
+
#
|
81
|
+
# @yieldparam [Object] node
|
82
|
+
# A node from one of the two trees.
|
83
|
+
#
|
84
|
+
# @since 0.3.2
|
85
|
+
#
|
86
|
+
def tdiff_recursive(tree,&block)
|
61
87
|
c = Hash.new { |hash,key| hash[key] = Hash.new(0) }
|
62
88
|
x = enum_for(:tdiff_each_child,self)
|
63
89
|
y = enum_for(:tdiff_each_child,tree)
|
@@ -122,9 +148,7 @@ module TDiff
|
|
122
148
|
changes = nil
|
123
149
|
|
124
150
|
# recurse down through unchanged nodes
|
125
|
-
unchanged.each { |x,y| x.
|
151
|
+
unchanged.each { |x,y| x.tdiff_recursive(y,&block) }
|
126
152
|
unchanged = nil
|
127
|
-
|
128
|
-
return self
|
129
153
|
end
|
130
154
|
end
|
data/lib/tdiff/unordered.rb
CHANGED
@@ -15,7 +15,7 @@ module TDiff
|
|
15
15
|
|
16
16
|
#
|
17
17
|
# Finds the differences between `self` and another tree, not respecting
|
18
|
-
# the
|
18
|
+
# the order of the nodes.
|
19
19
|
#
|
20
20
|
# @param [#tdiff_each_child] tree
|
21
21
|
# The other tree.
|
@@ -44,6 +44,33 @@ module TDiff
|
|
44
44
|
return self
|
45
45
|
end
|
46
46
|
|
47
|
+
yield ' ', self
|
48
|
+
|
49
|
+
tdiff_recursive_unordered(tree,&block)
|
50
|
+
return self
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
|
55
|
+
#
|
56
|
+
# Recursively compares the differences between the children nodes,
|
57
|
+
# without respecting the order of the nodes.
|
58
|
+
#
|
59
|
+
# @param [#tdiff_each_child] tree
|
60
|
+
# The other tree.
|
61
|
+
#
|
62
|
+
# @yield [change, node]
|
63
|
+
# The given block will be passed the added or removed nodes.
|
64
|
+
#
|
65
|
+
# @yieldparam [' ', '+', '-'] change
|
66
|
+
# The state-change of the node.
|
67
|
+
#
|
68
|
+
# @yieldparam [Object] node
|
69
|
+
# A node from one of the two trees.
|
70
|
+
#
|
71
|
+
# @since 0.3.2
|
72
|
+
#
|
73
|
+
def tdiff_recursive_unordered(tree,&block)
|
47
74
|
x = enum_for(:tdiff_each_child,self)
|
48
75
|
y = enum_for(:tdiff_each_child,tree)
|
49
76
|
|
@@ -79,10 +106,11 @@ module TDiff
|
|
79
106
|
changes = nil
|
80
107
|
|
81
108
|
# recurse down the unchanged nodes
|
82
|
-
unchanged.each
|
83
|
-
|
109
|
+
unchanged.each do |xi,yj|
|
110
|
+
xi.tdiff_recursive_unordered(yj,&block)
|
111
|
+
end
|
84
112
|
|
85
|
-
|
113
|
+
unchanged = nil
|
86
114
|
end
|
87
115
|
end
|
88
116
|
end
|
data/spec/tdiff_examples.rb
CHANGED
@@ -23,7 +23,7 @@ shared_examples_for 'TDiff' do |method|
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should tell when sub-nodes are added" do
|
26
|
-
changes = @tree.
|
26
|
+
changes = @tree.send(method,@added).select { |change,node| change == '+' }
|
27
27
|
|
28
28
|
changes.length.should == 1
|
29
29
|
changes[0][0].should == '+'
|
@@ -31,7 +31,7 @@ shared_examples_for 'TDiff' do |method|
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should tell when sub-nodes are removed" do
|
34
|
-
changes = @tree.
|
34
|
+
changes = @tree.send(method,@removed).select { |change,node| change == '-' }
|
35
35
|
|
36
36
|
changes.length.should == 1
|
37
37
|
changes[0][0].should == '-'
|
data/spec/tdiff_spec.rb
CHANGED
@@ -11,20 +11,24 @@ describe TDiff do
|
|
11
11
|
it "should detect when the order of children has changed" do
|
12
12
|
changes = @tree.tdiff(@changed_order).to_a
|
13
13
|
|
14
|
-
changes.length.should ==
|
15
|
-
changes[0][0].should == '-'
|
16
|
-
changes[0][1].should == @tree.children[0]
|
14
|
+
changes.length.should == 6
|
17
15
|
|
18
|
-
changes[
|
19
|
-
changes[
|
16
|
+
changes[0][0].should == ' '
|
17
|
+
changes[0][1].should == @tree
|
20
18
|
|
21
|
-
changes[
|
22
|
-
changes[
|
19
|
+
changes[1][0].should == '-'
|
20
|
+
changes[1][1].should == @tree.children[0]
|
23
21
|
|
24
|
-
changes[
|
25
|
-
changes[
|
22
|
+
changes[2][0].should == ' '
|
23
|
+
changes[2][1].should == @tree.children[1]
|
24
|
+
|
25
|
+
changes[3][0].should == '+'
|
26
|
+
changes[3][1].should == @changed_order.children[1]
|
26
27
|
|
27
28
|
changes[4][0].should == ' '
|
28
|
-
changes[4][1].should == @tree.children[1].children[
|
29
|
+
changes[4][1].should == @tree.children[1].children[0]
|
30
|
+
|
31
|
+
changes[5][0].should == ' '
|
32
|
+
changes[5][1].should == @tree.children[1].children[1]
|
29
33
|
end
|
30
34
|
end
|