Skip to content

Commit

Permalink
Merge pull request junit-team#675 from avandeursen/compactfailuretest
Browse files Browse the repository at this point in the history
Test class for String Comparison Compactors, and off-by-one refactoring of compactor.
  • Loading branch information
David Saff committed May 9, 2013
2 parents beb1f4a + dc730e3 commit 1ef54a1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main/java/org/junit/ComparisonFailure.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ private static class ComparisonCompactor {
* is exceeded, the Strings are shortened
*/
private int fContextLength;

private String fExpected;
private String fActual;

/**
* The length of the shared prefix / suffix of the expected and actual strings.
* Equals to zero if the strings do not share a common prefix/suffix.
*/
private int fPrefix;
private int fSuffix;

Expand Down Expand Up @@ -103,7 +109,7 @@ private String compact(String message) {
}

private String compactString(String source) {
String result = DELTA_START + source.substring(fPrefix, source.length() - fSuffix + 1) + DELTA_END;
String result = DELTA_START + source.substring(fPrefix, source.length() - fSuffix) + DELTA_END;
if (fPrefix > 0) {
result = computeCommonPrefix() + result;
}
Expand Down Expand Up @@ -131,16 +137,16 @@ private void findCommonSuffix() {
break;
}
}
fSuffix = fExpected.length() - expectedSuffix;
fSuffix = fExpected.length() - expectedSuffix - 1;
}

private String computeCommonPrefix() {
return (fPrefix > fContextLength ? ELLIPSIS : "") + fExpected.substring(Math.max(0, fPrefix - fContextLength), fPrefix);
}

private String computeCommonSuffix() {
int end = Math.min(fExpected.length() - fSuffix + 1 + fContextLength, fExpected.length());
return fExpected.substring(fExpected.length() - fSuffix + 1, end) + (fExpected.length() - fSuffix + 1 < fExpected.length() - fContextLength ? ELLIPSIS : "");
int end = Math.min(fExpected.length() - fSuffix + fContextLength, fExpected.length());
return fExpected.substring(fExpected.length() - fSuffix, end) + (fExpected.length() - fSuffix < fExpected.length() - fContextLength ? ELLIPSIS : "");
}

private boolean areStringsEqual() {
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/junit/tests/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.junit.tests.assertion.AssertionTest;
import org.junit.tests.assertion.ComparisonFailureTest;
import org.junit.tests.assertion.MultipleFailureExceptionTest;
import org.junit.tests.deprecated.JUnit4ClassRunnerTest;
import org.junit.tests.description.AnnotatedDescriptionTest;
Expand Down Expand Up @@ -108,6 +109,7 @@
AssertionTest.class,
CommandLineTest.class,
ExpectedTest.class,
ComparisonFailureTest.class,
MultipleFailureExceptionTest.class,
ForwardCompatibilityTest.class,
OldTests.class,
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/org/junit/tests/assertion/ComparisonFailureTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.junit.tests.assertion;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.ComparisonFailure;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ComparisonFailureTest {

private String expected, actual, message;

public ComparisonFailureTest(String e, String a, String m) {
expected = e;
actual = a;
message = m;
}

@Parameters(name = "compact-msg-{index}, exp=\"{1}\"")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
// simple base case
{ "a", "b", "expected:<[a]> but was:<[b]>" },

// common prefix
{ "ba", "bc", "expected:<b[a]> but was:<b[c]>" },

// common suffix
{ "ab", "cb", "expected:<[a]b> but was:<[c]b>" },

// common pre and suffix
{ "abc", "adc", "expected:<a[b]c> but was:<a[d]c>" },

// expected is subset of actual
{ "ab", "abc", "expected:<ab[]> but was:<ab[c]>" },

// expected is superset of actual
{ "abc", "ab", "expected:<ab[c]> but was:<ab[]>" },

// overlapping matches.
{ "abc", "abbc", "expected:<ab[]c> but was:<ab[b]c>" },

// long prefix yielding "..."
{ "01234567890123456789PRE:hello:POST",
"01234567890123456789PRE:world:POST",
"expected:<...4567890123456789PRE:[hello]:POST> but was:<...4567890123456789PRE:[world]:POST>" },

// long suffix yielding "..."
{ "PRE:hello:01234567890123456789POST",
"PRE:world:01234567890123456789POST",
"expected:<PRE:[hello]:0123456789012345678...> but was:<PRE:[world]:0123456789012345678...>"
},

// bug609972
{ "S&P500", "0", "expected:<[S&P50]0> but was:<[]0>" },

// empty expected string
{ "", "a", "expected:<[]> but was:<[a]>" },

// empty actual string
{ "a", "", "expected:<[a]> but was:<[]>" }

});
}

@Test
public void compactFailureMessage() {
ComparisonFailure failure = new ComparisonFailure("", expected, actual);
assertEquals(message, failure.getMessage());
}

}

0 comments on commit 1ef54a1

Please sign in to comment.