Skip to content

Commit

Permalink
fix: Fix equality for CtCodeSnippet (#4024)
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse authored Jul 6, 2021
1 parent da65942 commit 6cdcf2e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/spoon/support/visitor/equals/EqualsChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtTextBlock;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.declaration.CtCodeSnippet;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtModifiable;
Expand Down Expand Up @@ -109,6 +110,15 @@ public void scanCtModifiable(CtModifiable m) {
super.scanCtModifiable(m);
}

@Override
public void scanCtCodeSnippet(CtCodeSnippet snippet) {
final CtCodeSnippet peek = (CtCodeSnippet) this.other;
if (!snippet.getValue().equals(peek.getValue())) {
setNotEqual(CtRole.SNIPPET);
}
super.scanCtCodeSnippet(snippet);
}

@Override
public <T, A extends T> void visitCtAssignment(CtAssignment<T, A> assignment) {
if (!(assignment instanceof CtOperatorAssignment) && this.other instanceof CtOperatorAssignment) {
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/spoon/test/snippets/SnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import spoon.support.compiler.SnippetCompilationHelper;
import spoon.support.compiler.VirtualFile;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -239,4 +241,50 @@ public void testSnippetCompilationInUnnamedPackage() {
assertTrue(body.getStatements().get(0) instanceof CtLocalVariable);
assertEquals(1,body.getStatements().size());
}

@Test
public void testCodeSnippetExpressionsWithNonEqualValuesAreNotEqual() {
// contract: Two code snippet expressions with non-equal values are not equal
Factory factory = new Launcher().getFactory();

CtCodeSnippetExpression<Integer> one = factory.createCodeSnippetExpression("1");
CtCodeSnippetExpression<Integer> two = factory.createCodeSnippetExpression("2");

assertThat(one.equals(two), is(false));
}

@Test
public void testCodeSnippetExpressionsWithEqualValuesAreEqual() {
// contract: Two code snippet expressions with equal values, that are also otherwise equal,
// are equal
Factory factory = new Launcher().getFactory();

CtCodeSnippetExpression<Integer> one = factory.createCodeSnippetExpression("1");
CtCodeSnippetExpression<Integer> alsoOne = factory.createCodeSnippetExpression("1");

assertThat(one.equals(alsoOne), is(true));
}

@Test
public void testCodeSnippetStatementsWithNonEqualValuesAreNotEqual() {
// contract: Two code snippet statements with non-equal values are not equal
Factory factory = new Launcher().getFactory();

CtCodeSnippetStatement intDeclaration = factory.createCodeSnippetStatement("int a;");
CtCodeSnippetStatement doubleDeclaration = factory.createCodeSnippetStatement("double a;");

assertThat(intDeclaration.equals(doubleDeclaration), is(false));
}

@Test
public void testCodeSnippetStatementsWithEqualValuesAreEqual() {
// contract: Two code snippet statements with equal values, that are also otherwise equal,
// are equal
Factory factory = new Launcher().getFactory();

CtCodeSnippetStatement intDeclaration = factory.createCodeSnippetStatement("int a;");
CtCodeSnippetStatement alsoIntDeclaration = factory.createCodeSnippetStatement("int a;");

assertThat(intDeclaration.equals(alsoIntDeclaration), is(true));
}
}

0 comments on commit 6cdcf2e

Please sign in to comment.