Skip to content

Commit e78778a

Browse files
committed
Add not-contains assertion
1 parent 4d1f361 commit e78778a

File tree

12 files changed

+96
-15
lines changed

12 files changed

+96
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v1.1.0
2+
3+
- Add `not-contains` assertion on `stdout` and `stderr`
4+
15
# v1.0.1
26

37
- Remove unnecessary command logs

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ tests:
8181
stderr:
8282
contains:
8383
- invalid # Assert only contain work
84+
not-contains:
85+
- not in there # Validate that a string does not occur in stdout
8486
exactly: "/bin/sh: 1: invalid: not found"
8587
line-count: 1 # Assert amount of lines
8688
lines: # Assert specific lines

docs/manual.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ tests:
4646
stderr:
4747
contains:
4848
- invalid # Assert only contain work
49+
not-contains:
50+
- does not contains # validate that a string does not occur in stdout
4951
exactly: "/bin/sh: 1: invalid: not found"
5052
line-count: 1 # Assert amount of lines
5153
lines: # Assert specific lines

integration/unix/commander_test.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ tests:
3737
1: line1
3838
2: line2
3939
4: line4
40-
exit-code: 0
40+
exit-code: 0
41+
42+
it should validate not contains:
43+
command: echo lorem ipsum
44+
stdout:
45+
not-contains:
46+
- not contains

integration/windows/commander_test.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ tests:
3232
1: line1
3333
2: line2
3434
4: line4
35-
exit-code: 0
35+
exit-code: 0
36+
37+
it should validate not contains:
38+
command: echo lorem ipsum
39+
stdout:
40+
not-contains:
41+
- not contains

pkg/matcher/matcher.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const (
1313
Contains = "contains"
1414
// Equal matcher type
1515
Equal = "equal"
16+
// Not contains matcher type
17+
NotContains = "notcontains"
1618
)
1719

1820
// NewMatcher creates a new matcher by type
@@ -24,6 +26,8 @@ func NewMatcher(matcher string) Matcher {
2426
return ContainsMatcher{}
2527
case Equal:
2628
return EqualMatcher{}
29+
case NotContains:
30+
return NotContainsMatcher{}
2731
default:
2832
panic(fmt.Sprintf("Validator '%s' does not exist!", matcher))
2933
}
@@ -118,3 +122,18 @@ func (m EqualMatcher) Match(got interface{}, expected interface{}) MatcherResult
118122
Diff: diffText,
119123
}
120124
}
125+
126+
type NotContainsMatcher struct {
127+
}
128+
129+
func (m NotContainsMatcher) Match(got interface{}, expected interface{}) MatcherResult {
130+
result := true
131+
if strings.Contains(got.(string), expected.(string)) {
132+
result = false
133+
}
134+
135+
return MatcherResult{
136+
Success: result,
137+
Diff: "",
138+
}
139+
}

pkg/matcher/matcher_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,22 @@ func TestContainsMatcher_MatchFail(t *testing.T) {
7171
got := m.Match("hello world", "nope")
7272
assert.False(t, got.Success)
7373
}
74+
75+
func TestNewMatcher_NotContains(t *testing.T) {
76+
m := NewMatcher(NotContains)
77+
assert.IsType(t, NotContainsMatcher{}, m)
78+
}
79+
80+
func TestNotContainsMatcher(t *testing.T) {
81+
m := NotContainsMatcher{}
82+
r := m.Match("lore ipsum donor", "hello")
83+
assert.True(t, r.Success)
84+
assert.Empty(t, r.Diff)
85+
}
86+
87+
func TestNotContainsMatcher_Fails(t *testing.T) {
88+
m := NotContainsMatcher{}
89+
r := m.Match("lore ipsum donor", "donor")
90+
assert.False(t, r.Success)
91+
assert.Empty(t, r.Diff)
92+
}

pkg/runtime/runtime.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ type Expected struct {
6565

6666
//ExpectedOut represents the assertions on stdout and stderr
6767
type ExpectedOut struct {
68-
Contains []string
69-
Lines map[int]string
70-
Exactly string
71-
LineCount int
68+
Contains []string
69+
Lines map[int]string
70+
Exactly string
71+
LineCount int
72+
NotContains []string
7273
}
7374

7475
// CommandUnderTest represents the command under test

pkg/runtime/validator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ func validateExpectedOut(got string, expected ExpectedOut) matcher.MatcherResult
9292
}
9393
}
9494

95+
if len(expected.NotContains) > 0 {
96+
m = matcher.NewMatcher(matcher.NotContains)
97+
for _, c := range expected.NotContains {
98+
if result = m.Match(got, c); !result.Success {
99+
return result
100+
}
101+
}
102+
}
103+
95104
return result
96105
}
97106

pkg/runtime/validator_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@ func getExampleTest() TestCase {
8181
test := TestCase{
8282
Expected: Expected{
8383
Stdout: ExpectedOut{
84-
Lines: map[int]string{1: "hello"},
85-
LineCount: 1,
86-
Exactly: "hello",
87-
Contains: []string{"hello"},
84+
Lines: map[int]string{1: "hello"},
85+
LineCount: 1,
86+
Exactly: "hello",
87+
Contains: []string{"hello"},
88+
NotContains: []string{"not-exist"},
8889
},
8990
Stderr: ExpectedOut{
90-
Lines: map[int]string{1: "error"},
91-
LineCount: 1,
92-
Exactly: "error",
93-
Contains: []string{"error"},
91+
Lines: map[int]string{1: "error"},
92+
LineCount: 1,
93+
Exactly: "error",
94+
Contains: []string{"error"},
95+
NotContains: []string{"not-exist"},
9496
},
9597
LineCount: 1,
9698
},

0 commit comments

Comments
 (0)