Skip to content

Issue #1432 Asserts for charsequence #1439

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

Closed
wants to merge 9 commits into from
Closed

Issue #1432 Asserts for charsequence #1439

wants to merge 9 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Apr 16, 2017

Closed Pull Request #1438 opened this one as requested

Copy link
Member

@kcooney kcooney left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes.

I pushed another commit to the branch with some minor style fixes, so you'll probably want to pull from your github branch to your local branch before making new commits.

(yes, ideally we should use a plugin like "spotless" to fix and enforce our style, but that hasn't be implemented yet)

* @param actual the object to compare to
*/
public static void assertContentEquals(String message, String expected, CharSequence actual) {
if (expected == null && actual == null) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should consider writing it like this:

if (expected == actual) {
  return;
}
if (expected == null || actual == null) {
  failNotEquals(message, expected, actual
}
...

That way, we avoid calling actual.toString() when expected and actual point to the same instance.

That being said, since the method is named "assertContentEquals" (and not "assertEquals") perhaps passing in null for expected should be an error (resulting in a NullPointerException). We don't do that anywhere else in this class, but I thought I'd put the idea out there in case someone might want to argue for it.

Copy link
Author

Choose a reason for hiding this comment

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

Should I make these changes, or are they still open for discussion from others?

Copy link
Member

Choose a reason for hiding this comment

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

As a potential user of this method, how should it handle null values?

(I assume short-circuiting the method if expected == actual is not controversial)

Copy link

@IgorNosach25 IgorNosach25 Jun 8, 2018

Choose a reason for hiding this comment

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

Signature of method is not correct. We must have opportunity to compare 2 instances of Char Sequence interface.

Copy link
Member

Choose a reason for hiding this comment

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

We decided that the expected value should be a string. According to the CharSequence Javadoc "The result of comparing two objects that implement CharSequence is therefore, in general, undefined"

Copy link

@IgorNosach25 IgorNosach25 Jun 9, 2018

Choose a reason for hiding this comment

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

You are right. I was confused because the question is open. Please close topic if we don't need compare 2 CharSequence instances (or String, because CharSequence doesn't has contract for equals
method ). How I can see actual open issues? Please answer me in private message) thanks

Copy link

@IgorNosach25 IgorNosach25 Jun 9, 2018

Choose a reason for hiding this comment

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

In my view Junit must has only methods for comparing primitive types and objects by equals. There are tests that fail in JUnit also. It's strange for me. Also there are some explainings why these tests fall.

failNotEquals(message, expected, actual);
}

assertEquals(contentsDifferentMessage(message), expected.length(), actual.length());
Copy link
Member

@kcooney kcooney Apr 17, 2017

Choose a reason for hiding this comment

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

Moving the conversation about checking the length first to this pull request...

@sabi0 apparently suggested to check for the lengths to avoid the toString() call. I agree that it wouldn't be that helpful to failing early if the lengths differ with a message that just prints the lengths. We do, however, have one close precedent in JUnit, and that's when comparing arrays. If the lengths are different, we use a message like this:

"<user supplied message>: array lengths differed, expected.length=2 actual.length=1; arrays first differed at element [1]; expected:<false> but was:<end of array>"

(with no user-supplied message, the message starts with "array lengths...")

But I think assertArrayEquals does that because 1) arrays are often large, 2) the printed representation of arrays can be hard to read and 3) visually seeing the difference between two arrays in an error message is difficult. Here, we throw a ComparisonFailure, and the exception message generated by JUnit will show which parts of the string differ (plus IDEs often show a nice UI for ComparisonFailure). None of that applies here, so I think that even stating that the lengths are different in the exception isn't that useful for the method here.

Copy link
Author

Choose a reason for hiding this comment

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

Should I make these changes, or are they still open for discussion from others?

Copy link
Member

Choose a reason for hiding this comment

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

No one chimed in, so let's not mention the length difference in the exception message. I do think we should optimize for the expected == actual.

}

private static String contentsDifferentMessage(String message) {
return message == null ? "Contents different length" : message + " contents different length";

Choose a reason for hiding this comment

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

This could be simplified:

return (message != null ? message + ": " : "") + "Content of different length";

* @param expected the expected contents
* @param actual the object to compare to
*/
public static void assertContentEquals(String expected, CharSequence actual) {

Choose a reason for hiding this comment

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

Suggestion - why not make the expected a CharSequence too?

Copy link

Choose a reason for hiding this comment

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

See the original discussion: #1432 (comment)

Copy link

Choose a reason for hiding this comment

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

... and also here: #1438 (comment)

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.

4 participants