Skip to content

Commit 9787c93

Browse files
committed
hamcrest#90 add implementation for CharSequence length matcher
1 parent 688f638 commit 9787c93

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.TypeSafeMatcher;
6+
7+
/**
8+
* @author Marco Leichsenring
9+
*/
10+
public class CharSequenceLength extends TypeSafeMatcher<CharSequence> {
11+
private final int length;
12+
13+
public CharSequenceLength(int length) {
14+
this.length = length;
15+
}
16+
17+
@Override
18+
public boolean matchesSafely(CharSequence chars) {
19+
return chars.length() == length;
20+
}
21+
22+
@Override
23+
public void describeMismatchSafely(CharSequence chars, Description mismatchDescription) {
24+
mismatchDescription.appendValue(chars).appendText(" does not have length ").appendValue(length);
25+
}
26+
27+
@Override
28+
public void describeTo(Description description) {
29+
description.appendText("a string with length ").appendValue(length);
30+
}
31+
32+
/**
33+
* Creates a matcher of {@link CharSequence} that matches when a char sequence has the length
34+
* of the specified <code>argument</code>.
35+
* For example:
36+
*
37+
* <pre>
38+
* assertThat("text", length(4))
39+
* </pre>
40+
*
41+
* @param length
42+
* the expected length of the string
43+
*/
44+
public static Matcher<CharSequence> length(int length) {
45+
return new CharSequenceLength(length);
46+
}
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
5+
6+
import static org.hamcrest.core.CharSequenceLength.*;
7+
8+
public class CharSequenceLengthTest extends AbstractMatcherTest {
9+
private final Matcher<CharSequence> matcher = length(4);
10+
11+
@Override
12+
protected Matcher<?> createMatcher() {
13+
return length(4);
14+
}
15+
16+
public void test_matchesIfArgumentIsEqualToADoubleValueWithinSomeError() {
17+
assertMatches(matcher, "aaaa");
18+
assertMatches(matcher, "a b ");
19+
20+
assertDoesNotMatch("\"aaaaaa\" does not have length 4", matcher, "aaaaaa");
21+
assertMismatchDescription("\"aaaaaa\" does not have length <4>", matcher, "aaaaaa");
22+
}
23+
24+
}

‎hamcrest-library/src/main/java/org/hamcrest/Matchers.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,22 @@ public static <T> org.hamcrest.Matcher<T> sameInstance(T target) {
458458
public static <T> org.hamcrest.Matcher<T> theInstance(T target) {
459459
return org.hamcrest.core.IsSame.theInstance(target);
460460
}
461+
462+
/**
463+
* Creates a matcher of {@link CharSequence} that matches when a char sequence has the length
464+
* of the specified <code>argument</code>.
465+
* For example:
466+
*
467+
* <pre>
468+
* assertThat("text", length(4))
469+
* </pre>
470+
*
471+
* @param length
472+
* the expected length of the string
473+
*/
474+
public static Matcher<CharSequence> hasLength(int length) {
475+
return org.hamcrest.core.CharSequenceLength.length(length);
476+
}
461477

462478
/**
463479
* Creates a matcher that matches if the examined {@link String} contains the specified

0 commit comments

Comments
 (0)