|
| 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 | +} |
0 commit comments