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

Conversation

slarse
Copy link
Collaborator

@slarse slarse commented Jul 2, 2021

Fix #4022

This PR fixes equality between CtCodeSnippet instances. Previously, two code snippets would be considered equal regardless of their contained snippet (i.e. the raw string). With this PR, the raw snippet is now also considered part of the equality of code snippets (both statements and expressions).

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

assertThat(one, not(equalTo(two)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you have preferred asserThat over assertEquals?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you have preferred asserThat over assertEquals?

what about assertNotEquals(one, two); 😅

Copy link
Collaborator Author

@slarse slarse Jul 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you have preferred asserThat over assertEquals?

Many reasons. First, I prefer the way you write the Hamcrest matchers, i.e. assertThat(one, not(equalTo(two))). It's clear which is the actual value, and which is the expected one. For assertNotEquals(one, two), not so much (these are actually reversed, because it's assertNotEquals(expected, actual), but assertThat(actual, someMatcher(expected))).

Hamcerst matchers also almost always give more informative failure information than JUnit's built-in matchers when an assertion fails.

There's however a pretty big problem with this test that is unrelated to the assertion being used. Why is one the actual value, and two the expected value? Which method is actually under test here? Of course, neither of one or two is actual or expected, they are just two values. The method under test is equals, but that's hidden by the assertion.

The test should probably look like this instead:

Suggested change
assertThat(one, not(equalTo(two)));
boolean snippetsAreEqual = one.equals(two);
assertFalse(snippetsAreEqual);

Copy link
Collaborator Author

@slarse slarse Jul 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or more concisely, this would also be fine

Suggested change
assertThat(one, not(equalTo(two)));
assertFalse(one.equals(two));

Asserting true and false is probably the one place where I often use the built-in assertions to Hamcrest matchers, because the Hamcrest alternative is a bit more verbose than necessary:

Suggested change
assertThat(one, not(equalTo(two)));
assertThat(one.equals(two), is(false));

However, the Hamcrest failure message:

java.lang.AssertionError: 
Expected: is <false>
     but: was <true>
[... stacktrace ...]

Is much more readable than the JUnit failure message, which is essentially just the stacktrace:

java.lang.AssertionError
	at org.junit.Assert.fail(Assert.java:87)
	at org.junit.Assert.assertTrue(Assert.java:42)
	at org.junit.Assert.assertFalse(Assert.java:65)
	at org.junit.Assert.assertFalse(Assert.java:75)
	at spoon.test.snippets.SnippetTest.testSnippetsWithDifferingValuesAreNotEqual(SnippetTest.java:255)
[... more stacktrace ...]

So actually I'll probably go with the Hamcrest one after all :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java.lang.AssertionError:
Expected: is
but: was
[... stacktrace ...]

Agree, this is clearly better.

@slarse slarse changed the title wip: fix: Make CtCodeSnippetExpression equality include value wip: fix: Make CtCodeSnippet equality include value Jul 2, 2021
@slarse slarse changed the title wip: fix: Make CtCodeSnippet equality include value review: fix: Make CtCodeSnippet equality include value Jul 2, 2021
@slarse
Copy link
Collaborator Author

slarse commented Jul 6, 2021

@monperrus @nharrand ping for merge :)

@monperrus monperrus changed the title review: fix: Make CtCodeSnippet equality include value fix: Fix equality for CtCodeSnippet Jul 6, 2021
@monperrus monperrus merged commit 6cdcf2e into INRIA:master Jul 6, 2021
@monperrus
Copy link
Collaborator

Thanks a lot @slarse

@slarse slarse deleted the issue/4022-make-snippet-expressions-equality-include-value branch July 6, 2021 11:25
@monperrus monperrus mentioned this pull request Aug 19, 2021
woutersmeenk pushed a commit to woutersmeenk/spoon that referenced this pull request Aug 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: Question about equality of CtCodeSnippetExpression
4 participants