From 4eceb95409e844fdc33c9c706e1dc307bfd40303 Mon Sep 17 00:00:00 2001 From: Yohann Roussel Date: Wed, 19 Mar 2014 16:25:37 +0100 Subject: Initial Jack import. Change-Id: I953cf0a520195a7187d791b2885848ad0d5a9b43 --- hamcrest-core/Android.mk | 29 ++++ hamcrest-core/LICENSE.txt | 27 ++++ hamcrest-core/NOTICE | 27 ++++ hamcrest-core/README.android | 13 ++ .../src/org/hamcrest/BaseDescription.java | 127 ++++++++++++++++ hamcrest-core/src/org/hamcrest/BaseMatcher.java | 23 +++ hamcrest-core/src/org/hamcrest/CoreMatchers.java | 165 +++++++++++++++++++++ hamcrest-core/src/org/hamcrest/Description.java | 44 ++++++ hamcrest-core/src/org/hamcrest/Factory.java | 17 +++ hamcrest-core/src/org/hamcrest/Matcher.java | 47 ++++++ hamcrest-core/src/org/hamcrest/SelfDescribing.java | 16 ++ .../src/org/hamcrest/StringDescription.java | 60 ++++++++ hamcrest-core/src/org/hamcrest/core/AllOf.java | 51 +++++++ hamcrest-core/src/org/hamcrest/core/AnyOf.java | 51 +++++++ .../src/org/hamcrest/core/DescribedAs.java | 55 +++++++ hamcrest-core/src/org/hamcrest/core/Is.java | 68 +++++++++ .../src/org/hamcrest/core/IsAnything.java | 59 ++++++++ hamcrest-core/src/org/hamcrest/core/IsEqual.java | 71 +++++++++ .../src/org/hamcrest/core/IsInstanceOf.java | 44 ++++++ hamcrest-core/src/org/hamcrest/core/IsNot.java | 49 ++++++ hamcrest-core/src/org/hamcrest/core/IsNull.java | 55 +++++++ hamcrest-core/src/org/hamcrest/core/IsSame.java | 40 +++++ hamcrest-core/src/org/hamcrest/core/package.html | 7 + .../src/org/hamcrest/internal/ArrayIterator.java | 28 ++++ .../org/hamcrest/internal/SelfDescribingValue.java | 16 ++ .../internal/SelfDescribingValueIterator.java | 25 ++++ hamcrest-core/src/org/hamcrest/package.html | 9 ++ 27 files changed, 1223 insertions(+) create mode 100644 hamcrest-core/Android.mk create mode 100644 hamcrest-core/LICENSE.txt create mode 100644 hamcrest-core/NOTICE create mode 100644 hamcrest-core/README.android create mode 100644 hamcrest-core/src/org/hamcrest/BaseDescription.java create mode 100644 hamcrest-core/src/org/hamcrest/BaseMatcher.java create mode 100644 hamcrest-core/src/org/hamcrest/CoreMatchers.java create mode 100644 hamcrest-core/src/org/hamcrest/Description.java create mode 100644 hamcrest-core/src/org/hamcrest/Factory.java create mode 100644 hamcrest-core/src/org/hamcrest/Matcher.java create mode 100644 hamcrest-core/src/org/hamcrest/SelfDescribing.java create mode 100644 hamcrest-core/src/org/hamcrest/StringDescription.java create mode 100644 hamcrest-core/src/org/hamcrest/core/AllOf.java create mode 100644 hamcrest-core/src/org/hamcrest/core/AnyOf.java create mode 100644 hamcrest-core/src/org/hamcrest/core/DescribedAs.java create mode 100644 hamcrest-core/src/org/hamcrest/core/Is.java create mode 100644 hamcrest-core/src/org/hamcrest/core/IsAnything.java create mode 100644 hamcrest-core/src/org/hamcrest/core/IsEqual.java create mode 100644 hamcrest-core/src/org/hamcrest/core/IsInstanceOf.java create mode 100644 hamcrest-core/src/org/hamcrest/core/IsNot.java create mode 100644 hamcrest-core/src/org/hamcrest/core/IsNull.java create mode 100644 hamcrest-core/src/org/hamcrest/core/IsSame.java create mode 100644 hamcrest-core/src/org/hamcrest/core/package.html create mode 100644 hamcrest-core/src/org/hamcrest/internal/ArrayIterator.java create mode 100644 hamcrest-core/src/org/hamcrest/internal/SelfDescribingValue.java create mode 100644 hamcrest-core/src/org/hamcrest/internal/SelfDescribingValueIterator.java create mode 100644 hamcrest-core/src/org/hamcrest/package.html (limited to 'hamcrest-core') 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 Description appendValueList(String start, String separator, String end, T... values) { + return appendValueList(start, separator, end, Arrays.asList(values)); + } + + public Description appendValueList(String start, String separator, String end, Iterable values) { + return appendValueList(start, separator, end, values.iterator()); + } + + private Description appendValueList(String start, String separator, String end, Iterator values) { + return appendList(start, separator, end, new SelfDescribingValueIterator(values)); + } + + public Description appendList(String start, String separator, String end, Iterable values) { + return appendList(start, separator, end, values.iterator()); + } + + private Description appendList(String start, String separator, String end, Iterator 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 str 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 c 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 implements Matcher { + + /** + * @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 org.hamcrest.Matcher is(org.hamcrest.Matcher 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 org.hamcrest.Matcher 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 is(java.lang.Class type) { + return org.hamcrest.core.Is.is(type); + } + + /** + * Inverts the rule. + */ + public static org.hamcrest.Matcher not(org.hamcrest.Matcher 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 org.hamcrest.Matcher 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 org.hamcrest.Matcher equalTo(T operand) { + return org.hamcrest.core.IsEqual.equalTo(operand); + } + + /** + * Is the value an instance of a particular type? + */ + public static org.hamcrest.Matcher 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 org.hamcrest.Matcher allOf(org.hamcrest.Matcher... matchers) { + return org.hamcrest.core.AllOf.allOf(matchers); + } + + /** + * Evaluates to true only if ALL of the passed in matchers evaluate to true. + */ + public static org.hamcrest.Matcher allOf(java.lang.Iterable> matchers) { + return org.hamcrest.core.AllOf.allOf(matchers); + } + + /** + * Evaluates to true if ANY of the passed in matchers evaluate to true. + */ + public static org.hamcrest.Matcher anyOf(org.hamcrest.Matcher... matchers) { + return org.hamcrest.core.AnyOf.anyOf(matchers); + } + + /** + * Evaluates to true if ANY of the passed in matchers evaluate to true. + */ + public static org.hamcrest.Matcher anyOf(java.lang.Iterable> 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 org.hamcrest.Matcher sameInstance(T object) { + return org.hamcrest.core.IsSame.sameInstance(object); + } + + /** + * This matcher always evaluates to true. + */ + public static org.hamcrest.Matcher anything() { + return org.hamcrest.core.IsAnything.anything(); + } + + /** + * This matcher always evaluates to true. + * + * @param description A meaningful string used when describing itself. + */ + public static org.hamcrest.Matcher anything(java.lang.String description) { + return org.hamcrest.core.IsAnything.anything(description); + } + + /** + * This matcher always evaluates to true. With type inference. + */ + public static org.hamcrest.Matcher any(java.lang.Class type) { + return org.hamcrest.core.IsAnything.any(type); + } + + /** + * Matches if value is null. + */ + public static org.hamcrest.Matcher nullValue() { + return org.hamcrest.core.IsNull.nullValue(); + } + + /** + * Matches if value is null. With type inference. + */ + public static org.hamcrest.Matcher nullValue(java.lang.Class type) { + return org.hamcrest.core.IsNull.nullValue(type); + } + + /** + * Matches if value is not null. + */ + public static org.hamcrest.Matcher notNullValue() { + return org.hamcrest.core.IsNull.notNullValue(); + } + + /** + * Matches if value is not null. With type inference. + */ + public static org.hamcrest.Matcher notNullValue(java.lang.Class type) { + return org.hamcrest.core.IsNull.notNullValue(type); + } + + /** + * Wraps an existing matcher and overrides the description when it fails. + */ + public static org.hamcrest.Matcher describedAs(java.lang.String description, org.hamcrest.Matcher 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. + */ + Description appendValueList(String start, String separator, String end, + T... values); + + /** + * Appends a list of values to the description. + */ + Description appendValueList(String start, String separator, String end, + Iterable values); + + /** + * Appends a list of {@link org.hamcrest.SelfDescribing} objects + * to the description. + */ + Description appendList(String start, String separator, String end, + Iterable 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. + *

+ * Matcher implementations should NOT directly implement this interface. + * Instead, extend 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. + *

+ * 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 extends SelfDescribing { + + /** + * Evaluates the matcher for argument item. + *

+ * 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 true if item matches, otherwise false. + * + * @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 false. + */ +public class AllOf extends BaseMatcher { + private final Iterable> matchers; + + public AllOf(Iterable> matchers) { + this.matchers = matchers; + } + + public boolean matches(Object o) { + for (Matcher 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 Matcher allOf(Matcher... matchers) { + return allOf(Arrays.asList(matchers)); + } + + /** + * Evaluates to true only if ALL of the passed in matchers evaluate to true. + */ + @Factory + public static Matcher allOf(Iterable> matchers) { + return new AllOf(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 true. + */ +public class AnyOf extends BaseMatcher { + + private final Iterable> matchers; + + public AnyOf(Iterable> matchers) { + this.matchers = matchers; + } + + public boolean matches(Object o) { + for (Matcher 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 Matcher anyOf(Matcher... matchers) { + return anyOf(Arrays.asList(matchers)); + } + + /** + * Evaluates to true if ANY of the passed in matchers evaluate to true. + */ + @Factory + public static Matcher anyOf(Iterable> matchers) { + return new AnyOf(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 extends BaseMatcher { + private final String descriptionTemplate; + private final Matcher matcher; + private final Object[] values; + + private final static Pattern ARG_PATTERN = Pattern.compile("%([0-9]+)"); + + public DescribedAs(String descriptionTemplate, Matcher 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 Matcher describedAs(String description, Matcher matcher, Object... values) { + return new DescribedAs(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 extends BaseMatcher { + + private final Matcher matcher; + + public Is(Matcher 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 Matcher is(Matcher matcher) { + return new Is(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 Matcher 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 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 true. + */ +public class IsAnything extends BaseMatcher { + + 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 Matcher anything() { + return new IsAnything(); + } + + /** + * This matcher always evaluates to true. + * + * @param description A meaningful string used when describing itself. + */ + @Factory + public static Matcher anything(String description) { + return new IsAnything(description); + } + + /** + * This matcher always evaluates to true. With type inference. + */ + @Factory + public static Matcher any(@SuppressWarnings("unused")Class type) { + return new IsAnything(); + } +} 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 extends BaseMatcher { + 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 Matcher equalTo(T operand) { + return new IsEqual(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 { + 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 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 extends BaseMatcher { + private final Matcher matcher; + + public IsNot(Matcher 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 Matcher not(Matcher matcher) { + return new IsNot(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 Matcher 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 extends BaseMatcher { + public boolean matches(Object o) { + return o == null; + } + + public void describeTo(Description description) { + description.appendText("null"); + } + + /** + * Matches if value is null. + */ + @Factory + public static Matcher nullValue() { + return new IsNull(); + } + + /** + * Matches if value is not null. + */ + @Factory + public static Matcher notNullValue() { + return not(IsNull.nullValue()); + } + + /** + * Matches if value is null. With type inference. + */ + @Factory + public static Matcher nullValue(@SuppressWarnings("unused") Class type) { + return nullValue(); + } + + /** + * Matches if value is not null. With type inference. + */ + @Factory + public static Matcher notNullValue(@SuppressWarnings("unused") Class 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 extends BaseMatcher { + 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 Matcher sameInstance(T object) { + return new IsSame(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 @@ + + + + +

Fundamental matchers of objects and values, and composite matchers.

+ + 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 { + 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 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 implements Iterator { + private Iterator values; + + public SelfDescribingValueIterator(Iterator values) { + this.values = values; + } + + public boolean hasNext() { + return values.hasNext(); + } + + public SelfDescribing next() { + return new SelfDescribingValue(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 @@ + + + + +

The stable API defining Matcher and its associated interfaces and classes. + Hamcrest sub-projects define their convenience classes in the org.hamcrest package. +

+ + -- cgit v1.1