-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser_test.go
217 lines (204 loc) · 6.23 KB
/
parser_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package mokku
import (
"strings"
"testing"
)
func TestParser(t *testing.T) {
for _, tc := range []struct {
name string
src string
exp *targetInterface
}{
{
name: "named empty interface",
src: `type Foo interface{}`,
exp: &targetInterface{TypeName: "Foo"},
},
{
name: "anonymous empty interface",
src: `interface{}`,
exp: &targetInterface{},
},
{
name: "single niladic method with no return params",
src: `type Bar interface{
Act()
}`,
exp: &targetInterface{
TypeName: "Bar",
Methods: []*method{{"Act", "( )", "( )", false}},
},
},
{
name: "two niladic methods with no return params",
src: `type FooBar interface{
Act()
Do()
}`,
exp: &targetInterface{
TypeName: "FooBar",
Methods: []*method{{"Act", "( )", "( )", false}, {"Do", "( )", "( )", false}},
},
},
{
name: "single method with single input parameter and no return params",
src: `type FooBar interface{
Act(x string)
}`,
exp: &targetInterface{
TypeName: "FooBar",
Methods: []*method{{"Act", `( x string )`, "( x )", false}},
},
},
{
name: "single method with multiple complex input parameters and no return params",
src: `type FooBar interface{
Act(x, y string, z chan []struct{a [0]int})
}`,
exp: &targetInterface{
TypeName: "FooBar",
Methods: []*method{{"Act", `( x , y string , z chan [ ] struct { a [ 0 ] int } )`, "( x , y , z )", false}},
},
},
{
name: "single method with no input parameters and one return parameter",
src: `type FooBar interface {
Act() error
}`,
exp: &targetInterface{
TypeName: "FooBar",
Methods: []*method{{"Act", `( ) error`, "( )", true}},
},
},
{
name: "single method with no input parameters and pointer return parameter",
src: `type FooBar interface {
Act() *foo
}`,
exp: &targetInterface{
TypeName: "FooBar",
Methods: []*method{{"Act", `( ) * foo`, "( )", true}},
},
},
{
name: "trailing comma",
src: `type Calculator interface {
Add(
a int,
b int,
) int
}`,
exp: &targetInterface{
TypeName: "Calculator",
Methods: []*method{{"Add", `( a int , b int , ) int`, `( a , b , )`, true}},
},
},
{
name: "mega complex example",
src: `type GoodLuck interface {
First()
Second(ctx context.Context, _ []*fish, s, ss string) (error, int, chan struct{})
Third(vararg ...map[string]interface{}) (a, b string, e error)
}`,
exp: &targetInterface{
TypeName: "GoodLuck",
Methods: []*method{
{"First", `( )`, "( )", false},
{"Second", `( ctx context . Context , _ [ ] * fish , s , ss string ) ( error , int , chan struct { } )`, "( ctx , _ , s , ss )", true}, // TODO: figure out what the default value is likely to be for _s as _ is useless as params.
{"Third", `( vararg ... map [ string ] interface { } ) ( a , b string , e error )`, "( vararg ... )", true},
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
p := newParser([]byte(tc.src))
got, err := p.parse()
if err != nil {
t.Fatalf("got unexpected error when calling parse parser: '%s'", err.Error())
} else if got == nil {
t.Fatal("expected non-nil parsed result but was nil")
}
if got.TypeName != tc.exp.TypeName {
t.Errorf("expected name '%s' but got '%s'", tc.exp.TypeName, got.TypeName)
}
for i := range tc.exp.Methods {
expLen, gotLen := len(tc.exp.Methods), len(got.Methods)
if expLen != gotLen {
t.Errorf("expected %d methods but got %d", expLen, gotLen)
t.Errorf("exp: '%+v'", tc.exp.Methods)
t.Errorf("got: '%+v'", got.Methods)
break
}
expName, gotName := tc.exp.Methods[i].Name, got.Methods[i].Name
if expName != gotName {
t.Errorf("expected method of index %d to have name '%s' but was '%s'", i, expName, gotName)
}
expSignature, gotSignature := tc.exp.Methods[i].Signature, got.Methods[i].Signature
if expSignature != gotSignature {
t.Errorf("expected method of index %d to had different signatures:\nexp: %s\nwas: %s", i, expSignature, gotSignature)
}
expParams, gotParams := tc.exp.Methods[i].OrderedParams, got.Methods[i].OrderedParams
if expParams != gotParams {
t.Errorf("expected method of index %d to had different ordered params:\nexp: %s\nwas: %s", i, expParams, gotParams)
}
expHasReturn, gotHasReturn := tc.exp.Methods[i].HasReturn, got.Methods[i].HasReturn
if expHasReturn != gotHasReturn {
t.Errorf("expected method of index %d to had different HasReturn:\nexp: %v\nwas: %v", i, expHasReturn, gotHasReturn)
}
}
})
}
t.Run("error cases", func(t *testing.T) {
for _, tc := range []struct {
name string
src string
exp string
}{
{
name: "just curly brackets",
src: `{}`,
exp: "unable to find interface declaration",
},
{
name: "missing closing curly bracket",
src: `type Foo interface{`,
exp: "unable to find method definition",
},
{
name: "missing closing round bracket",
src: `type Foo interface{ Act(a string, }`,
exp: "unable to find method definition",
},
} {
t.Run(tc.name, func(t *testing.T) {
p := newParser([]byte(tc.src))
_, err := p.parse()
if err == nil {
t.Fatalf("missing expected error when calling parse parser with: '%s'", tc.src)
}
if !strings.Contains(err.Error(), tc.exp) {
t.Errorf("expected an error message containing '%s' but got '%s'", tc.exp, err.Error())
}
})
}
})
}
func TestOrderedParamsFromSignature(t *testing.T) {
for _, tc := range []struct{ name, sig, exp string }{
{"not a method", "", ""},
{"niladic", "( )", "( )"},
{"niladic with return param", "( ) error", "( )"},
{"niladic with return params", "( ) ( int, error )", "( )"},
{"monadic", "( a string )", "( a )"},
{"dyadic with first parameter type absent", "( a, b string )", "( a , b )"},
{"dyadic", "( a int, b string )", "( a , b )"},
{"triadic with only last parameter type present", "( a, b, c string )", "( a , b , c )"},
{"complex case", "( a map[int]interface{}, b chan []struct{}, c ...string )", "( a , b , c ... )"},
} {
t.Run(tc.name, func(t *testing.T) {
if got := parseArgs(tc.sig); got != tc.exp {
t.Errorf("expected '%s' but got '%s'", tc.exp, got)
}
})
}
}