diff options
author | Paul Duffin <paulduffin@google.com> | 2017-01-20 13:50:23 +0000 |
---|---|---|
committer | Paul Duffin <paulduffin@google.com> | 2017-01-20 13:50:23 +0000 |
commit | 74299c53a59140905e0259cb0a1130cb719733d8 (patch) | |
tree | aa4a6a53609f47d386dfa114be0abfc01e0e5dff /hamcrest-core/src/main/java/org/hamcrest | |
parent | f1015b50e715aefc3070d83d95340cbc29811329 (diff) | |
download | hamcrest-74299c53a59140905e0259cb0a1130cb719733d8.tar.gz |
Match upstream file structure
Bug: 30946317
Test: make checkbuild and run cts CtsTextTestCases
Change-Id: I6c1773018f95855eb6599d66f6fb5923c7d48b03
Diffstat (limited to 'hamcrest-core/src/main/java/org/hamcrest')
26 files changed, 1214 insertions, 0 deletions
diff --git a/hamcrest-core/src/main/java/org/hamcrest/BaseDescription.java b/hamcrest-core/src/main/java/org/hamcrest/BaseDescription.java new file mode 100644 index 0000000..4c98e5f --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/BaseDescription.java @@ -0,0 +1,127 @@ +package org.hamcrest;
+
+import static java.lang.String.valueOf;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import org.hamcrest.internal.ArrayIterator;
+import org.hamcrest.internal.SelfDescribingValueIterator;
+
+/**
+ * A {@link Description} that is stored as a string.
+ */
+public abstract class BaseDescription implements Description {
+ public Description appendText(String text) {
+ append(text);
+ return this;
+ }
+
+ public Description appendDescriptionOf(SelfDescribing value) {
+ value.describeTo(this);
+ return this;
+ }
+
+ public Description appendValue(Object value) {
+ if (value == null) {
+ append("null");
+ } else if (value instanceof String) {
+ toJavaSyntax((String) value);
+ } else if (value instanceof Character) {
+ append('"');
+ toJavaSyntax((Character) value);
+ append('"');
+ } else if (value instanceof Short) {
+ append('<');
+ append(valueOf(value));
+ append("s>");
+ } else if (value instanceof Long) {
+ append('<');
+ append(valueOf(value));
+ append("L>");
+ } else if (value instanceof Float) {
+ append('<');
+ append(valueOf(value));
+ append("F>");
+ } else if (value.getClass().isArray()) {
+ appendValueList("[",", ","]", new ArrayIterator(value));
+ } else {
+ append('<');
+ append(valueOf(value));
+ append('>');
+ }
+ return this;
+ }
+
+ public <T> Description appendValueList(String start, String separator, String end, T... values) {
+ return appendValueList(start, separator, end, Arrays.asList(values));
+ }
+
+ public <T> Description appendValueList(String start, String separator, String end, Iterable<T> values) {
+ return appendValueList(start, separator, end, values.iterator());
+ }
+
+ private <T> Description appendValueList(String start, String separator, String end, Iterator<T> values) {
+ return appendList(start, separator, end, new SelfDescribingValueIterator<T>(values));
+ }
+
+ public Description appendList(String start, String separator, String end, Iterable<? extends SelfDescribing> values) {
+ return appendList(start, separator, end, values.iterator());
+ }
+
+ private Description appendList(String start, String separator, String end, Iterator<? extends SelfDescribing> i) {
+ boolean separate = false;
+
+ append(start);
+ while (i.hasNext()) {
+ if (separate) append(separator);
+ appendDescriptionOf(i.next());
+ separate = true;
+ }
+ append(end);
+
+ return this;
+ }
+
+
+ /** Append the String <var>str</var> to the description.
+ * The default implementation passes every character to {@link #append(char)}.
+ * Override in subclasses to provide an efficient implementation.
+ */
+ protected void append(String str) {
+ for (int i = 0; i < str.length(); i++) {
+ append(str.charAt(i));
+ }
+ }
+
+ /** Append the char <var>c</var> to the description.
+ */
+ protected abstract void append(char c);
+
+ private void toJavaSyntax(String unformatted) {
+ append('"');
+ for (int i = 0; i < unformatted.length(); i++) {
+ toJavaSyntax(unformatted.charAt(i));
+ }
+ append('"');
+ }
+
+ private void toJavaSyntax(char ch) {
+ switch (ch) {
+ case '"':
+ append("\\\"");
+ break;
+ case '\n':
+ append("\\n");
+ break;
+ case '\r':
+ append("\\r");
+ break;
+ case '\t':
+ append("\\t");
+ break;
+ default:
+ append(ch);
+ }
+ }
+}
diff --git a/hamcrest-core/src/main/java/org/hamcrest/BaseMatcher.java b/hamcrest-core/src/main/java/org/hamcrest/BaseMatcher.java new file mode 100644 index 0000000..3fdd6f7 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/BaseMatcher.java @@ -0,0 +1,23 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.hamcrest; + +/** + * BaseClass for all Matcher implementations. + * + * @see Matcher + */ +public abstract class BaseMatcher<T> implements Matcher<T> { + + /** + * @see Matcher#_dont_implement_Matcher___instead_extend_BaseMatcher_() + */ + public final void _dont_implement_Matcher___instead_extend_BaseMatcher_() { + // See Matcher interface for an explanation of this method. + } + + @Override + public String toString() { + return StringDescription.toString(this); + } +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/CoreMatchers.java b/hamcrest-core/src/main/java/org/hamcrest/CoreMatchers.java new file mode 100644 index 0000000..dd36acb --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/CoreMatchers.java @@ -0,0 +1,165 @@ +// Generated source. +package org.hamcrest; + +public class CoreMatchers { + + /** + * Decorates another Matcher, retaining the behavior but allowing tests + * to be slightly more expressive. + * + * eg. assertThat(cheese, equalTo(smelly)) + * vs assertThat(cheese, is(equalTo(smelly))) + */ + public static <T> org.hamcrest.Matcher<T> is(org.hamcrest.Matcher<T> matcher) { + return org.hamcrest.core.Is.is(matcher); + } + + /** + * This is a shortcut to the frequently used is(equalTo(x)). + * + * eg. assertThat(cheese, is(equalTo(smelly))) + * vs assertThat(cheese, is(smelly)) + */ + public static <T> org.hamcrest.Matcher<T> is(T value) { + return org.hamcrest.core.Is.is(value); + } + + /** + * This is a shortcut to the frequently used is(instanceOf(SomeClass.class)). + * + * eg. assertThat(cheese, is(instanceOf(Cheddar.class))) + * vs assertThat(cheese, is(Cheddar.class)) + */ + public static org.hamcrest.Matcher<java.lang.Object> is(java.lang.Class<?> type) { + return org.hamcrest.core.Is.is(type); + } + + /** + * Inverts the rule. + */ + public static <T> org.hamcrest.Matcher<T> not(org.hamcrest.Matcher<T> matcher) { + return org.hamcrest.core.IsNot.not(matcher); + } + + /** + * This is a shortcut to the frequently used not(equalTo(x)). + * + * eg. assertThat(cheese, is(not(equalTo(smelly)))) + * vs assertThat(cheese, is(not(smelly))) + */ + public static <T> org.hamcrest.Matcher<T> not(T value) { + return org.hamcrest.core.IsNot.not(value); + } + + /** + * Is the value equal to another value, as tested by the + * {@link java.lang.Object#equals} invokedMethod? + */ + public static <T> org.hamcrest.Matcher<T> equalTo(T operand) { + return org.hamcrest.core.IsEqual.equalTo(operand); + } + + /** + * Is the value an instance of a particular type? + */ + public static org.hamcrest.Matcher<java.lang.Object> instanceOf(java.lang.Class<?> type) { + return org.hamcrest.core.IsInstanceOf.instanceOf(type); + } + + /** + * Evaluates to true only if ALL of the passed in matchers evaluate to true. + */ + public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? extends T>... matchers) { + return org.hamcrest.core.AllOf.<T>allOf(matchers); + } + + /** + * Evaluates to true only if ALL of the passed in matchers evaluate to true. + */ + public static <T> org.hamcrest.Matcher<T> allOf(java.lang.Iterable<org.hamcrest.Matcher<? extends T>> matchers) { + return org.hamcrest.core.AllOf.allOf(matchers); + } + + /** + * Evaluates to true if ANY of the passed in matchers evaluate to true. + */ + public static <T> org.hamcrest.Matcher<T> anyOf(org.hamcrest.Matcher<? extends T>... matchers) { + return org.hamcrest.core.AnyOf.<T>anyOf(matchers); + } + + /** + * Evaluates to true if ANY of the passed in matchers evaluate to true. + */ + public static <T> org.hamcrest.Matcher<T> anyOf(java.lang.Iterable<org.hamcrest.Matcher<? extends T>> matchers) { + return org.hamcrest.core.AnyOf.anyOf(matchers); + } + + /** + * Creates a new instance of IsSame + * + * @param object The predicate evaluates to true only when the argument is + * this object. + */ + public static <T> org.hamcrest.Matcher<T> sameInstance(T object) { + return org.hamcrest.core.IsSame.sameInstance(object); + } + + /** + * This matcher always evaluates to true. + */ + public static <T> org.hamcrest.Matcher<T> anything() { + return org.hamcrest.core.IsAnything.anything(); + } + + /** + * This matcher always evaluates to true. + * + * @param description A meaningful string used when describing itself. + */ + public static <T> org.hamcrest.Matcher<T> anything(java.lang.String description) { + return org.hamcrest.core.IsAnything.anything(description); + } + + /** + * This matcher always evaluates to true. With type inference. + */ + public static <T> org.hamcrest.Matcher<T> any(java.lang.Class<T> type) { + return org.hamcrest.core.IsAnything.any(type); + } + + /** + * Matches if value is null. + */ + public static <T> org.hamcrest.Matcher<T> nullValue() { + return org.hamcrest.core.IsNull.nullValue(); + } + + /** + * Matches if value is null. With type inference. + */ + public static <T> org.hamcrest.Matcher<T> nullValue(java.lang.Class<T> type) { + return org.hamcrest.core.IsNull.nullValue(type); + } + + /** + * Matches if value is not null. + */ + public static <T> org.hamcrest.Matcher<T> notNullValue() { + return org.hamcrest.core.IsNull.notNullValue(); + } + + /** + * Matches if value is not null. With type inference. + */ + public static <T> org.hamcrest.Matcher<T> notNullValue(java.lang.Class<T> type) { + return org.hamcrest.core.IsNull.notNullValue(type); + } + + /** + * Wraps an existing matcher and overrides the description when it fails. + */ + public static <T> org.hamcrest.Matcher<T> describedAs(java.lang.String description, org.hamcrest.Matcher<T> matcher, java.lang.Object... values) { + return org.hamcrest.core.DescribedAs.describedAs(description, matcher, values); + } + +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/Description.java b/hamcrest-core/src/main/java/org/hamcrest/Description.java new file mode 100644 index 0000000..36ddeda --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/Description.java @@ -0,0 +1,44 @@ +package org.hamcrest;
+
+/**
+ * A description of a Matcher. A Matcher will describe itself to a description
+ * which can later be used for reporting.
+ *
+ * @see Matcher#describeTo(Description)
+ */
+public interface Description {
+
+ /**
+ * Appends some plain text to the description.
+ */
+ Description appendText(String text);
+
+ /**
+ * Appends the description of a {@link SelfDescribing} value to this description.
+ */
+ Description appendDescriptionOf(SelfDescribing value);
+
+ /**
+ * Appends an arbitary value to the description.
+ */
+ Description appendValue(Object value);
+
+ /**
+ * Appends a list of values to the description.
+ */
+ <T> Description appendValueList(String start, String separator, String end,
+ T... values);
+
+ /**
+ * Appends a list of values to the description.
+ */
+ <T> Description appendValueList(String start, String separator, String end,
+ Iterable<T> values);
+
+ /**
+ * Appends a list of {@link org.hamcrest.SelfDescribing} objects
+ * to the description.
+ */
+ Description appendList(String start, String separator, String end,
+ Iterable<? extends SelfDescribing> values);
+}
diff --git a/hamcrest-core/src/main/java/org/hamcrest/Factory.java b/hamcrest-core/src/main/java/org/hamcrest/Factory.java new file mode 100644 index 0000000..a8bf5f9 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/Factory.java @@ -0,0 +1,17 @@ +package org.hamcrest; + +import static java.lang.annotation.ElementType.METHOD; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; + +/** + * Marks a Hamcrest static factory method so tools recognise them. + * A factory method is an equivalent to a named constructor. + * + * @author Joe Walnes + */ +@Retention(RUNTIME) +@Target({METHOD}) +public @interface Factory { +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/Matcher.java b/hamcrest-core/src/main/java/org/hamcrest/Matcher.java new file mode 100644 index 0000000..fd10207 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/Matcher.java @@ -0,0 +1,47 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.hamcrest; + +/** + * A matcher over acceptable values. + * A matcher is able to describe itself to give feedback when it fails. + * <p/> + * Matcher implementations should <b>NOT directly implement this interface</b>. + * Instead, <b>extend</b> the {@link BaseMatcher} abstract class, + * which will ensure that the Matcher API can grow to support + * new features and remain compatible with all Matcher implementations. + * <p/> + * For easy access to common Matcher implementations, use the static factory + * methods in {@link CoreMatchers}. + * + * @see CoreMatchers + * @see BaseMatcher + */ +@SuppressWarnings({"unused", "UnusedDeclaration"}) +public interface Matcher<T> extends SelfDescribing { + + /** + * Evaluates the matcher for argument <var>item</var>. + * <p/> + * This method matches against Object, instead of the generic type T. This is + * because the caller of the Matcher does not know at runtime what the type is + * (because of type erasure with Java generics). It is down to the implementations + * to check the correct type. + * + * @param item the object against which the matcher is evaluated. + * @return <code>true</code> if <var>item</var> matches, otherwise <code>false</code>. + * + * @see BaseMatcher + */ + boolean matches(Object item); + + /** + * This method simply acts a friendly reminder not to implement Matcher directly and + * instead extend BaseMatcher. It's easy to ignore JavaDoc, but a bit harder to ignore + * compile errors . + * + * @see Matcher for reasons why. + * @see BaseMatcher + */ + void _dont_implement_Matcher___instead_extend_BaseMatcher_(); +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/MatcherAssert.java b/hamcrest-core/src/main/java/org/hamcrest/MatcherAssert.java new file mode 100644 index 0000000..3eb234a --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/MatcherAssert.java @@ -0,0 +1,24 @@ +/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest;
+
+
+public class MatcherAssert {
+ public static <T> void assertThat(T actual, Matcher<T> matcher) {
+ assertThat("", actual, matcher);
+ }
+
+ public static <T> void assertThat(String reason, T actual, Matcher<T> matcher) {
+ if (!matcher.matches(actual)) {
+ Description description = new StringDescription();
+ description.appendText(reason)
+ .appendText("\nExpected: ")
+ .appendDescriptionOf(matcher)
+ .appendText("\n got: ")
+ .appendValue(actual)
+ .appendText("\n");
+
+ throw new java.lang.AssertionError(description.toString());
+ }
+ }
+}
diff --git a/hamcrest-core/src/main/java/org/hamcrest/SelfDescribing.java b/hamcrest-core/src/main/java/org/hamcrest/SelfDescribing.java new file mode 100644 index 0000000..cd53070 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/SelfDescribing.java @@ -0,0 +1,16 @@ +package org.hamcrest; + +/** + * The ability of an object to describe itself. + */ +public interface SelfDescribing { + /** + * Generates a description of the object. The description may be part of a + * a description of a larger object of which this is just a component, so it + * should be worded appropriately. + * + * @param description + * The description to be built or appended to. + */ + void describeTo(Description description); +}
\ No newline at end of file diff --git a/hamcrest-core/src/main/java/org/hamcrest/StringDescription.java b/hamcrest-core/src/main/java/org/hamcrest/StringDescription.java new file mode 100644 index 0000000..66709ee --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/StringDescription.java @@ -0,0 +1,60 @@ +package org.hamcrest;
+
+import java.io.IOException;
+
+/**
+ * A {@link Description} that is stored as a string.
+ */
+public class StringDescription extends BaseDescription {
+ private final Appendable out;
+
+ public StringDescription() {
+ this(new StringBuilder());
+ }
+
+ public StringDescription(Appendable out) {
+ this.out = out;
+ }
+
+ /**
+ * Return the description of a {@link SelfDescribing} object as a String.
+ *
+ * @param selfDescribing
+ * The object to be described.
+ * @return
+ * The description of the object.
+ */
+ public static String toString(SelfDescribing value) {
+ return new StringDescription().appendDescriptionOf(value).toString();
+ }
+
+ /**
+ * Alias for {@link #toString(SelfDescribing)}.
+ */
+ public static String asString(SelfDescribing selfDescribing) {
+ return toString(selfDescribing);
+ }
+
+ protected void append(String str) {
+ try {
+ out.append(str);
+ } catch (IOException e) {
+ throw new RuntimeException("Could not write description", e);
+ }
+ }
+
+ protected void append(char c) {
+ try {
+ out.append(c);
+ } catch (IOException e) {
+ throw new RuntimeException("Could not write description", e);
+ }
+ }
+
+ /**
+ * Returns the description as a string.
+ */
+ public String toString() {
+ return out.toString();
+ }
+}
diff --git a/hamcrest-core/src/main/java/org/hamcrest/TypeSafeMatcher.java b/hamcrest-core/src/main/java/org/hamcrest/TypeSafeMatcher.java new file mode 100644 index 0000000..7f18fd3 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/TypeSafeMatcher.java @@ -0,0 +1,58 @@ +package org.hamcrest; + +import java.lang.reflect.Method; + +/** + * Convenient base class for Matchers that require a non-null value of a specific type. + * This simply implements the null check, checks the type and then casts. + * + * @author Joe Walnes + */ +public abstract class TypeSafeMatcher<T> extends BaseMatcher<T> { + + private Class expectedType; + + /** + * Subclasses should implement this. The item will already have been checked for + * the specific type and will never be null. + */ + public abstract boolean matchesSafely(T item); + + protected TypeSafeMatcher() { + expectedType = findExpectedType(getClass()); + } + + private static Class<?> findExpectedType(Class<?> fromClass) { + for (Class<?> c = fromClass; c != Object.class; c = c.getSuperclass()) { + for (Method method : c.getDeclaredMethods()) { + if (isMatchesSafelyMethod(method)) { + return method.getParameterTypes()[0]; + } + } + } + + throw new Error("Cannot determine correct type for matchesSafely() method."); + } + + private static boolean isMatchesSafelyMethod(Method method) { + return method.getName().equals("matchesSafely") + && method.getParameterTypes().length == 1 + && !method.isSynthetic(); + } + + protected TypeSafeMatcher(Class<T> expectedType) { + this.expectedType = expectedType; + } + + /** + * Method made final to prevent accidental override. + * If you need to override this, there's no point on extending TypeSafeMatcher. + * Instead, extend the {@link BaseMatcher}. + */ + @SuppressWarnings({"unchecked"}) + public final boolean matches(Object item) { + return item != null + && expectedType.isInstance(item) + && matchesSafely((T) item); + } +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/AllOf.java b/hamcrest-core/src/main/java/org/hamcrest/core/AllOf.java new file mode 100644 index 0000000..f619a7d --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/AllOf.java @@ -0,0 +1,51 @@ +package org.hamcrest.core; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Matcher; +import org.hamcrest.Description; +import org.hamcrest.Factory; + +import java.util.Arrays; + +/** + * Calculates the logical conjunction of two matchers. Evaluation is + * shortcut, so that the second matcher is not called if the first + * matcher returns <code>false</code>. + */ +public class AllOf<T> extends BaseMatcher<T> { + private final Iterable<Matcher<? extends T>> matchers; + + public AllOf(Iterable<Matcher<? extends T>> matchers) { + this.matchers = matchers; + } + + public boolean matches(Object o) { + for (Matcher<? extends T> matcher : matchers) { + if (!matcher.matches(o)) { + return false; + } + } + return true; + } + + public void describeTo(Description description) { + description.appendList("(", " and ", ")", matchers); + } + + /** + * Evaluates to true only if ALL of the passed in matchers evaluate to true. + */ + @Factory + public static <T> Matcher<T> allOf(Matcher<? extends T>... matchers) { + return allOf(Arrays.asList(matchers)); + } + + /** + * Evaluates to true only if ALL of the passed in matchers evaluate to true. + */ + @Factory + public static <T> Matcher<T> allOf(Iterable<Matcher<? extends T>> matchers) { + return new AllOf<T>(matchers); + } + +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/AnyOf.java b/hamcrest-core/src/main/java/org/hamcrest/core/AnyOf.java new file mode 100644 index 0000000..e7e9181 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/AnyOf.java @@ -0,0 +1,51 @@ +package org.hamcrest.core; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Matcher; +import org.hamcrest.Description; +import org.hamcrest.Factory; + +import java.util.Arrays; + +/** + * Calculates the logical disjunction of two matchers. Evaluation is + * shortcut, so that the second matcher is not called if the first + * matcher returns <code>true</code>. + */ +public class AnyOf<T> extends BaseMatcher<T> { + + private final Iterable<Matcher<? extends T>> matchers; + + public AnyOf(Iterable<Matcher<? extends T>> matchers) { + this.matchers = matchers; + } + + public boolean matches(Object o) { + for (Matcher<? extends T> matcher : matchers) { + if (matcher.matches(o)) { + return true; + } + } + return false; + } + + public void describeTo(Description description) { + description.appendList("(", " or ", ")", matchers); + } + + /** + * Evaluates to true if ANY of the passed in matchers evaluate to true. + */ + @Factory + public static <T> Matcher<T> anyOf(Matcher<? extends T>... matchers) { + return anyOf(Arrays.asList(matchers)); + } + + /** + * Evaluates to true if ANY of the passed in matchers evaluate to true. + */ + @Factory + public static <T> Matcher<T> anyOf(Iterable<Matcher<? extends T>> matchers) { + return new AnyOf<T>(matchers); + } +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/DescribedAs.java b/hamcrest-core/src/main/java/org/hamcrest/core/DescribedAs.java new file mode 100644 index 0000000..7b8c151 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/DescribedAs.java @@ -0,0 +1,55 @@ +/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest.core;
+
+import java.util.regex.Pattern;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.BaseMatcher;
+
+/**
+ * Provides a custom description to another matcher.
+ */
+public class DescribedAs<T> extends BaseMatcher<T> {
+ private final String descriptionTemplate;
+ private final Matcher<T> matcher;
+ private final Object[] values;
+
+ private final static Pattern ARG_PATTERN = Pattern.compile("%([0-9]+)");
+
+ public DescribedAs(String descriptionTemplate, Matcher<T> matcher, Object[] values) {
+ this.descriptionTemplate = descriptionTemplate;
+ this.matcher = matcher;
+ this.values = values.clone();
+ }
+
+ public boolean matches(Object o) {
+ return matcher.matches(o);
+ }
+
+ public void describeTo(Description description) {
+ java.util.regex.Matcher arg = ARG_PATTERN.matcher(descriptionTemplate);
+
+ int textStart = 0;
+ while (arg.find()) {
+ description.appendText(descriptionTemplate.substring(textStart, arg.start()));
+ int argIndex = Integer.parseInt(arg.group(1));
+ description.appendValue(values[argIndex]);
+ textStart = arg.end();
+ }
+
+ if (textStart < descriptionTemplate.length()) {
+ description.appendText(descriptionTemplate.substring(textStart));
+ }
+ }
+
+ /**
+ * Wraps an existing matcher and overrides the description when it fails.
+ */
+ @Factory
+ public static <T> Matcher<T> describedAs(String description, Matcher<T> matcher, Object... values) {
+ return new DescribedAs<T>(description, matcher, values);
+ }
+}
diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/Is.java b/hamcrest-core/src/main/java/org/hamcrest/core/Is.java new file mode 100644 index 0000000..f9152e9 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/Is.java @@ -0,0 +1,68 @@ +package org.hamcrest.core; + +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.hamcrest.core.IsEqual.equalTo; +import org.hamcrest.Factory; +import org.hamcrest.Matcher; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; + +/** + * Decorates another Matcher, retaining the behavior but allowing tests + * to be slightly more expressive. + * + * eg. assertThat(cheese, equalTo(smelly)) + * vs assertThat(cheese, is(equalTo(smelly))) + */ +public class Is<T> extends BaseMatcher<T> { + + private final Matcher<T> matcher; + + public Is(Matcher<T> matcher) { + this.matcher = matcher; + } + + public boolean matches(Object arg) { + return matcher.matches(arg); + } + + public void describeTo(Description description) { + description.appendText("is ").appendDescriptionOf(matcher); + } + + /** + * Decorates another Matcher, retaining the behavior but allowing tests + * to be slightly more expressive. + * + * eg. assertThat(cheese, equalTo(smelly)) + * vs assertThat(cheese, is(equalTo(smelly))) + */ + @Factory + public static <T> Matcher<T> is(Matcher<T> matcher) { + return new Is<T>(matcher); + } + + /** + * This is a shortcut to the frequently used is(equalTo(x)). + * + * eg. assertThat(cheese, is(equalTo(smelly))) + * vs assertThat(cheese, is(smelly)) + */ + @Factory + public static <T> Matcher<T> is(T value) { + return is(equalTo(value)); + } + + /** + * This is a shortcut to the frequently used is(instanceOf(SomeClass.class)). + * + * eg. assertThat(cheese, is(instanceOf(Cheddar.class))) + * vs assertThat(cheese, is(Cheddar.class)) + */ + @Factory + public static Matcher<Object> is(Class<?> type) { + return is(instanceOf(type)); + } + +} + diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/IsAnything.java b/hamcrest-core/src/main/java/org/hamcrest/core/IsAnything.java new file mode 100644 index 0000000..c5ca49d --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/IsAnything.java @@ -0,0 +1,59 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.hamcrest.core; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Factory; +import org.hamcrest.BaseMatcher; + + +/** + * A matcher that always returns <code>true</code>. + */ +public class IsAnything<T> extends BaseMatcher<T> { + + private final String description; + + public IsAnything() { + this("ANYTHING"); + } + + public IsAnything(String description) { + this.description = description; + } + + public boolean matches(Object o) { + return true; + } + + public void describeTo(Description description) { + description.appendText(this.description); + } + + /** + * This matcher always evaluates to true. + */ + @Factory + public static <T> Matcher<T> anything() { + return new IsAnything<T>(); + } + + /** + * This matcher always evaluates to true. + * + * @param description A meaningful string used when describing itself. + */ + @Factory + public static <T> Matcher<T> anything(String description) { + return new IsAnything<T>(description); + } + + /** + * This matcher always evaluates to true. With type inference. + */ + @Factory + public static <T> Matcher<T> any(@SuppressWarnings("unused")Class<T> type) { + return new IsAnything<T>(); + } +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/IsEqual.java b/hamcrest-core/src/main/java/org/hamcrest/core/IsEqual.java new file mode 100644 index 0000000..b9f17c5 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/IsEqual.java @@ -0,0 +1,71 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.hamcrest.core; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Factory; +import org.hamcrest.BaseMatcher; + +import java.lang.reflect.Array; + + +/** + * Is the value equal to another value, as tested by the + * {@link java.lang.Object#equals} invokedMethod? + */ +public class IsEqual<T> extends BaseMatcher<T> { + private final Object object; + + public IsEqual(T equalArg) { + object = equalArg; + } + + public boolean matches(Object arg) { + return areEqual(object, arg); + } + + public void describeTo(Description description) { + description.appendValue(object); + } + + private static boolean areEqual(Object o1, Object o2) { + if (o1 == null || o2 == null) { + return o1 == null && o2 == null; + } else if (isArray(o1)) { + return isArray(o2) && areArraysEqual(o1, o2); + } else { + return o1.equals(o2); + } + } + + private static boolean areArraysEqual(Object o1, Object o2) { + return areArrayLengthsEqual(o1, o2) + && areArrayElementsEqual(o1, o2); + } + + private static boolean areArrayLengthsEqual(Object o1, Object o2) { + return Array.getLength(o1) == Array.getLength(o2); + } + + private static boolean areArrayElementsEqual(Object o1, Object o2) { + for (int i = 0; i < Array.getLength(o1); i++) { + if (!areEqual(Array.get(o1, i), Array.get(o2, i))) return false; + } + return true; + } + + private static boolean isArray(Object o) { + return o.getClass().isArray(); + } + + /** + * Is the value equal to another value, as tested by the + * {@link java.lang.Object#equals} invokedMethod? + */ + @Factory + public static <T> Matcher<T> equalTo(T operand) { + return new IsEqual<T>(operand); + } + +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java b/hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java new file mode 100644 index 0000000..df20824 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java @@ -0,0 +1,44 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.hamcrest.core; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Factory; +import org.hamcrest.BaseMatcher; + + +/** + * Tests whether the value is an instance of a class. + */ +public class IsInstanceOf extends BaseMatcher<Object> { + private final Class<?> theClass; + + /** + * Creates a new instance of IsInstanceOf + * + * @param theClass The predicate evaluates to true for instances of this class + * or one of its subclasses. + */ + public IsInstanceOf(Class<?> theClass) { + this.theClass = theClass; + } + + public boolean matches(Object item) { + return theClass.isInstance(item); + } + + public void describeTo(Description description) { + description.appendText("an instance of ") + .appendText(theClass.getName()); + } + + /** + * Is the value an instance of a particular type? + */ + @Factory + public static Matcher<Object> instanceOf(Class<?> type) { + return new IsInstanceOf(type); + } + +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/IsNot.java b/hamcrest-core/src/main/java/org/hamcrest/core/IsNot.java new file mode 100644 index 0000000..cb6946c --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/IsNot.java @@ -0,0 +1,49 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.hamcrest.core; + +import static org.hamcrest.core.IsEqual.equalTo; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Factory; +import org.hamcrest.BaseMatcher; + + +/** + * Calculates the logical negation of a matcher. + */ +public class IsNot<T> extends BaseMatcher<T> { + private final Matcher<T> matcher; + + public IsNot(Matcher<T> matcher) { + this.matcher = matcher; + } + + public boolean matches(Object arg) { + return !matcher.matches(arg); + } + + public void describeTo(Description description) { + description.appendText("not ").appendDescriptionOf(matcher); + } + + /** + * Inverts the rule. + */ + @Factory + public static <T> Matcher<T> not(Matcher<T> matcher) { + return new IsNot<T>(matcher); + } + + /** + * This is a shortcut to the frequently used not(equalTo(x)). + * + * eg. assertThat(cheese, is(not(equalTo(smelly)))) + * vs assertThat(cheese, is(not(smelly))) + */ + @Factory + public static <T> Matcher<T> not(T value) { + return not(equalTo(value)); + } + +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/IsNull.java b/hamcrest-core/src/main/java/org/hamcrest/core/IsNull.java new file mode 100644 index 0000000..737dcf2 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/IsNull.java @@ -0,0 +1,55 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.hamcrest.core; + +import static org.hamcrest.core.IsNot.not; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Factory; +import org.hamcrest.BaseMatcher; + +/** + * Is the value null? + */ +public class IsNull<T> extends BaseMatcher<T> { + public boolean matches(Object o) { + return o == null; + } + + public void describeTo(Description description) { + description.appendText("null"); + } + + /** + * Matches if value is null. + */ + @Factory + public static <T> Matcher<T> nullValue() { + return new IsNull<T>(); + } + + /** + * Matches if value is not null. + */ + @Factory + public static <T> Matcher<T> notNullValue() { + return not(IsNull.<T>nullValue()); + } + + /** + * Matches if value is null. With type inference. + */ + @Factory + public static <T> Matcher<T> nullValue(@SuppressWarnings("unused") Class<T> type) { + return nullValue(); + } + + /** + * Matches if value is not null. With type inference. + */ + @Factory + public static <T> Matcher<T> notNullValue(@SuppressWarnings("unused") Class<T> type) { + return notNullValue(); + } +} + diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/IsSame.java b/hamcrest-core/src/main/java/org/hamcrest/core/IsSame.java new file mode 100644 index 0000000..b3ad77e --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/IsSame.java @@ -0,0 +1,40 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.hamcrest.core; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Factory; +import org.hamcrest.BaseMatcher; + + +/** + * Is the value the same object as another value? + */ +public class IsSame<T> extends BaseMatcher<T> { + private final T object; + + public IsSame(T object) { + this.object = object; + } + + public boolean matches(Object arg) { + return arg == object; + } + + public void describeTo(Description description) { + description.appendText("same(") .appendValue(object) .appendText(")"); + } + + /** + * Creates a new instance of IsSame + * + * @param object The predicate evaluates to true only when the argument is + * this object. + */ + @Factory + public static <T> Matcher<T> sameInstance(T object) { + return new IsSame<T>(object); + } + +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/package.html b/hamcrest-core/src/main/java/org/hamcrest/core/package.html new file mode 100644 index 0000000..7bb0ffe --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/core/package.html @@ -0,0 +1,7 @@ +<html> +<head> +</head> +<body> + <p>Fundamental matchers of objects and values, and composite matchers.</p> +</body> +</html> diff --git a/hamcrest-core/src/main/java/org/hamcrest/internal/ArrayIterator.java b/hamcrest-core/src/main/java/org/hamcrest/internal/ArrayIterator.java new file mode 100644 index 0000000..093cdba --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/internal/ArrayIterator.java @@ -0,0 +1,28 @@ +package org.hamcrest.internal; + +import java.lang.reflect.Array; +import java.util.Iterator; + +public class ArrayIterator implements Iterator<Object> { + private final Object array; + private int currentIndex = 0; + + public ArrayIterator(Object array) { + if (!array.getClass().isArray()) { + throw new IllegalArgumentException("not an array"); + } + this.array = array; + } + + public boolean hasNext() { + return currentIndex < Array.getLength(array); + } + + public Object next() { + return Array.get(array, currentIndex++); + } + + public void remove() { + throw new UnsupportedOperationException("cannot remove items from an array"); + } +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/internal/SelfDescribingValue.java b/hamcrest-core/src/main/java/org/hamcrest/internal/SelfDescribingValue.java new file mode 100644 index 0000000..0634527 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/internal/SelfDescribingValue.java @@ -0,0 +1,16 @@ +package org.hamcrest.internal; + +import org.hamcrest.Description; +import org.hamcrest.SelfDescribing; + +public class SelfDescribingValue<T> implements SelfDescribing { + private T value; + + public SelfDescribingValue(T value) { + this.value = value; + } + + public void describeTo(Description description) { + description.appendValue(value); + } +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/internal/SelfDescribingValueIterator.java b/hamcrest-core/src/main/java/org/hamcrest/internal/SelfDescribingValueIterator.java new file mode 100644 index 0000000..58bedf6 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/internal/SelfDescribingValueIterator.java @@ -0,0 +1,25 @@ +package org.hamcrest.internal; + +import java.util.Iterator; + +import org.hamcrest.SelfDescribing; + +public class SelfDescribingValueIterator<T> implements Iterator<SelfDescribing> { + private Iterator<T> values; + + public SelfDescribingValueIterator(Iterator<T> values) { + this.values = values; + } + + public boolean hasNext() { + return values.hasNext(); + } + + public SelfDescribing next() { + return new SelfDescribingValue<T>(values.next()); + } + + public void remove() { + values.remove(); + } +} diff --git a/hamcrest-core/src/main/java/org/hamcrest/internal/package.html b/hamcrest-core/src/main/java/org/hamcrest/internal/package.html new file mode 100644 index 0000000..1c9bf9d --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/internal/package.html @@ -0,0 +1,5 @@ +<html> +<body> + {@hide} +</body> +</html> diff --git a/hamcrest-core/src/main/java/org/hamcrest/package.html b/hamcrest-core/src/main/java/org/hamcrest/package.html new file mode 100644 index 0000000..143c704 --- /dev/null +++ b/hamcrest-core/src/main/java/org/hamcrest/package.html @@ -0,0 +1,9 @@ +<html> +<head> +</head> +<body> + <p>The stable API defining Matcher and its associated interfaces and classes. + Hamcrest sub-projects define their convenience classes in the org.hamcrest package. + </p> +</body> +</html> |