Repository: SimonBaeumer/commander
Pull request #76 (2019-04-11 12:17:57)

Better diff for contains assertion
Fixes #

## Checklist

 - [x] Added unit / integration tests for windows, macOS and Linux? 
 - [x] Added a changelog entry in [CHANGELOG.md](../CHANGELOG.md)?
 - [x] Updated the documentation ([README.md](../README.md), [docs](../docs))?
 - [x] Does your change work on `Linux`, `Windows` and `macOS`?


Patch:
diff --git a/.travis.yml b/.travis.yml
index 4370574..9941a4c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -37,7 +37,7 @@ jobs:
     script:
       - curl -L https://github.com/SimonBaeumer/commander/releases/download/v0.3.0/commander-darwin-amd64 -o ~/bin/commander
       - chmod +x ~/bin/commander
-      - make test-integration
+      - make integration
 
   - name: windows Unit
     os: windows
@@ -53,7 +53,7 @@ jobs:
       - choco install curl
       - curl -L https://github.com/SimonBaeumer/commander/releases/download/v0.3.0/commander-windows-amd64 -o C:\Windows\system32\commander.exe
     script:
-      - make test-integration-windows
+      - make integration-windows
 
   - name: Unit tests
     before_script:
@@ -66,7 +66,7 @@ jobs:
       - ./test-reporter after-build -t gocov --exit-code $TRAVIS_TEST_RESULT
 
   - name: Integration test
-    script: make test-integration
+    script: make integration
 
   - stage: deploy
     name: "Deployment"
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2839218..39b0113 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
  - Add `not-contains` assertion on `stdout` and `stderr`
  - Add validation for invalid data types in `stdout` and `stderr` assertions
  - More logging for `--verbose` option on the `test` command
+ - Add better diff format for `contains` and `not-contains` assertions on `stdout` and `stderr`
 
 # v1.0.1
 
diff --git a/Makefile b/Makefile
index e738075..5ae81ee 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ exe = cmd/commander/*
 cmd = commander
 TRAVIS_TAG ?= "0.0.0"
 
-.PHONY: deps lint test test-integration git-hooks init
+.PHONY: deps lint test integration integration-windows git-hooks init
 
 init: git-hooks
 
@@ -30,11 +30,11 @@ test-coverage:
 	$(info INFO: Starting build $@)
 	go test -coverprofile c.out ./...
 
-test-integration: build
+integration: build
 	$(info INFO: Starting build $@)
 	commander test commander_unix.yaml
 
-test-integration-windows: build
+integration-windows: build
 	$(info INFO: Starting build $@)
 	commander test commander_windows.yaml
 
diff --git a/README.md b/README.md
index 7195b9f..9ab9c11 100644
--- a/README.md
+++ b/README.md
@@ -185,7 +185,7 @@ $ make test
 $ make test-coverage
 
 # Integration tests
-$ make test-integration
+$ make integration
 
 # Add depdencies to vendor
 $ make deps
diff --git a/pkg/matcher/matcher.go b/pkg/matcher/matcher.go
index ab9c1f6..6a5f3a6 100644
--- a/pkg/matcher/matcher.go
+++ b/pkg/matcher/matcher.go
@@ -81,18 +81,20 @@ func (m ContainsMatcher) Match(got interface{}, expected interface{}) MatcherRes
 		result = false
 	}
 
-	diff := difflib.UnifiedDiff{
-		A:        difflib.SplitLines(fmt.Sprintf("%s", got.(string))),
-		B:        difflib.SplitLines(fmt.Sprintf("%s", expected.(string))),
-		FromFile: "Got",
-		ToFile:   "Expected",
-		Context:  3,
-	}
-	diffText, _ := difflib.GetUnifiedDiffString(diff)
+	diff := `
+Expected
+
+%s
+
+to contain
+
+%s
+`
+	diff = fmt.Sprintf(diff, got, expected)
 
 	return MatcherResult{
 		Success: result,
-		Diff:    diffText,
+		Diff:    diff,
 	}
 }
 
@@ -132,8 +134,19 @@ func (m NotContainsMatcher) Match(got interface{}, expected interface{}) Matcher
 		result = false
 	}
 
+	diff := `
+Expected
+
+%s
+
+to not contain
+
+%s
+`
+	diff = fmt.Sprintf(diff, got, expected)
+
 	return MatcherResult{
 		Success: result,
-		Diff:    "",
+		Diff:    diff,
 	}
 }


Test patch:
diff --git a/pkg/matcher/matcher_test.go b/pkg/matcher/matcher_test.go
index 7c46c14..f4d0e5c 100644
--- a/pkg/matcher/matcher_test.go
+++ b/pkg/matcher/matcher_test.go
@@ -81,12 +81,14 @@ func TestNotContainsMatcher(t *testing.T) {
 	m := NotContainsMatcher{}
 	r := m.Match("lore ipsum donor", "hello")
 	assert.True(t, r.Success)
-	assert.Empty(t, r.Diff)
+	assert.Equal(t, "\nExpected\n\nlore ipsum donor\n\nto not contain\n\nhello\n", r.Diff)
 }
 
 func TestNotContainsMatcher_Fails(t *testing.T) {
 	m := NotContainsMatcher{}
 	r := m.Match("lore ipsum donor", "donor")
 	assert.False(t, r.Success)
-	assert.Empty(t, r.Diff)
+
+	diffText := "\nExpected\n\nlore ipsum donor\n\nto not contain\n\ndonor\n"
+	assert.Equal(t, diffText, r.Diff)
 }