Skip to content

Commit 717115e

Browse files
committed
tidying tests
1 parent c818866 commit 717115e

5 files changed

Lines changed: 18 additions & 46 deletions

File tree

‎hamcrest-core/src/main/java/org/hamcrest/core/StringContains.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@
1010
*/
1111
public class StringContains extends SubstringMatcher {
1212
public StringContains(String substring) {
13-
super(substring);
13+
super("containing", substring);
1414
}
1515

1616
@Override
1717
protected boolean evalSubstringOf(String s) {
1818
return s.contains(substring);
1919
}
2020

21-
@Override
22-
protected String relationship() {
23-
return "containing";
24-
}
25-
2621
/**
2722
* Creates a matcher that matches if the examined {@link String} contains the specified
2823
* {@link String} anywhere.

‎hamcrest-core/src/main/java/org/hamcrest/core/StringEndsWith.java‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@
99
* Tests if the argument is a string that contains a substring.
1010
*/
1111
public class StringEndsWith extends SubstringMatcher {
12-
public StringEndsWith(String substring) {
13-
super(substring);
14-
}
12+
public StringEndsWith(String substring) { super("ending with", substring); }
1513

1614
@Override
1715
protected boolean evalSubstringOf(String s) {
1816
return s.endsWith(substring);
1917
}
2018

21-
@Override
22-
protected String relationship() {
23-
return "ending with";
24-
}
25-
2619
/**
2720
* Creates a matcher that matches if the examined {@link String} ends with the specified
2821
* {@link String}.

‎hamcrest-core/src/main/java/org/hamcrest/core/StringStartsWith.java‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@
99
* Tests if the argument is a string that contains a substring.
1010
*/
1111
public class StringStartsWith extends SubstringMatcher {
12-
public StringStartsWith(String substring) {
13-
super(substring);
14-
}
12+
public StringStartsWith(String substring) { super("starting with", substring); }
1513

1614
@Override
1715
protected boolean evalSubstringOf(String s) {
1816
return s.startsWith(substring);
1917
}
2018

21-
@Override
22-
protected String relationship() {
23-
return "starting with";
24-
}
25-
2619
/**
2720
* Creates a matcher that matches if the examined {@link String} starts with the specified
2821
* {@link String}.
@@ -38,4 +31,4 @@ public static Matcher<String> startsWith(String prefix) {
3831
return new StringStartsWith(prefix);
3932
}
4033

41-
}
34+
}

‎hamcrest-core/src/main/java/org/hamcrest/core/SubstringMatcher.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ public abstract class SubstringMatcher extends TypeSafeMatcher<String> {
88
// TODO: Replace String with CharSequence to allow for easy interoperability between
99
// String, StringBuffer, StringBuilder, CharBuffer, etc (joe).
1010

11+
private final String relationship;
1112
protected final String substring;
1213

13-
protected SubstringMatcher(final String substring) {
14+
protected SubstringMatcher(String relationship, String substring) {
15+
this.relationship = relationship;
1416
this.substring = substring;
1517
}
1618

@@ -26,12 +28,11 @@ public void describeMismatchSafely(String item, Description mismatchDescription)
2628
@Override
2729
public void describeTo(Description description) {
2830
description.appendText("a string ")
29-
.appendText(relationship())
31+
.appendText(relationship)
3032
.appendText(" ")
3133
.appendValue(substring);
3234
}
3335

3436
protected abstract boolean evalSubstringOf(String string);
3537

36-
protected abstract String relationship();
37-
}
38+
}

‎hamcrest-core/src/test/java/org/hamcrest/core/StringContainsTest.java‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,27 @@
66

77
import org.hamcrest.AbstractMatcherTest;
88
import org.hamcrest.Matcher;
9+
import org.junit.Test;
910

1011

1112
public class StringContainsTest extends AbstractMatcherTest {
1213
static final String EXCERPT = "EXCERPT";
13-
Matcher<String> stringContains = containsString(EXCERPT);
14+
final Matcher<String> stringContains = containsString(EXCERPT);
1415

1516
@Override
1617
protected Matcher<?> createMatcher() {
1718
return stringContains;
1819
}
1920

20-
public void testEvaluatesToTrueIfArgumentContainsSpecifiedSubstring() {
21-
assertTrue("should be true if excerpt at beginning",
22-
stringContains.matches(EXCERPT + "END"));
23-
assertTrue("should be true if excerpt at end",
24-
stringContains.matches("START" + EXCERPT));
25-
assertTrue("should be true if excerpt in middle",
26-
stringContains.matches("START" + EXCERPT + "END"));
27-
assertTrue("should be true if excerpt is repeated",
28-
stringContains.matches(EXCERPT + EXCERPT));
29-
30-
assertFalse("should not be true if excerpt is not in string",
31-
stringContains.matches("Something else"));
32-
assertFalse("should not be true if part of excerpt is in string",
33-
stringContains.matches(EXCERPT.substring(1)));
21+
public void testMatchesSubstrings() {
22+
assertMatches(stringContains, EXCERPT + "END");
23+
assertMatches(stringContains, "START" + EXCERPT);
24+
assertMatches(stringContains, "START" + EXCERPT + "END");
25+
assertMatches(stringContains, EXCERPT + EXCERPT);
26+
assertMismatchDescription("was \"Something else\"", stringContains, "Something else");
27+
assertMismatchDescription("was \"XC\"", stringContains, "XC");
3428
}
3529

36-
public void testEvaluatesToTrueIfArgumentIsEqualToSubstring() {
37-
assertTrue("should be true if excerpt is entire string",
38-
stringContains.matches(EXCERPT));
39-
}
4030

4131
public void testHasAReadableDescription() {
4232
assertDescription("a string containing \"EXCERPT\"", stringContains);

0 commit comments

Comments
 (0)