From 4e9a583030ac48a98a37a6f37966a71b74f1912d Mon Sep 17 00:00:00 2001 From: MykolaGolubyev Date: Fri, 4 May 2018 07:15:01 -0400 Subject: [PATCH] expose ContainAnalyzer as contain matcher --- .../main/java/com/twosigma/webtau/Ddjt.java | 8 ++- .../expectation/contain/ContainMatcher.java | 62 +++++++++++++++++++ .../expectation/equality/EqualMatcher.java | 2 +- .../contain/ContainMatcherTest.groovy | 38 ++++++++++++ .../java/com/twosigma/webtau/WebTauDsl.java | 6 +- 5 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 webtau-core/src/main/java/com/twosigma/webtau/expectation/contain/ContainMatcher.java create mode 100644 webtau-core/src/test/groovy/com/twosigma/webtau/expectation/contain/ContainMatcherTest.groovy diff --git a/webtau-core/src/main/java/com/twosigma/webtau/Ddjt.java b/webtau-core/src/main/java/com/twosigma/webtau/Ddjt.java index 126a5c919..541f69f85 100644 --- a/webtau-core/src/main/java/com/twosigma/webtau/Ddjt.java +++ b/webtau-core/src/main/java/com/twosigma/webtau/Ddjt.java @@ -3,6 +3,7 @@ import com.twosigma.webtau.data.table.TableData; import com.twosigma.webtau.expectation.*; import com.twosigma.webtau.expectation.code.ThrowExceptionMatcher; +import com.twosigma.webtau.expectation.contain.ContainMatcher; import com.twosigma.webtau.expectation.equality.EqualMatcher; import com.twosigma.webtau.expectation.ranges.GreaterThanMatcher; @@ -15,9 +16,6 @@ * Convenient class for a single static * imports */ public class Ddjt { - private Ddjt() { - } - public static TableData header(String... columnNames) { return new TableData(Arrays.stream(columnNames)); } @@ -34,6 +32,10 @@ public static EqualMatcher equal(Object expected) { return new EqualMatcher(expected); } + public static ContainMatcher contain(Object expected) { + return new ContainMatcher(expected); + } + public static GreaterThanMatcher beGreaterThan(Comparable base) { return new GreaterThanMatcher(base); } diff --git a/webtau-core/src/main/java/com/twosigma/webtau/expectation/contain/ContainMatcher.java b/webtau-core/src/main/java/com/twosigma/webtau/expectation/contain/ContainMatcher.java new file mode 100644 index 000000000..5bad73ec9 --- /dev/null +++ b/webtau-core/src/main/java/com/twosigma/webtau/expectation/contain/ContainMatcher.java @@ -0,0 +1,62 @@ +package com.twosigma.webtau.expectation.contain; + +import com.twosigma.webtau.data.render.DataRenderers; +import com.twosigma.webtau.expectation.ActualPath; +import com.twosigma.webtau.expectation.ValueMatcher; + +public class ContainMatcher implements ValueMatcher { + private ContainAnalyzer containAnalyzer; + private final Object expected; + + public ContainMatcher(Object expected) { + this.expected = expected; + } + + @Override + public String matchingMessage() { + return "co contain " + DataRenderers.render(expected); + } + + @Override + public String matchedMessage(ActualPath actualPath, Object actual) { + return "contains " + DataRenderers.render(expected); + } + + @Override + public String mismatchedMessage(ActualPath actualPath, Object actual) { + return containAnalyzer.generateMismatchReport(); + } + + @Override + public boolean matches(ActualPath actualPath, Object actual) { + containAnalyzer = ContainAnalyzer.containAnalyzer(); + return analyzeContain(actualPath, actual); + } + + @Override + public String negativeMatchingMessage() { + return "to not contain " + DataRenderers.render(expected); + } + + @Override + public String negativeMatchedMessage(ActualPath actualPath, Object actual) { + return "does not contain " + DataRenderers.render(expected); + } + + @Override + public String negativeMismatchedMessage(ActualPath actualPath, Object actual) { + return actualPath + " contains " + DataRenderers.render(expected) + "\nactual: " + + DataRenderers.render(actual); + } + + @Override + public boolean negativeMatches(ActualPath actualPath, Object actual) { + containAnalyzer = ContainAnalyzer.negativeContainAnalyzer(); + return analyzeContain(actualPath, actual); + } + + private boolean analyzeContain(ActualPath actualPath, Object actual) { + containAnalyzer.contains(actualPath, actual, expected); + return containAnalyzer.contains(); + } +} diff --git a/webtau-core/src/main/java/com/twosigma/webtau/expectation/equality/EqualMatcher.java b/webtau-core/src/main/java/com/twosigma/webtau/expectation/equality/EqualMatcher.java index 2d09e266a..9f113887a 100644 --- a/webtau-core/src/main/java/com/twosigma/webtau/expectation/equality/EqualMatcher.java +++ b/webtau-core/src/main/java/com/twosigma/webtau/expectation/equality/EqualMatcher.java @@ -6,7 +6,7 @@ public class EqualMatcher implements ValueMatcher { private EqualComparator equalComparator; - private Object expected; + private final Object expected; public EqualMatcher(Object expected) { this.expected = expected; diff --git a/webtau-core/src/test/groovy/com/twosigma/webtau/expectation/contain/ContainMatcherTest.groovy b/webtau-core/src/test/groovy/com/twosigma/webtau/expectation/contain/ContainMatcherTest.groovy new file mode 100644 index 000000000..3a24094a6 --- /dev/null +++ b/webtau-core/src/test/groovy/com/twosigma/webtau/expectation/contain/ContainMatcherTest.groovy @@ -0,0 +1,38 @@ +package com.twosigma.webtau.expectation.contain + +import org.junit.Test + +import static com.twosigma.webtau.Ddjt.actual +import static com.twosigma.webtau.Ddjt.code +import static com.twosigma.webtau.Ddjt.contain +import static com.twosigma.webtau.Ddjt.throwException + +class ContainMatcherTest { + @Test + void "should throw exception when value doesn't contain expected value"() { + code { + actual("hello world").should(contain("world!")) + } should throwException("\ndoes not contain:\n" + + "\n" + + "[value]: actual: hello world\n" + + " expected to contain: world!") + } + + @Test + void "should throw exception when value contain expected value, but should not"() { + code { + actual("hello world").shouldNot(contain("world")) + } should throwException("[value] contains world\n" + + "actual: hello world") + } + + @Test + void "should pass when value contains expected value"() { + actual("hello world").should(contain("world")) + } + + @Test + void "should pass when value doesn't contains expected value and should not"() { + actual("hello world").shouldNot(contain("world!")) + } +} diff --git a/webtau/src/main/java/com/twosigma/webtau/WebTauDsl.java b/webtau/src/main/java/com/twosigma/webtau/WebTauDsl.java index c0c8026c0..2d08913c8 100644 --- a/webtau/src/main/java/com/twosigma/webtau/WebTauDsl.java +++ b/webtau/src/main/java/com/twosigma/webtau/WebTauDsl.java @@ -24,7 +24,7 @@ import static com.twosigma.webtau.reporter.TokenizedMessage.tokenizedMessage; import static com.twosigma.webtau.reporter.IntegrationTestsMessageBuilder.*; -public class WebTauDsl { +public class WebTauDsl extends Ddjt { public static final WebTauConfig cfg = WebTauConfig.INSTANCE; public static final CurrentWebDriver driver = new CurrentWebDriver(); @@ -79,10 +79,6 @@ public static ValueMatcher beVisible() { return new VisibleValueMatcher(); } - public static ValueMatcher beGreaterThan(Comparable base) { - return new GreaterThanMatcher(base); - } - public static ValueMatcher getBeVisible() { return beVisible(); }