Skip to content

Commit c3fd671

Browse files
committed
fix: action serialization is now consistent. fix #334.
1 parent 6b47e46 commit c3fd671

File tree

4 files changed

+185
-87
lines changed

4 files changed

+185
-87
lines changed

core/src/main/java/com/github/gumtreediff/io/ActionsIoUtils.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,29 @@ public void writeTo(Writer writer) throws Exception {
102102
// Write the actions
103103
fmt.startActions();
104104
for (Action a : actions) {
105-
Tree src = a.getNode();
106105
if (a instanceof Move) {
107-
Tree dst = mappings.getDstForSrc(src);
108-
fmt.moveAction((Move) a, src, dst.getParent(), ((Move) a).getPosition());
106+
Move m = (Move) a;
107+
fmt.moveAction(m, m.getNode(), m.getParent(), m.getPosition());
109108
} else if (a instanceof Update) {
110-
Tree dst = mappings.getDstForSrc(src);
111-
fmt.updateAction((Update) a, src, dst);
109+
Update u = (Update) a;
110+
Tree dst = mappings.getDstForSrc(u.getNode());
111+
fmt.updateAction(u, u.getNode(), dst);
112112
} else if (a instanceof Insert) {
113+
Insert ins = (Insert) a;
113114
Tree dst = a.getNode();
114115
if (dst.isRoot())
115-
fmt.insertRoot((Insert) a, src);
116+
fmt.insertRoot(ins, ins.getNode());
116117
else
117-
fmt.insertAction((Insert) a, src, dst.getParent(), dst.getParent().getChildPosition(dst));
118+
fmt.insertAction(ins, ins.getNode(), ins.getParent(), ins.getPosition());
118119
} else if (a instanceof Delete) {
119-
fmt.deleteAction((Delete) a, src);
120+
Delete del = (Delete) a;
121+
fmt.deleteAction(del, del.getNode());
120122
} else if (a instanceof TreeInsert) {
121-
Tree dst = a.getNode();
122-
fmt.insertTreeAction((TreeInsert) a, src, dst.getParent(), dst.getParent().getChildPosition(dst));
123+
TreeInsert ins = (TreeInsert) a;
124+
fmt.insertTreeAction(ins, ins.getNode(), ins.getParent(), ins.getPosition());
123125
} else if (a instanceof TreeDelete) {
124-
fmt.deleteTreeAction((TreeDelete) a, src);
126+
TreeDelete del = (TreeDelete) a;
127+
fmt.deleteTreeAction(del, del.getNode());
125128
}
126129

127130
}

core/src/test/java/com/github/gumtreediff/test/TestActionIo.java

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* This file is part of GumTree.
3+
*
4+
* GumTree is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* GumTree is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
* Copyright 2011-2015 Jean-Rémy Falleri <[email protected]>
18+
* Copyright 2011-2015 Floréal Morandat <[email protected]>
19+
*/
20+
21+
package com.github.gumtreediff.test;
22+
23+
import com.github.gumtreediff.actions.ChawatheScriptGenerator;
24+
import com.github.gumtreediff.actions.EditScript;
25+
import com.github.gumtreediff.io.ActionsIoUtils;
26+
import com.github.gumtreediff.io.TreeIoUtils;
27+
import com.github.gumtreediff.matchers.MappingStore;
28+
import com.github.gumtreediff.tree.Tree;
29+
import com.github.gumtreediff.utils.Pair;
30+
import com.github.gumtreediff.tree.TreeContext;
31+
32+
33+
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.BeforeEach;
35+
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.hamcrest.MatcherAssert.assertThat;
38+
import static org.hamcrest.Matchers.*;
39+
40+
import java.io.IOException;
41+
42+
public class TestActionIoUtils {
43+
44+
@Test
45+
public void testActionsIoUtilsInsert() throws Exception {
46+
String tree1 = "<tree type=\"0\" typeLabel=\"0\" pos=\"0\" length=\"100\"></tree>";
47+
String tree2 = "<tree type=\"0\" typeLabel=\"0\" pos=\"101\" length=\"100\">"
48+
+ "<tree type=\"1\" typeLabel=\"1\" pos=\"102\" length=\"2\"></tree></tree>";
49+
TreeContext tc1 = TreeIoUtils.fromXml().generateFrom().string(tree1);
50+
TreeContext tc2 = TreeIoUtils.fromXml().generateFrom().string(tree2);
51+
MappingStore ms = new MappingStore(tc1.getRoot(), tc2.getRoot());
52+
ms.addMapping(tc1.getRoot(), tc2.getRoot());
53+
ChawatheScriptGenerator gen = new ChawatheScriptGenerator();
54+
EditScript es = gen.computeActions(ms);
55+
assertEquals("===\ninsert-node\n---\n1 [102,104]\nto\n0 [0,100]\nat 0", es.get(0).toString());
56+
assertEquals("===\n" +
57+
"match\n" +
58+
"---\n" +
59+
"0 [0,100]\n" +
60+
"0 [101,201]\n" +
61+
"===\n" +
62+
"insert-node\n" +
63+
"---\n" +
64+
"1 [102,104]\n" +
65+
"to\n" +
66+
"0 [0,100]\n" +
67+
"at 0\n", ActionsIoUtils.toText(tc1, es, ms).toString());
68+
assertEquals("{\n" +
69+
" \"matches\": [\n" +
70+
" {\n" +
71+
" \"src\": \"0 [0,100]\",\n" +
72+
" \"dest\": \"0 [101,201]\"\n" +
73+
" }\n" +
74+
" ],\n" +
75+
" \"actions\": [\n" +
76+
" {\n" +
77+
" \"action\": \"insert-node\",\n" +
78+
" \"tree\": \"1 [102,104]\",\n" +
79+
" \"parent\": \"0 [0,100]\",\n" +
80+
" \"at\": 0\n" +
81+
" }\n" +
82+
" ]\n" +
83+
"}", ActionsIoUtils.toJson(tc1, es, ms).toString());
84+
assertEquals("<?xml version=\"1.0\" ?>\n" +
85+
"<matches>\n" +
86+
" <match src=\"0 [0,100]\" dest=\"0 [101,201]\"/>\n" +
87+
"</matches>\n" +
88+
"<actions>\n" +
89+
" <insert-node tree=\"1 [102,104]\" parent=\"0 [0,100]\" at=\"0\"/>\n" +
90+
"</actions>\n", ActionsIoUtils.toXml(tc1, es, ms).toString());
91+
}
92+
93+
@Test
94+
public void testActionsIoUtilsMove() throws Exception {
95+
String tree1 = "<tree type=\"0\" typeLabel=\"0\" pos=\"0\" length=\"100\">"
96+
+ "<tree type=\"1\" typeLabel=\"1\" pos=\"1\" length=\"10\"/>"
97+
+ "<tree type=\"2\" typeLabel=\"2\" pos=\"11\" length=\"10\"/></tree>";
98+
String tree2 = "<tree type=\"0\" typeLabel=\"0\" pos=\"101\" length=\"100\">"
99+
+ "<tree type=\"2\" typeLabel=\"2\" pos=\"101\" length=\"10\"/>"
100+
+ "<tree type=\"1\" typeLabel=\"1\" pos=\"111\" length=\"10\"/></tree>";
101+
TreeContext tc1 = TreeIoUtils.fromXml().generateFrom().string(tree1);
102+
TreeContext tc2 = TreeIoUtils.fromXml().generateFrom().string(tree2);
103+
MappingStore ms = new MappingStore(tc1.getRoot(), tc2.getRoot());
104+
ms.addMapping(tc1.getRoot(), tc2.getRoot());
105+
ms.addMapping(tc1.getRoot().getChild(0), tc2.getRoot().getChild(1));
106+
ms.addMapping(tc1.getRoot().getChild(1), tc2.getRoot().getChild(0));
107+
ChawatheScriptGenerator gen = new ChawatheScriptGenerator();
108+
EditScript es = gen.computeActions(ms);
109+
assertEquals("===\n" +
110+
"move-tree\n" +
111+
"---\n" +
112+
"1 [1,11]\n" +
113+
"to\n" +
114+
"0 [0,100]\n" +
115+
"at 1", es.get(0).toString());
116+
assertThat(ActionsIoUtils.toText(tc1, es, ms).toString(), containsString("===\n" +
117+
"move-tree\n" +
118+
"---\n" +
119+
"1 [1,11]\n" +
120+
"to\n" +
121+
"0 [0,100]\n" +
122+
"at 1\n"));
123+
assertThat(ActionsIoUtils.toJson(tc1, es, ms).toString(), containsString(" \"actions\": [\n" +
124+
" {\n" +
125+
" \"action\": \"move-tree\",\n" +
126+
" \"tree\": \"1 [1,11]\",\n" +
127+
" \"parent\": \"0 [0,100]\",\n" +
128+
" \"at\": 1\n" +
129+
" }\n" +
130+
" ]\n"));
131+
assertThat(ActionsIoUtils.toXml(tc1, es, ms).toString(), containsString("<actions>\n" +
132+
" <move-tree tree=\"1 [1,11]\" parent=\"0 [0,100]\" at=\"1\"/>\n" +
133+
"</actions>\n"));
134+
}
135+
136+
@Test
137+
public void testActionsIoUtilsUpdate() throws Exception {
138+
String tree1 = "<tree type=\"0\" typeLabel=\"0\" pos=\"0\" length=\"100\">"
139+
+ "<tree type=\"0\" label=\"foo\" typeLabel=\"0\" pos=\"1\" length=\"10\"/></tree>";
140+
String tree2 = "<tree type=\"0\" typeLabel=\"0\" pos=\"101\" length=\"100\">"
141+
+ "<tree type=\"0\" label=\"bar\" typeLabel=\"0\" pos=\"101\" length=\"10\"/></tree>";
142+
TreeContext tc1 = TreeIoUtils.fromXml().generateFrom().string(tree1);
143+
TreeContext tc2 = TreeIoUtils.fromXml().generateFrom().string(tree2);
144+
MappingStore ms = new MappingStore(tc1.getRoot(), tc2.getRoot());
145+
ms.addMapping(tc1.getRoot(), tc2.getRoot());
146+
ms.addMapping(tc1.getRoot().getChild(0), tc2.getRoot().getChild(0));
147+
ChawatheScriptGenerator gen = new ChawatheScriptGenerator();
148+
EditScript es = gen.computeActions(ms);
149+
assertEquals("===\n" +
150+
"update-node\n" +
151+
"---\n" +
152+
"0: foo [1,11]\n" +
153+
"replace foo by bar", es.get(0).toString());
154+
assertThat(ActionsIoUtils.toText(tc1, es, ms).toString(), containsString("===\n" +
155+
"update-node\n" +
156+
"---\n" +
157+
"0: foo [1,11]\n" +
158+
"replace foo by bar\n"));
159+
assertThat(ActionsIoUtils.toJson(tc1, es, ms).toString(), containsString(" \"actions\": [\n" +
160+
" {\n" +
161+
" \"action\": \"update-node\",\n" +
162+
" \"tree\": \"0: foo [1,11]\",\n" +
163+
" \"label\": \"bar\"\n" +
164+
" }\n" +
165+
" ]\n"));
166+
assertThat(ActionsIoUtils.toXml(tc1, es, ms).toString(), containsString("<actions>\n" +
167+
" <update-node tree=\"0: foo [1,11]\" label=\"bar\"/>\n" +
168+
"</actions>\n"));
169+
}
170+
}

gumtree_checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
<module name="MethodParamPad"/>
116116
<module name="OperatorWrap">
117117
<property name="option" value="NL"/>
118-
<property name="tokens" value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR, LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR "/>
118+
<property name="tokens" value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR, LT, MINUS, MOD, NOT_EQUAL, QUESTION, SL, SR, STAR "/>
119119
</module>
120120
<module name="AnnotationLocation">
121121
<property name="tokens" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF"/>

0 commit comments

Comments
 (0)