Skip to content

Commit f2d7ec9

Browse files
authored
Merge pull request #34 from maxatome/smuggle_fields-path
Smuggle fields-path
2 parents 19ecfb1 + f10ffaf commit f2d7ec9

File tree

7 files changed

+443
-18
lines changed

7 files changed

+443
-18
lines changed

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ Extremely flexible golang deep comparison, extends the go testing package.
2222

2323
## Latest news
2424

25+
- 2018/12/07: [`Smuggle`](https://godoc.org/github.com/maxatome/go-testdeep#Smuggle)
26+
operator and its friends
27+
[`CmpSmuggle`](https://godoc.org/github.com/maxatome/go-testdeep#CmpSmuggle)
28+
&
29+
[`T.Smuggle`](https://godoc.org/github.com/maxatome/go-testdeep#T.Smuggle)
30+
are now more lax and gain the ability to work on struct
31+
fields-paths.
32+
[`Shallow`](https://godoc.org/github.com/maxatome/go-testdeep#Shallow)
33+
operator and its friends
34+
[`CmpShallow`](https://godoc.org/github.com/maxatome/go-testdeep#CmpShallow)
35+
&
36+
[`T.Shallow`](https://godoc.org/github.com/maxatome/go-testdeep#T.Shallow)
37+
can now work on strings;
2538
- 2018/10/31: new
2639
[`ContainsKey`](https://godoc.org/github.com/maxatome/go-testdeep#ContainsKey)
2740
operator and its friends
@@ -30,15 +43,6 @@ Extremely flexible golang deep comparison, extends the go testing package.
3043
[`T.ContainsKey`](https://godoc.org/github.com/maxatome/go-testdeep#T.ContainsKey);
3144
reworked to handle arrays, slices and maps;
3245
- 2018/08/18: works around golang issue in `go test -race` cases;
33-
- 2018/07/15: new
34-
[`NaN`](https://godoc.org/github.com/maxatome/go-testdeep#NaN) &
35-
[`NotNaN`](https://godoc.org/github.com/maxatome/go-testdeep#NotNaN) &
36-
operators (and their friends
37-
[`CmpNaN`](https://godoc.org/github.com/maxatome/go-testdeep#CmpNaN),
38-
[`CmpNotNaN`](https://godoc.org/github.com/maxatome/go-testdeep#CmpNotNaN),
39-
[`T.NaN`](https://godoc.org/github.com/maxatome/go-testdeep#T.NaN)
40-
&
41-
[`T.NotNaN`](https://godoc.org/github.com/maxatome/go-testdeep#T.NotNaN));
4246
- see [commits history](https://github.com/maxatome/go-testdeep/commits/master)
4347
for other/older changes.
4448

@@ -493,7 +497,8 @@ See functions returning [`TestDeep` interface](https://godoc.org/github.com/maxa
493497
compares the contents of a slice or a pointer on a slice;
494498
- [`Smuggle`](https://godoc.org/github.com/maxatome/go-testdeep#Smuggle)
495499
changes data contents or mutates it into another type via a custom
496-
function before stepping down in favor of generic comparison process;
500+
function or a struct fields-path before stepping down in favor of
501+
generic comparison process;
497502
- [`String`](https://godoc.org/github.com/maxatome/go-testdeep#String)
498503
checks a string, [`error`](https://golang.org/ref/spec#Errors) or
499504
[`fmt.Stringer`](https://golang.org/pkg/fmt/#Stringer) interfaces

cmp_funcs_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,60 @@ func ExampleCmpSmuggle_complex() {
17721772
// true
17731773
}
17741774

1775+
func ExampleCmpSmuggle_field_path() {
1776+
t := &testing.T{}
1777+
1778+
type Body struct {
1779+
Name string
1780+
Value interface{}
1781+
}
1782+
type Request struct {
1783+
Body *Body
1784+
}
1785+
type Transaction struct {
1786+
Request
1787+
}
1788+
type ValueNum struct {
1789+
Num int
1790+
}
1791+
1792+
got := &Transaction{
1793+
Request: Request{
1794+
Body: &Body{
1795+
Name: "test",
1796+
Value: &ValueNum{Num: 123},
1797+
},
1798+
},
1799+
}
1800+
1801+
// Want to check whether Num is between 100 and 200?
1802+
ok := CmpSmuggle(t, got, func(t *Transaction) (int, error) {
1803+
if t.Request.Body == nil ||
1804+
t.Request.Body.Value == nil {
1805+
return 0, errors.New("Request.Body or Request.Body.Value is nil")
1806+
}
1807+
if v, ok := t.Request.Body.Value.(*ValueNum); ok && v != nil {
1808+
return v.Num, nil
1809+
}
1810+
return 0, errors.New("Request.Body.Value isn't *ValueNum or nil")
1811+
}, Between(100, 200))
1812+
fmt.Println("check Num by hand:", ok)
1813+
1814+
// Same, but automagically generated...
1815+
ok = CmpSmuggle(t, got, "Request.Body.Value.Num", Between(100, 200))
1816+
fmt.Println("check Num using a fields-path:", ok)
1817+
1818+
// And as Request is an anonymous field, can be simplified further
1819+
// as it can be omitted
1820+
ok = CmpSmuggle(t, got, "Body.Value.Num", Between(100, 200))
1821+
fmt.Println("check Num using an other fields-path:", ok)
1822+
1823+
// Output:
1824+
// check Num by hand: true
1825+
// check Num using a fields-path: true
1826+
// check Num using an other fields-path: true
1827+
}
1828+
17751829
func ExampleCmpString() {
17761830
t := &testing.T{}
17771831

example_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,63 @@ func ExampleSmuggle_complex() {
18641864
// true
18651865
}
18661866

1867+
func ExampleSmuggle_field_path() {
1868+
t := &testing.T{}
1869+
1870+
type Body struct {
1871+
Name string
1872+
Value interface{}
1873+
}
1874+
type Request struct {
1875+
Body *Body
1876+
}
1877+
type Transaction struct {
1878+
Request
1879+
}
1880+
type ValueNum struct {
1881+
Num int
1882+
}
1883+
1884+
got := &Transaction{
1885+
Request: Request{
1886+
Body: &Body{
1887+
Name: "test",
1888+
Value: &ValueNum{Num: 123},
1889+
},
1890+
},
1891+
}
1892+
1893+
// Want to check whether Num is between 100 and 200?
1894+
ok := CmpDeeply(t, got,
1895+
Smuggle(
1896+
func(t *Transaction) (int, error) {
1897+
if t.Request.Body == nil ||
1898+
t.Request.Body.Value == nil {
1899+
return 0, errors.New("Request.Body or Request.Body.Value is nil")
1900+
}
1901+
if v, ok := t.Request.Body.Value.(*ValueNum); ok && v != nil {
1902+
return v.Num, nil
1903+
}
1904+
return 0, errors.New("Request.Body.Value isn't *ValueNum or nil")
1905+
},
1906+
Between(100, 200)))
1907+
fmt.Println("check Num by hand:", ok)
1908+
1909+
// Same, but automagically generated...
1910+
ok = CmpDeeply(t, got, Smuggle("Request.Body.Value.Num", Between(100, 200)))
1911+
fmt.Println("check Num using a fields-path:", ok)
1912+
1913+
// And as Request is an anonymous field, can be simplified further
1914+
// as it can be omitted
1915+
ok = CmpDeeply(t, got, Smuggle("Body.Value.Num", Between(100, 200)))
1916+
fmt.Println("check Num using an other fields-path:", ok)
1917+
1918+
// Output:
1919+
// check Num by hand: true
1920+
// check Num using a fields-path: true
1921+
// check Num using an other fields-path: true
1922+
}
1923+
18671924
func ExampleString() {
18681925
t := &testing.T{}
18691926

t_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,60 @@ func ExampleT_Smuggle_complex() {
17721772
// true
17731773
}
17741774

1775+
func ExampleT_Smuggle_field_path() {
1776+
t := NewT(&testing.T{})
1777+
1778+
type Body struct {
1779+
Name string
1780+
Value interface{}
1781+
}
1782+
type Request struct {
1783+
Body *Body
1784+
}
1785+
type Transaction struct {
1786+
Request
1787+
}
1788+
type ValueNum struct {
1789+
Num int
1790+
}
1791+
1792+
got := &Transaction{
1793+
Request: Request{
1794+
Body: &Body{
1795+
Name: "test",
1796+
Value: &ValueNum{Num: 123},
1797+
},
1798+
},
1799+
}
1800+
1801+
// Want to check whether Num is between 100 and 200?
1802+
ok := t.Smuggle(got, func(t *Transaction) (int, error) {
1803+
if t.Request.Body == nil ||
1804+
t.Request.Body.Value == nil {
1805+
return 0, errors.New("Request.Body or Request.Body.Value is nil")
1806+
}
1807+
if v, ok := t.Request.Body.Value.(*ValueNum); ok && v != nil {
1808+
return v.Num, nil
1809+
}
1810+
return 0, errors.New("Request.Body.Value isn't *ValueNum or nil")
1811+
}, Between(100, 200))
1812+
fmt.Println("check Num by hand:", ok)
1813+
1814+
// Same, but automagically generated...
1815+
ok = t.Smuggle(got, "Request.Body.Value.Num", Between(100, 200))
1816+
fmt.Println("check Num using a fields-path:", ok)
1817+
1818+
// And as Request is an anonymous field, can be simplified further
1819+
// as it can be omitted
1820+
ok = t.Smuggle(got, "Body.Value.Num", Between(100, 200))
1821+
fmt.Println("check Num using an other fields-path:", ok)
1822+
1823+
// Output:
1824+
// check Num by hand: true
1825+
// check Num using a fields-path: true
1826+
// check Num using an other fields-path: true
1827+
}
1828+
17751829
func ExampleT_String() {
17761830
t := NewT(&testing.T{})
17771831

td_code.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ var _ types.TestDeepStringer = tdCodeResult{}
156156
func (r tdCodeResult) String() string {
157157
if r.Reason == "" {
158158
return fmt.Sprintf(" value: %s\nit failed but didn't say why",
159-
util.ToString(r.Value))
159+
util.IndentString(util.ToString(r.Value), " "))
160160
}
161161
return fmt.Sprintf(" value: %s\nit failed coz: %s",
162-
util.ToString(r.Value), r.Reason)
162+
util.IndentString(util.ToString(r.Value), " "), r.Reason)
163163
}

0 commit comments

Comments
 (0)