truth-0.7.0/0000755000175000017500000000000012017424360012543 5ustar moellermoellertruth-0.7.0/LICENSE0000644000175000017500000002614212017424351013555 0ustar moellermoeller Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] 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. truth-0.7.0/build_tests.sh0000755000175000017500000000004612017424351015423 0ustar moellermoeller# To run all tests, just... mvn test truth-0.7.0/.gitignore0000644000175000017500000000006112017424360014530 0ustar moellermoellerbin target .project .classpath .settings .clover truth-0.7.0/README0000644000175000017500000000004612017424360013423 0ustar moellermoellerTruth: We've made failure a strategy. truth-0.7.0/pom.xml0000644000175000017500000000147212017424360014064 0ustar moellermoeller 4.0.0 org.junit.contrib truth 0.7.0 junit junit 4.8.2 jar compile cgruber-junit-snapshot-repo JUnit Snapshot Repo https://github.com/cgruber/junit/raw/maven-snapshot/repo/ truth-0.7.0/src/0000755000175000017500000000000012017424360013332 5ustar moellermoellertruth-0.7.0/src/main/0000755000175000017500000000000012017424360014256 5ustar moellermoellertruth-0.7.0/src/main/java/0000755000175000017500000000000012017424360015177 5ustar moellermoellertruth-0.7.0/src/main/java/org/0000755000175000017500000000000012017424360015766 5ustar moellermoellertruth-0.7.0/src/main/java/org/junit/0000755000175000017500000000000012017424360017117 5ustar moellermoellertruth-0.7.0/src/main/java/org/junit/contrib/0000755000175000017500000000000012017424360020557 5ustar moellermoellertruth-0.7.0/src/main/java/org/junit/contrib/truth/0000755000175000017500000000000012017424360021725 5ustar moellermoellertruth-0.7.0/src/main/java/org/junit/contrib/truth/AbstractVerb.java0000644000175000017500000000322412017424360025153 0ustar moellermoellerpackage org.junit.contrib.truth; import org.junit.contrib.truth.subjects.Subject; import org.junit.contrib.truth.subjects.SubjectFactory; import org.junit.contrib.truth.util.GwtCompatible; @GwtCompatible public class AbstractVerb { private final FailureStrategy failureStrategy; public AbstractVerb(FailureStrategy failureStrategy) { this.failureStrategy = failureStrategy; } protected FailureStrategy getFailureStrategy() { return failureStrategy; } /** * Triggers the failure strategy with an empty failure message */ public void fail() { failureStrategy.fail(""); } /** * Triggers the failure strategy with the given failure message */ public void fail(String message) { failureStrategy.fail(message); } /** * The recommended method of extension of Truth to new types, which is * documented in {@link DelegationTest }. * * @see DelegationTest * @param factory a SubjectFactory implementation * @returns A custom verb for the type returned by the SubjectFactory */ public , T, SF extends SubjectFactory> DelegatedVerb about(SF factory) { return new DelegatedVerb(getFailureStrategy(), factory); } /** * A special Verb implementation which wraps a SubjectFactory */ public static class DelegatedVerb, T> extends AbstractVerb { private final SubjectFactory factory; public DelegatedVerb(FailureStrategy fs, SubjectFactory factory) { super(fs); this.factory = factory; } public S that(T target) { return factory.getSubject(getFailureStrategy(), target); } } } truth-0.7.0/src/main/java/org/junit/contrib/truth/Expect.java0000644000175000017500000000464212017424360024026 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import java.util.ArrayList; import java.util.List; import org.junit.contrib.truth.util.GwtIncompatible; import org.junit.rules.MethodRule; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.Statement; @GwtIncompatible("JUnit4") @SuppressWarnings("deprecation") public class Expect extends TestVerb implements MethodRule { protected static class ExpectationGatherer implements FailureStrategy { List messages = new ArrayList(); @Override public void fail(String message) { messages.add(message); } } private final ExpectationGatherer gatherer; private boolean inRuleContext = false; public static Expect create() { return new Expect(new ExpectationGatherer()); } Expect(ExpectationGatherer gatherer) { super(gatherer); this.gatherer = gatherer; } @Override protected FailureStrategy getFailureStrategy() { if (!inRuleContext) { String message = "assertion made on Expect instance, but it's not enabled as a @Rule."; throw new IllegalStateException(message); } return super.getFailureStrategy(); } // TODO(cgruber): Make this override TestRule when 4.9 is released. @Override public Statement apply(final Statement base, FrameworkMethod method, Object target) { return new Statement() { @Override public void evaluate() throws Throwable { inRuleContext = true; base.evaluate(); inRuleContext = false; if (!gatherer.messages.isEmpty()) { String message = "All failed expectations:\n"; for (int i = 0; i < gatherer.messages.size(); i++) { message += " " + (i + 1) + ". " + gatherer.messages.get(i) + "\n"; } throw new AssertionError(message); } } }; } } truth-0.7.0/src/main/java/org/junit/contrib/truth/FailureStrategy.java0000644000175000017500000000144112017424360025702 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import org.junit.contrib.truth.util.GwtCompatible; @GwtCompatible public interface FailureStrategy { void fail(String message); } truth-0.7.0/src/main/java/org/junit/contrib/truth/TestVerb.java0000644000175000017500000000505312017424360024331 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import java.util.Arrays; import java.util.Collection; import java.util.List; import org.junit.contrib.truth.subjects.BooleanSubject; import org.junit.contrib.truth.subjects.CollectionSubject; import org.junit.contrib.truth.subjects.DefaultSubject; import org.junit.contrib.truth.subjects.IntegerSubject; import org.junit.contrib.truth.subjects.IterableSubject; import org.junit.contrib.truth.subjects.ListSubject; import org.junit.contrib.truth.subjects.StringSubject; import org.junit.contrib.truth.util.GwtCompatible; @GwtCompatible public class TestVerb extends AbstractVerb { public TestVerb(FailureStrategy failureStrategy) { super(failureStrategy); } public DefaultSubject that(Object target) { return new DefaultSubject(getFailureStrategy(), target); } public IntegerSubject that(Long target) { return new IntegerSubject(getFailureStrategy(), target); } public IntegerSubject that(Integer target) { return new IntegerSubject(getFailureStrategy(), target); } public BooleanSubject that(Boolean target) { return new BooleanSubject(getFailureStrategy(), target); } public StringSubject that(String target) { return new StringSubject(getFailureStrategy(), target); } public > IterableSubject, T, C> that(Iterable target) { return IterableSubject.create(getFailureStrategy(), target); } public > CollectionSubject, T, C> that(Collection target) { return CollectionSubject.create(getFailureStrategy(), target); } public > ListSubject, T, C> that(List target) { return ListSubject.create(getFailureStrategy(), target); } public > ListSubject, T, C> that(T[] target) { return that(Arrays.asList(target)); } } truth-0.7.0/src/main/java/org/junit/contrib/truth/Truth.gwt.xml0000755000175000017500000000016612017424360024363 0ustar moellermoeller truth-0.7.0/src/main/java/org/junit/contrib/truth/Truth.java0000644000175000017500000000462412017424360023704 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import org.junit.contrib.truth.util.GwtCompatible; import org.junit.contrib.truth.util.GwtIncompatible; import org.junit.internal.AssumptionViolatedException; /** * Truth - a proposition framework for tests, supporting JUnit style * assertion and assumption semantics in a fluent style. * * Truth is the simplest entry point class. A developer can statically * import the ASSERT and ASSUME constants to get easy access to the * library's capabilities. Then, instead of writing: *
{@code 
 * Assert.assertEquals(a,b);
 * Assert.assertTrue(c);
 * Assert.assertTrue(d.contains(a) && d.contains(e));
 * Assert.assertTrue(d.contains(a) || d.contains(q) || d.contains(z));
 * }
* one would write: *
{@code 
 * ASSERT.that(a).equals(b);
 * ASSERT.that(c).isTrue();
 * ASSERT.that(d).contains(a).and().contains(b);
 * // or
 * ASSERT.that(d).containsAllOf(a, b);
 * ASSERT.that(d).containsAnyOf(a, q, z);
 * }
* * Tests should be easier to read, and flow more clearly. * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @GwtCompatible(emulated = true) public class Truth { public static final FailureStrategy THROW_ASSERTION_ERROR = new FailureStrategy() { @Override public void fail(String message) { throw new AssertionError(message); } }; @GwtIncompatible("JUnit4") public static final FailureStrategy THROW_ASSUMPTION_ERROR = new FailureStrategy() { @Override public void fail(String message) { throw new AssumptionViolatedException(message); } }; public static final TestVerb ASSERT = new TestVerb(THROW_ASSERTION_ERROR); @GwtIncompatible("JUnit4") public static final TestVerb ASSUME = new TestVerb(THROW_ASSUMPTION_ERROR); } truth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/0000755000175000017500000000000012017424360023547 5ustar moellermoellertruth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/BooleanSubject.java0000644000175000017500000000242412017424360027313 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.subjects; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.util.GwtCompatible; /** * Propositions for boolean subjects * * @author Christian Gruber (cgruber@israfil.net) */ @GwtCompatible public class BooleanSubject extends Subject { public BooleanSubject(FailureStrategy failureStrategy, Boolean subject) { super(failureStrategy, subject); } public void isTrue() { if (getSubject() == null || !getSubject()) { fail("is true"); } } public void isFalse() { if (getSubject() == null || getSubject()) { fail("is false"); } } } truth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/CollectionSubject.java0000644000175000017500000000653512017424360030036 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.subjects; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.util.GwtCompatible; @GwtCompatible public class CollectionSubject, T, C extends Collection> extends IterableSubject { @SuppressWarnings("unchecked") public static > CollectionSubject, T, C> create( FailureStrategy failureStrategy, Collection list) { return new CollectionSubject(failureStrategy, list); } // TODO: Arguably this should even be package private protected CollectionSubject(FailureStrategy failureStrategy, C list) { super(failureStrategy, list); } /** * Attests that a Collection contains the provided object or fails. */ @Override public And contains(Object item) { if (!getSubject().contains(item)) { fail("contains", item); } return nextChain(); } @Override public And isEmpty() { if (!getSubject().isEmpty()) { fail("isEmpty"); } return nextChain(); } /** * Attests that a Collection contains at least one of the provided * objects or fails. */ public And containsAnyOf(Object ... items) { Collection collection = getSubject(); for (Object item : items) { if (collection.contains(item)) { return nextChain(); } } fail("contains", (Object[])items); return nextChain(); } /** * Attests that a Collection contains all of the provided objects or fails. * This copes with duplicates in both the Collection and the parameters. */ public And containsAllOf(Object ... items) { Collection collection = getSubject(); // Arrays.asList() does not support remove() so we need a mutable copy. List required = new ArrayList(Arrays.asList(items)); for (Object item : collection) { required.remove(item); } if (!required.isEmpty()) { // Try and make a useful message when dealing with duplicates. Set missing = new HashSet(required); Object[] params = new Object[missing.size()]; int n = 0; for (Object item : missing) { int count = countOf(item, items); params[n++] = (count > 1) ? count + " copies of " + item : item; } fail("contains", params); } return nextChain(); } private static int countOf(Object t, Object... items) { int count = 0; for (Object item : items) { if (t == null ? (item == null) : t.equals(item)) { count++; } } return count; } } truth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/DefaultSubject.java0000644000175000017500000000171312017424360027320 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.subjects; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.util.GwtCompatible; @GwtCompatible public class DefaultSubject extends Subject { public DefaultSubject(FailureStrategy failureStrategy, Object o) { super(failureStrategy, o); } } truth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/IntegerSubject.java0000644000175000017500000000737612017424360027344 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.subjects; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.util.GwtCompatible; /** * Propositions for Integral numeric subjects * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @GwtCompatible public class IntegerSubject extends Subject { public IntegerSubject(FailureStrategy failureStrategy, Long i) { super(failureStrategy, i); } public IntegerSubject(FailureStrategy failureStrategy, Integer i) { super(failureStrategy, i == null ? null : new Long(i.longValue())); } /** * Attests that a Subject is inclusively within the {@code lower} and * {@code upper} bounds provided or fails. * * @throws IllegalArgumentException * if the lower bound is greater than the upper. */ public And isInclusivelyInRange(long lower, long upper) { ensureOrderedBoundaries(lower, upper); if (!(lower <= getSubject() && getSubject() <= upper)) { fail("is inclusively in range", lower, upper); } return nextChain(); } /** * Attests that a Subject is exclusively within the {@code lower} and * {@code upper} bounds provided or fails. * * @throws IllegalArgumentException * if the lower bound is greater than the upper. */ public And isBetween(long lower, long upper) { ensureOrderedBoundaries(lower, upper); if (!(lower < getSubject() && getSubject() < upper)) { fail("is in between", lower, upper); } return nextChain(); } /** * Guards against inverted lower/upper boundaries, and throws if * they are so inverted. */ private void ensureOrderedBoundaries(long lower, long upper) { if (lower > upper) { throw new IllegalArgumentException( "Range inclusion parameter lower (" + lower + ") " + " should not be greater than upper (" + upper + ")"); } } public And isEqualTo(Integer other) { return isEqualTo((other == null) ? null : new Long(other.longValue())); } public And isEqualTo(Long other) { if (getSubject() == null) { if(other != null) { fail("is equal to", other); } } else { // Coerce to a long. if (!new Long(getSubject().longValue()).equals(other)) { fail("is equal to", other); } } return nextChain(); } public And isNotEqualTo(Integer other) { return isNotEqualTo((other == null) ? null : new Long(other.longValue())); } public And isNotEqualTo(Long other) { if (getSubject() == null) { if(other == null) { fail("is not equal to", other); } } else { // Coerce to a long. if (new Long(getSubject().longValue()).equals(other)) { fail("is not equal to", other); } } return nextChain(); } public And is(int other) { return super.is((long)other); } public And is(short other) { return super.is((long)other); } public And is(byte other) { return super.is((long)other); } } truth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/IterableSubject.java0000644000175000017500000000522512017424360027465 0ustar moellermoeller/* * Copyright (C) 2011 Google, Inc. * * 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. */ package org.junit.contrib.truth.subjects; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.util.GwtCompatible; /** * @author Kevin Bourrillion */ @GwtCompatible public class IterableSubject, T, C extends Iterable> extends Subject { @SuppressWarnings("unchecked") public static > IterableSubject, T, C> create( FailureStrategy failureStrategy, Iterable list) { return new IterableSubject(failureStrategy, list); } // TODO: Arguably this should even be package private protected IterableSubject(FailureStrategy failureStrategy, C list) { super(failureStrategy, list); } public And contains(Object item) { for (Object t : getSubject()) { if (item == t || item != null && item.equals(t)) { return nextChain(); } } fail("contains", item); throw new AssertionError(); } /** * Attests that a Collection contains the provided object or fails. */ public And isEmpty() { if (getSubject().iterator().hasNext()) { fail("isEmpty"); } return nextChain(); } public And hasContentsInOrder(Object... expected) { // TODO(kevinb): prettier error message List target = new ArrayList(); for (Object t : getSubject()) { target.add(t); } check().that(target).isEqualTo(Arrays.asList(expected)); return nextChain(); } public And hasContentsAnyOrder(Object... expected) { check().that(createFakeMultiset(getSubject())) .isEqualTo(createFakeMultiset(Arrays.asList(expected))); return nextChain(); } private static Map createFakeMultiset(Iterable iterable) { Map map = new HashMap(); for (Object t : iterable) { Integer count = map.get(t); map.put(t, (count == null) ? 1 : count + 1); } return map; } } truth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/ListSubject.java0000644000175000017500000001122112017424360026642 0ustar moellermoeller/* * Copyright (c) 2011 David Beaumont * * 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. */ package org.junit.contrib.truth.subjects; import java.util.Comparator; import java.util.List; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.util.GwtCompatible; @GwtCompatible public class ListSubject, T, C extends List> extends CollectionSubject { @SuppressWarnings("unchecked") public static > ListSubject, T, C> create( FailureStrategy failureStrategy, List list) { return new ListSubject(failureStrategy, list); } protected ListSubject(FailureStrategy failureStrategy, C list) { super(failureStrategy, list); } /** * Attests that a List contains the specified sequence. */ public And containsSequence(List sequence) { if (sequence.isEmpty()) { return nextChain(); } List list = getSubject(); while (true) { int first = list.indexOf(sequence.get(0)); if (first < 0) { break; // Not found } int last = first + sequence.size(); if (last > list.size()) { break; // Not enough room left } if (sequence.equals(list.subList(first, last))) { return nextChain(); } list = list.subList(first + 1, list.size()); } fail("contains sequence", sequence); return nextChain(); } /** * Attests that a List is strictly ordered according to the natural ordering of its elements. * Null elements are not permitted. * * @throws ClassCastException if any pair of elements is not mutually Comparable. * @throws NullPointerException if any element is null. */ public And isOrdered() { return pairwiseCheck(new PairwiseChecker() { @SuppressWarnings("unchecked") @Override public void check(T prev, T next) { if (((Comparable) prev).compareTo(next) >= 0) { fail("is strictly ordered", prev, next); } } }); } /** * Attests that a List is partially ordered according to the natural ordering of its elements. * Null elements are not permitted. * * @throws ClassCastException if any pair of elements is not mutually Comparable. * @throws NullPointerException if any element is null. */ public And isPartiallyOrdered() { return pairwiseCheck(new PairwiseChecker() { @SuppressWarnings("unchecked") @Override public void check(T prev, T next) { if (((Comparable) prev).compareTo(next) > 0) { fail("is partially ordered", prev, next); } } }); } /** * Attests that a List is strictly ordered according to the given comparator. * Null elements are not permitted. * * @throws ClassCastException if any pair of elements is not mutually Comparable. * @throws NullPointerException if any element is null. */ public And isOrdered(final Comparator comparator) { return pairwiseCheck(new PairwiseChecker() { @Override public void check(T prev, T next) { if (comparator.compare(prev, next) >= 0) { fail("is strictly ordered", prev, next); } } }); } /** * Attests that a List is partially ordered according to the given comparator. * Null elements are not permitted. * * @throws ClassCastException if any pair of elements is not mutually Comparable. * @throws NullPointerException if any element is null. */ public And isPartiallyOrdered(final Comparator comparator) { return pairwiseCheck(new PairwiseChecker() { @Override public void check(T prev, T next) { if (comparator.compare(prev, next) > 0) { fail("is partially ordered", prev, next); } } }); } private And pairwiseCheck(PairwiseChecker checker) { List list = getSubject(); if (list.size() > 1) { T prev = list.get(0); for (int n = 1; n < list.size(); n++) { T next = list.get(n); checker.check(prev, next); prev = next; } } return nextChain(); } private interface PairwiseChecker { void check(T prev, T next); } } truth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/StringSubject.java0000644000175000017500000000361012017424360027200 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.subjects; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.util.GwtCompatible; /** * Propositions for String subjects * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @GwtCompatible public class StringSubject extends Subject { public StringSubject(FailureStrategy failureStrategy, String string) { super(failureStrategy, string); } public And contains(String string) { if (getSubject() == null) { if (string != null) { fail("contains", string); } } else if (!getSubject().contains(string)) { fail("contains", string); } return nextChain(); } public And startsWith(String string) { if (getSubject() == null) { if (string != null) { fail("starts with", string); } } else if (!getSubject().startsWith(string)) { fail("starts with", string); } return nextChain(); } public And endsWith(String string) { if (getSubject() == null) { if (string != null) { fail("ends with", string); } } else if (!getSubject().endsWith(string)) { fail("ends with", string); } return nextChain(); } } truth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/Subject.java0000644000175000017500000000777212017424360026026 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.subjects; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.TestVerb; import org.junit.contrib.truth.util.GwtCompatible; import org.junit.contrib.truth.util.GwtIncompatible; /** * Propositions for arbitrarily typed subjects and for properties * of Object * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @GwtCompatible(emulated = true) public class Subject,T> { private final FailureStrategy failureStrategy; private final T subject; private final And chain; public Subject(FailureStrategy failureStrategy, T subject) { this.failureStrategy = failureStrategy; this.subject = subject; this.chain = new And(){ @SuppressWarnings("unchecked") @Override public S and() { return (S)Subject.this; } }; } /** * A method which wraps the current Subject concrete * subtype in a chaining "And" object. */ protected final And nextChain() { return chain; } public And is(T other) { if (getSubject() == null) { if(other != null) { fail("is", other); } } else { if (!getSubject().equals(other)) { fail("is", other); } } return nextChain(); } public And isNull() { if (getSubject() != null) { failWithoutSubject("is null"); } return nextChain(); } public And isNotNull() { if (getSubject() == null) { failWithoutSubject("is not null"); } return nextChain(); } public And isEqualTo(Object other) { if (getSubject() == null) { if(other != null) { fail("is equal to", other); } } else { if (!getSubject().equals(other)) { fail("is equal to", other); } } return nextChain(); } public And isNotEqualTo(Object other) { if (getSubject() == null) { if(other == null) { fail("is not equal to", other); } } else { if (getSubject().equals(other)) { fail("is not equal to", other); } } return nextChain(); } @GwtIncompatible("Class.isInstance") public And isA(Class clazz) { if (!clazz.isInstance(getSubject())) { fail("is a", clazz.getName()); } return nextChain(); } @GwtIncompatible("Class.isInstance") public And isNotA(Class clazz) { if (clazz.isInstance(getSubject())) { fail("is not a", clazz.getName()); } return nextChain(); } protected T getSubject() { return subject; } protected TestVerb check() { return new TestVerb(failureStrategy); } protected void fail(String verb, Object... messageParts) { String message = "Not true that "; message += "<" + getSubject() + "> " + verb; for (Object part : messageParts) { message += " <" + part + ">"; } failureStrategy.fail(message); } protected void failWithoutSubject(String verb) { String message = "Not true that "; message += "the subject " + verb; failureStrategy.fail(message); } /** * A convenience class to allow for chaining in the fluent API * style, such that subjects can make propositions in series. * i.e. ASSERT.that(blah).isNotNull().and().contains(b).and().isNotEmpty(); */ public static interface And { /** * Returns the next object in the chain of anded objects. */ C and(); } } truth-0.7.0/src/main/java/org/junit/contrib/truth/subjects/SubjectFactory.java0000644000175000017500000000203612017424360027342 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.subjects; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.util.GwtCompatible; /** * A custom subject factory which will return a FooSubject (which * is a Subject). * * @author Christian Gruber (cgruber@israfil.net) */ @GwtCompatible public interface SubjectFactory, T> { S getSubject(FailureStrategy fs, T that); } truth-0.7.0/src/main/java/org/junit/contrib/truth/util/0000755000175000017500000000000012017424360022702 5ustar moellermoellertruth-0.7.0/src/main/java/org/junit/contrib/truth/util/GwtCompatible.java0000644000175000017500000000241112017424360026304 0ustar moellermoeller/* * Copyright (C) 2009 The Guava Authors * * 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. */ package org.junit.contrib.truth.util; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * This is a straight copy of {@link com.google.common.annotations.GwtCompatible} * copied to Truth so that it can be used to GWT enable guava tests without creating * a dependency from Truth back to Guava. */ @Retention(RetentionPolicy.CLASS) @Target({ ElementType.TYPE, ElementType.METHOD }) @Documented @GwtCompatible public @interface GwtCompatible { boolean serializable() default false; boolean emulated() default false; } truth-0.7.0/src/main/java/org/junit/contrib/truth/util/GwtIncompatible.java0000644000175000017500000000237412017424360026643 0ustar moellermoeller/* * Copyright (C) 2009 The Guava Authors * * 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. */ package org.junit.contrib.truth.util; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * This is a straight copy of {@link com.google.common.annotations.GwtIncompatible} * copied to Truth so that it can be used to GWT enable guava tests without creating * a dependency from Truth back to Guava. */ @Retention(RetentionPolicy.CLASS) @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD }) @Documented @GwtCompatible public @interface GwtIncompatible { String value(); } truth-0.7.0/src/test/0000755000175000017500000000000012017424360014311 5ustar moellermoellertruth-0.7.0/src/test/java/0000755000175000017500000000000012017424360015232 5ustar moellermoellertruth-0.7.0/src/test/java/org/0000755000175000017500000000000012017424360016021 5ustar moellermoellertruth-0.7.0/src/test/java/org/junit/0000755000175000017500000000000012017424360017152 5ustar moellermoellertruth-0.7.0/src/test/java/org/junit/contrib/0000755000175000017500000000000012017424360020612 5ustar moellermoellertruth-0.7.0/src/test/java/org/junit/contrib/truth/0000755000175000017500000000000012017424360021760 5ustar moellermoellertruth-0.7.0/src/test/java/org/junit/contrib/truth/AbstractVerbTest.java0000644000175000017500000000306412017424360026050 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import static org.junit.contrib.truth.Truth.ASSERT; import org.junit.Test; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; /** * Tests for Boolean Subjects. * * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(Theories.class) public class AbstractVerbTest { private String failureMessage = null; private AbstractVerb captureFailure = new AbstractVerb(new FailureStrategy() { @Override public void fail(String message) { failureMessage = message; } }); @DataPoints public static String[] strings = new String[] {"a", "b"}; @Test public void noArgFail() { captureFailure.fail(); ASSERT.that(failureMessage).is(""); } @Theory public void argFail(String message) { captureFailure.fail(message); ASSERT.that(failureMessage).is(message); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/AllTests.java0000644000175000017500000000263712017424360024366 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import org.junit.contrib.truth.delegatetest.DelegationTest; import org.junit.contrib.truth.extensiontest.ExtensionTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; /** * Test suite * * TODO(cgruber): See if we even need this - Maven doesn't, and it * ends up being redundant in eclipse. * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(Suite.class) @SuiteClasses({ BooleanTest.class, CollectionTest.class, ExpectFailureTest.class, ExpectTest.class, IntegerTest.class, ListTest.class, StringTest.class, SubjectTest.class, // Extensions-tests. DelegationTest.class, ExtensionTest.class }) public class AllTests { } truth-0.7.0/src/test/java/org/junit/contrib/truth/BooleanTest.java0000644000175000017500000000315312017424360025044 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import static org.junit.Assert.fail; import static org.junit.contrib.truth.Truth.ASSERT; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** * Tests for Boolean Subjects. * * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(JUnit4.class) public class BooleanTest { @Test public void isTrue() { ASSERT.that(true).isTrue(); } @Test public void isTrueFailing() { try { ASSERT.that(false).isTrue(); fail("Should have thrown"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()).contains("Not true that is true"); } } @Test public void isFalse() { ASSERT.that(false).isFalse(); } @Test public void isFalseFailing() { try { ASSERT.that(true).isFalse(); fail("Should have thrown"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()).contains("Not true that is false"); } } } truth-0.7.0/src/test/java/org/junit/contrib/truth/CollectionTest.java0000644000175000017500000001101312017424360025552 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import static org.junit.Assert.fail; import static org.junit.contrib.truth.Truth.ASSERT; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import java.util.Arrays; import java.util.Collection; /** * Tests for Collection Subjects. * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(JUnit4.class) public class CollectionTest { @Test public void listContains() { ASSERT.that(collection(1, 2, 3)).contains(1); } @Test public void listContainsWithChaining() { ASSERT.that(collection(1, 2, 3)).contains(1).and().contains(2); } @Test public void listContainsWithNull() { ASSERT.that(collection(1, null, 3)).contains(null); } @Test public void listContainsWith2KindsOfChaining() { Collection foo = collection(1, 2, 3); Collection bar = foo; ASSERT.that(foo).is(bar).and().contains(1).and().contains(2); } @Test public void listContainsFailureWithChaining() { try { ASSERT.that(collection(1, 2, 3)).contains(1).and().contains(5); fail("Should have thrown."); } catch (AssertionError e) {} } @Test public void listContainsFailure() { try { ASSERT.that(collection(1, 2, 3)).contains(5); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that"); } } @Test public void listContainsAnyOf() { ASSERT.that(collection(1, 2, 3)).containsAnyOf(1, 5); } @Test public void listContainsAnyOfWithNull() { ASSERT.that(collection(1, null, 3)).containsAnyOf(null, 5); } @Test public void listContainsAnyOfFailure() { try { ASSERT.that(collection(1, 2, 3)).containsAnyOf(5, 6, 0); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that"); } } @Test public void listContainsAllOf() { ASSERT.that(collection(1, 2, 3)).containsAllOf(1, 2); } @Test public void listContainsAllOfWithDuplicates() { ASSERT.that(collection(1, 2, 2, 2, 3)).containsAllOf(2, 2); } @Test public void listContainsAllOfWithNull() { ASSERT.that(collection(1, null, 3)).containsAllOf(3, null); } @Test public void listContainsAllOfFailure() { try { ASSERT.that(collection(1, 2, 3)).containsAllOf(1, 2, 4); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that").and().contains("<4>"); } } @Test public void listContainsAllOfWithDuplicatesFailure() { try { ASSERT.that(collection(1, 2, 3)).containsAllOf(1, 2, 2, 2, 3, 4); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that") .and().contains("<3 copies of 2>") .and().contains("<4>"); } } /* * Slightly subtle test to ensure that if multiple equal elements are found * to be missing we only reference it once in the output message. */ @Test public void listContainsAllOfWithDuplicateMissingElements() { try { ASSERT.that(collection(1, 2)).containsAllOf(4, 4, 4); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that") .and().endsWith("contains <3 copies of 4>"); } } @Test public void listContainsAllOfWithNullFailure() { try { ASSERT.that(collection(1, null, 3)).containsAllOf(1, null, null, 3); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that") .and().contains("<2 copies of null>"); } } /** * Helper that returns a general Collection rather than a List. * This ensures that we test CollectionSubject (rather than ListSubject). */ private static Collection collection(T... items) { return Arrays.asList(items); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/ExampleTest.java0000644000175000017500000000445212017424360025063 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theory; import org.junit.experimental.theories.Theories; import org.junit.runner.RunWith; import static org.junit.Assert.fail; import org.junit.Rule; import org.junit.Test; import java.util.Arrays; import static org.junit.contrib.truth.Truth.ASSERT; import static org.junit.contrib.truth.Truth.ASSUME; import org.junit.contrib.truth.Expect; import static org.junit.contrib.truth.delegatetest.FooSubject.FOO; import org.junit.contrib.truth.delegatetest.Foo; @RunWith(Theories.class) public class ExampleTest { @Test public void stringContains() { ASSERT.that("abc").contains("c"); } @Test public void listContains() { ASSERT.that(Arrays.asList(1, 2, 3)).contains(1); } @Test public void listContainsWithChaining() { ASSERT.that(Arrays.asList(1, 2, 3)).contains(1).and().contains(2); } @Test public void equalityFail() { int x = 2 + 2; try { ASSERT.that(x).isEqualTo(5); fail("Should have thrown"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()).contains("Not true that <4> is equal to <5>"); } } @DataPoints public static int[] ints = { -1, 0, 1, 2 }; @Theory public void divideBySelf(int x) { ASSUME.that(x).isNotEqualTo(0); ASSERT.that(x / x).isEqualTo(1); } @Rule public Expect EXPECT = Expect.create(); @Test public void expectRange() { int x = 4; EXPECT.that(x).isNotNull(); EXPECT.that(x).isBetween(3, 5); EXPECT.that(x).isEqualTo(4); } @Test public void customTypeCompares() { ASSERT.about(FOO).that(new Foo(5)).matches(new Foo(2 + 3)); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/ExpectFailureTest.java0000644000175000017500000000455712017424360026236 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import java.util.Arrays; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.truth.Expect.ExpectationGatherer; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.Statement; /** * Tests (and effectively sample code) for the Expect * verb (implemented as a rule) * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(JUnit4.class) public class ExpectFailureTest { @Rule public Expect EXPECT = new FailingExpect(new ExpectationGatherer()); @Test public void expectFailNotEquals() { EXPECT.that(4).isNotEqualTo(4); } @Test public void expectFailStringContains() { EXPECT.that("abc").contains("x").and().contains("y").and().contains("z"); } @Test public void expectFailContainsAllOf() { EXPECT.that(Arrays.asList("a", "b", "c")).containsAllOf("a", "c", "d"); } @Test public void expectFailContainsAnyOf() { EXPECT.that(Arrays.asList("a", "b", "c")).containsAnyOf("z", "q"); } public static class FailingExpect extends Expect { protected FailingExpect(ExpectationGatherer gatherer) { super(gatherer); } @Override public Statement apply(Statement base, FrameworkMethod method, Object target) { final Statement s = super.apply(base, method, target); return new Statement() { @Override public void evaluate() throws Throwable { try { s.evaluate(); } catch (AssertionError e) { return; //ignore - we're happy that it threw. } throw new AssertionError("Should have thrown error with caught assertion failures.."); } }; } } } truth-0.7.0/src/test/java/org/junit/contrib/truth/ExpectTest.java0000644000175000017500000000426212017424360024717 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.MethodRule; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.Statement; /** * Tests (and effectively sample code) for the Expect verb (implemented as a * rule) * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(JUnit4.class) public class ExpectTest { private Expect oopsNotARule = Expect.create(); private Expect EXPECT = Expect.create(); private ExpectedException thrown = ExpectedException.none(); @Rule public MethodRule wrapper = new MethodRule() { @Override public Statement apply(Statement base, FrameworkMethod method, Object target) { Statement expected = EXPECT.apply(base, method, target); return thrown.apply(expected, method, target); } }; @Test public void expectTrue() { EXPECT.that(4).isEqualTo(4); } @Test public void expectFail() { thrown.expectMessage("All failed expectations:"); thrown.expectMessage("1. Not true that contains "); thrown.expectMessage("2. Not true that contains "); thrown.expectMessage("3. Not true that contains "); EXPECT.that("abc").contains("x").and().contains("y").and().contains("z"); } @Test public void warnWhenExpectIsNotRule() { String message = "assertion made on Expect instance, but it's not enabled as a @Rule."; thrown.expectMessage(message); oopsNotARule.that(true).is(true); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/IntegerTest.java0000644000175000017500000001121312017424360025056 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import static org.junit.Assert.fail; import static org.junit.contrib.truth.Truth.ASSERT; import static org.junit.contrib.truth.Truth.ASSUME; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.truth.Expect; import org.junit.internal.AssumptionViolatedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** * Tests for Integer Subjects. * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(JUnit4.class) public class IntegerTest { @Rule public Expect EXPECT = Expect.create(); @Test public void simpleEquality() { ASSERT.that(2 + 2).isEqualTo(4).and().isBetween(3, 5); } @Test public void intIsInt() { ASSERT.that(4).is(4); } @Test public void simpleInequality() { ASSERT.that(2 + 2).isNotEqualTo(5); } @Test public void equalityFail() { try { ASSERT.that(2 + 2).isEqualTo(5); fail("Should have thrown"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()).contains("Not true that <4> is equal to <5>"); } } @Test public void inequalityFail() { try { ASSERT.that(2 + 2).isNotEqualTo(4); fail("Should have thrown"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()).contains("Not true that <4> is not equal to <4>"); } } @Test public void additionAssumptionFail() { try { ASSUME.that(2 + 2).isEqualTo(5); fail("Should have thrown"); } catch (AssumptionViolatedException expected) {} } @Test public void inclusiveRangeContainment() { EXPECT.that(2).isInclusivelyInRange(2, 4); EXPECT.that(3).isInclusivelyInRange(2, 4); EXPECT.that(4).isInclusivelyInRange(2, 4); } @Test public void inclusiveRangeContainmentFailure() { try { ASSERT.that(1).isInclusivelyInRange(2, 4); fail("Should have thrown"); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that <1> is inclusively in range <2> <4>"); } try { ASSERT.that(5).isInclusivelyInRange(2, 4); fail("Should have thrown"); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that <5> is inclusively in range <2> <4>"); } } @Test public void inclusiveRangeContainmentInversionError() { try { ASSERT.that(Integer.MAX_VALUE).isInclusivelyInRange(4, 2); fail("Should have thrown"); } catch (IllegalArgumentException e) {} } @Test public void exclusiveRangeContainment() { EXPECT.that(3).isBetween(2, 5); EXPECT.that(4).isBetween(2, 5); } @Test public void exclusiveRangeContainmentFailure() { try { ASSERT.that(5).isBetween(2, 5); fail("Should have thrown"); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that <5> is in between <2> <5>"); } } @Test public void exclusiveRangeContainmentInversionError() { try { ASSERT.that(Integer.MAX_VALUE).isBetween(5, 2); fail("Should have thrown"); } catch (IllegalArgumentException e) {} } @Test public void equalityOfNulls() { ASSERT.that((Integer)null).isEqualTo((Long)null); } @Test public void equalityOfNullsFail() { try { ASSERT.that((Long)null).isEqualTo(5); fail("Should have thrown"); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that is equal to <5>"); } try { ASSERT.that(5).isEqualTo((Integer)null); fail("Should have thrown"); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that <5> is equal to "); } } @Test public void inequalityOfNulls() { ASSERT.that((Long)null).isNotEqualTo(4); ASSERT.that(4).isNotEqualTo((Long)null); } @Test public void inequalityOfNullsFail() { try { ASSERT.that((Long)null).isNotEqualTo((Integer)null); fail("Should have thrown"); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that is not equal to "); } } } truth-0.7.0/src/test/java/org/junit/contrib/truth/IterableTest.java0000644000175000017500000000550712017424360025221 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import static org.junit.Assert.fail; import static org.junit.contrib.truth.Truth.ASSERT; import java.util.Arrays; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** * Tests for Collection Subjects. * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(JUnit4.class) public class IterableTest { @Test public void iterableContainsWithNull() { ASSERT.that(iterable(1, null, 3)).contains(null); } @Test public void iterableContainsWith2KindsOfChaining() { Iterable foo = iterable(1, 2, 3); Iterable bar = foo; ASSERT.that(foo).is(bar).and().contains(1).and().contains(2); } @Test public void iterableContainsFailureWithChaining() { try { ASSERT.that(iterable(1, 2, 3)).contains(1).and().contains(5); fail("Should have thrown."); } catch (AssertionError e) {} } @Test public void iterableContainsFailure() { try { ASSERT.that(iterable(1, 2, 3)).contains(5); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that"); } } @Test public void iterablehasContentsAnyOrder() { ASSERT.that(iterable(1, 2, 3)).hasContentsAnyOrder(2, 3, 1); } @Test public void iterablehasContentsAnyOrder_Fail() { try { ASSERT.that(iterable(1, 2, 3)).hasContentsAnyOrder(2, 3, 4); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that"); } } @Test public void iterablehasContentsInOrder() { ASSERT.that(iterable(1, 2, 3)).hasContentsInOrder(1, 2, 3); } @Test public void iterablehasContentsInOrder_Fail() { try { ASSERT.that(iterable(1, 2, 3)).hasContentsInOrder(2, 3, 1); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("Not true that"); } } /** * Helper that returns a general Collection rather than a List. * This ensures that we test CollectionSubject (rather than ListSubject). */ private static Iterable iterable(T... items) { return Arrays.asList(items); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/ListTest.java0000644000175000017500000001252212017424360024400 0ustar moellermoeller/* * Copyright (c) 2011 David Beaumont * * 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. */ package org.junit.contrib.truth; import static org.junit.Assert.fail; import static org.junit.contrib.truth.Truth.ASSERT; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import java.util.Arrays; import java.util.Comparator; /** * Tests for List Subjects. * * @author David Beaumont */ @RunWith(JUnit4.class) public class ListTest { @Test public void listContainsSequenceWithEmptyList() { ASSERT.that(Arrays.asList(1, 2, 3)).containsSequence(Arrays.asList()); } @Test public void listContainsSequenceWithSingleton() { ASSERT.that(Arrays.asList(1)).containsSequence(Arrays.asList(1)); } @Test public void listContainsSequenceAtEnd() { ASSERT.that(Arrays.asList(1, 2, 3)).containsSequence(Arrays.asList(2, 3)); } @Test public void listContainsSequenceAtStart() { ASSERT.that(Arrays.asList(1, 2, 3)).containsSequence(Arrays.asList(1, 2)); } @Test public void listContainsSequenceWithManyFalseStarts() { ASSERT.that(Arrays.asList(1, 1, 2, 1, 1, 2, 3, 4)).containsSequence(Arrays.asList(1, 2, 3)); } @Test public void listContainsSequenceTooShortFailure() { try { ASSERT.that(Arrays.asList(1, 2, 3)).containsSequence(Arrays.asList(1, 2, 3, 4)); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("contains sequence").and().contains("[1, 2, 3, 4]"); } } @Test public void listContainsSequenceNotContiguousFailure() { try { ASSERT.that(Arrays.asList(1, 2, 2, 3)).containsSequence(Arrays.asList(1, 2, 3)); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("contains sequence").and().contains("[1, 2, 3]"); } } @Test public void listIsOrdered() { ASSERT.that(Arrays.asList()).isOrdered(); ASSERT.that(Arrays.asList(1)).isOrdered(); ASSERT.that(Arrays.asList(1, 2, 3, 4)).isOrdered(); } @Test public void isOrderedFailure() { try { ASSERT.that(Arrays.asList(1, 2, 2, 4)).isOrdered(); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("is strictly ordered").and().contains("<2> <2>"); } } @Test public void isOrderedWithNonComparableElementsFailure() { try { ASSERT.that(Arrays.asList(1, "2", 3, "4")).isOrdered(); fail("Should have thrown."); } catch (ClassCastException e) {} } @Test public void listIsPartiallyOrdered() { ASSERT.that(Arrays.asList()).isPartiallyOrdered(); ASSERT.that(Arrays.asList(1)).isPartiallyOrdered(); ASSERT.that(Arrays.asList(1, 1, 2, 3, 3, 3, 4)).isPartiallyOrdered(); } @Test public void isPartiallyOrderedFailure() { try { ASSERT.that(Arrays.asList(1, 3, 2, 4)).isPartiallyOrdered(); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("is partially ordered").and().contains("<3> <2>"); } } @Test public void isPartiallyOrderedWithNonComparableElementsFailure() { try { ASSERT.that(Arrays.asList(1, "2", 2, "3")).isPartiallyOrdered(); fail("Should have thrown."); } catch (ClassCastException e) {} } @Test public void listIsOrderedWithComparator() { ASSERT.that(Arrays.asList()).isOrdered(COMPARE_AS_DECIMAL); ASSERT.that(Arrays.asList("1")).isOrdered(COMPARE_AS_DECIMAL); // Note: Use "10" and "20" to distinguish numerical and lexicographical ordering. ASSERT.that(Arrays.asList("1", "2", "10", "20")).isOrdered(COMPARE_AS_DECIMAL); } @Test public void listIsOrderedWithComparatorFailure() { try { ASSERT.that(Arrays.asList("1", "2", "2", "10")).isOrdered(COMPARE_AS_DECIMAL); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("is strictly ordered").and().contains("<2> <2>"); } } @Test public void listIsPartiallyOrderedWithComparator() { ASSERT.that(Arrays.asList()).isPartiallyOrdered(COMPARE_AS_DECIMAL); ASSERT.that(Arrays.asList("1")).isPartiallyOrdered(COMPARE_AS_DECIMAL); ASSERT.that(Arrays.asList("1", "1", "2", "10", "10", "10", "20")) .isPartiallyOrdered(COMPARE_AS_DECIMAL); } @Test public void listIsPartiallyOrderedWithComparatorFailure() { try { ASSERT.that(Arrays.asList("1", "10", "2", "20")).isPartiallyOrdered(COMPARE_AS_DECIMAL); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).contains("is partially ordered").and().contains("<10> <2>"); } } private static final Comparator COMPARE_AS_DECIMAL = new Comparator() { @Override public int compare(String a, String b) { return Integer.valueOf(a).compareTo(Integer.valueOf(b)); } }; } truth-0.7.0/src/test/java/org/junit/contrib/truth/StringTest.java0000644000175000017500000000757412017424360024746 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import static org.junit.Assert.fail; import static org.junit.contrib.truth.Truth.ASSERT; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** * Tests for String Subjects. * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(JUnit4.class) public class StringTest { @Test public void stringContains() { ASSERT.that("abc").contains("c"); } @Test public void stringContainsFail() { try { ASSERT.that("abc").contains("d"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()) .contains("Not true that contains "); return; } fail("Should have thrown"); } @Test public void chain() { ASSERT.that("abc").contains("a").and().contains("b"); } @Test public void stringEquality() { ASSERT.that("abc").isEqualTo("abc"); } @Test public void stringEqualityFail() { try { ASSERT.that("abc").isEqualTo("abd"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()) .contains("Not true that is equal to "); return; } fail("Should have thrown"); } @Test public void stringStartsWith() { ASSERT.that("abc").startsWith("ab"); } @Test public void stringStartsWithFail() { try { ASSERT.that("abc").startsWith("bc"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()) .contains("Not true that starts with "); return; } fail("Should have thrown"); } @Test public void stringEndsWith() { ASSERT.that("abc").endsWith("bc"); } @Test public void stringEndsWithFail() { try { ASSERT.that("abc").endsWith("ab"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()) .contains("Not true that ends with "); return; } fail("Should have thrown"); } @Test public void emptyStringTests() { ASSERT.that("").contains(""); ASSERT.that("").startsWith(""); ASSERT.that("").endsWith(""); ASSERT.that("a").contains(""); ASSERT.that("a").startsWith(""); ASSERT.that("a").endsWith(""); } @Test public void stringNullNullTests() { ASSERT.that((String)null).contains(null); ASSERT.that((String)null).startsWith(null); ASSERT.that((String)null).endsWith(null); } @Test public void stringNullContains() { try { ASSERT.that((String)null).contains("a"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()) .contains("Not true that contains "); return; } fail("Should have thrown"); } @Test public void stringNullStartsWith() { try { ASSERT.that((String)null).startsWith("a"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()) .contains("Not true that starts with "); return; } fail("Should have thrown"); } @Test public void stringNullEndsWith() { try { ASSERT.that((String)null).endsWith("a"); } catch (AssertionError expected) { ASSERT.that(expected.getMessage()) .contains("Not true that ends with "); return; } fail("Should have thrown"); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/SubjectTest.java0000644000175000017500000001513712017424360025071 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth; import static org.junit.Assert.fail; import static org.junit.contrib.truth.Truth.ASSERT; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** * Tests for generic Subject behaviour. * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(JUnit4.class) public class SubjectTest { @Test public void identityOfNulls() { Object o = null; ASSERT.that(o).is(null); } @Test public void identityOfNullsFailure() { Object o = null; try { ASSERT.that(o).is("a"); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is "); } } @Test public void identityOfObject() { Object a = new Object(); Object b = a; ASSERT.that(a).is(b); } @Test public void identityOfObjectFailure() { Object a = new Object() { @Override public String toString() { return "Object 1"; } }; Object b = new Object() { @Override public String toString() { return "Object 2"; } }; try { ASSERT.that(a).is(b); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is "); } } // Ignore this until we fix identity @Ignore @Test public void identityOfObjectFailureWithComparableObjects() { Object a = "ab"; Object b = new StringBuilder().append("a").append('b').toString(); try { ASSERT.that(a).is(b); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is "); } } @Test public void isNull() { Object o = null; ASSERT.that(o).isNull(); } @Test public void isNullFail() { Object o = new Object(); try { ASSERT.that(o).isNull(); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that the subject is null"); } } @Test public void isNotNull() { Object o = new Object(); ASSERT.that(o).isNotNull(); } @Test public void isNotNullFail() { Object o = null; try { ASSERT.that(o).isNotNull(); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that the subject is not null"); } } @Test public void equalityOfNulls() { Object o = null; ASSERT.that(o).isEqualTo(null); } @Test public void equalityOfNullsFailure() { Object o = null; try { ASSERT.that(o).isEqualTo("a"); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is equal to "); } } @Test public void equalityOfObjectBasedOnIdentity() { Object a = new Object(); Object b = a; ASSERT.that(a).isEqualTo(b); } @Test public void equalityOfObjectFailure() { Object a = new Object() { @Override public String toString() { return "Object 1"; } }; Object b = new Object() { @Override public String toString() { return "Object 2"; } }; try { ASSERT.that(a).isEqualTo(b); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is equal to "); } } @Test public void equalityOfComparableObjects() { Object a = "ab"; Object b = new StringBuilder().append("a").append('b').toString(); ASSERT.that(a).isEqualTo(b); } @Test public void equalityOfComparableObjectsFailure() { Object a = "ab"; Object b = new StringBuilder().append("a").append('a').toString(); try { ASSERT.that(a).isEqualTo(b); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is equal to "); } } @Test public void inequalityOfNulls() { Object o = null; ASSERT.that(o).isNotEqualTo("a"); } @Test public void inequalityOfNullsFailure() { Object o = null; try { ASSERT.that(o).isNotEqualTo(null); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is not equal to "); } } @Test public void inequalityOfObjectBasedOnIdentity() { Object a = new Object(); Object b = new Object(); ASSERT.that(a).isNotEqualTo(b); } @Test public void inequalityOfObjectFailure() { Object a = new Object() { @Override public String toString() { return "Object 1"; } }; Object b = a; try { ASSERT.that(a).isNotEqualTo(b); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is not equal to "); } } @Test public void inequalityOfComparableObjects() { Object a = "ab"; Object b = new StringBuilder().append("a").append('a').toString(); ASSERT.that(a).isNotEqualTo(b); } @Test public void inequalityOfComparableObjectsFailure() { Object a = "ab"; Object b = new StringBuilder().append("a").append('b').toString(); try { ASSERT.that(a).isNotEqualTo(b); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is not equal to "); } } @Test public void isA() { ASSERT.that("a").isA(String.class); } @Test public void isAFail() { try { ASSERT.that("a").isA(Long.class); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is a "); } } @Test public void isNotA() { ASSERT.that("a").isNotA(Long.class); } @Test public void isNotAFail() { try { ASSERT.that("a").isNotA(String.class); fail("Should have thrown."); } catch (AssertionError e) { ASSERT.that(e.getMessage()).isEqualTo("Not true that is not a "); } } } truth-0.7.0/src/test/java/org/junit/contrib/truth/delegatetest/0000755000175000017500000000000012017424360024432 5ustar moellermoellertruth-0.7.0/src/test/java/org/junit/contrib/truth/delegatetest/DelegationTest.java0000644000175000017500000000204412017424360030210 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.delegatetest; import static org.junit.contrib.truth.Truth.ASSERT; import static org.junit.contrib.truth.delegatetest.FooSubject.FOO; import org.junit.Test; /** * A test that's more or less intended to show how one uses an extended verb. * */ public class DelegationTest { @Test public void customTypeCompares() { ASSERT.about(FOO).that(new Foo(5)).matches(new Foo(2 + 3)); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/delegatetest/Foo.java0000644000175000017500000000163712017424360026027 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.delegatetest; /** * A custom object for demonstration of the delegation aproach to * extending truth. * * @author Christian Gruber (cgruber@israfil.net) */ public class Foo { final int value; public Foo(int value) { this.value = value; } } truth-0.7.0/src/test/java/org/junit/contrib/truth/delegatetest/FooSubject.java0000644000175000017500000000300512017424360027336 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.delegatetest; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.subjects.Subject; import org.junit.contrib.truth.subjects.SubjectFactory; /** * A simple example Subject to demonstrate extension. * * @author Christian Gruber (christianedwardgruber@gmail.com) */ public class FooSubject extends Subject { public static final SubjectFactory FOO = new SubjectFactory() { @Override public FooSubject getSubject(FailureStrategy fs, Foo target) { return new FooSubject(fs, target); } }; public FooSubject(FailureStrategy failureStrategy, Foo subject) { super(failureStrategy, subject); } public And matches(Foo object) { if (getSubject().value != object.value) { fail("matches", getSubject(), object); } return nextChain(); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/extensiontest/0000755000175000017500000000000012017424360024674 5ustar moellermoellertruth-0.7.0/src/test/java/org/junit/contrib/truth/extensiontest/ExtendedVerb.java0000644000175000017500000000261712017424360030124 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.extensiontest; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.TestVerb; import org.junit.contrib.truth.Truth; /** * An extended verb to demonstrate (and test) the subclassing mechanism for * extension. Note this is not the preferred approach to extension, which is * {@link TestVerb#for() } * * @author David Saff * @author Christian Gruber (christianedwardgruber@gmail.com) * */ public class ExtendedVerb extends TestVerb { public static ExtendedVerb ASSERT = new ExtendedVerb( Truth.THROW_ASSERTION_ERROR); public ExtendedVerb(FailureStrategy failureStrategy) { super(failureStrategy); } public MySubject that(MyType subject) { return new MySubject(getFailureStrategy(), subject); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/extensiontest/ExtensionTest.java0000644000175000017500000000221712017424360030355 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.extensiontest; import static org.junit.contrib.truth.extensiontest.ExtendedVerb.ASSERT; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** * A test that's more or less intended to show how one uses an extended verb. * * @author David Saff * @author Christian Gruber (cgruber@israfil.net) */ @RunWith(JUnit4.class) public class ExtensionTest { @Test public void customTypeCompares() { ASSERT.that(new MyType(5)).matches(new MyType(2 + 3)); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/extensiontest/MySubject.java0000644000175000017500000000235312017424360027447 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.extensiontest; import org.junit.contrib.truth.FailureStrategy; import org.junit.contrib.truth.subjects.Subject; /** * A simple example Subject to demonstrate extension. * * @author Christian Gruber (christianedwardgruber@gmail.com) */ public class MySubject extends Subject { public MySubject(FailureStrategy failureStrategy, MyType subject) { super(failureStrategy, subject); } public And matches(MyType object) { if (getSubject().value != object.value) { fail("matches", getSubject(), object); } return nextChain(); } } truth-0.7.0/src/test/java/org/junit/contrib/truth/extensiontest/MyType.java0000644000175000017500000000157612017424360026777 0ustar moellermoeller/* * Copyright (c) 2011 David Saff * Copyright (c) 2011 Christian Gruber * * 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. */ package org.junit.contrib.truth.extensiontest; /** * Sample custom type to test extension verbs. * * @author Christian Gruber (cgruber@israfil.net) */ public class MyType { final int value; public MyType(int value) { this.value = value; } }