Skip to content

Commit 7a64e23

Browse files
author
jon.m.reid
committed
- Added missing tests for descriptions
- Added missing tests for nested matchers - Added missing comments - Code unification (for example, put createMatcher first in tests)
1 parent 7ca7be5 commit 7a64e23

24 files changed

Lines changed: 178 additions & 72 deletions

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* A {@link Description} that is stored as a string.
1313
*/
1414
public abstract class BaseDescription implements Description {
15+
1516
public Description appendText(String text) {
1617
append(text);
1718
return this;
@@ -83,18 +84,19 @@ private Description appendList(String start, String separator, String end, Itera
8384
return this;
8485
}
8586

86-
87-
/** Append the String <var>str</var> to the description.
88-
* The default implementation passes every character to {@link #append(char)}.
89-
* Override in subclasses to provide an efficient implementation.
87+
/**
88+
* Append the String <var>str</var> to the description.
89+
* The default implementation passes every character to {@link #append(char)}.
90+
* Override in subclasses to provide an efficient implementation.
9091
*/
9192
protected void append(String str) {
9293
for (int i = 0; i < str.length(); i++) {
9394
append(str.charAt(i));
9495
}
9596
}
9697

97-
/** Append the char <var>c</var> to the description.
98+
/**
99+
* Append the char <var>c</var> to the description.
98100
*/
99101
protected abstract void append(char c);
100102

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
* Decorates another Matcher, retaining the behavior but allowing tests
1212
* to be slightly more expressive.
1313
*
14-
* eg. assertThat(cheese, equalTo(smelly))
15-
* vs assertThat(cheese, is(equalTo(smelly)))
14+
* For example: assertThat(cheese, equalTo(smelly))
15+
* vs. assertThat(cheese, is(equalTo(smelly)))
1616
*/
1717
public class Is<T> extends BaseMatcher<T> {
18-
1918
private final Matcher<T> matcher;
2019

2120
public Is(Matcher<T> matcher) {
@@ -34,8 +33,8 @@ public void describeTo(Description description) {
3433
* Decorates another Matcher, retaining the behavior but allowing tests
3534
* to be slightly more expressive.
3635
*
37-
* eg. assertThat(cheese, equalTo(smelly))
38-
* vs assertThat(cheese, is(equalTo(smelly)))
36+
* For example: assertThat(cheese, equalTo(smelly))
37+
* vs. assertThat(cheese, is(equalTo(smelly)))
3938
*/
4039
@Factory
4140
public static <T> Matcher<T> is(Matcher<T> matcher) {
@@ -45,8 +44,8 @@ public static <T> Matcher<T> is(Matcher<T> matcher) {
4544
/**
4645
* This is a shortcut to the frequently used is(equalTo(x)).
4746
*
48-
* eg. assertThat(cheese, is(equalTo(smelly)))
49-
* vs assertThat(cheese, is(smelly))
47+
* For example: assertThat(cheese, is(equalTo(smelly)))
48+
* vs. assertThat(cheese, is(smelly))
5049
*/
5150
@Factory
5251
public static <T> Matcher<T> is(T value) {
@@ -56,13 +55,11 @@ public static <T> Matcher<T> is(T value) {
5655
/**
5756
* This is a shortcut to the frequently used is(instanceOf(SomeClass.class)).
5857
*
59-
* eg. assertThat(cheese, is(instanceOf(Cheddar.class)))
60-
* vs assertThat(cheese, is(Cheddar.class))
58+
* For example: assertThat(cheese, is(instanceOf(Cheddar.class)))
59+
* vs. assertThat(cheese, is(Cheddar.class))
6160
*/
6261
@Factory
6362
public static Matcher<Object> is(Class<?> type) {
6463
return is(instanceOf(type));
6564
}
66-
6765
}
68-

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public static <T> Matcher<T> not(Matcher<T> matcher) {
4040
/**
4141
* This is a shortcut to the frequently used not(equalTo(x)).
4242
*
43-
* eg. assertThat(cheese, is(not(equalTo(smelly))))
44-
* vs assertThat(cheese, is(not(smelly)))
43+
* For example: assertThat(cheese, is(not(equalTo(smelly))))
44+
* vs. assertThat(cheese, is(not(smelly)))
4545
*/
4646
@Factory
4747
public static <T> Matcher<T> not(T value) {

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayWithSize.java‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import org.hamcrest.Matcher;
99
import org.hamcrest.TypeSafeMatcher;
1010

11+
/**
12+
* Matches if array size satisfies a nested matcher.
13+
*/
1114
public class IsArrayWithSize<E> extends TypeSafeMatcher<E[]> {
1215
private final Matcher<Integer> sizeMatcher;
1316

@@ -25,16 +28,28 @@ public void describeTo(Description description) {
2528
.appendDescriptionOf(sizeMatcher);
2629
}
2730

31+
/**
32+
* Does array size satisfy a given matcher?
33+
*/
2834
@Factory
29-
public static <E> Matcher<E[]> arrayWithSize(int size) {
30-
return arrayWithSize(equalTo(size));
35+
public static <E> Matcher<E[]> arrayWithSize(Matcher<Integer> sizeMatcher) {
36+
return new IsArrayWithSize<E>(sizeMatcher);
3137
}
3238

39+
/**
40+
* This is a shortcut to the frequently used arrayWithSize(equalTo(x)).
41+
*
42+
* For example, assertThat(arrayWithSize(equal_to(x)))
43+
* vs. assertThat(arrayWithSize(x))
44+
*/
3345
@Factory
34-
public static <E> Matcher<E[]> arrayWithSize(Matcher<Integer> sizeMatcher) {
35-
return new IsArrayWithSize<E>(sizeMatcher);
46+
public static <E> Matcher<E[]> arrayWithSize(int size) {
47+
return arrayWithSize(equalTo(size));
3648
}
3749

50+
/**
51+
* Matches an empty array.
52+
*/
3853
@Factory
3954
public static <E> Matcher<E[]> emptyArray() {
4055
Matcher<E[]> isEmpty = arrayWithSize(0);

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsCollectionWithSize.java‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import org.hamcrest.Matcher;
1010
import org.hamcrest.TypeSafeMatcher;
1111

12+
/**
13+
* Matches if collection size satisfies a nested matcher.
14+
*/
1215
public class IsCollectionWithSize<E> extends TypeSafeMatcher<Collection<E>> {
1316
private final Matcher<Integer> sizeMatcher;
1417

@@ -26,13 +29,22 @@ public void describeTo(Description description) {
2629
.appendDescriptionOf(sizeMatcher);
2730
}
2831

32+
/**
33+
* Does collection size satisfy a given matcher?
34+
*/
2935
@Factory
30-
public static <E> Matcher<Collection<E>> hasSize(int size) {
31-
return hasSize(equalTo(size));
36+
public static <E> Matcher<Collection<E>> hasSize(Matcher<Integer> size) {
37+
return new IsCollectionWithSize<E>(size);
3238
}
3339

40+
/**
41+
* This is a shortcut to the frequently used hasSize(equalTo(x)).
42+
*
43+
* For example, assertThat(hasSize(equal_to(x)))
44+
* vs. assertThat(hasSize(x))
45+
*/
3446
@Factory
35-
public static <E> Matcher<Collection<E>> hasSize(Matcher<Integer> size) {
36-
return new IsCollectionWithSize<E>(size);
47+
public static <E> Matcher<Collection<E>> hasSize(int size) {
48+
return hasSize(equalTo(size));
3749
}
3850
}

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsEmptyCollection.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
import org.hamcrest.Matcher;
88
import org.hamcrest.TypeSafeMatcher;
99

10+
/**
11+
* Tests if collection is empty.
12+
*/
1013
public class IsEmptyCollection<E> extends TypeSafeMatcher<Collection<E>> {
14+
1115
@Override
1216
public boolean matchesSafely(Collection<E> item) {
1317
return item.isEmpty();
@@ -17,6 +21,9 @@ public void describeTo(Description description) {
1721
description.appendText("an empty collection");
1822
}
1923

24+
/**
25+
* Matches an empty collection.
26+
*/
2027
@Factory
2128
public static <E> Matcher<Collection<E>> empty() {
2229
return new IsEmptyCollection<E>();

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsEmptyIterable.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import org.hamcrest.Matcher;
66
import org.hamcrest.TypeSafeMatcher;
77

8+
/**
9+
* Tests if collection is empty.
10+
*/
811
public class IsEmptyIterable<E> extends TypeSafeMatcher<Iterable<E>> {
12+
913
@Override
1014
public boolean matchesSafely(Iterable<E> iterable) {
1115
return !iterable.iterator().hasNext();
@@ -15,6 +19,9 @@ public void describeTo(Description description) {
1519
description.appendText("is an empty iterable");
1620
}
1721

22+
/**
23+
* Matches an empty iterable.
24+
*/
1825
@Factory
1926
public static <E> Matcher<Iterable<E>> emptyIterable() {
2027
return new IsEmptyIterable<E>();

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsIterableWithSize.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public void describeTo(Description description) {
3030
}
3131

3232
@Factory
33-
public static <E> Matcher<Iterable<E>> iterableWithSize(int size) {
34-
return iterableWithSize(equalTo(size));
33+
public static <E> Matcher<Iterable<E>> iterableWithSize(Matcher<Integer> sizeMatcher) {
34+
return new IsIterableWithSize<E>(sizeMatcher);
3535
}
3636

3737
@Factory
38-
public static <E> Matcher<Iterable<E>> iterableWithSize(Matcher<Integer> sizeMatcher) {
39-
return new IsIterableWithSize<E>(sizeMatcher);
38+
public static <E> Matcher<Iterable<E>> iterableWithSize(int size) {
39+
return iterableWithSize(equalTo(size));
4040
}
4141
}

‎hamcrest-library/src/main/java/org/hamcrest/number/OrderingComparison.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,33 @@ else if (compare == 0) {
4141
}
4242
}
4343

44+
/**
45+
* Is value > expected?
46+
*/
4447
@Factory
4548
public static <T extends Comparable<T>> Matcher<T> greaterThan(T value) {
4649
return new OrderingComparison<T>(value, -1, -1);
4750
}
4851

52+
/**
53+
* Is value >= expected?
54+
*/
4955
@Factory
5056
public static <T extends Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value) {
5157
return new OrderingComparison<T>(value, -1, 0);
5258
}
5359

60+
/**
61+
* Is value < expected?
62+
*/
5463
@Factory
5564
public static <T extends Comparable<T>> Matcher<T> lessThan(T value) {
5665
return new OrderingComparison<T>(value, 1, 1);
5766
}
5867

68+
/**
69+
* Is value <= expected?
70+
*/
5971
@Factory
6072
public static <T extends Comparable<T>> Matcher<T> lessThanOrEqualTo(T value) {
6173
return new OrderingComparison<T>(value, 0, 1);

‎hamcrest-library/src/main/java/org/hamcrest/object/HasToString.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.hamcrest.Matcher;
99

1010
public class HasToString<T> extends BaseMatcher<T> {
11-
1211
private final Matcher<String> toStringMatcher;
1312

1413
public HasToString(Matcher<String> toStringMatcher) {
@@ -26,11 +25,20 @@ public void describeTo(Description description) {
2625
.appendText(")");
2726
}
2827

28+
/**
29+
* Evaluates whether item.toString() satisfies a given matcher.
30+
*/
2931
@Factory
3032
public static <T> Matcher<T> hasToString(Matcher<String> toStringMatcher) {
3133
return new HasToString<T>(toStringMatcher);
3234
}
3335

36+
/**
37+
* This is a shortcut to the frequently used has_string(equalTo(x)).
38+
*
39+
* For example, assertThat(hasToString(equal_to(x)))
40+
* vs. assertThat(hasToString(x))
41+
*/
3442
@Factory
3543
public static <T> Matcher<T> hasToString(String expectedToString) {
3644
return new HasToString<T>(equalTo(expectedToString));

0 commit comments

Comments
 (0)