-
Notifications
You must be signed in to change notification settings - Fork 0
/
enum_test.go
196 lines (188 loc) · 8.36 KB
/
enum_test.go
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
package scale_codec
import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestEnumParser(t *testing.T) {
// to extract better info from the goyacc parser set:
//yyDebug = 50
const input = `
enum Nested {
Number(int32)
}
enum MyEnum {
Single
Int(uint64)
Bool(bool)
A(Option<bool>)
B(Result<uint64, uint64>)
C(Option<Nested>)
D(Result<Nested, uint64>)
E(Result<Nested, Nested>)
F(Result<uint64, Nested>)
G((uint64, bool))
H(Option<(uint64, bool)>)
J(Result<(uint64, bool), bool>)
K((Option<bool>, Result<bool, bool>))
L(Result<Option<(uint64, bool)>, uint64>)
M(Option<Nested>)
N(Result<Nested, bool>)
O(Result<bool, Nested>)
P(Result<Nested, Error>)
Q((Nested, uint64, Error))
R((Result<uint64, bool>, Option<uint64>, Error))
}`
expectedEnum := []Enum{
{
Name: "Nested",
Variants: []EnumField{
{
Name: "Number",
Type: "*scale_codec.Integer[int32]",
TypeConstructor: "new(scale_codec.Integer[int32])",
},
},
},
{
Name: "MyEnum",
Variants: []EnumField{
{
Name: "Single",
Type: "*scale_codec.SimpleVariant",
TypeConstructor: "new(scale_codec.SimpleVariant)",
},
{
Name: "Int",
Type: "*scale_codec.Integer[uint64]",
TypeConstructor: "new(scale_codec.Integer[uint64])",
},
{
Name: "Bool",
Type: "*scale_codec.Bool",
TypeConstructor: "new(scale_codec.Bool)",
},
{
Name: "A",
Type: "*scale_codec.OptionG[*scale_codec.Bool]",
TypeConstructor: "new(scale_codec.OptionG[*scale_codec.Bool])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader,scale_codec.BoolFromRawBytes)",
},
{
Name: "B",
Type: "*scale_codec.ResultG[*scale_codec.Integer[uint64],*scale_codec.Integer[uint64]]",
TypeConstructor: "new(scale_codec.ResultG[*scale_codec.Integer[uint64],*scale_codec.Integer[uint64]])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, scale_codec.IntegerFromRawBytes[uint64], scale_codec.IntegerFromRawBytes[uint64])",
},
{
Name: "C",
Type: "*scale_codec.OptionG[Nested]",
TypeConstructor: "new(scale_codec.OptionG[Nested])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, UnmarshalNested)",
},
{
Name: "D",
Type: "*scale_codec.ResultG[Nested,*scale_codec.Integer[uint64]]",
TypeConstructor: "new(scale_codec.ResultG[Nested,*scale_codec.Integer[uint64]])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, UnmarshalNested, scale_codec.IntegerFromRawBytes[uint64])",
},
{
Name: "E",
Type: "*scale_codec.ResultG[Nested,Nested]",
TypeConstructor: "new(scale_codec.ResultG[Nested,Nested])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, UnmarshalNested, UnmarshalNested)",
},
{
Name: "F",
Type: "*scale_codec.ResultG[*scale_codec.Integer[uint64],Nested]",
TypeConstructor: "new(scale_codec.ResultG[*scale_codec.Integer[uint64],Nested])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, scale_codec.IntegerFromRawBytes[uint64], UnmarshalNested)",
},
{
Name: "G",
Type: "*T2[*scale_codec.Integer[uint64],*scale_codec.Bool]",
TypeConstructor: "new(T2[*scale_codec.Integer[uint64],*scale_codec.Bool])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader,scale_codec.IntegerFromRawBytes[uint64],scale_codec.BoolFromRawBytes)",
},
{
Name: "H",
Type: "*scale_codec.OptionG[*T2[*scale_codec.Integer[uint64],*scale_codec.Bool]]",
TypeConstructor: "new(scale_codec.OptionG[*T2[*scale_codec.Integer[uint64],*scale_codec.Bool]])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader,UnmarshalT2FromRawBytes[*scale_codec.Integer[uint64],*scale_codec.Bool](scale_codec.IntegerFromRawBytes[uint64],scale_codec.BoolFromRawBytes))",
},
{
Name: "J",
Type: "*scale_codec.ResultG[*T2[*scale_codec.Integer[uint64],*scale_codec.Bool],*scale_codec.Bool]",
TypeConstructor: "new(scale_codec.ResultG[*T2[*scale_codec.Integer[uint64],*scale_codec.Bool],*scale_codec.Bool])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, UnmarshalT2FromRawBytes[*scale_codec.Integer[uint64],*scale_codec.Bool](scale_codec.IntegerFromRawBytes[uint64],scale_codec.BoolFromRawBytes), scale_codec.BoolFromRawBytes)",
},
{
Name: "K",
Type: "*T2[*scale_codec.OptionG[*scale_codec.Bool],*scale_codec.ResultG[*scale_codec.Bool,*scale_codec.Bool]]",
TypeConstructor: "new(T2[*scale_codec.OptionG[*scale_codec.Bool],*scale_codec.ResultG[*scale_codec.Bool,*scale_codec.Bool]])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader,scale_codec.UnmarshalOptionFromRawBytes[*scale_codec.Bool](scale_codec.BoolFromRawBytes),scale_codec.UnmarshalResultFromRawBytes[*scale_codec.Bool,*scale_codec.Bool](scale_codec.BoolFromRawBytes,scale_codec.BoolFromRawBytes))",
},
{
Name: "L",
Type: "*scale_codec.ResultG[*scale_codec.OptionG[*T2[*scale_codec.Integer[uint64],*scale_codec.Bool]],*scale_codec.Integer[uint64]]",
TypeConstructor: "new(scale_codec.ResultG[*scale_codec.OptionG[*T2[*scale_codec.Integer[uint64],*scale_codec.Bool]],*scale_codec.Integer[uint64]])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, scale_codec.UnmarshalOptionFromRawBytes[*T2[*scale_codec.Integer[uint64],*scale_codec.Bool]](UnmarshalT2FromRawBytes[*scale_codec.Integer[uint64],*scale_codec.Bool](scale_codec.IntegerFromRawBytes[uint64],scale_codec.BoolFromRawBytes)), scale_codec.IntegerFromRawBytes[uint64])",
},
{
Name: "M",
Type: "*scale_codec.OptionG[Nested]",
TypeConstructor: "new(scale_codec.OptionG[Nested])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, UnmarshalNested)",
},
{
Name: "N",
Type: "*scale_codec.ResultG[Nested,*scale_codec.Bool]",
TypeConstructor: "new(scale_codec.ResultG[Nested,*scale_codec.Bool])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, UnmarshalNested, scale_codec.BoolFromRawBytes)",
},
{
Name: "O",
Type: "*scale_codec.ResultG[*scale_codec.Bool,Nested]",
TypeConstructor: "new(scale_codec.ResultG[*scale_codec.Bool,Nested])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, scale_codec.BoolFromRawBytes, UnmarshalNested)",
},
{
Name: "P",
Type: "*scale_codec.ResultG[Nested,Error]",
TypeConstructor: "new(scale_codec.ResultG[Nested,Error])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader, UnmarshalNested, UnmarshalError)",
},
{
Name: "Q",
Type: "*T3[Nested,*scale_codec.Integer[uint64],Error]",
TypeConstructor: "new(T3[Nested,*scale_codec.Integer[uint64],Error])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader,UnmarshalNested,scale_codec.IntegerFromRawBytes[uint64],UnmarshalError)",
},
{
Name: "R",
Type: "*T3[*scale_codec.ResultG[*scale_codec.Integer[uint64],*scale_codec.Bool],*scale_codec.OptionG[*scale_codec.Integer[uint64]],Error]",
TypeConstructor: "new(T3[*scale_codec.ResultG[*scale_codec.Integer[uint64],*scale_codec.Bool],*scale_codec.OptionG[*scale_codec.Integer[uint64]],Error])",
UnmarshalScale: "return i.Inner.UnmarshalSCALE(reader,scale_codec.UnmarshalResultFromRawBytes[*scale_codec.Integer[uint64],*scale_codec.Bool](scale_codec.IntegerFromRawBytes[uint64],scale_codec.BoolFromRawBytes),scale_codec.UnmarshalOptionFromRawBytes[*scale_codec.Integer[uint64]](scale_codec.IntegerFromRawBytes[uint64]),UnmarshalError)",
},
},
},
}
result := ParseEnum("", strings.NewReader(input))
if result != 0 {
t.Fatalf("error to parse enum")
}
for i, expected := range expectedEnum {
actual := Enums[i]
if expected.Name != actual.Name {
t.Fatalf("\nexpected: %v\ngot: %v", expected.Name, actual.Name)
}
for j, variant := range expected.Variants {
actualVariant := actual.Variants[j]
if !reflect.DeepEqual(variant, actualVariant) {
t.Fatalf("\nexpected: %v\ngot: %v", variant, actualVariant)
}
}
}
fmt.Println(GenericTuple)
}