Skip to content

Commit

Permalink
expose ContainAnalyzer as contain matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
MykolaGolubyev committed May 4, 2018
1 parent c1d0924 commit 4e9a583
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 9 deletions.
8 changes: 5 additions & 3 deletions webtau-core/src/main/java/com/twosigma/webtau/Ddjt.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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!"))
}
}
6 changes: 1 addition & 5 deletions webtau/src/main/java/com/twosigma/webtau/WebTauDsl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 4e9a583

Please sign in to comment.