| 1 |
// Copyright (c) 2018-2021, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/td" |
| 13 |
) |
| 14 |
|
| 15 |
func ExampleEqDeeply() { |
| 16 |
type MyStruct struct { |
| 17 |
Name string |
| 18 |
Num int |
| 19 |
Items []int |
| 20 |
} |
| 21 |
|
| 22 |
got := &MyStruct{ |
| 23 |
Name: "Foobar", |
| 24 |
Num: 12, |
| 25 |
Items: []int{4, 5, 9, 3, 8}, |
| 26 |
} |
| 27 |
|
| 28 |
if td.EqDeeply(got, |
| 29 |
td.Struct(&MyStruct{}, |
| 30 |
td.StructFields{ |
| 31 |
"Name": td.Re("^Foo"), |
| 32 |
"Num": td.Between(10, 20), |
| 33 |
"Items": td.ArrayEach(td.Between(3, 9)), |
| 34 |
})) { |
| 35 |
fmt.Println("Match!") |
| 36 |
} else { |
| 37 |
fmt.Println("NO!") |
| 38 |
} |
| 39 |
|
| 40 |
// Output: |
| 41 |
// Match! |
| 42 |
} |
| 43 |
|
| 44 |
func ExampleEqDeeplyError() { |
| 45 |
//line /testdeep/example.go:1 |
| 46 |
type MyStruct struct { |
| 47 |
Name string |
| 48 |
Num int |
| 49 |
Items []int |
| 50 |
} |
| 51 |
|
| 52 |
got := &MyStruct{ |
| 53 |
Name: "Foobar", |
| 54 |
Num: 12, |
| 55 |
Items: []int{4, 5, 9, 3, 8}, |
| 56 |
} |
| 57 |
|
| 58 |
err := td.EqDeeplyError(got, |
| 59 |
td.Struct(&MyStruct{}, |
| 60 |
td.StructFields{ |
| 61 |
"Name": td.Re("^Foo"), |
| 62 |
"Num": td.Between(10, 20), |
| 63 |
"Items": td.ArrayEach(td.Between(3, 8)), |
| 64 |
})) |
| 65 |
if err != nil { |
| 66 |
fmt.Println(err) |
| 67 |
} |
| 68 |
|
| 69 |
// Output: |
| 70 |
// DATA.Items[2]: values differ |
| 71 |
// got: 9 |
| 72 |
// expected: 3 ≤ got ≤ 8 |
| 73 |
// [under operator Between at example.go:18] |
| 74 |
} |