-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphpdoctest.go
117 lines (98 loc) · 2.39 KB
/
phpdoctest.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
package phpdoctest
import (
"testing"
"github.com/quasilyte/parsing-in-go/phpdoc"
)
type parser interface {
Parse(s string) (phpdoc.Type, error)
}
func RunBenchmark(b *testing.B, p parser) {
tests := []struct {
label string
input string
}{
{`simple`, `int`},
{`normal`, `Foo|Bar|null`},
{`complex`, `(?a|c|false)&d`},
{`array`, `int[][]`},
{`classname`, `A\B\C\D`},
{`whitespace`, ` ( int ) `},
}
for _, test := range tests {
input := test.input
b.Run(test.label, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := p.Parse(input)
if err != nil {
b.Fatal(err)
}
}
})
}
}
func Run(t *testing.T, p parser) {
tests := []struct {
input string
expect string
}{
{`int`, `int`},
{`float`, `float`},
{`string`, `string`},
{`bool`, `bool`},
{` ( int ) `, `int`},
{`A`, `A`},
{`A\B`, `A\B`},
{`A\B\C`, `A\B\C`},
{`(int)`, `int`},
{`((int))`, `int`},
{`?int`, `?(int)`},
{`??int`, `?(?(int))`},
{`?x[]`, `(?(x))[]`},
{`?x[][]`, `((?(x))[])[]`},
{`?(int[])[]`, `(?((int)[]))[]`},
{`int?`, `(int)?`},
{`(int[])[]?`, `(((int)[])[])?`},
{`int[][]`, `((int)[])[]`},
{`(int)[][]`, `((int)[])[]`},
{`(int[])[]`, `((int)[])[]`},
{`int|null[]`, `(int)|((null)[])`},
{`int|float[]`, `(int)|((float)[])`},
{`(int|float)[]`, `((int)|(float))[]`},
{`int&float[]`, `(int)&((float)[])`},
{`(int&float)[]`, `((int)&(float))[]`},
{`x|y|z`, `(x)|((y)|(z))`},
{`x&y&z`, `(x)&((y)&(z))`},
{`x&y|z`, `((x)&(y))|(z)`},
{`x|y&z`, `(x)|((y)&(z))`},
{`x&(y|z)`, `(x)&((y)|(z))`},
{`x&(y|z)[]`, `(x)&(((y)|(z))[])`},
{`x&(y|z)[][]`, `(x)&((((y)|(z))[])[])`},
{`?int|Reader&Writer|false`, `(?(int))|(((Reader)&(Writer))|(false))`},
}
for _, test := range tests {
typ, err := p.Parse(` ` + test.input + ` `)
if err != nil {
t.Errorf("parse `%s` error: %v", test.input, err)
continue
}
if typ == nil {
t.Errorf("parse `%s` returned type is nil!", test.input)
continue
}
if typ.String() != test.expect {
t.Errorf("parse `%s`:\nhave `%s`\nwant `%s`",
test.input, typ.String(), test.expect)
continue
}
typ2, err := p.Parse(typ.String())
if err != nil {
t.Errorf("re-parse `%s` error: %v", typ.String(), err)
continue
}
if typ.String() != typ2.String() {
t.Errorf("re-parsed representation mismatch:\nA: `%s`\nB: `%s`",
typ.String(), typ2.String())
continue
}
}
}