Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix equality for CtCodeSnippet #4024

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
slarse marked this conversation as resolved.
Show resolved Hide resolved
}
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));
}
}