File tree Expand file tree Collapse file tree
main/java/org/hamcrest/core
test/java/org/hamcrest/core
hamcrest-library/src/main/java/org/hamcrest Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments