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

Added equalToWithDiff for comparing strings with diff message #339

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions hamcrest/hamcrest.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
testImplementation(group: 'junit', name: 'junit', version: '4.13') {
transitive = false
}
implementation "io.github.java-diff-utils:java-diff-utils:4.5"
}

jar {
Expand Down
13 changes: 13 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,19 @@ public static Matcher<java.lang.String> equalToIgnoringCase(java.lang.String exp
return org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase(expectedString);
}

/**
* Creates a matcher of {@link String} that matches when the examined string is equal to
* the specified expectedString, with markdown style diff message when mismatch
* For example:
* <pre>assertThat("Foo", equalToWithDiff("FOO"))</pre>
*
* @param expectedString
* the expected value of matched strings
*/
public static Matcher<java.lang.String> equalToWithDiff(java.lang.String expectedString) {
return org.hamcrest.text.IsEqualWithDiff.equalToWithDiff(expectedString);
}

/**
* @deprecated {@link #equalToCompressingWhiteSpace(String)}
* @param expectedString
Expand Down
92 changes: 92 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/text/IsEqualWithDiff.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.hamcrest.text;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import com.github.difflib.text.DiffRowGenerator;
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.text.DiffRow;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class IsEqualWithDiff extends TypeSafeMatcher<String> {
private final String string;

public IsEqualWithDiff(String string) {
if (string == null) {
throw new IllegalArgumentException("Non-null value required");
}
this.string = string;
}

@Override
public boolean matchesSafely(String item) {
return string.equals(item);
}

@Override
public void describeMismatchSafely(String item, Description mismatchDescription) {
mismatchDescription.appendText("Diff: \n\t");
List<String> expected = Arrays.asList(string.split("\n"));
List<String> actual = Arrays.asList(item.split("\n"));

DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.mergeOriginalRevised(true)
.reportLinesUnchanged(false)
.inlineDiffByWord(true)
.oldTag(new Function<Boolean, String>() {
@Override
public String apply(Boolean aBoolean) {
return "~";
}
})
.newTag(new Function<Boolean, String>() {
@Override
public String apply(Boolean aBoolean) {
return "**";
}
})
.build();
List<DiffRow> rows = null;
try {
rows = generator.generateDiffRows(
actual,
expected
);
} catch (DiffException e) {
e.printStackTrace();
}

for (int ii = 0; ii < rows.size(); ii++) {
if (rows.get(ii).getTag() == DiffRow.Tag.CHANGE)
{
mismatchDescription.appendText(String.format("At line %d: ", ii));
mismatchDescription.appendText(rows.get(ii).getOldLine()).appendText("\n\t");
}
}
}

@Override
public void describeTo(Description description) {
description.appendText("a string equal to ")
.appendValue(string)
.appendText(" with diff when mismatch");
}

/**
* Creates a matcher of {@link String} that matches when the examined string is equal to
* the specified expectedString, with markdown style diff output message when mismatch.
* For example:
* <pre>assertThat("Foo", equalToIgnoringCase("FOO"))</pre>
*
* @param expectedString
* the expected value of matched strings
*/
public static Matcher<String> equalToWithDiff(String expectedString) {
return new IsEqualWithDiff(expectedString);
}
}
41 changes: 41 additions & 0 deletions hamcrest/src/test/java/org/hamcrest/text/IsEqualWithDiffTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.hamcrest.text;

import org.hamcrest.Matcher;

import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.junit.Test;

import static org.hamcrest.AbstractMatcherTest.assertMatches;
import static org.hamcrest.text.IsEqualWithDiff.equalToWithDiff;


public class IsEqualWithDiffTest {
/*
Test the correctness of diff message
*/
@Test public void
testMismatchesWithCorrectDiffMessage() {
final String actual = "This is first see you, bye\nhbha";
final Description expectedDescription = new StringDescription()
.appendText("Diff: \n\t")
.appendText("At line 0: This is first ~see~**hello** ~you~**world**, bye\n\t")
.appendText("At line 1: ~hbha~**haha**\n\t");
Description actualDescription = new StringDescription();
final Matcher<String> matcher = equalToWithDiff("This is first hello world, bye\nhaha");
matcher.describeMismatch(actual, actualDescription);

assertMatches(equalToWithDiff(expectedDescription.toString()), actualDescription.toString());
}

/*
Test the correctness of Matcher itself
*/
@Test public void
testMatcherCorrectness() {
final String actual = "This is first see you, bye\nhbha";
final String expected = "This is first see you, bye\nhbha";

assertMatches(equalToWithDiff(expected), actual);
}
}