Skip to content

Commit e750946

Browse files
matchers: add table data section (#1526)
1 parent f140ae1 commit e750946

7 files changed

Lines changed: 171 additions & 10 deletions

File tree

webtau-core-groovy/src/test/groovy/org/testingisdocumenting/webtau/data/table/TableDataGroovyTest.groovy

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,6 @@ class TableDataGroovyTest {
9898
48 | null }
9999
}
100100

101-
// @Test
102-
// void "matching a single value inside"() {
103-
// def table = ["col A" | "col B" | "col C"] {
104-
// _____________________________
105-
// 10 | "hello" | "world"
106-
// 20 | "next" | "event" }
107-
//
108-
// table.should contain("hello")
109-
// }
110-
111101
@Test
112102
void "should generate multiple rows from multi-values"() {
113103
def tableData = createTableDataWithPermute()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.handlers
18+
19+
import org.junit.Test
20+
import org.testingisdocumenting.webtau.data.table.TableData
21+
22+
import static org.testingisdocumenting.webtau.Matchers.*
23+
import static org.testingisdocumenting.webtau.WebTauCore.*
24+
25+
class TableDataMatchersGroovyExamplesTest {
26+
@Test
27+
void equalityMismatch() {
28+
code {
29+
// table-equal-mismatch
30+
def summary = loadFromCsv("summary.csv")
31+
def expected = [ "ColumnA" | "ColumnB" ] {
32+
____________________________
33+
10 | greaterThan(15)
34+
anyOf(20, 22) | 40 }
35+
summary.should == expected
36+
// table-equal-mismatch
37+
} should throwException(AssertionError)
38+
}
39+
40+
@Test
41+
void containsMismatch() {
42+
code {
43+
// table-contain-mismatch
44+
def summary = loadFromCsv("summary.csv")
45+
summary.should contain(["ColumnA": 20, "ColumnB": greaterThan(15)])
46+
// table-contain-mismatch
47+
} should throwException(AssertionError)
48+
}
49+
50+
private static TableData loadFromCsv(String fileName) {
51+
return ["ColumnA" | "ColumnB" ] {
52+
_________________________
53+
10| 20
54+
30| 40 }
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.handlers;
18+
19+
import org.junit.Test;
20+
import org.testingisdocumenting.webtau.data.table.TableData;
21+
22+
import static org.testingisdocumenting.webtau.Matchers.*;
23+
import static org.testingisdocumenting.webtau.WebTauCore.*;
24+
import static org.testingisdocumenting.webtau.testutils.TestConsoleOutput.*;
25+
26+
public class TableDataMatchersJavaExamplesTest {
27+
@Test
28+
public void equalityMismatch() {
29+
runExpectExceptionCaptureAndValidateOutput(AssertionError.class, "table-equal-console-output", """
30+
X failed expecting [value] to equal ColumnA │ ColumnB \s
31+
10 │ <greater than 15>
32+
<any of [20, 22]> │ 40:
33+
[value][1].ColumnA: actual: 30 <java.lang.Integer>
34+
expected: 20 <java.lang.Integer>
35+
actual: 30 <java.lang.Integer>
36+
expected: 22 <java.lang.Integer> (Xms)
37+
\s
38+
ColumnA │ ColumnB
39+
10 │ 20
40+
**30** │ 40""", () -> {
41+
// table-equal-mismatch
42+
var summary = loadFromCsv("summary.csv");
43+
TableData expected = table("ColumnA", "ColumnB",
44+
_________________________,
45+
10, greaterThan(15),
46+
anyOf(20, 22), 40);
47+
48+
actual(summary).should(equal(expected));
49+
// table-equal-mismatch
50+
});
51+
}
52+
53+
@Test
54+
public void containsMismatch() {
55+
runExpectExceptionCaptureAndValidateOutput(AssertionError.class, "table-contain-console-output", """
56+
X failed expecting [value] to contain {"ColumnA": 20, "ColumnB": <greater than 15>}: no match found (Xms)
57+
\s
58+
ColumnA │ ColumnB
59+
10 │ 20
60+
30 │ 40""", () -> {
61+
// table-contain-mismatch
62+
var summary = loadFromCsv("summary.csv");
63+
actual(summary).should(contain(map("ColumnA", 20, "ColumnB", greaterThan(15))));
64+
// table-contain-mismatch
65+
});
66+
}
67+
68+
private static TableData loadFromCsv(String fileName) {
69+
return table("ColumnA", "ColumnB",
70+
___________________,
71+
10, 20,
72+
30, 40);
73+
}
74+
}

webtau-docs/znai/browser/tables.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Java:
2121
}
2222
```
2323

24+
More details in [Matchers](matchers/tables)
25+
2426
# Extracting Table Data
2527

2628
Use `:identifier: extractTableData` to extract `TableData` for further processing, e.g. saving to a disk:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Equality Comparison
2+
3+
When two tables are not equal, WebTau generates all the required info to investigate:
4+
5+
```tabs
6+
Groovy:
7+
:include-file: org/testingisdocumenting/webtau/expectation/equality/handlers/TableDataMatchersGroovyExamplesTest.groovy {
8+
surroundedBy: "table-equal-mismatch"
9+
}
10+
Java:
11+
:include-file: org/testingisdocumenting/webtau/expectation/equality/handlers/TableDataMatchersJavaExamplesTest.java {
12+
surroundedBy: "table-equal-mismatch"
13+
}
14+
```
15+
16+
:include-cli-output: doc-artifacts/table-equal-console-output.txt {
17+
title: "console output"
18+
}
19+
20+
# Contain
21+
22+
Use `contain` matcher to check if one map is a subset of another:
23+
24+
```tabs
25+
Groovy:
26+
:include-file: org/testingisdocumenting/webtau/expectation/equality/handlers/TableDataMatchersGroovyExamplesTest.groovy {
27+
surroundedBy: "table-contain-mismatch"
28+
}
29+
Java:
30+
:include-file: org/testingisdocumenting/webtau/expectation/equality/handlers/TableDataMatchersJavaExamplesTest.java {
31+
surroundedBy: "table-contain-mismatch"
32+
}
33+
```
34+
35+
:include-cli-output: doc-artifacts/table-contain-console-output.txt {
36+
title: "console output"
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Doc: [TableData Matcher](matchers/tables)

webtau-docs/znai/toc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ matchers
9797
java-beans-and-records
9898
strings
9999
maps
100+
tables
100101
any-of
101102
custom-compare-to-handler
102103
groovy-standalone-runner

0 commit comments

Comments
 (0)