-
Notifications
You must be signed in to change notification settings - Fork 0
/
definition_test.go
183 lines (178 loc) · 3.79 KB
/
definition_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
package tl
import (
"testing"
"github.com/stretchr/testify/require"
)
var bareString = Type{
Name: "string",
Bare: true,
}
var bareLong = Type{
Name: "long",
Bare: true,
}
func TestDefinition(t *testing.T) {
for _, tt := range []struct {
Case string
Input string
String string
Definition Definition
}{
{
Case: "inputPhoneCall",
Input: "inputPhoneCall#1e36fded id:long access_hash:long = InputPhoneCall",
Definition: Definition{
ID: 0x1e36fded,
Name: "inputPhoneCall",
Params: []Parameter{
{
Name: "id",
Type: bareLong,
},
{
Name: "access_hash",
Type: bareLong,
},
},
Type: Type{Name: "InputPhoneCall"},
},
},
{
Case: "userWithoutCRC",
Input: "user id:int first_name:string last_name:string = User;",
String: "user#d23c81a3 id:int first_name:string last_name:string = User",
Definition: Definition{
ID: 0xd23c81a3,
Name: "user",
Type: Type{Name: "User"},
Params: []Parameter{
{Name: "id", Type: Type{Name: "int", Bare: true}},
{Name: "first_name", Type: bareString},
{Name: "last_name", Type: bareString},
},
},
},
{
Case: "OK",
Input: "ok = Ok;",
String: "ok#d4edbe69 = Ok",
Definition: Definition{
ID: 0xd4edbe69,
Name: "ok",
Type: Type{Name: "Ok"},
},
},
{
Case: "GroupWithoutFieldNames",
String: "group#60fc45e0 int string string = Group",
Input: "group int string string = Group",
Definition: Definition{
ID: 0x60fc45e0,
Name: "group",
Type: Type{Name: "Group"},
Params: []Parameter{
{Type: Type{Name: "int", Bare: true}},
{Type: bareString},
{Type: bareString},
},
},
},
{
Case: "Zero",
Input: "0=0",
String: "0#971b4490 = 0",
Definition: Definition{
Name: "0",
ID: 0x971b4490,
Type: Type{Name: "0", Bare: true},
},
},
{
Case: "inputMediaUploadedPhoto",
Input: "inputMediaUploadedPhoto#1e287d04 flags:# file:InputFile stickers:flags.0?Vector<InputDocument> ttl_seconds:flags.1?int = InputMedia",
Definition: Definition{
Name: "inputMediaUploadedPhoto",
ID: 0x1e287d04,
Type: Type{Name: "InputMedia"},
Params: []Parameter{
{Flags: true, Name: "flags"},
{Name: "file", Type: Type{Name: "InputFile"}},
{
Name: "stickers",
Flag: &Flag{
Name: "flags",
Index: 0,
},
Type: Type{
Name: "Vector",
GenericArg: &Type{
Name: "InputDocument",
},
},
},
{
Name: "ttl_seconds",
Flag: &Flag{
Name: "flags",
Index: 1,
},
Type: Type{
Name: "int",
Bare: true,
},
},
},
},
},
{
Case: "invokeWithLayer",
Input: "invokeWithLayer#da9b0d0d {X:Type} layer:int query:!X = X",
Definition: Definition{
Name: "invokeWithLayer",
ID: 0xda9b0d0d,
Type: Type{Name: "X"},
GenericParams: []string{"X"},
Params: []Parameter{
{
Name: "layer",
Type: Type{Name: "int", Bare: true},
},
{
Name: "query",
Type: Type{Name: "X", GenericRef: true},
},
},
},
},
} {
var (
input = tt.Input
expectedDef = tt.Definition
expectedStr = tt.Input
)
if tt.String != "" {
expectedStr = tt.String
}
t.Run(tt.Case, func(t *testing.T) {
var d Definition
if err := d.Parse(input); err != nil {
t.Fatal(err)
}
require.Equal(t, expectedDef, d)
require.Equal(t, expectedStr, d.String())
})
}
t.Run("Error", func(t *testing.T) {
for _, invalid := range []string{
"=0",
"0 :{.0?InputFi00=0",
} {
t.Run(invalid, func(t *testing.T) {
var d Definition
if err := d.Parse(invalid); err == nil {
t.Error("should error")
}
})
}
})
}