summaryrefslogtreecommitdiffstats
path: root/hamcrest-core
diff options
context:
space:
mode:
authorYohann Roussel <yroussel@google.com>2014-03-19 16:25:37 +0100
committerYohann Roussel <yroussel@google.com>2014-03-20 15:13:33 +0100
commit4eceb95409e844fdc33c9c706e1dc307bfd40303 (patch)
treeee9f4f3fc79f757c79081c336bce4f1782c6ccd8 /hamcrest-core
parent3d2402901b1a6462e2cf47a6fd09711f327961c3 (diff)
downloadtoolchain_jack-4eceb95409e844fdc33c9c706e1dc307bfd40303.zip
toolchain_jack-4eceb95409e844fdc33c9c706e1dc307bfd40303.tar.gz
toolchain_jack-4eceb95409e844fdc33c9c706e1dc307bfd40303.tar.bz2
Initial Jack import.
Change-Id: I953cf0a520195a7187d791b2885848ad0d5a9b43
Diffstat (limited to 'hamcrest-core')
-rw-r--r--hamcrest-core/Android.mk29
-rw-r--r--hamcrest-core/LICENSE.txt27
-rw-r--r--hamcrest-core/NOTICE27
-rw-r--r--hamcrest-core/README.android13
-rw-r--r--hamcrest-core/src/org/hamcrest/BaseDescription.java127
-rw-r--r--hamcrest-core/src/org/hamcrest/BaseMatcher.java23
-rw-r--r--hamcrest-core/src/org/hamcrest/CoreMatchers.java165
-rw-r--r--hamcrest-core/src/org/hamcrest/Description.java44
-rw-r--r--hamcrest-core/src/org/hamcrest/Factory.java17
-rw-r--r--hamcrest-core/src/org/hamcrest/Matcher.java47
-rw-r--r--hamcrest-core/src/org/hamcrest/SelfDescribing.java16
-rw-r--r--hamcrest-core/src/org/hamcrest/StringDescription.java60
-rw-r--r--hamcrest-core/src/org/hamcrest/core/AllOf.java51
-rw-r--r--hamcrest-core/src/org/hamcrest/core/AnyOf.java51
-rw-r--r--hamcrest-core/src/org/hamcrest/core/DescribedAs.java55
-rw-r--r--hamcrest-core/src/org/hamcrest/core/Is.java68
-rw-r--r--hamcrest-core/src/org/hamcrest/core/IsAnything.java59
-rw-r--r--hamcrest-core/src/org/hamcrest/core/IsEqual.java71
-rw-r--r--hamcrest-core/src/org/hamcrest/core/IsInstanceOf.java44
-rw-r--r--hamcrest-core/src/org/hamcrest/core/IsNot.java49
-rw-r--r--hamcrest-core/src/org/hamcrest/core/IsNull.java55
-rw-r--r--hamcrest-core/src/org/hamcrest/core/IsSame.java40
-rw-r--r--hamcrest-core/src/org/hamcrest/core/package.html7
-rw-r--r--hamcrest-core/src/org/hamcrest/internal/ArrayIterator.java28
-rw-r--r--hamcrest-core/src/org/hamcrest/internal/SelfDescribingValue.java16
-rw-r--r--hamcrest-core/src/org/hamcrest/internal/SelfDescribingValueIterator.java25
-rw-r--r--hamcrest-core/src/org/hamcrest/package.html9
27 files changed, 1223 insertions, 0 deletions
diff --git a/hamcrest-core/Android.mk b/hamcrest-core/Android.mk
new file mode 100644
index 0000000..9c48dcf
--- /dev/null
+++ b/hamcrest-core/Android.mk
@@ -0,0 +1,29 @@
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_MODULE := hamcrest-core-jack
+
+LOCAL_BUILD_HOST_DEX := true
+
+LOCAL_JAVA_LIBRARIES := core-hostdex
+
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_HOST_JAVA_LIBRARY)
diff --git a/hamcrest-core/LICENSE.txt b/hamcrest-core/LICENSE.txt
new file mode 100644
index 0000000..5ebe92c
--- /dev/null
+++ b/hamcrest-core/LICENSE.txt
@@ -0,0 +1,27 @@
+BSD License
+
+Copyright (c) 2000-2006, www.hamcrest.org
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of
+conditions and the following disclaimer. Redistributions in binary form must reproduce
+the above copyright notice, this list of conditions and the following disclaimer in
+the documentation and/or other materials provided with the distribution.
+
+Neither the name of Hamcrest nor the names of its contributors may be used to endorse
+or promote products derived from this software without specific prior written
+permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.
diff --git a/hamcrest-core/NOTICE b/hamcrest-core/NOTICE
new file mode 100644
index 0000000..dcdcc42
--- /dev/null
+++ b/hamcrest-core/NOTICE
@@ -0,0 +1,27 @@
+BSD License
+
+Copyright (c) 2000-2006, www.hamcrest.org
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of
+conditions and the following disclaimer. Redistributions in binary form must reproduce
+the above copyright notice, this list of conditions and the following disclaimer in
+the documentation and/or other materials provided with the distribution.
+
+Neither the name of Hamcrest nor the names of its contributors may be used to endorse
+or promote products derived from this software without specific prior written
+permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.
diff --git a/hamcrest-core/README.android b/hamcrest-core/README.android
new file mode 100644
index 0000000..b8c9a64
--- /dev/null
+++ b/hamcrest-core/README.android
@@ -0,0 +1,13 @@
+URL: http://code.google.com/p/hamcrest/
+Tag: hamcrest-java-1.1/hamcrest-core
+License: BSD 2
+Description: Provides a library of matcher objects (also known as constraints or predicates) allowing 'match' rules to be defined declaratively, to be used in other frameworks. Typical scenarios include testing frameworks, mocking libraries and UI validation rules.
+Local Modifications: No.
+
+SVN: http://hamcrest.googlecode.com/svn/tags/
+
+Note: hamcrest is ported to github, and hamcrest 1.1 can be retrieved by checkouting
+commit 7fabb5cd0a17ec1a0f5112ace91e7a23ad79032a
+from repository https://github.com/hamcrest/JavaHamcrest.
+Oddly though, differences between this version and the SVN one are present in the source files.
+This current project share the same sources than the SVN version.
diff --git a/hamcrest-core/src/org/hamcrest/BaseDescription.java b/hamcrest-core/src/org/hamcrest/BaseDescription.java
new file mode 100644
index 0000000..4c98e5f
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/BaseMatcher.java b/hamcrest-core/src/org/hamcrest/BaseMatcher.java
new file mode 100644
index 0000000..3fdd6f7
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/CoreMatchers.java b/hamcrest-core/src/org/hamcrest/CoreMatchers.java
new file mode 100644
index 0000000..fac3acf
--- /dev/null
+++ b/hamcrest-core/src/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.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.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/org/hamcrest/Description.java b/hamcrest-core/src/org/hamcrest/Description.java
new file mode 100644
index 0000000..36ddeda
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/Factory.java b/hamcrest-core/src/org/hamcrest/Factory.java
new file mode 100644
index 0000000..a8bf5f9
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/Matcher.java b/hamcrest-core/src/org/hamcrest/Matcher.java
new file mode 100644
index 0000000..fd10207
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/SelfDescribing.java b/hamcrest-core/src/org/hamcrest/SelfDescribing.java
new file mode 100644
index 0000000..cd53070
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/StringDescription.java b/hamcrest-core/src/org/hamcrest/StringDescription.java
new file mode 100644
index 0000000..66709ee
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/AllOf.java b/hamcrest-core/src/org/hamcrest/core/AllOf.java
new file mode 100644
index 0000000..f619a7d
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/AnyOf.java b/hamcrest-core/src/org/hamcrest/core/AnyOf.java
new file mode 100644
index 0000000..e7e9181
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/DescribedAs.java b/hamcrest-core/src/org/hamcrest/core/DescribedAs.java
new file mode 100644
index 0000000..7b8c151
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/Is.java b/hamcrest-core/src/org/hamcrest/core/Is.java
new file mode 100644
index 0000000..f9152e9
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/IsAnything.java b/hamcrest-core/src/org/hamcrest/core/IsAnything.java
new file mode 100644
index 0000000..c5ca49d
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/IsEqual.java b/hamcrest-core/src/org/hamcrest/core/IsEqual.java
new file mode 100644
index 0000000..b9f17c5
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/IsInstanceOf.java b/hamcrest-core/src/org/hamcrest/core/IsInstanceOf.java
new file mode 100644
index 0000000..df20824
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/IsNot.java b/hamcrest-core/src/org/hamcrest/core/IsNot.java
new file mode 100644
index 0000000..cb6946c
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/IsNull.java b/hamcrest-core/src/org/hamcrest/core/IsNull.java
new file mode 100644
index 0000000..737dcf2
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/IsSame.java b/hamcrest-core/src/org/hamcrest/core/IsSame.java
new file mode 100644
index 0000000..b3ad77e
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/core/package.html b/hamcrest-core/src/org/hamcrest/core/package.html
new file mode 100644
index 0000000..7bb0ffe
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/internal/ArrayIterator.java b/hamcrest-core/src/org/hamcrest/internal/ArrayIterator.java
new file mode 100644
index 0000000..093cdba
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/internal/SelfDescribingValue.java b/hamcrest-core/src/org/hamcrest/internal/SelfDescribingValue.java
new file mode 100644
index 0000000..0634527
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/internal/SelfDescribingValueIterator.java b/hamcrest-core/src/org/hamcrest/internal/SelfDescribingValueIterator.java
new file mode 100644
index 0000000..58bedf6
--- /dev/null
+++ b/hamcrest-core/src/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/org/hamcrest/package.html b/hamcrest-core/src/org/hamcrest/package.html
new file mode 100644
index 0000000..143c704
--- /dev/null
+++ b/hamcrest-core/src/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>