-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparse_test.go
More file actions
132 lines (118 loc) · 2.52 KB
/
parse_test.go
File metadata and controls
132 lines (118 loc) · 2.52 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
package ifacecodegen
import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestParserEmtptySource(t *testing.T) {
opts := ParseOptions{}
pkg, err := Parse(opts)
if pkg != nil {
t.Fatalf("Pkg should be nil, actual %v", pkg)
}
if err == nil {
t.Fatalf("Error should not be nil")
}
if actual, expected := err.Error(), "Source should not be nil"; actual != expected {
t.Errorf("expected %v, actual %v", expected, actual)
}
}
func TestParserInvalidSource(t *testing.T) {
opts := ParseOptions{
Source: strings.NewReader(`foo`),
}
pkg, err := Parse(opts)
if pkg != nil {
t.Fatalf("Pkg should be nil, actual %v", pkg)
}
if err == nil {
t.Fatalf("Error should not be nil")
}
if !strings.HasPrefix(err.Error(), "failed parsing source 1:1") {
t.Errorf("Error should be about parsing")
}
}
func TestParserParsePackage(t *testing.T) {
opts := ParseOptions{
Source: strings.NewReader(`
package foo
`),
}
pkg, err := Parse(opts)
if err != nil {
t.Fatalf("Error should be nil, actual %v", err)
}
if pkg == nil {
t.Fatalf("Pkg should not be nil")
}
if actual, expected := pkg, (&Package{Name: "foo"}); !reflect.DeepEqual(actual, expected) {
t.Errorf("expected %v, actual %v", expected, actual)
}
}
func TestParser(t *testing.T) {
opts := ParseOptions{
Source: strings.NewReader(`
package foo
type Service interface {
Foo(string) Bar
}
`),
}
pkg, err := Parse(opts)
if err != nil {
t.Fatalf("Error should be nil, actual %v", err)
}
if pkg == nil {
t.Fatalf("Pkg should not be nil")
}
expected := &Package{
Name: "foo",
Interfaces: []*Interface{
&Interface{
Name: "Service",
Methods: []*Method{
&Method{
Name: "Foo",
In: []*Parameter{
&Parameter{
Name: "_param1",
Type: TypeBuiltin("string"),
},
},
Out: []*Parameter{
&Parameter{
Name: "_result1",
Type: &TypeExported{
Package: "foo",
Type: TypeBuiltin("Bar"),
},
},
},
},
},
},
},
}
if actual := pkg; !reflect.DeepEqual(actual, expected) {
t.Errorf("expected %v, actual %v", expected, actual)
}
}
func debugPkg(pkg *Package) {
for _, iface := range pkg.Interfaces {
fmt.Println("interface", iface.Name)
for _, m := range iface.Methods {
fmt.Println(" - method", m.Name)
if len(m.In) > 0 {
for _, mo := range m.In {
fmt.Println(" in ", mo.Name, mo.Type)
}
}
if len(m.Out) > 0 {
for _, mo := range m.Out {
fmt.Println(" out ", mo.Name, mo.Type)
}
}
}
}
}