Skip to content

Commit 851214b

Browse files
authored
Merge pull request #76 from SimonBaeumer/better-diff-for-contains-assertion
Better diff for contains assertion
2 parents 34c15d3 + 93b0be8 commit 851214b

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
script:
3838
- curl -L https://github.com/SimonBaeumer/commander/releases/download/v0.3.0/commander-darwin-amd64 -o ~/bin/commander
3939
- chmod +x ~/bin/commander
40-
- make test-integration
40+
- make integration
4141

4242
- name: windows Unit
4343
os: windows
@@ -53,7 +53,7 @@ jobs:
5353
- choco install curl
5454
- curl -L https://github.com/SimonBaeumer/commander/releases/download/v0.3.0/commander-windows-amd64 -o C:\Windows\system32\commander.exe
5555
script:
56-
- make test-integration-windows
56+
- make integration-windows
5757

5858
- name: Unit tests
5959
before_script:
@@ -66,7 +66,7 @@ jobs:
6666
- ./test-reporter after-build -t gocov --exit-code $TRAVIS_TEST_RESULT
6767

6868
- name: Integration test
69-
script: make test-integration
69+
script: make integration
7070

7171
- stage: deploy
7272
name: "Deployment"

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Add `not-contains` assertion on `stdout` and `stderr`
44
- Add validation for invalid data types in `stdout` and `stderr` assertions
55
- More logging for `--verbose` option on the `test` command
6+
- Add better diff format for `contains` and `not-contains` assertions on `stdout` and `stderr`
67

78
# v1.0.1
89

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ exe = cmd/commander/*
22
cmd = commander
33
TRAVIS_TAG ?= "0.0.0"
44

5-
.PHONY: deps lint test test-integration git-hooks init
5+
.PHONY: deps lint test integration integration-windows git-hooks init
66

77
init: git-hooks
88

@@ -30,11 +30,11 @@ test-coverage:
3030
$(info INFO: Starting build $@)
3131
go test -coverprofile c.out ./...
3232

33-
test-integration: build
33+
integration: build
3434
$(info INFO: Starting build $@)
3535
commander test commander_unix.yaml
3636

37-
test-integration-windows: build
37+
integration-windows: build
3838
$(info INFO: Starting build $@)
3939
commander test commander_windows.yaml
4040

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ $ make test
185185
$ make test-coverage
186186
187187
# Integration tests
188-
$ make test-integration
188+
$ make integration
189189
190190
# Add depdencies to vendor
191191
$ make deps

pkg/matcher/matcher.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,20 @@ func (m ContainsMatcher) Match(got interface{}, expected interface{}) MatcherRes
8181
result = false
8282
}
8383

84-
diff := difflib.UnifiedDiff{
85-
A: difflib.SplitLines(fmt.Sprintf("%s", got.(string))),
86-
B: difflib.SplitLines(fmt.Sprintf("%s", expected.(string))),
87-
FromFile: "Got",
88-
ToFile: "Expected",
89-
Context: 3,
90-
}
91-
diffText, _ := difflib.GetUnifiedDiffString(diff)
84+
diff := `
85+
Expected
86+
87+
%s
88+
89+
to contain
90+
91+
%s
92+
`
93+
diff = fmt.Sprintf(diff, got, expected)
9294

9395
return MatcherResult{
9496
Success: result,
95-
Diff: diffText,
97+
Diff: diff,
9698
}
9799
}
98100

@@ -132,8 +134,19 @@ func (m NotContainsMatcher) Match(got interface{}, expected interface{}) Matcher
132134
result = false
133135
}
134136

137+
diff := `
138+
Expected
139+
140+
%s
141+
142+
to not contain
143+
144+
%s
145+
`
146+
diff = fmt.Sprintf(diff, got, expected)
147+
135148
return MatcherResult{
136149
Success: result,
137-
Diff: "",
150+
Diff: diff,
138151
}
139152
}

pkg/matcher/matcher_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ func TestNotContainsMatcher(t *testing.T) {
8181
m := NotContainsMatcher{}
8282
r := m.Match("lore ipsum donor", "hello")
8383
assert.True(t, r.Success)
84-
assert.Empty(t, r.Diff)
84+
assert.Equal(t, "\nExpected\n\nlore ipsum donor\n\nto not contain\n\nhello\n", r.Diff)
8585
}
8686

8787
func TestNotContainsMatcher_Fails(t *testing.T) {
8888
m := NotContainsMatcher{}
8989
r := m.Match("lore ipsum donor", "donor")
9090
assert.False(t, r.Success)
91-
assert.Empty(t, r.Diff)
91+
92+
diffText := "\nExpected\n\nlore ipsum donor\n\nto not contain\n\ndonor\n"
93+
assert.Equal(t, diffText, r.Diff)
9294
}

0 commit comments

Comments
 (0)