Skip to content

Commit a72e811

Browse files
matchers: any value matcher (#1528)
1 parent 2d09437 commit a72e811

8 files changed

Lines changed: 187 additions & 0 deletions

File tree

webtau-core-groovy/src/test/groovy/org/testingisdocumenting/webtau/MatchersGroovyTest.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ class MatchersGroovyTest {
107107
}
108108
}
109109

110+
@Test
111+
void "any value table matcher example"() {
112+
// any-value-table-example
113+
TableData summaryTable = loadFromCsv("summary.csv")
114+
summaryTable.should == ["ColumnA" | "ColumnB" ] {
115+
_________________________
116+
10| anyValue
117+
30| 40 }
118+
// any-value-table-example
119+
}
120+
110121
@Test
111122
void "java records and table data equality"() {
112123
def wishListItem1 = new WishLitItem("id1", "tea set", true, [])
@@ -204,6 +215,13 @@ class MatchersGroovyTest {
204215
return 10.5
205216
}
206217

218+
private static TableData loadFromCsv(String fileName) {
219+
return ["ColumnA" | "ColumnB" ] {
220+
_________________________
221+
10| 20
222+
30| 40 }
223+
}
224+
207225
private int countRecords() {
208226
numberOfRecords += 1
209227
return numberOfRecords
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.expectation.equality
18+
19+
import org.junit.Test
20+
21+
import static org.testingisdocumenting.webtau.Matchers.*
22+
23+
class AnyValueMatcherTest {
24+
@Test
25+
void "positive"() {
26+
def value = 100
27+
value.shouldBe anyValue
28+
}
29+
30+
@Test
31+
void "negative"() {
32+
def value = 100
33+
34+
code {
35+
value.shouldNotBe anyValue
36+
} should throwException("anyValue matches every value")
37+
}
38+
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/Matchers.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
* Convenient place to discover all the available matchers
3636
*/
3737
public class Matchers {
38+
/**
39+
* any value matcher. use it in places like table or complex structures where you want to ignore a value
40+
*/
41+
public static final ValueMatcher anyValue = new AnyValueMatcher();
42+
3843
/**
3944
* visible matcher to check if UI element is visible
4045
* @see #hidden
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2023 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.expectation.equality;
18+
19+
import org.testingisdocumenting.webtau.data.ValuePath;
20+
import org.testingisdocumenting.webtau.data.render.PrettyPrintable;
21+
import org.testingisdocumenting.webtau.data.render.PrettyPrinter;
22+
import org.testingisdocumenting.webtau.expectation.ValueMatcher;
23+
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
24+
25+
import static org.testingisdocumenting.webtau.WebTauCore.*;
26+
27+
public class AnyValueMatcher implements ValueMatcher, PrettyPrintable {
28+
@Override
29+
public TokenizedMessage matchingTokenizedMessage(ValuePath actualPath, Object actual) {
30+
return tokenizedMessage().matcher("to match any value");
31+
}
32+
33+
@Override
34+
public TokenizedMessage matchedTokenizedMessage(ValuePath actualPath, Object actual) {
35+
return tokenizedMessage().matcher("matches every value");
36+
}
37+
38+
@Override
39+
public TokenizedMessage mismatchedTokenizedMessage(ValuePath actualPath, Object actual) {
40+
throw new IllegalStateException();
41+
}
42+
43+
@Override
44+
public boolean matches(ValuePath actualPath, Object actual) {
45+
return true;
46+
}
47+
48+
@Override
49+
public TokenizedMessage negativeMatchingTokenizedMessage(ValuePath actualPath, Object actual) {
50+
return tokenizedMessage().matcher("to mismatch any value");
51+
}
52+
53+
@Override
54+
public TokenizedMessage negativeMatchedTokenizedMessage(ValuePath actualPath, Object actual) {
55+
throw new IllegalStateException();
56+
}
57+
58+
@Override
59+
public TokenizedMessage negativeMismatchedTokenizedMessage(ValuePath actualPath, Object actual) {
60+
return tokenizedMessage().id("anyValue").matcher("matches every value");
61+
}
62+
63+
@Override
64+
public boolean negativeMatches(ValuePath actualPath, Object actual) {
65+
return false;
66+
}
67+
68+
@Override
69+
public void prettyPrint(PrettyPrinter printer) {
70+
printer.printDelimiter("<");
71+
printer.print(PrettyPrinter.CLASSIFIER_COLOR, "any value");
72+
printer.printDelimiter(">");
73+
}
74+
75+
@Override
76+
public String toString() {
77+
return PrettyPrinter.renderAsTextWithoutColors(this);
78+
}
79+
}

webtau-core/src/test/java/org/testingisdocumenting/webtau/MatchersTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,29 @@ public void anyOfMatcherWithOtherMatcherExample() {
291291
actual(message).shouldNotBe(anyOf("hello", contain("super")));
292292
}
293293

294+
@Test
295+
public void anyValueTableMatcherExample() {
296+
runCaptureAndValidateOutput("any-value-table-output", """
297+
. [value] equals ColumnA │ ColumnB \s
298+
10 │ <any value>
299+
30 │ 40 (Xms)""", () -> {
300+
// any-value-table-example
301+
TableData summaryTable = loadFromCsv("summary.csv");
302+
actual(summaryTable).should(equal(table("ColumnA", "ColumnB",
303+
____________________,
304+
10, anyValue,
305+
30, 40)));
306+
// any-value-table-example
307+
});
308+
}
309+
310+
private static TableData loadFromCsv(String fileName) {
311+
return table("ColumnA", "ColumnB",
312+
____________________,
313+
10, 20,
314+
30, 40);
315+
}
316+
294317
@Test
295318
public void listFailureExample() {
296319
doc.console.capture("list-failure", () -> {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Match Every Value
2+
3+
Use `anyValue` matcher to match every expected value. Use it in places of complex data expectations where you need to
4+
provide a value but your focus is on different data.
5+
6+
```tabs
7+
Groovy:
8+
:include-file: org/testingisdocumenting/webtau/MatchersGroovyTest.groovy {
9+
surroundedBy: "any-value-table-example"
10+
}
11+
12+
Java:
13+
:include-file: org/testingisdocumenting/webtau/MatchersTest.java {
14+
surroundedBy: "any-value-table-example"
15+
}
16+
```
17+
18+
:include-cli-output: doc-artifacts/any-value-table-output.txt {
19+
title: "console output"
20+
}
21+
22+
:include-markdown: static-import.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: [any value matcher](matchers/any-value)

webtau-docs/znai/toc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ matchers
9999
maps
100100
tables
101101
any-of
102+
any-value
102103
custom-compare-to-handler
103104
groovy-standalone-runner
104105
introduction

0 commit comments

Comments
 (0)