Repository: SpoonLabs/gumtree-spoon-ast-diff
Pull request #90 (2019-04-09 12:41:29)

test: add test to show that varargs are supported


Patch:
diff --git a/src/main/java/gumtree/spoon/builder/NodeCreator.java b/src/main/java/gumtree/spoon/builder/NodeCreator.java
index e43d697..609145d 100644
--- a/src/main/java/gumtree/spoon/builder/NodeCreator.java
+++ b/src/main/java/gumtree/spoon/builder/NodeCreator.java
@@ -8,6 +8,7 @@ import com.github.gumtreediff.tree.ITree;
 
 import spoon.reflect.declaration.CtMethod;
 import spoon.reflect.declaration.CtModifiable;
+import spoon.reflect.declaration.CtParameter;
 import spoon.reflect.declaration.CtVariable;
 import spoon.reflect.declaration.ModifierKind;
 import spoon.reflect.reference.CtTypeReference;
@@ -89,4 +90,5 @@ class NodeCreator extends CtInheritanceScanner {
 
 		super.visitCtMethod(e);
 	}
+
 }
diff --git a/src/main/java/gumtree/spoon/diff/Diff.java b/src/main/java/gumtree/spoon/diff/Diff.java
index 884c5b6..623a0ad 100644
--- a/src/main/java/gumtree/spoon/diff/Diff.java
+++ b/src/main/java/gumtree/spoon/diff/Diff.java
@@ -56,11 +56,17 @@ public interface Diff {
 	 */
 	boolean containsOperation(OperationKind kind, String nodeKind, String nodeLabel);
 
+	/**
+	 * Useful for update operation
+	 */
+	boolean containsOperations(OperationKind kind, String nodeKind, String nodeLabel, String newLabel);
+
 	/**
 	 * low level if you want to test on all operations and not only root operations
 	 */
 	boolean containsOperations(List<Operation> operations, OperationKind kind, String nodeKind, String nodeLabel);
 
+
 	/**
 	 * outputs debug information to System.out
 	 */
diff --git a/src/main/java/gumtree/spoon/diff/DiffImpl.java b/src/main/java/gumtree/spoon/diff/DiffImpl.java
index 66c58af..8f87fa5 100644
--- a/src/main/java/gumtree/spoon/diff/DiffImpl.java
+++ b/src/main/java/gumtree/spoon/diff/DiffImpl.java
@@ -194,6 +194,19 @@ public class DiffImpl implements Diff {
 						&& operation.getAction().getNode().getLabel().equals(nodeLabel));
 	}
 
+	@Override
+	public boolean containsOperations(OperationKind kind, String nodeKind, String nodeLabel, String newLabel) {
+		if (kind != OperationKind.Update) {
+			throw new IllegalArgumentException();
+		}
+		return getRootOperations().stream()
+				.anyMatch(operation -> operation instanceof UpdateOperation //
+						&& ((UpdateOperation) operation).getAction().getNode().getLabel().equals(nodeLabel)
+						&& ((UpdateOperation)operation).getAction().getValue().equals(newLabel)
+
+				);
+	}
+
 	@Override
 	public void debugInformation() {
 		System.err.println(toDebugString());
@@ -208,16 +221,18 @@ public class DiffImpl implements Diff {
 		for (Operation operation : ops) {
 			ITree node = operation.getAction().getNode();
 			final CtElement nodeElement = operation.getNode();
-			String label = "\"" + node.getLabel() + "\"";
-			if (operation instanceof UpdateOperation) {
-				label += " to \"" + ((Update) operation.getAction()).getValue() + "\"";
-			}
 			String nodeType = context.getTypeLabel(node.getType());
 			if (nodeElement != null) {
 				nodeType += "(" + nodeElement.getClass().getSimpleName() + ")";
 			}
-			result += "\"" + operation.getAction().getClass().getSimpleName() + "\", \"" + nodeType + "\", " + label
-					+ " (size: " + node.getDescendants().size() + ")" + node.toTreeString();
+			result += "OperationKind." + operation.getAction().getClass().getSimpleName() + ", \"" + nodeType + "\", \"" + node.getLabel()+ "\"";
+
+			if (operation instanceof UpdateOperation) {
+				// adding the new value for update
+				result += ",  \"" + ((Update) operation.getAction()).getValue() + "\"";
+			}
+
+			result += " (size: " + node.getDescendants().size() + ")" + node.toTreeString();
 		}
 		return result;
 	}


Test patch:
diff --git a/src/test/java/gumtree/spoon/AstComparatorTest.java b/src/test/java/gumtree/spoon/AstComparatorTest.java
index ed1ccde..42dc236 100644
--- a/src/test/java/gumtree/spoon/AstComparatorTest.java
+++ b/src/test/java/gumtree/spoon/AstComparatorTest.java
@@ -1658,4 +1658,25 @@ public class AstComparatorTest {
 		assertEquals(1, result.getRootOperations().size());
 
 	}
+
+	@Test
+	public void testVarargs() throws Exception {
+		// https://github.com/GumTreeDiff/gumtree/issues/120
+		CtClass c1 = Launcher.parseClass(" class BehaviorCall {\n"
+				+ "   void foo(String s)}\n" + "}\n" + "\n" + "}");
+
+		CtClass c2 = Launcher.parseClass(" class BehaviorCall {\n"
+				+ "   void foo(String... s)}\n" + "}\n" + "\n" + "}");
+
+		AstComparator diff = new AstComparator();
+		Diff result = diff.compare(c1, c2);
+
+		List<Operation> actions = result.getRootOperations();
+		result.debugInformation();
+
+		assertEquals(1, actions.size());
+		// the type is now an array
+		assertTrue(result.containsOperations(OperationKind.Update, "VARIABLE_TYPE(CtTypeReferenceImpl)", "java.lang.String", "java.lang.String[]"));
+	}
+
 }