Skip to content

Commit 3545476

Browse files
author
joeretro
committed
Readded core constraints.
1 parent a1db5f7 commit 3545476

26 files changed

Lines changed: 828 additions & 0 deletions
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.core;
4+
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.Description;
7+
8+
/**
9+
* Always evaluate to true or false (depending on what's passed into the constructor).
10+
*/
11+
public class Always implements Matcher {
12+
13+
private final boolean matches;
14+
private final String description;
15+
16+
public Always(boolean matches) {
17+
this(matches, "always evaluates to " + matches);
18+
}
19+
20+
public Always(boolean matches, String description) {
21+
this.matches = matches;
22+
this.description = description;
23+
}
24+
25+
public boolean match(Object o) {
26+
return matches;
27+
}
28+
29+
public void describeTo(Description description) {
30+
description.append(this.description);
31+
}
32+
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.core;
4+
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.Description;
7+
8+
9+
/**
10+
* Calculates the logical conjunction of two matchers. Evaluation is
11+
* shortcut, so that the second matcher is not called if the first
12+
* matcher returns <code>false</code>.
13+
*/
14+
public class And implements Matcher {
15+
private final Matcher left;
16+
private final Matcher right;
17+
18+
public And(Matcher left, Matcher right) {
19+
this.left = left;
20+
this.right = right;
21+
}
22+
23+
public boolean match(Object o) {
24+
return left.match(o) && right.match(o);
25+
}
26+
27+
public void describeTo(Description description) {
28+
description.append("(");
29+
left.describeTo(description);
30+
description.append(" and ");
31+
right.describeTo(description);
32+
description.append(")");
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.core;
4+
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.Description;
7+
8+
/**
9+
* Provides a custom description to another matcher.
10+
*/
11+
public class DescribedAs implements Matcher {
12+
13+
private final String description;
14+
private final Matcher matcher;
15+
16+
public DescribedAs(String description, Matcher matcher) {
17+
this.description = description;
18+
this.matcher = matcher;
19+
}
20+
21+
public boolean match(Object o) {
22+
return matcher.match(o);
23+
}
24+
25+
public void describeTo(Description description) {
26+
description.append(this.description);
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.core;
4+
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.Description;
7+
8+
9+
/**
10+
* A matcher that always returns <code>true</code>.
11+
*/
12+
public class IsAnything implements Matcher {
13+
public static final IsAnything INSTANCE = new IsAnything();
14+
private final String description;
15+
16+
public IsAnything() {
17+
this("ANYTHING");
18+
}
19+
20+
public IsAnything(String description) {
21+
this.description = description;
22+
}
23+
24+
public boolean match(Object o) {
25+
return true;
26+
}
27+
28+
public void describeTo(Description description) {
29+
description.append(this.description);
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.Matcher;
4+
import org.hamcrest.Description;
5+
6+
public class IsCompatibleType implements Matcher {
7+
private final Class type;
8+
9+
public IsCompatibleType(Class type) {
10+
this.type = type;
11+
}
12+
13+
public boolean match(Object o) {
14+
return o instanceof Class && type.isAssignableFrom((Class) o);
15+
}
16+
17+
public void describeTo(Description description) {
18+
description.append("type < ").append(type.getName());
19+
}
20+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.core;
4+
5+
import org.hamcrest.Formatting;
6+
import org.hamcrest.Matcher;
7+
import org.hamcrest.Description;
8+
9+
import java.lang.reflect.Array;
10+
11+
12+
/**
13+
* Is the value equal to another value, as tested by the
14+
* {@link java.lang.Object#equals} invokedMethod?
15+
*/
16+
public class IsEqual implements Matcher {
17+
private final Object object;
18+
19+
public IsEqual(Object equalArg) {
20+
object = equalArg;
21+
}
22+
23+
public boolean match(Object arg) {
24+
return areEqual(object, arg);
25+
}
26+
27+
public void describeTo(Description description) {
28+
description.append("eq(")
29+
.append(Formatting.toReadableString(object))
30+
.append(")");
31+
}
32+
33+
private static boolean areEqual(Object o1, Object o2) {
34+
if (o1 == null || o2 == null) {
35+
return o1 == null && o2 == null;
36+
} else if (isArray(o1)) {
37+
return isArray(o2) && areArraysEqual(o1, o2);
38+
} else {
39+
return o1.equals(o2);
40+
}
41+
}
42+
43+
private static boolean areArraysEqual(Object o1, Object o2) {
44+
return areArrayLengthsEqual(o1, o2)
45+
&& areArrayElementsEqual(o1, o2);
46+
}
47+
48+
private static boolean areArrayLengthsEqual(Object o1, Object o2) {
49+
return Array.getLength(o1) == Array.getLength(o2);
50+
}
51+
52+
private static boolean areArrayElementsEqual(Object o1, Object o2) {
53+
for (int i = 0; i < Array.getLength(o1); i++) {
54+
if (!areEqual(Array.get(o1, i), Array.get(o2, i))) return false;
55+
}
56+
return true;
57+
}
58+
59+
private static boolean isArray(Object o) {
60+
return o.getClass().isArray();
61+
}
62+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.Formatting;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.Description;
6+
7+
import java.util.Arrays;
8+
import java.util.Collection;
9+
10+
public class IsIn implements Matcher {
11+
private final Collection collection;
12+
13+
public IsIn(Collection collection) {
14+
this.collection = collection;
15+
}
16+
17+
public IsIn(Object[] elements) {
18+
collection = Arrays.asList(elements);
19+
}
20+
21+
public boolean match(Object o) {
22+
return collection.contains(o);
23+
}
24+
25+
public void describeTo(Description description) {
26+
description.append("one of ");
27+
Formatting.join(collection, description, "{", "}");
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.core;
4+
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.Description;
7+
8+
9+
/**
10+
* Tests whether the value is an instance of a class.
11+
*/
12+
public class IsInstanceOf implements Matcher {
13+
private final Class theClass;
14+
15+
/**
16+
* Creates a new instance of IsInstanceOf
17+
*
18+
* @param theClass The predicate evaluates to true for instances of this class
19+
* or one of its subclasses.
20+
*/
21+
public IsInstanceOf(Class theClass) {
22+
this.theClass = theClass;
23+
}
24+
25+
public boolean match(Object arg) {
26+
return theClass.isInstance(arg);
27+
}
28+
29+
public void describeTo(Description description) {
30+
description.append("an instance of ")
31+
.append(theClass.getName());
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest.core;
4+
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.Description;
7+
8+
9+
/**
10+
* Calculates the logical negation of a matcher.
11+
*/
12+
public class IsNot implements Matcher {
13+
private final Matcher matcher;
14+
15+
public IsNot(Matcher matcher) {
16+
this.matcher = matcher;
17+
}
18+
19+
public boolean match(Object arg) {
20+
return !matcher.match(arg);
21+
}
22+
23+
public void describeTo(Description description) {
24+
description.append("not ");
25+
matcher.describeTo(description);
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.Matcher;
4+
import org.hamcrest.Description;
5+
6+
7+
public class IsNothing implements Matcher {
8+
public static final IsNothing INSTANCE = new IsNothing();
9+
private final String description;
10+
11+
public IsNothing() {
12+
this("NOTHING");
13+
}
14+
15+
public IsNothing(String description) {
16+
this.description = description;
17+
}
18+
19+
public boolean match(Object o) {
20+
return false;
21+
}
22+
23+
public void describeTo(Description description) {
24+
description.append(this.description);
25+
}
26+
}

0 commit comments

Comments
 (0)