| 1 |
// Copyright (c) 2023, 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 |
//go:build go1.18 |
| 8 |
// +build go1.18 |
| 9 |
|
| 10 |
package td_test |
| 11 |
|
| 12 |
import ( |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/maxatome/go-testdeep/internal/test" |
| 17 |
"github.com/maxatome/go-testdeep/td" |
| 18 |
) |
| 19 |
|
| 20 |
func TestAnchor(tt *testing.T) { |
| 21 |
ttt := test.NewTestingTB(tt.Name()) |
| 22 |
t := td.NewT(ttt) |
| 23 |
type MyStruct struct { |
| 24 |
PNum *int |
| 25 |
Num int64 |
| 26 |
Str string |
| 27 |
Slice []int |
| 28 |
Map map[string]bool |
| 29 |
Time time.Time |
| 30 |
} |
| 31 |
n := 42 |
| 32 |
got := MyStruct{ |
| 33 |
PNum: &n, |
| 34 |
Num: 136, |
| 35 |
Str: "Pipo bingo", |
| 36 |
Time: timeParse(tt, "2019-01-02T11:22:33.123456Z"), |
| 37 |
} |
| 38 |
|
| 39 |
td.CmpTrue(tt, |
| 40 |
t.Cmp(got, MyStruct{ |
| 41 |
PNum: td.Anchor[*int](t, td.Ptr(td.Between(40, 45))), |
| 42 |
Num: td.Anchor[int64](t, td.Between(int64(135), int64(137))), |
| 43 |
Str: td.Anchor[string](t, td.HasPrefix("Pipo")), |
| 44 |
Time: td.Anchor[time.Time](t, td.TruncTime(timeParse(tt, "2019-01-02T11:22:00Z"), time.Minute)), |
| 45 |
})) |
| 46 |
|
| 47 |
td.CmpTrue(tt, |
| 48 |
t.Cmp(got, MyStruct{ |
| 49 |
PNum: td.A[*int](t, td.Ptr(td.Between(40, 45))), |
| 50 |
Num: td.A[int64](t, td.Between(int64(135), int64(137))), |
| 51 |
Str: td.A[string](t, td.HasPrefix("Pipo")), |
| 52 |
Time: td.A[time.Time](t, td.TruncTime(timeParse(tt, "2019-01-02T11:22:00Z"), time.Minute)), |
| 53 |
})) |
| 54 |
} |