Repository: SpoonLabs/gumtree-spoon-ast-diff
Pull request #104 (2019-06-11 14:48:43)

add arguments to pass the paths corresponding to the files under
comparison.


Patch:
diff --git a/src/main/java/gumtree/spoon/AstComparator.java b/src/main/java/gumtree/spoon/AstComparator.java
index 86a1a04..e9f0761 100644
--- a/src/main/java/gumtree/spoon/AstComparator.java
+++ b/src/main/java/gumtree/spoon/AstComparator.java
@@ -78,12 +78,19 @@ public class AstComparator {
 	}
 
 	/**
-	 * compares two snippet
+	 * compares two snippets
 	 */
 	public Diff compare(String left, String right) {
 		return compare(getCtType(left), getCtType(right));
 	}
 
+	/**
+	 * compares two snippets that come from the files given as argument
+	 */
+	public Diff compare(String left, String right, String filenameLeft, String filenameRight) {
+		return compare(getCtType(left, filenameLeft), getCtType(right, filenameRight));
+	}
+
 	/**
 	 * compares two AST nodes
 	 */
@@ -119,7 +126,11 @@ public class AstComparator {
 	}
 
 	public CtType<?> getCtType(String content) {
-		VirtualFile resource = new VirtualFile(content, "/test");
+		return getCtType(content, "/test");
+	}
+
+	public CtType<?> getCtType(String content, String filename) {
+		VirtualFile resource = new VirtualFile(content, filename);
 		return getCtType(resource);
 	}
 


Test patch:
diff --git a/src/test/java/gumtree/spoon/AstComparatorTest.java b/src/test/java/gumtree/spoon/AstComparatorTest.java
index 6afc1cb..27980d0 100644
--- a/src/test/java/gumtree/spoon/AstComparatorTest.java
+++ b/src/test/java/gumtree/spoon/AstComparatorTest.java
@@ -20,6 +20,7 @@ import gumtree.spoon.builder.NodeCreator;
 import gumtree.spoon.builder.SpoonGumTreeBuilder;
 import gumtree.spoon.diff.Diff;
 import gumtree.spoon.diff.DiffImpl;
+import gumtree.spoon.diff.operations.InsertOperation;
 import gumtree.spoon.diff.operations.MoveOperation;
 import gumtree.spoon.diff.operations.Operation;
 import gumtree.spoon.diff.operations.OperationKind;
@@ -240,8 +241,7 @@ public class AstComparatorTest {
 		List<Operation> actions = result.getRootOperations();
 		System.out.println(actions);
 		assertEquals(2, actions.size());
-		assertTrue(result.containsOperation(OperationKind.Delete, "Invocation",
-				"addKeyListener"));
+		assertTrue(result.containsOperation(OperationKind.Delete, "Invocation", "addKeyListener"));
 		assertTrue(result.containsOperation(OperationKind.Delete, "Class", "KeyHandler"));
 
 		CtElement ancestor = result.commonAncestor();
@@ -1654,6 +1654,34 @@ public class AstComparatorTest {
 
 	}
 
+	@Test
+	public void testFileName() throws Exception {
+		AstComparator diff = new AstComparator();
+		String left = "public class Calculator {\n" + "    public int add(int a, int b){\n" + "        return a + b;\n"
+				+ "    }\n" + "}";
+		String right = "public class Calculator {\n" + "    public int add(int a, int b){\n"
+				+ "    a = a + b;    return a + b;\n" + "    }\n" + "}";
+		Diff result = diff.compare(left, right);
+
+		assertEquals(1, result.getRootOperations().size());
+		// by default, name is "test"
+		assertEquals("test", result.getRootOperations().get(0).getSrcNode().getPosition().getFile().getName());
+
+		// let's put other names
+		result = diff.compare(left, right, "mleft.java", "mright.java");
+
+		assertEquals(1, result.getRootOperations().size());
+
+		assertTrue(result.getRootOperations().get(0) instanceof InsertOperation);
+
+		assertEquals("mright.java", result.getRootOperations().get(0).getSrcNode().getPosition().getFile().getName());
+
+		InsertOperation ins = (InsertOperation) result.getRootOperations().get(0);
+
+		assertEquals("mleft.java", ins.getParent().getPosition().getFile().getName());
+
+	}
+
 	@Test
 	public void testVarargs() throws Exception {
 		// https://github.com/GumTreeDiff/gumtree/issues/120