Skip to content

Commit

Permalink
Merge f2c064a into a9cff18
Browse files Browse the repository at this point in the history
  • Loading branch information
maxatome committed Jun 24, 2018
2 parents a9cff18 + f2c064a commit 60475df
Show file tree
Hide file tree
Showing 11 changed files with 753 additions and 137 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ Extremely flexible golang deep comparison, extends the go testing package.

## Latest news

- 2018/06/24: [`Contains`](https://godoc.org/github.com/maxatome/go-testdeep#Contains)
(and its friends
[`CmpContains`](https://godoc.org/github.com/maxatome/go-testdeep#CmpContains)
&
[`T.Contains`](https://godoc.org/github.com/maxatome/go-testdeep#T.Contains))
reworked to handle arrays, slices and maps;
- 2018/06/19: new
[ContextConfig](https://godoc.org/github.com/maxatome/go-testdeep#ContextConfig)
feature `FailureIsFatal` available. See
Expand All @@ -35,12 +41,6 @@ Extremely flexible golang deep comparison, extends the go testing package.
&
[`T.CmpNotPanic`](https://godoc.org/github.com/maxatome/go-testdeep#T.CmpNotPanic)
methods
- 2018/06/15: new
[`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));
- see [commits history](https://github.com/maxatome/go-testdeep/commits/master)
for other/older changes.

Expand Down
6 changes: 3 additions & 3 deletions cmp_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ func CmpCode(t TestingT, got interface{}, fn interface{}, args ...interface{}) b

// CmpContains is a shortcut for:
//
// CmpDeeply(t, got, Contains(expected), args...)
// CmpDeeply(t, got, Contains(expectedValue), args...)
//
// Returns true if the test is OK, false if it fails.
//
// "args..." are optional and allow to name the test. This name is
// 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 CmpContains(t TestingT, got interface{}, expected string, args ...interface{}) bool {
func CmpContains(t TestingT, got interface{}, expectedValue interface{}, args ...interface{}) bool {
t.Helper()
return CmpDeeply(t, got, Contains(expected), args...)
return CmpDeeply(t, got, Contains(expectedValue), args...)
}

// CmpEmpty is a shortcut for:
Expand Down
115 changes: 108 additions & 7 deletions cmp_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,89 @@ func ExampleCmpCode() {
// true
}

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

ok := CmpContains(t, [...]int{11, 22, 33, 44}, 22)
fmt.Println("array contains 22:", ok)

ok = CmpContains(t, [...]int{11, 22, 33, 44}, Between(20, 25))
fmt.Println("array contains at least one item in [20 .. 25]:", ok)

ok = CmpContains(t, []int{11, 22, 33, 44}, 22)
fmt.Println("slice contains 22:", ok)

ok = CmpContains(t, []int{11, 22, 33, 44}, Between(20, 25))
fmt.Println("slice contains at least one item in [20 .. 25]:", ok)

// Output:
// array contains 22: true
// array contains at least one item in [20 .. 25]: true
// slice contains 22: true
// slice contains at least one item in [20 .. 25]: true
}

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

num := 123
got := [...]*int{&num, nil}

ok := CmpContains(t, got, nil)
fmt.Println("array contains untyped nil:", ok)

ok = CmpContains(t, got, (*int)(nil))
fmt.Println("array contains *int nil:", ok)

ok = CmpContains(t, got, Nil())
fmt.Println("array contains Nil():", ok)

ok = CmpContains(t, got, (*byte)(nil))
fmt.Println("array contains *byte nil:", ok) // types differ: *byte ≠ *int

// Output:
// array contains untyped nil: true
// array contains *int nil: true
// array contains Nil(): true
// array contains *byte nil: false
}

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

ok := CmpContains(t, map[string]int{"foo": 11, "bar": 22, "zip": 33}, 22)
fmt.Println("map contains value 22:", ok)

ok = CmpContains(t, map[string]int{"foo": 11, "bar": 22, "zip": 33}, Between(20, 25))
fmt.Println("map contains at least one value in [20 .. 25]:", ok)

// Output:
// map contains value 22: true
// map contains at least one value in [20 .. 25]: true
}

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

got := "foobar"

ok := CmpContains(t, got, "oob", "checks %s", got)
fmt.Println(ok)
fmt.Println("contains `oob` string:", ok)

ok = CmpContains(t, got, 'b', "checks %s", got)
fmt.Println("contains 'b' rune:", ok)

ok = CmpContains(t, got, byte('a'), "checks %s", got)
fmt.Println("contains 'a' byte:", ok)

ok = CmpContains(t, got, Between('n', 'p'), "checks %s", got)
fmt.Println("contains at least one character ['n' .. 'p']:", ok)

// Output:
// true
// contains `oob` string: true
// contains 'b' rune: true
// contains 'a' byte: true
// contains at least one character ['n' .. 'p']: true
}

func ExampleCmpContains_stringer() {
Expand All @@ -318,10 +391,24 @@ func ExampleCmpContains_stringer() {
got := bytes.NewBufferString("foobar")

ok := CmpContains(t, got, "oob", "checks %s", got)
fmt.Println(ok)
fmt.Println("contains `oob` string:", ok)

ok = CmpContains(t, got, 'b', "checks %s", got)
fmt.Println("contains 'b' rune:", ok)

ok = CmpContains(t, got, byte('a'), "checks %s", got)
fmt.Println("contains 'a' byte:", ok)

// Be careful! TestDeep operators in Contains() do not work with
// fmt.Stringer nor error interfaces
ok = CmpContains(t, got, Between('n', 'p'), "checks %s", got)
fmt.Println("try TestDeep operator:", ok)

// Output:
// true
// contains `oob` string: true
// contains 'b' rune: true
// contains 'a' byte: true
// try TestDeep operator: false
}

func ExampleCmpContains_error() {
Expand All @@ -330,10 +417,24 @@ func ExampleCmpContains_error() {
got := errors.New("foobar")

ok := CmpContains(t, got, "oob", "checks %s", got)
fmt.Println(ok)
fmt.Println("contains `oob` string:", ok)

ok = CmpContains(t, got, 'b', "checks %s", got)
fmt.Println("contains 'b' rune:", ok)

ok = CmpContains(t, got, byte('a'), "checks %s", got)
fmt.Println("contains 'a' byte:", ok)

// Be careful! TestDeep operators in Contains() do not work with
// fmt.Stringer nor error interfaces
ok = CmpContains(t, got, Between('n', 'p'), "checks %s", got)
fmt.Println("try TestDeep operator:", ok)

// Output:
// true
// contains `oob` string: true
// contains 'b' rune: true
// contains 'a' byte: true
// try TestDeep operator: false
}

func ExampleCmpEmpty() {
Expand Down
178 changes: 141 additions & 37 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,147 @@ func ExampleCode() {
// true
}

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

ok := CmpDeeply(t, [...]int{11, 22, 33, 44}, Contains(22))
fmt.Println("array contains 22:", ok)

ok = CmpDeeply(t, [...]int{11, 22, 33, 44}, Contains(Between(20, 25)))
fmt.Println("array contains at least one item in [20 .. 25]:", ok)

ok = CmpDeeply(t, []int{11, 22, 33, 44}, Contains(22))
fmt.Println("slice contains 22:", ok)

ok = CmpDeeply(t, []int{11, 22, 33, 44}, Contains(Between(20, 25)))
fmt.Println("slice contains at least one item in [20 .. 25]:", ok)

// Output:
// array contains 22: true
// array contains at least one item in [20 .. 25]: true
// slice contains 22: true
// slice contains at least one item in [20 .. 25]: true
}

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

num := 123
got := [...]*int{&num, nil}

ok := CmpDeeply(t, got, Contains(nil))
fmt.Println("array contains untyped nil:", ok)

ok = CmpDeeply(t, got, Contains((*int)(nil)))
fmt.Println("array contains *int nil:", ok)

ok = CmpDeeply(t, got, Contains(Nil()))
fmt.Println("array contains Nil():", ok)

ok = CmpDeeply(t, got, Contains((*byte)(nil)))
fmt.Println("array contains *byte nil:", ok) // types differ: *byte ≠ *int

// Output:
// array contains untyped nil: true
// array contains *int nil: true
// array contains Nil(): true
// array contains *byte nil: false
}

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

ok := CmpDeeply(t,
map[string]int{"foo": 11, "bar": 22, "zip": 33}, Contains(22))
fmt.Println("map contains value 22:", ok)

ok = CmpDeeply(t,
map[string]int{"foo": 11, "bar": 22, "zip": 33},
Contains(Between(20, 25)))
fmt.Println("map contains at least one value in [20 .. 25]:", ok)

// Output:
// map contains value 22: true
// map contains at least one value in [20 .. 25]: true
}

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

got := "foobar"

ok := CmpDeeply(t, got, Contains("oob"), "checks %s", got)
fmt.Println("contains `oob` string:", ok)

ok = CmpDeeply(t, got, Contains('b'), "checks %s", got)
fmt.Println("contains 'b' rune:", ok)

ok = CmpDeeply(t, got, Contains(byte('a')), "checks %s", got)
fmt.Println("contains 'a' byte:", ok)

ok = CmpDeeply(t, got, Contains(Between('n', 'p')), "checks %s", got)
fmt.Println("contains at least one character ['n' .. 'p']:", ok)

// Output:
// contains `oob` string: true
// contains 'b' rune: true
// contains 'a' byte: true
// contains at least one character ['n' .. 'p']: true
}

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

// bytes.Buffer implements fmt.Stringer
got := bytes.NewBufferString("foobar")

ok := CmpDeeply(t, got, Contains("oob"), "checks %s", got)
fmt.Println("contains `oob` string:", ok)

ok = CmpDeeply(t, got, Contains('b'), "checks %s", got)
fmt.Println("contains 'b' rune:", ok)

ok = CmpDeeply(t, got, Contains(byte('a')), "checks %s", got)
fmt.Println("contains 'a' byte:", ok)

// Be careful! TestDeep operators in Contains() do not work with
// fmt.Stringer nor error interfaces
ok = CmpDeeply(t, got, Contains(Between('n', 'p')), "checks %s", got)
fmt.Println("try TestDeep operator:", ok)

// Output:
// contains `oob` string: true
// contains 'b' rune: true
// contains 'a' byte: true
// try TestDeep operator: false
}

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

got := errors.New("foobar")

ok := CmpDeeply(t, got, Contains("oob"), "checks %s", got)
fmt.Println("contains `oob` string:", ok)

ok = CmpDeeply(t, got, Contains('b'), "checks %s", got)
fmt.Println("contains 'b' rune:", ok)

ok = CmpDeeply(t, got, Contains(byte('a')), "checks %s", got)
fmt.Println("contains 'a' byte:", ok)

// Be careful! TestDeep operators in Contains() do not work with
// fmt.Stringer nor error interfaces
ok = CmpDeeply(t, got, Contains(Between('n', 'p')), "checks %s", got)
fmt.Println("try TestDeep operator:", ok)

// Output:
// contains `oob` string: true
// contains 'b' rune: true
// contains 'a' byte: true
// try TestDeep operator: false
}

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

Expand Down Expand Up @@ -1617,43 +1758,6 @@ func ExampleHasSuffix_error() {
// true
}

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

got := "foobar"

ok := CmpDeeply(t, got, Contains("oob"), "checks %s", got)
fmt.Println(ok)

// Output:
// true
}

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

// bytes.Buffer implements fmt.Stringer
got := bytes.NewBufferString("foobar")

ok := CmpDeeply(t, got, Contains("oob"), "checks %s", got)
fmt.Println(ok)

// Output:
// true
}

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

got := errors.New("foobar")

ok := CmpDeeply(t, got, Contains("oob"), "checks %s", got)
fmt.Println(ok)

// Output:
// true
}

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

Expand Down
Loading

0 comments on commit 60475df

Please sign in to comment.