Skip to content

Commit

Permalink
Merge pull request #47 from maxatome/tdhttp
Browse files Browse the repository at this point in the history
tdhttp helper + colored output
  • Loading branch information
maxatome authored Jan 14, 2019
2 parents ea9fe41 + 7abc133 commit 4ea8fb8
Show file tree
Hide file tree
Showing 22 changed files with 1,751 additions and 97 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ matrix:
script:
- export GORACE="halt_on_error=1"
- go get -t ./...
- go test -race -tags safe $GO_TEST_SAFE_FLAGS ./...
- go test -race $GO_TEST_UNSAFE_FLAGS ./...
- env TESTDEEP_COLOR=off go test -race -tags safe $GO_TEST_SAFE_FLAGS ./...
- env TESTDEEP_COLOR=off go test -race $GO_TEST_UNSAFE_FLAGS ./...
- if [ "$USE_GOMETALINTER" = 1 ]; then
gometalinter
--disable-all
Expand Down
79 changes: 72 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ go-testdeep
- [Installation](#installation)
- [Presentation](#presentation)
- [Available operators](#available-operators)
- [Helpers](#helpers)
- [`tdhttp` or HTTP API testing helper](#tdhttp-or-http-api-testing-helper)
- [Environment variables](#environment-variables)
- [Operators vs go types](#operators-vs-go-types)
- [See also](#see-also)
- [License](#license)
Expand All @@ -25,6 +28,10 @@ go-testdeep

## Latest news

- 2019/01/13: test failures output is now colored by default. See
[Environment variables](#environment-variables) to configure it;
- 2019/01/07: introducing TestDeep helpers. First one is
[`tdhttp` or HTTP API testing helper](#tdhttp-or-http-api-testing-helper);
- 2018/12/16: [`Between`], [`Gt`], [`Gte`], [`Lt`] & [`Lte`] operators
now handle strings as well;
- 2018/12/07: [`Smuggle`] operator and its friends
Expand All @@ -37,12 +44,6 @@ go-testdeep
&
[`T.Shallow`](https://godoc.org/github.com/maxatome/go-testdeep#T.Shallow)
can now work on strings;
- 2018/10/31: new [`ContainsKey`] operator and its
friends
[`CmpContainsKey`](https://godoc.org/github.com/maxatome/go-testdeep#CmpContainsKey)
&
[`T.ContainsKey`](https://godoc.org/github.com/maxatome/go-testdeep#T.ContainsKey);
reworked to handle arrays, slices and maps;
- see [commits history](https://github.com/maxatome/go-testdeep/commits/master)
for other/older changes.

Expand Down Expand Up @@ -248,7 +249,7 @@ using a package that uses this function behind the scene.
This function works very well, but it is not flexible. Both
compared structures must match exactly.

The purpose of testdeep package is to do its best to introduce this
The purpose of `testdeep` package is to do its best to introduce this
missing flexibility using *operators* when the expected value (or
one of its component) cannot be matched exactly.

Expand Down Expand Up @@ -484,6 +485,68 @@ See functions returning [`TestDeep` interface][`TestDeep`]:
- [`Zero`] checks data against its zero'ed conterpart.


## Helpers

The goal of helpers is to make use of `go-testdeep` even more powerful
by providing common features using [TestDeep operators](#available-operators)
behind the scene.

### `tdhttp` or HTTP API testing helper

The package `github.com/maxatome/go-testdeep/helpers/tdhttp` provides
some functions to easily test HTTP handlers.

See [`tdhttp`] documentation for details or
[FAQ](doc/FAQ.md#what-about-testing-the-response-using-my-api) for an
example of use.


## Environment variables

- `TESTDEEP_MAX_ERRORS` maximum number of errors to report before
stopping during one comparison (one [`CmpDeeply`] execution for
example). It defaults to 10;
- `TESTDEEP_COLOR` enable (`on`) or disable (`off`) the color
output. It defaults to `on`;
- `TESTDEEP_COLOR_TITLE` color of the test failure title. See below
for color format, it defaults to `cyan`;
- `TESTDEEP_COLOR_OK` color of the test expected value. See below
for color format, it defaults to `green`;
- `TESTDEEP_COLOR_BAD` color of the test got value. See below
for color format, it defaults to `red`;

### Color format

A color in `TESTDEEP_COLOR_*` environment variables has the following
format:

```
foreground_color # set foreground color, background one untouched
foreground_color:background_color # set foreground AND background color
:background_color # set background color, foreground one untouched
```

`foreground_color` and `background_color` can be:
- `black`
- `red`
- `green`
- `yellow`
- `blue`
- `magenta`
- `cyan`
- `white`
- `gray`

For example:

```
TESTDEEP_COLOR_OK=black:green \
TESTDEEP_COLOR_BAD=white:red \
TESTDEEP_COLOR_TITLE=yellow \
go test
```


## Operators vs go types

| Operator vs go type | nil | bool | string | {u,}int* | float* | complex* | array | slice | map | struct | pointer | interface¹ | chan | func | operator |
Expand Down Expand Up @@ -647,3 +710,5 @@ See [FAQ](doc/FAQ.md).
[`fmt.Stringer`]: https://golang.org/pkg/fmt/#Stringer
[`time.Time`]: https://golang.org/pkg/time/
[`math.NaN`]: https://golang.org/pkg/math/#NaN

[`tdhttp`]: https://godoc.org/github.com/maxatome/go-testdeep/helpers/tdhttp
29 changes: 15 additions & 14 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"

"github.com/maxatome/go-testdeep"
"github.com/maxatome/go-testdeep/helpers/tdutil"
"github.com/maxatome/go-testdeep/internal/ctxerr"
"github.com/maxatome/go-testdeep/internal/dark"
"github.com/maxatome/go-testdeep/internal/test"
Expand Down Expand Up @@ -89,7 +90,7 @@ func cmpErrorStr(t *testing.T, err *ctxerr.Error,
expected: %s
Full error:
> %s`,
test.BuildTestName(args),
tdutil.BuildTestName(args...),
fieldName, indent(got, 10), indent(expected.Exact, 10),
strings.Replace(err.Error(), "\n\t", "\n\t> ", -1))
return false
Expand All @@ -101,7 +102,7 @@ func cmpErrorStr(t *testing.T, err *ctxerr.Error,
should contain: %s
Full error:
> %s`,
test.BuildTestName(args),
tdutil.BuildTestName(args...),
fieldName,
indent(got, 16), indent(expected.Contain, 16),
strings.Replace(err.Error(), "\n\t", "\n\t> ", -1))
Expand All @@ -114,7 +115,7 @@ func cmpErrorStr(t *testing.T, err *ctxerr.Error,
should match: %s
Full error:
> %s`,
test.BuildTestName(args),
tdutil.BuildTestName(args...),
fieldName,
indent(got, 14), indent(expected.Match.String(), 14),
strings.Replace(err.Error(), "\n\t", "\n\t> ", -1))
Expand Down Expand Up @@ -160,7 +161,7 @@ func matchError(t *testing.T, err *ctxerr.Error, expectedError expectedError,
t.Errorf(`%sLocation of the origin of the error
got: %v
expected: %v`,
test.BuildTestName(args), err.Location.IsInitialized(), expectedError.Located)
tdutil.BuildTestName(args...), err.Location.IsInitialized(), expectedError.Located)
return false
}

Expand All @@ -169,14 +170,14 @@ func matchError(t *testing.T, err *ctxerr.Error, expectedError expectedError,
t.Errorf(`%sFile of the origin of the error
got: line %d of %s
expected: *_test.go`,
test.BuildTestName(args), err.Location.Line, err.Location.File)
tdutil.BuildTestName(args...), err.Location.Line, err.Location.File)
return false
}

if expectedError.Origin != nil {
if err.Origin == nil {
t.Errorf(`%sError should originate from another Error`,
test.BuildTestName(args))
tdutil.BuildTestName(args...))
return false
}

Expand All @@ -185,7 +186,7 @@ func matchError(t *testing.T, err *ctxerr.Error, expectedError expectedError,
}
if err.Origin != nil {
t.Errorf(`%sError should NOT originate from another Error`,
test.BuildTestName(args))
tdutil.BuildTestName(args...))
return false
}

Expand All @@ -198,7 +199,7 @@ func _checkError(t *testing.T, got, expected interface{},

err := testdeep.EqDeeplyError(got, expected)
if err == nil {
t.Errorf("%sAn Error should have occurred", test.BuildTestName(args))
t.Errorf("%sAn Error should have occurred", tdutil.BuildTestName(args...))
return false
}

Expand All @@ -210,7 +211,7 @@ func _checkError(t *testing.T, got, expected interface{},
if testdeep.EqDeeply(got, expected) {
t.Errorf(`%sBoolean context failed
got: true
expected: false`, test.BuildTestName(args))
expected: false`, tdutil.BuildTestName(args...))
return false
}

Expand Down Expand Up @@ -265,7 +266,7 @@ func checkErrorForEach(t *testing.T,
expectedError expectedError, args ...interface{}) (ret bool) {
t.Helper()

globalTestName := test.BuildTestName(args)
globalTestName := tdutil.BuildTestName(args...)
ret = true

for idx, got := range gotList {
Expand All @@ -289,13 +290,13 @@ func _checkOK(t *testing.T, got, expected interface{},
if !testdeep.EqDeeply(got, expected) {
t.Errorf(`%sBoolean context failed
got: false
expected: true`, test.BuildTestName(args))
expected: true`, tdutil.BuildTestName(args...))
return false
}

if err := testdeep.EqDeeplyError(got, expected); err != nil {
t.Errorf(`%sEqDeeplyError returned an error: %s`,
test.BuildTestName(args), err)
tdutil.BuildTestName(args...), err)
return false
}

Expand Down Expand Up @@ -350,7 +351,7 @@ func checkOKForEach(t *testing.T, gotList []interface{}, expected interface{},
args ...interface{}) (ret bool) {
t.Helper()

globalTestName := test.BuildTestName(args)
globalTestName := tdutil.BuildTestName(args...)
ret = true

for idx, got := range gotList {
Expand Down Expand Up @@ -390,6 +391,6 @@ func equalTypes(t *testing.T, got testdeep.TestDeep, expected interface{},
t.Errorf(`%sFailed test
got: %s
expected: %s`,
test.BuildTestName(args), gotStr, expectedStr)
tdutil.BuildTestName(args...), gotStr, expectedStr)
return false
}
20 changes: 4 additions & 16 deletions cmp_deeply.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ package testdeep

import (
"bytes"
"fmt"
"reflect"
"strings"
"testing" // used by t.Helper() workaround below

"github.com/maxatome/go-testdeep/helpers/tdutil"
"github.com/maxatome/go-testdeep/internal/ctxerr"
)

Expand All @@ -28,22 +27,11 @@ func formatError(t TestingT, isFatal bool, err *ctxerr.Error, args ...interface{
const failedTest = "Failed test"

var buf *bytes.Buffer
switch len(args) {
case 0:
if len(args) == 0 {
buf = bytes.NewBufferString(failedTest + "\n")
case 1:
buf = bytes.NewBufferString(failedTest + " '")
fmt.Fprint(buf, args[0]) // nolint: errcheck
buf.WriteString("'\n")
default:
} else {
buf = bytes.NewBufferString(failedTest + " '")
if str, ok := args[0].(string); ok && strings.ContainsRune(str, '%') {
fmt.Fprintf(buf, str, args[1:]...) // nolint: errcheck
} else {
// create a new slice to fool govet and avoid "call has possible
// formatting directive" errors
fmt.Fprint(buf, args[:]...) // nolint: errcheck
}
tdutil.FbuildTestName(buf, args...)
buf.WriteString("'\n")
}

Expand Down
Loading

0 comments on commit 4ea8fb8

Please sign in to comment.