Repository navigation
Expand file tree
/
Copy pathenum_test.go
More file actions
295 lines (265 loc) · 6.94 KB
/
Copy pathenum_test.go
File metadata and controls
295 lines (265 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// SPDX-FileCopyrightText: 2026 Arne K. (TRC-Loop)
// SPDX-License-Identifier: BSD-3-Clause
package rustyon
import (
"bytes"
"reflect"
"strings"
"testing"
)
type Shape interface{ area() float64 }
type Circle struct{ Radius float64 }
type Rect struct {
_ struct{} `ron:",tuple"`
W, H float64
}
type Point struct{}
type Label string
type Polygon struct{ Points []tupleStruct }
func (c Circle) area() float64 { return 3 * c.Radius * c.Radius }
func (r Rect) area() float64 { return r.W * r.H }
func (Point) area() float64 { return 0 }
func (Label) area() float64 { return 0 }
func (p *Polygon) area() float64 { return float64(len(p.Points)) }
type Color int
const (
Red Color = iota
Green
Blue
)
func shapeRegistry() *Registry {
r := NewRegistry()
RegisterEnumIn[Shape](r,
Variant[Circle]("Circle"),
Variant[Rect]("Rect"),
Variant[Point]("Point"),
Variant[Label]("Label"),
Variant[*Polygon]("Polygon"),
)
RegisterUnitEnumIn(r, map[Color]string{Red: "Red", Green: "Green", Blue: "Blue"})
return r
}
type scene struct {
Shapes []Shape
Main Shape
None Shape
Color Color
Colors map[Color]int
ByName map[string]Shape
}
func TestEnumRoundTrip(t *testing.T) {
r := shapeRegistry()
in := scene{
Shapes: []Shape{Circle{1.5}, Rect{W: 2, H: 3}, Point{}, Label("hi"), &Polygon{[]tupleStruct{{X: 1, Y: 2}}}},
Main: Circle{2},
Color: Blue,
Colors: map[Color]int{Green: 1},
ByName: map[string]Shape{"p": Point{}},
}
var buf bytes.Buffer
enc := NewEncoder(&buf)
enc.SetRegistry(r)
enc.SetIndent("", " ")
if err := enc.Encode(in); err != nil {
t.Fatal(err)
}
want := `(
shapes: [
Circle(
radius: 1.5,
),
Rect(2.0, 3.0),
Point,
Label("hi"),
Polygon(
points: [
(1, 2),
],
),
],
main: Circle(
radius: 2.0,
),
none: None,
color: Blue,
colors: {
Green: 1,
},
by_name: {
"p": Point,
},
)
`
if buf.String() != want {
t.Errorf("got\n%s\nwant\n%s", buf.String(), want)
}
dec := NewDecoder(&buf)
dec.SetRegistry(r)
var out scene
if err := dec.Decode(&out); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(in, out) {
t.Errorf("round trip mismatch\nin %#v\nout %#v", in, out)
}
}
func TestEnumDecodeForms(t *testing.T) {
r := shapeRegistry()
tests := []struct {
src string
want Shape
}{
{"Circle(radius: 1.0)", Circle{1}},
{"Circle((radius: 1.0))", Circle{1}}, // unwrap_variant_newtypes inverse
{"Rect(1.0, 2.0)", Rect{W: 1, H: 2}},
{"Point", Point{}},
{"Point()", Point{}},
{`"Point"`, Point{}},
{`Label("x")`, Label("x")},
{"Some(Point)", Point{}},
{"None", nil},
}
for _, tt := range tests {
dec := NewDecoder(strings.NewReader(tt.src))
dec.SetRegistry(r)
var got Shape
if err := dec.Decode(&got); err != nil {
t.Errorf("%s: %v", tt.src, err)
continue
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("%s: got %#v, want %#v", tt.src, got, tt.want)
}
}
}
func TestEnumDecodeConcrete(t *testing.T) {
r := shapeRegistry()
dec := NewDecoder(strings.NewReader("Circle(radius: 4.0)"))
dec.SetRegistry(r)
var c Circle
if err := dec.Decode(&c); err != nil {
t.Fatal(err)
}
if c.Radius != 4 {
t.Errorf("got %+v", c)
}
dec = NewDecoder(strings.NewReader(`Label("direct")`))
dec.SetRegistry(r)
var l Label
if err := dec.Decode(&l); err != nil {
t.Fatal(err)
}
if l != "direct" {
t.Errorf("got %q", l)
}
}
func TestEnumErrors(t *testing.T) {
r := shapeRegistry()
tests := []struct {
src string
ptr any
msg string
}{
{"Triangle(1.0)", new(Shape), `unknown variant "Triangle"`},
{"(radius: 1.0)", new(Shape), "expected an enum variant"},
{"5", new(Shape), "expected an enum variant"},
{"Circle(radius: \"x\")", new(Shape), "radius"},
{"Purple", new(Color), `unknown variant "Purple"`},
{"1", new(Color), "expected a unit variant"},
}
for _, tt := range tests {
dec := NewDecoder(strings.NewReader(tt.src))
dec.SetRegistry(r)
err := dec.Decode(tt.ptr)
if err == nil || !strings.Contains(err.Error(), tt.msg) {
t.Errorf("%s: got %v, want error containing %q", tt.src, err, tt.msg)
}
}
var buf bytes.Buffer
enc := NewEncoder(&buf)
enc.SetRegistry(r)
if err := enc.Encode(Color(99)); err == nil || !strings.Contains(err.Error(), "no registered name") {
t.Errorf("unknown unit value: got %v", err)
}
}
func TestRegisterPanics(t *testing.T) {
mustPanic := func(name string, f func()) {
t.Helper()
defer func() {
if recover() == nil {
t.Errorf("%s: expected panic", name)
}
}()
f()
}
r := NewRegistry()
mustPanic("not interface", func() { RegisterEnumIn[Circle](r, Variant[Circle]("C")) })
mustPanic("not implementing", func() { RegisterEnumIn[Shape](r, Variant[int]("I")) })
mustPanic("pointer receiver needs pointer", func() { RegisterEnumIn[Shape](r, Variant[Polygon]("P")) })
mustPanic("invalid name", func() { RegisterEnumIn[Shape](r, Variant[Circle]("bad name")) })
mustPanic("zero spec", func() { RegisterEnumIn[Shape](r, VariantSpec{}) })
mustPanic("interface variant", func() { RegisterEnumIn[any](r, Variant[Shape]("S")) })
mustPanic("duplicate name", func() {
RegisterEnumIn[Shape](r, Variant[Circle]("X"), Variant[Rect]("X"))
})
mustPanic("duplicate type", func() {
RegisterEnumIn[Shape](r, Variant[Circle]("A"), Variant[Circle]("B"))
})
mustPanic("unit invalid name", func() { RegisterUnitEnumIn(r, map[Color]string{Red: ""}) })
mustPanic("unit duplicate", func() { RegisterUnitEnumIn(r, map[Color]string{Red: "A", Blue: "A"}) })
}
type ambiguousA interface{ a() }
type ambiguousB interface{ b() }
type both struct{ V int }
func (both) a() {}
func (both) b() {}
func TestVariantInSeveralEnums(t *testing.T) {
r := NewRegistry()
RegisterEnumIn[ambiguousA](r, Variant[both]("InA"))
RegisterEnumIn[ambiguousB](r, Variant[both]("InB"))
var buf bytes.Buffer
enc := NewEncoder(&buf)
enc.SetRegistry(r)
v := struct {
A ambiguousA
B ambiguousB
C any
}{both{1}, both{2}, both{3}}
if err := enc.Encode(v); err != nil {
t.Fatal(err)
}
// The static interface type selects the name; without one the type is
// ambiguous and encodes as a plain struct.
want := "(a:InA(v:1),b:InB(v:2),c:(v:3))\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
}
func TestDefaultRegistry(t *testing.T) {
type defaultShape interface{ isDefault() }
RegisterEnum[defaultShape](Variant[defaultVariant]("DefaultVariant"))
RegisterUnitEnum(map[defaultUnit]string{1: "One"})
out, err := Marshal(struct {
S defaultShape
U defaultUnit
}{defaultVariant{}, 1})
if err != nil {
t.Fatal(err)
}
if string(out) != "(s:DefaultVariant,u:One)" {
t.Errorf("got %s", out)
}
var back struct {
S defaultShape
U defaultUnit
}
if err := Unmarshal(out, &back); err != nil {
t.Fatal(err)
}
if back.S != (defaultVariant{}) || back.U != 1 {
t.Errorf("got %+v", back)
}
}
type defaultVariant struct{}
func (defaultVariant) isDefault() {}
type defaultUnit uint8