Skip to content

Commit

Permalink
New Cmp() function (& *T method), shorter version of CmpDeeply()
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Apr 27, 2019
1 parent 7f5f3e0 commit 7357343
Show file tree
Hide file tree
Showing 28 changed files with 713 additions and 633 deletions.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ go-testdeep

## Latest news

- 2019/04/27: new [`Cmp`] function and
[`T.Cmp`](https://godoc.org/github.com/maxatome/go-testdeep#T.Cmp)
method, shorter versions of
[`CmpDeeply`](https://godoc.org/github.com/maxatome/go-testdeep#CmpDeeply)
and [`T.CmpDeeply`](https://godoc.org/github.com/maxatome/go-testdeep#T.CmpDeeply);
- 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
Expand Down Expand Up @@ -77,7 +82,7 @@ func TestCreateRecord(t *testing.T) {

if td.CmpNoError(t, err) {
// If you know the exact value of all fields of newly created record
td.CmpDeeply(t, record,
td.Cmp(t, record,
&Record{
Id: 245,
Name: "Bob",
Expand All @@ -88,7 +93,7 @@ func TestCreateRecord(t *testing.T) {

// But as often you cannot guess the values of DB generated fields,
// you can choose to ignore them and only test the non-zero ones
td.CmpDeeply(t, record,
td.Cmp(t, record,
td.Struct(
&Record{
Name: "Bob",
Expand All @@ -98,7 +103,7 @@ func TestCreateRecord(t *testing.T) {
"Name & Age fields of newly created record")

// Anyway, it is better to be able to test all fields!
td.CmpDeeply(t, record,
td.Cmp(t, record,
td.Struct(
&Record{
Name: "Bob",
Expand All @@ -118,7 +123,7 @@ Imagine `CreateRecord` does not set correctly `CreatedAt` field, then:
go test -run=TestCreateRecord
```

outputs for last `td.CmpDeeply` call:
outputs for last `td.Cmp` call:

![error output](doc/colored-newly1.svg)

Expand Down Expand Up @@ -164,7 +169,7 @@ func TestCreateRecord(tt *testing.T) {
t := t.RootName("RECORD") // Use RECORD instead of DATA in failure reports

// If you know the exact value of all fields of newly created record
t.CmpDeeply(record,
t.Cmp(record,
&Record{
Id: 245,
Name: "Bob",
Expand All @@ -186,8 +191,8 @@ func TestCreateRecord(tt *testing.T) {
},
"Newly created record")

// Or using CmpDeeply method, it's a matter of taste
t.CmpDeeply(record,
// Or using Cmp method, it's a matter of taste
t.Cmp(record,
td.Struct(
Record{
Name: "Bob",
Expand Down Expand Up @@ -269,7 +274,7 @@ func TestCreateRecord(t *testing.T) {
}
```

With `testdeep`, it is a way simple, thanks to [`CmpDeeply`] function:
With `testdeep`, it is a way simple, thanks to [`Cmp`] function:

```go
import (
Expand All @@ -283,8 +288,8 @@ func TestCreateRecord(t *testing.T) {
before := time.Now()
record, err := CreateRecord("Bob", 23)

if td.CmpDeeply(t, err, nil) {
td.CmpDeeply(t, record,
if td.Cmp(t, err, nil) {
td.Cmp(t, record,
td.Struct(
Record{
Name: "Bob",
Expand All @@ -303,7 +308,7 @@ Of course not only structs can be compared. A lot of
[operators](#available-operators) can be found below to cover most
(all?) needed tests.

The [`CmpDeeply`] function is the keystone of this package,
The [`Cmp`] function is the keystone of this package,
but to make the writing of tests even easier, the family of `Cmp*`
functions are provided and act as shortcuts. Using
[`CmpNoError`](https://godoc.org/github.com/maxatome/go-testdeep#CmpNoError)
Expand Down Expand Up @@ -480,7 +485,7 @@ example of use.
## Environment variables

- `TESTDEEP_MAX_ERRORS` maximum number of errors to report before
stopping during one comparison (one [`CmpDeeply`] execution for
stopping during one comparison (one [`Cmp`] execution for
example). It defaults to 10;
- `TESTDEEP_COLOR` enable (`on`) or disable (`off`) the color
output. It defaults to `on`;
Expand Down Expand Up @@ -682,7 +687,7 @@ See [FAQ](doc/FAQ.md).

[`T`]: https://godoc.org/github.com/maxatome/go-testdeep#T
[`TestDeep`]: https://godoc.org/github.com/maxatome/go-testdeep#TestDeep
[`CmpDeeply`]: https://godoc.org/github.com/maxatome/go-testdeep#CmpDeeply
[`Cmp`]: https://godoc.org/github.com/maxatome/go-testdeep#Cmp

[`error`]: https://golang.org/ref/spec#Errors
[`fmt.Stringer`]: https://golang.org/pkg/fmt/#Stringer
Expand Down
18 changes: 16 additions & 2 deletions cmp_deeply.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func cmpDeeply(ctx ctxerr.Context, t TestingT, got, expected interface{},
return false
}

// CmpDeeply returns true if "got" matches "expected". "expected" can
// Cmp returns true if "got" matches "expected". "expected" can
// be the same type as "got" is, or contains some TestDeep
// operators. If "got" does not match "expected", it returns false and
// the reason of failure is logged with the help of "t" Error()
Expand All @@ -76,9 +76,23 @@ func cmpDeeply(ctx ctxerr.Context, t TestingT, got, expected interface{},
// logged as is in case of failure. If len(args) > 1 and the first
// item of args is a string and contains a '%' rune then fmt.Fprintf
// is used to compose the name, else args are passed to fmt.Fprint.
func CmpDeeply(t TestingT, got, expected interface{},
func Cmp(t TestingT, got, expected interface{},
args ...interface{}) bool {
// Work around https://github.com/golang/go/issues/26995 issue
// when corrected, this block should be replaced by t.Helper()
if tt, ok := t.(*testing.T); ok {
tt.Helper()
} else {
t.Helper()
}

return cmpDeeply(newContext(), t, got, expected, args...)
}

// CmpDeeply works the same as Cmp and is still available for
// compatibility purpose. Use shorter Cmp in new code.
func CmpDeeply(t TestingT, got, expected interface{},
args ...interface{}) bool {
// Work around https://github.com/golang/go/issues/26995 issue
// when corrected, this block should be replaced by t.Helper()
if tt, ok := t.(*testing.T); ok {
Expand Down
Loading

0 comments on commit 7357343

Please sign in to comment.