Skip to content

Commit

Permalink
Merge f10ffaf into 19ecfb1
Browse files Browse the repository at this point in the history
  • Loading branch information
maxatome committed Dec 7, 2018
2 parents 19ecfb1 + f10ffaf commit 7d32fe4
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 18 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ Extremely flexible golang deep comparison, extends the go testing package.

## Latest news

- 2018/12/07: [`Smuggle`](https://godoc.org/github.com/maxatome/go-testdeep#Smuggle)
operator and its friends
[`CmpSmuggle`](https://godoc.org/github.com/maxatome/go-testdeep#CmpSmuggle)
&
[`T.Smuggle`](https://godoc.org/github.com/maxatome/go-testdeep#T.Smuggle)
are now more lax and gain the ability to work on struct
fields-paths.
[`Shallow`](https://godoc.org/github.com/maxatome/go-testdeep#Shallow)
operator and its friends
[`CmpShallow`](https://godoc.org/github.com/maxatome/go-testdeep#CmpShallow)
&
[`T.Shallow`](https://godoc.org/github.com/maxatome/go-testdeep#T.Shallow)
can now work on strings;
- 2018/10/31: new
[`ContainsKey`](https://godoc.org/github.com/maxatome/go-testdeep#ContainsKey)
operator and its friends
Expand All @@ -30,15 +43,6 @@ Extremely flexible golang deep comparison, extends the go testing package.
[`T.ContainsKey`](https://godoc.org/github.com/maxatome/go-testdeep#T.ContainsKey);
reworked to handle arrays, slices and maps;
- 2018/08/18: works around golang issue in `go test -race` cases;
- 2018/07/15: new
[`NaN`](https://godoc.org/github.com/maxatome/go-testdeep#NaN) &
[`NotNaN`](https://godoc.org/github.com/maxatome/go-testdeep#NotNaN) &
operators (and their friends
[`CmpNaN`](https://godoc.org/github.com/maxatome/go-testdeep#CmpNaN),
[`CmpNotNaN`](https://godoc.org/github.com/maxatome/go-testdeep#CmpNotNaN),
[`T.NaN`](https://godoc.org/github.com/maxatome/go-testdeep#T.NaN)
&
[`T.NotNaN`](https://godoc.org/github.com/maxatome/go-testdeep#T.NotNaN));
- see [commits history](https://github.com/maxatome/go-testdeep/commits/master)
for other/older changes.

Expand Down Expand Up @@ -493,7 +497,8 @@ See functions returning [`TestDeep` interface](https://godoc.org/github.com/maxa
compares the contents of a slice or a pointer on a slice;
- [`Smuggle`](https://godoc.org/github.com/maxatome/go-testdeep#Smuggle)
changes data contents or mutates it into another type via a custom
function before stepping down in favor of generic comparison process;
function or a struct fields-path before stepping down in favor of
generic comparison process;
- [`String`](https://godoc.org/github.com/maxatome/go-testdeep#String)
checks a string, [`error`](https://golang.org/ref/spec#Errors) or
[`fmt.Stringer`](https://golang.org/pkg/fmt/#Stringer) interfaces
Expand Down
54 changes: 54 additions & 0 deletions cmp_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,60 @@ func ExampleCmpSmuggle_complex() {
// true
}

func ExampleCmpSmuggle_field_path() {
t := &testing.T{}

type Body struct {
Name string
Value interface{}
}
type Request struct {
Body *Body
}
type Transaction struct {
Request
}
type ValueNum struct {
Num int
}

got := &Transaction{
Request: Request{
Body: &Body{
Name: "test",
Value: &ValueNum{Num: 123},
},
},
}

// Want to check whether Num is between 100 and 200?
ok := CmpSmuggle(t, got, func(t *Transaction) (int, error) {
if t.Request.Body == nil ||
t.Request.Body.Value == nil {
return 0, errors.New("Request.Body or Request.Body.Value is nil")
}
if v, ok := t.Request.Body.Value.(*ValueNum); ok && v != nil {
return v.Num, nil
}
return 0, errors.New("Request.Body.Value isn't *ValueNum or nil")
}, Between(100, 200))
fmt.Println("check Num by hand:", ok)

// Same, but automagically generated...
ok = CmpSmuggle(t, got, "Request.Body.Value.Num", Between(100, 200))
fmt.Println("check Num using a fields-path:", ok)

// And as Request is an anonymous field, can be simplified further
// as it can be omitted
ok = CmpSmuggle(t, got, "Body.Value.Num", Between(100, 200))
fmt.Println("check Num using an other fields-path:", ok)

// Output:
// check Num by hand: true
// check Num using a fields-path: true
// check Num using an other fields-path: true
}

func ExampleCmpString() {
t := &testing.T{}

Expand Down
57 changes: 57 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,63 @@ func ExampleSmuggle_complex() {
// true
}

func ExampleSmuggle_field_path() {
t := &testing.T{}

type Body struct {
Name string
Value interface{}
}
type Request struct {
Body *Body
}
type Transaction struct {
Request
}
type ValueNum struct {
Num int
}

got := &Transaction{
Request: Request{
Body: &Body{
Name: "test",
Value: &ValueNum{Num: 123},
},
},
}

// Want to check whether Num is between 100 and 200?
ok := CmpDeeply(t, got,
Smuggle(
func(t *Transaction) (int, error) {
if t.Request.Body == nil ||
t.Request.Body.Value == nil {
return 0, errors.New("Request.Body or Request.Body.Value is nil")
}
if v, ok := t.Request.Body.Value.(*ValueNum); ok && v != nil {
return v.Num, nil
}
return 0, errors.New("Request.Body.Value isn't *ValueNum or nil")
},
Between(100, 200)))
fmt.Println("check Num by hand:", ok)

// Same, but automagically generated...
ok = CmpDeeply(t, got, Smuggle("Request.Body.Value.Num", Between(100, 200)))
fmt.Println("check Num using a fields-path:", ok)

// And as Request is an anonymous field, can be simplified further
// as it can be omitted
ok = CmpDeeply(t, got, Smuggle("Body.Value.Num", Between(100, 200)))
fmt.Println("check Num using an other fields-path:", ok)

// Output:
// check Num by hand: true
// check Num using a fields-path: true
// check Num using an other fields-path: true
}

func ExampleString() {
t := &testing.T{}

Expand Down
54 changes: 54 additions & 0 deletions t_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,60 @@ func ExampleT_Smuggle_complex() {
// true
}

func ExampleT_Smuggle_field_path() {
t := NewT(&testing.T{})

type Body struct {
Name string
Value interface{}
}
type Request struct {
Body *Body
}
type Transaction struct {
Request
}
type ValueNum struct {
Num int
}

got := &Transaction{
Request: Request{
Body: &Body{
Name: "test",
Value: &ValueNum{Num: 123},
},
},
}

// Want to check whether Num is between 100 and 200?
ok := t.Smuggle(got, func(t *Transaction) (int, error) {
if t.Request.Body == nil ||
t.Request.Body.Value == nil {
return 0, errors.New("Request.Body or Request.Body.Value is nil")
}
if v, ok := t.Request.Body.Value.(*ValueNum); ok && v != nil {
return v.Num, nil
}
return 0, errors.New("Request.Body.Value isn't *ValueNum or nil")
}, Between(100, 200))
fmt.Println("check Num by hand:", ok)

// Same, but automagically generated...
ok = t.Smuggle(got, "Request.Body.Value.Num", Between(100, 200))
fmt.Println("check Num using a fields-path:", ok)

// And as Request is an anonymous field, can be simplified further
// as it can be omitted
ok = t.Smuggle(got, "Body.Value.Num", Between(100, 200))
fmt.Println("check Num using an other fields-path:", ok)

// Output:
// check Num by hand: true
// check Num using a fields-path: true
// check Num using an other fields-path: true
}

func ExampleT_String() {
t := NewT(&testing.T{})

Expand Down
4 changes: 2 additions & 2 deletions td_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ var _ types.TestDeepStringer = tdCodeResult{}
func (r tdCodeResult) String() string {
if r.Reason == "" {
return fmt.Sprintf(" value: %s\nit failed but didn't say why",
util.ToString(r.Value))
util.IndentString(util.ToString(r.Value), " "))
}
return fmt.Sprintf(" value: %s\nit failed coz: %s",
util.ToString(r.Value), r.Reason)
util.IndentString(util.ToString(r.Value), " "), r.Reason)
}
Loading

0 comments on commit 7d32fe4

Please sign in to comment.