Debian Codegraph @

td/td_struct_lazy_test.go

raw
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 package td_test
8
9 import (
10 "testing"
11
12 "github.com/maxatome/go-testdeep/internal/test"
13 "github.com/maxatome/go-testdeep/td"
14 )
15
16 func TestStructLazy(t *testing.T) {
17 got := struct {
18 ValInt int
19 ValStr string
20 }{0, "foobar"}
21
22 t.Run("Struct OK", func(t *testing.T) {
23 got.ValInt = 123
24
25 checkOK(t, got, td.Struct(nil, td.StructFields{
26 "ValStr": "foobar",
27 }))
28
29 checkOK(t, &got, td.Struct(nil, td.StructFields{
30 "ValInt": 123,
31 "ValStr": "foobar",
32 }))
33
34 checkOK(t, &got, td.Struct(nil, td.StructFields{"=Val*": td.NotZero()}))
35 })
36
37 t.Run("SStruct OK", func(t *testing.T) {
38 got.ValInt = 0
39
40 checkOK(t, got, td.SStruct(nil, td.StructFields{
41 "ValStr": "foobar",
42 }))
43
44 checkOK(t, &got, td.SStruct(nil, td.StructFields{
45 "ValInt": 0,
46 "ValStr": "foobar",
47 }))
48
49 got.ValInt = 123
50 checkOK(t, &got, td.SStruct(nil, td.StructFields{"=Val*": td.NotZero()}))
51 })
52
53 got.ValInt = 666
54
55 ops := []struct {
56 name string
57 new func(any, ...td.StructFields) td.TestDeep
58 }{
59 {"Struct", td.Struct},
60 {"SStruct", td.SStruct},
61 }
62 for _, op := range ops {
63 t.Run(op.name+" errors", func(t *testing.T) {
64 under := mustContain("under operator " + op.name + " at td_struct_lazy_test.go:")
65 badUsage := mustBe("bad usage of " + op.name + " operator")
66
67 checkError(t, got, op.new(nil, td.StructFields{"Zip": 345}),
68 expectedError{
69 Message: badUsage,
70 Path: mustBe("DATA"),
71 Summary: mustBe(`struct { ValInt int; ValStr string } has no field "Zip"`),
72 Under: under,
73 })
74
75 checkError(t, got, op.new(nil, td.StructFields{">\tZip": 345}),
76 expectedError{
77 Message: badUsage,
78 Path: mustBe("DATA"),
79 Summary: mustBe(`struct { ValInt int; ValStr string } has no field "Zip" (from ">\tZip")`),
80 Under: under,
81 })
82
83 checkError(t, got, op.new(nil, td.StructFields{"ValInt": "zip"}),
84 expectedError{
85 Message: mustBe("type mismatch"),
86 Path: mustBe("DATA.ValInt"),
87 Got: mustBe("int"),
88 Expected: mustBe("string"),
89 })
90
91 checkError(t, 123,
92 op.new(nil, td.StructFields{}),
93 expectedError{
94 Message: mustBe("bad kind"),
95 Path: mustBe("DATA"),
96 Got: mustContain("int"),
97 Expected: mustContain("struct OR *struct"),
98 Under: under,
99 })
100
101 n := 123
102 checkError(t, &n,
103 op.new(nil, td.StructFields{}),
104 expectedError{
105 Message: mustBe("bad kind"),
106 Path: mustBe("DATA"),
107 Got: mustContain("*int"),
108 Expected: mustContain("struct OR *struct"),
109 Under: under,
110 })
111
112 type myInt int
113 checkError(t, myInt(123),
114 op.new(nil, td.StructFields{}),
115 expectedError{
116 Message: mustBe("bad kind"),
117 Path: mustBe("DATA"),
118 Got: mustContain("int (td_test.myInt type)"),
119 Expected: mustContain("struct OR *struct"),
120 Under: under,
121 })
122
123 mi := myInt(123)
124 checkError(t, &mi,
125 op.new(nil, td.StructFields{}),
126 expectedError{
127 Message: mustBe("bad kind"),
128 Path: mustBe("DATA"),
129 Got: mustContain("*int (*td_test.myInt type)"),
130 Expected: mustContain("struct OR *struct"),
131 Under: under,
132 })
133
134 checkError(t, nil, op.new(nil, td.StructFields{}),
135 expectedError{
136 Message: mustBe("values differ"),
137 Path: mustBe("DATA"),
138 Got: mustBe("nil"),
139 Expected: mustContain("Struct(<any struct type>{})"),
140 Under: under,
141 })
142
143 checkError(t, (*struct{ x int })(nil), op.new(nil, td.StructFields{}),
144 expectedError{
145 Message: mustBe("values differ"),
146 Path: mustBe("DATA"),
147 Got: mustBe("(*struct { x int })(<nil>)"),
148 Expected: mustContain("non-nil"),
149 Under: under,
150 })
151 })
152
153 t.Run(op.name+" String", func(t *testing.T) {
154 test.EqualStr(t, op.new(nil).String(), op.name+`(<any struct type>{})`)
155 test.EqualStr(t,
156 op.new(nil,
157 td.StructFields{
158 "ValBool": false,
159 "= Val*": td.NotZero(),
160 "> Foo": 12,
161 "=~Bar[6-9]$": "zip",
162 }).String(),
163 op.name+`(<any struct type>{
164 = Val*: NotZero()
165 =~Bar[6-9]$: "zip"
166 > Foo: 12
167 ValBool: false
168 })`)
169 })
170 }
171 }
172
173 func TestStructLazyTypeBehind(t *testing.T) {
174 equalTypes(t, td.Struct(nil, nil), nil)
175 equalTypes(t, td.SStruct(nil, nil), nil)
176 }