Skip to content

Commit d3a0a81

Browse files
committed
Allow multiple needles in Contains and DoesNotContain
Signed-off-by: apostasie <[email protected]>
1 parent ffaec96 commit d3a0a81

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

mod/tigron/expect/comparators.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,28 @@ func All(comparators ...test.Comparator) test.Comparator {
4141

4242
// Contains can be used as a parameter for expected.Output and ensures a comparison string is found contained in the
4343
// output.
44-
func Contains(compare string) test.Comparator {
44+
func Contains(compare string, more ...string) test.Comparator {
4545
return func(stdout, _ string, t *testing.T) {
4646
t.Helper()
47+
4748
assertive.Contains(assertive.WithFailLater(t), stdout, compare, "Inspecting output (contains)")
49+
50+
for _, m := range more {
51+
assertive.Contains(assertive.WithFailLater(t), stdout, m, "Inspecting output (contains)")
52+
}
4853
}
4954
}
5055

5156
// DoesNotContain is to be used for expected.Output to ensure a comparison string is NOT found in the output.
52-
func DoesNotContain(compare string) test.Comparator {
57+
func DoesNotContain(compare string, more ...string) test.Comparator {
5358
return func(stdout, _ string, t *testing.T) {
5459
t.Helper()
60+
5561
assertive.DoesNotContain(assertive.WithFailLater(t), stdout, compare, "Inspecting output (does not contain)")
62+
63+
for _, m := range more {
64+
assertive.DoesNotContain(assertive.WithFailLater(t), stdout, m, "Inspecting output (does not contain)")
65+
}
5666
}
5767
}
5868

mod/tigron/expect/comparators_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
)
3131

3232
func TestExpect(t *testing.T) {
33+
// TODO: write more tests once we can mock t in Comparator signature
3334
t.Parallel()
3435

3536
expect.Contains("b")("a b c", "contains works", t)
@@ -39,7 +40,9 @@ func TestExpect(t *testing.T) {
3940

4041
expect.All(
4142
expect.Contains("b"),
43+
expect.Contains("b", "c"),
4244
expect.DoesNotContain("d"),
45+
expect.DoesNotContain("d", "e"),
4346
expect.Equals("a b c"),
4447
expect.Match(regexp.MustCompile("[a-z ]+")),
4548
)("a b c", "all", t)

mod/tigron/expect/doc.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ The last parameter of `test.Expects` accepts a `test.Comparator`, which allows t
5353
output on `stdout`.
5454

5555
The following ready-made `test.Comparator` generators are provided:
56-
- `expect.Contains(string)`: verifies that stdout contains the string parameter
57-
- `expect.DoesNotContain(string)`: negation of above
56+
- `expect.Contains(string, ...string)`: verifies that stdout does contain the provided parameters
57+
- `expect.DoesNotContain(string, ...string)`: verifies that stdout does not contain any of the passed parameters
5858
- `expect.Equals(string)`: strict equality
5959
- `expect.Match(*regexp.Regexp)`: regexp matching
6060
- `expect.All(comparators ...Comparator)`: allows to bundle together a bunch of other comparators

0 commit comments

Comments
 (0)