Debian Codegraph @

td/t_anchor_test.go

raw
1 // Copyright (c) 2019, 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 "testing"
11 "time"
12
13 "github.com/maxatome/go-testdeep/internal/test"
14 "github.com/maxatome/go-testdeep/td"
15 )
16
17 func timeParse(t *testing.T, s string) time.Time {
18 dt, err := time.Parse(time.RFC3339Nano, s)
19 if err != nil {
20 t.Helper()
21 t.Fatalf("Cannot parse `%s`: %s", s, err)
22 }
23 return dt
24 }
25
26 func TestT_Anchor(tt *testing.T) {
27 ttt := test.NewTestingTB(tt.Name())
28 t := td.NewT(ttt)
29 type MyStruct struct {
30 PNum *int
31 Num int64
32 Str string
33 Slice []int
34 Map map[string]bool
35 Time time.Time
36 }
37 n := 42
38 got := MyStruct{
39 PNum: &n,
40 Num: 136,
41 Str: "Pipo bingo",
42 Time: timeParse(tt, "2019-01-02T11:22:33.123456Z"),
43 }
44
45 // Using T.Anchor()
46 td.CmpTrue(tt,
47 t.Cmp(got, MyStruct{
48 PNum: t.Anchor(td.Ptr(td.Between(40, 45))).(*int),
49 Num: t.Anchor(td.Between(int64(135), int64(137))).(int64),
50 Str: t.Anchor(td.HasPrefix("Pipo"), "").(string),
51 Time: t.Anchor(td.TruncTime(timeParse(tt, "2019-01-02T11:22:00Z"), time.Minute)).(time.Time),
52 }))
53
54 // Using T.A()
55 td.CmpTrue(tt,
56 t.Cmp(got, MyStruct{
57 PNum: t.A(td.Ptr(td.Between(40, 45))).(*int),
58 Num: t.A(td.Between(int64(135), int64(137))).(int64),
59 Str: t.A(td.HasPrefix("Pipo"), "").(string),
60 Time: t.A(td.TruncTime(timeParse(tt, "2019-01-02T11:22:00Z"), time.Minute)).(time.Time),
61 }))
62
63 // Testing persistence
64 got = MyStruct{Num: 136}
65
66 tt.Run("without persistence", func(tt *testing.T) {
67 numOp := t.Anchor(td.Between(int64(135), int64(137))).(int64)
68
69 td.CmpTrue(tt, t.Cmp(got, MyStruct{Num: numOp}))
70 td.CmpFalse(tt, t.Cmp(got, MyStruct{Num: numOp}))
71 })
72
73 tt.Run("with persistence", func(tt *testing.T) {
74 numOp := t.Anchor(td.Between(int64(135), int64(137))).(int64)
75 defer t.AnchorsPersistTemporarily()()
76
77 td.CmpTrue(tt, t.Cmp(got, MyStruct{Num: numOp}))
78 td.CmpTrue(tt, t.Cmp(got, MyStruct{Num: numOp}))
79
80 t.ResetAnchors() // force reset anchored operators
81 td.CmpFalse(tt, t.Cmp(got, MyStruct{Num: numOp}))
82 })
83
84 // Errors
85 tt.Run("errors", func(tt *testing.T) {
86 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(nil) }),
87 "Cannot anchor a nil TestDeep operator")
88
89 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(td.Ignore(), 1, 2) }),
90 "usage: Anchor(OPERATOR[, MODEL]), too many parameters")
91
92 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(td.Ignore(), nil) }),
93 "Untyped nil value is not valid as model for an anchor")
94
95 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(td.Between(1, 2), 12.3) }),
96 "Operator Between TypeBehind() returned int which differs from model type float64. Omit model or ensure its type is int")
97
98 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(td.Ignore()) }),
99 "Cannot anchor operator Ignore as TypeBehind() returned nil. Use model parameter to specify the type to return")
100 })
101 }
102
103 type privStruct struct {
104 num int64
105 }
106
107 func (p privStruct) Num() int64 {
108 return p.num
109 }
110
111 func TestAddAnchorableStructType(tt *testing.T) {
112 type MyStruct struct {
113 Priv privStruct
114 }
115
116 ttt := test.NewTestingTB(tt.Name())
117 t := td.NewT(ttt)
118
119 // We want to anchor this operator
120 op := td.Smuggle((privStruct).Num, int64(42))
121
122 // Without making privStruct anchorable, it does not work
123 td.Cmp(tt, ttt.CatchFatal(func() { t.A(op, privStruct{}) }),
124 "td_test.privStruct struct type is not supported as an anchor. Try AddAnchorableStructType")
125
126 // Make privStruct anchorable
127 td.AddAnchorableStructType(func(nextAnchor int) privStruct {
128 return privStruct{num: int64(2e9 - nextAnchor)}
129 })
130
131 td.CmpTrue(tt,
132 t.Cmp(MyStruct{Priv: privStruct{num: 42}},
133 MyStruct{
134 Priv: t.A(op, privStruct{}).(privStruct), // ← now it works
135 }))
136
137 // Error
138 test.CheckPanic(tt,
139 func() { td.AddAnchorableStructType(123) },
140 "usage: AddAnchorableStructType(func (nextAnchor int) STRUCT_TYPE)")
141 }
142
143 func TestT_AnchorsPersist(tt *testing.T) {
144 ttt := test.NewTestingTB(tt.Name())
145
146 t1 := td.NewT(ttt)
147 t2 := td.NewT(ttt)
148 t3 := td.NewT(t1)
149
150 tt.Run("without anchors persistence", func(tt *testing.T) {
151 // Anchors persistence is shared for a same testing.TB
152 td.CmpFalse(tt, t1.DoAnchorsPersist())
153 td.CmpFalse(tt, t2.DoAnchorsPersist())
154 td.CmpFalse(tt, t3.DoAnchorsPersist())
155
156 func() {
157 defer t1.AnchorsPersistTemporarily()()
158 td.CmpTrue(tt, t1.DoAnchorsPersist())
159 td.CmpTrue(tt, t2.DoAnchorsPersist())
160 td.CmpTrue(tt, t3.DoAnchorsPersist())
161 }()
162 td.CmpFalse(tt, t1.DoAnchorsPersist())
163 td.CmpFalse(tt, t2.DoAnchorsPersist())
164 td.CmpFalse(tt, t3.DoAnchorsPersist())
165 })
166
167 tt.Run("with anchors persistence", func(tt *testing.T) {
168 t3.SetAnchorsPersist(true)
169
170 td.CmpTrue(tt, t1.DoAnchorsPersist())
171 td.CmpTrue(tt, t2.DoAnchorsPersist())
172 td.CmpTrue(tt, t3.DoAnchorsPersist())
173
174 func() {
175 defer t1.AnchorsPersistTemporarily()()
176 td.CmpTrue(tt, t1.DoAnchorsPersist())
177 td.CmpTrue(tt, t2.DoAnchorsPersist())
178 td.CmpTrue(tt, t3.DoAnchorsPersist())
179 }()
180 td.CmpTrue(tt, t1.DoAnchorsPersist())
181 td.CmpTrue(tt, t2.DoAnchorsPersist())
182 td.CmpTrue(tt, t3.DoAnchorsPersist())
183 })
184 }