From 72231250ed81e10d66bfe70701e64fa5fe50f712 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Wed, 15 Jun 2016 23:09:44 +0200 Subject: Publish --- .../test/java/com/yahoo/test/MatchersTestCase.java | 33 ++++++++++ .../test/java/com/yahoo/test/OrderTesterTest.java | 76 ++++++++++++++++++++++ .../com/yahoo/test/PatternMatchersTestCase.java | 40 ++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 testutil/src/test/java/com/yahoo/test/MatchersTestCase.java create mode 100644 testutil/src/test/java/com/yahoo/test/OrderTesterTest.java create mode 100644 testutil/src/test/java/com/yahoo/test/PatternMatchersTestCase.java (limited to 'testutil/src/test') diff --git a/testutil/src/test/java/com/yahoo/test/MatchersTestCase.java b/testutil/src/test/java/com/yahoo/test/MatchersTestCase.java new file mode 100644 index 00000000000..c560a0e0624 --- /dev/null +++ b/testutil/src/test/java/com/yahoo/test/MatchersTestCase.java @@ -0,0 +1,33 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.test; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; + +import org.hamcrest.Matcher; +import org.junit.Test; + +/** + * Tests for com.yahoo.test.Matchers. + * + * @author Steinar Knutsen + */ +public class MatchersTestCase { + + @Test + public final void testHasItemWithMethodObjectString() { + @SuppressWarnings("rawtypes") + final Matcher m = Matchers.hasItemWithMethod("nalle", + "toLowerCase"); + assertEquals( + false, + m.matches(Arrays.asList(new Object[] { Integer.valueOf(1), + Character.valueOf('c'), "blbl" }))); + assertEquals( + true, + m.matches(Arrays.asList(new Object[] { Character.valueOf('c'), + "NALLE" }))); + } + +} diff --git a/testutil/src/test/java/com/yahoo/test/OrderTesterTest.java b/testutil/src/test/java/com/yahoo/test/OrderTesterTest.java new file mode 100644 index 00000000000..a9e4212ffc7 --- /dev/null +++ b/testutil/src/test/java/com/yahoo/test/OrderTesterTest.java @@ -0,0 +1,76 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.test; + +import org.junit.Test; + +/** + * @author Vegard Sjonfjell + */ +public class OrderTesterTest { + private class PartialInt implements Comparable { + int value; + + public PartialInt(int value) { + this.value = value; + } + + @Override + public int compareTo(PartialInt other) { + if (Math.abs(value - other.value) < 3) { + return 0; + } + else if (value > other.value) { + return 1; + } + else if (value < other.value) { + return 1; + } + + return 0; + } + + @Override + public String toString() { + return Integer.toString(value); + } + } + + @Test + public void testTotalOrderTester() { + new TotalOrderTester() + .theseObjects(3, 3) + .areLessThan(4) + .areLessThan(5) + .areLessThan(6) + .testOrdering(); + } + + @Test + public void testPartialOrderTester() { + new PartialOrderTester() + .theseObjects(new PartialInt(3)) + .areLessThan(new PartialInt(3), new PartialInt(3)) + .areLessThan(new PartialInt(4)) + .areLessThan(new PartialInt(4)) + .areLessThan(new PartialInt(5)) + .testOrdering(); + } + + @Test (expected = AssertionError.class) + public void testTotalOrderTesterFailsOnIncorrectOrdering() { + new TotalOrderTester() + .theseObjects(3) + .areLessThan(2) + .areLessThan(5) + .testOrdering(); + } + + @Test (expected = AssertionError.class) + public void testPartialOrderTesterFailsOnIncorrectOrdering() { + new PartialOrderTester() + .theseObjects(new PartialInt(6)) + .areLessThan(new PartialInt(2)) + .areLessThan(new PartialInt(3)) + .testOrdering(); + } +} diff --git a/testutil/src/test/java/com/yahoo/test/PatternMatchersTestCase.java b/testutil/src/test/java/com/yahoo/test/PatternMatchersTestCase.java new file mode 100644 index 00000000000..6c9eac53fb1 --- /dev/null +++ b/testutil/src/test/java/com/yahoo/test/PatternMatchersTestCase.java @@ -0,0 +1,40 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.test; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Test; + +/** + * Check CollectionPatternMatcher, LinePatternMatcher and PatternMatcher. + * + * @author Steinar Knutsen + */ +public class PatternMatchersTestCase { + + @Test + public final void testCollections() { + CollectionPatternMatcher cm = new CollectionPatternMatcher("a.*"); + String[] coll = new String[] {}; + assertEquals(false, cm.matches(Arrays.asList(coll))); + coll = new String[] { "ba", "ab" }; + assertEquals(true, cm.matches(Arrays.asList(coll))); + } + + @Test + public final void testLines() { + LinePatternMatcher lp = new LinePatternMatcher("a"); + assertEquals(true, lp.matches("a\nab")); + assertEquals(false, lp.matches("ab\nb")); + } + + @Test + public final void testPatterns() { + PatternMatcher m = new PatternMatcher(".*a.*"); + assertEquals(true, m.matches("ab")); + assertEquals(false, m.matches("b")); + } + +} -- cgit v1.2.3