From 04c3c57ca6b0f7e2985425d07faa9a72bc1817fa Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 Mar 2016 11:43:46 +0100 Subject: Move isA to ExtraMatchers.isInstance --- .../backends/python/PythonCodeGeneratorTests.java | 37 ++------------------ .../zwobble/couscous/tests/util/ExtraMatchers.java | 39 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 src/test/java/org/zwobble/couscous/tests/util/ExtraMatchers.java (limited to 'src') diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCodeGeneratorTests.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCodeGeneratorTests.java index bdbf47e..7557f92 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCodeGeneratorTests.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCodeGeneratorTests.java @@ -15,6 +15,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.zwobble.couscous.ast.ArrayNode.array; import static org.zwobble.couscous.ast.LiteralNode.literal; +import static org.zwobble.couscous.tests.util.ExtraMatchers.isInstance; import static org.zwobble.couscous.util.ExtraLists.list; public class PythonCodeGeneratorTests { @@ -28,48 +29,16 @@ public class PythonCodeGeneratorTests { } private Matcher isPythonLiteral(String value) { - return isA(PythonStringLiteralNode.class, hasFeature( + return isInstance(PythonStringLiteralNode.class, hasFeature( "value", node -> node.getValue(), equalTo(value))); } - private static Matcher isA(Class type, Matcher downcastMatcher) { - return new DiagnosingMatcher() { - @Override - protected boolean matches(Object item, Description mismatch) { - if (null == item) { - mismatch.appendText("null"); - return false; - } - - if (!type.isInstance(item)) { - mismatch.appendValue(item).appendText(" is a " + item.getClass().getName()); - return false; - } - - if (!downcastMatcher.matches(item)) { - downcastMatcher.describeMismatch(item, mismatch); - return false; - } - - return true; - } - - @Override - public void describeTo(Description description) { - description - .appendText(type.getSimpleName()) - .appendText(" with "); - downcastMatcher.describeTo(description); - } - }; - } - private static Matcher isPythonList( List> expectedElements) { - return isA(PythonListNode.class, hasFeature( + return isInstance(PythonListNode.class, hasFeature( "elements", list -> list.getElements(), contains(expectedElements))); diff --git a/src/test/java/org/zwobble/couscous/tests/util/ExtraMatchers.java b/src/test/java/org/zwobble/couscous/tests/util/ExtraMatchers.java new file mode 100644 index 0000000..3107d53 --- /dev/null +++ b/src/test/java/org/zwobble/couscous/tests/util/ExtraMatchers.java @@ -0,0 +1,39 @@ +package org.zwobble.couscous.tests.util; + +import org.hamcrest.Description; +import org.hamcrest.DiagnosingMatcher; +import org.hamcrest.Matcher; + +public class ExtraMatchers { + public static Matcher isInstance(Class type, Matcher downcastMatcher) { + return new DiagnosingMatcher() { + @Override + protected boolean matches(Object item, Description mismatch) { + if (null == item) { + mismatch.appendText("null"); + return false; + } + + if (!type.isInstance(item)) { + mismatch.appendValue(item).appendText(" is a " + item.getClass().getName()); + return false; + } + + if (!downcastMatcher.matches(item)) { + downcastMatcher.describeMismatch(item, mismatch); + return false; + } + + return true; + } + + @Override + public void describeTo(Description description) { + description + .appendText(type.getSimpleName()) + .appendText(" with "); + downcastMatcher.describeTo(description); + } + }; + } +} -- cgit v1.2.3