-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement_test.go
More file actions
167 lines (127 loc) · 3.57 KB
/
Copy pathimplement_test.go
File metadata and controls
167 lines (127 loc) · 3.57 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
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
package implement
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"os"
"strings"
"fmt"
"io/ioutil"
)
const(
localStruct = "*Struct"
packageStruct = "*bytes.Buffer"
primitive = "string"
errType = "error"
)
var (
gopath = os.Getenv("GOPATH")
path = strings.Join([]string{gopath, "src", "github.com", "thisisfineio", "implement", "test_data"}, string(os.PathSeparator))
goroot = os.Getenv("GOROOT")
iopath = strings.Join([]string{goroot, "src", "io"}, string(os.PathSeparator))
)
const customExpected = `
type Implementation struct{}
func (i *Implementation) Func() {
}
func (i *Implementation) ParamFunc(s *Struct) {
}
func (i *Implementation) ReturnFunc() string {
return ""
}
func (i *Implementation) MultiIntParam(i int, j int) {
}
func (i *Implementation) TupleFunc() (string, error) {
return "", nil
}
func (i *Implementation) MultiParam(s *Struct, reader io.Reader) {
}
func (i *Implementation) Combo(s *Struct, r io.Reader) (string, error) {
return "", nil
}
func (i *Implementation) FunctionParam(f func(int) (interface{}, error)) {
}
func (i *Implementation) FunctionParamAndReturn(f func(int) (interface{}, error)) (*Struct, error) {
return nil, nil
}
func (i *Implementation) AnonymousFuncs(f func(func(int, int) func()) func(int)) func(int) {
return nil
}
func (i *Implementation) PackageStruct(b *bytes.Buffer) *bytes.Reader {
return nil
}
func (i *Implementation) ValueFunc(s Struct) Struct {
return Struct{}
}
func (i *Implementation) SliceFunc(s []string) []string {
return nil
}
`
var (
m = make(map[string]string)
)
func init(){
m["TestInterface"] = "Implementation"
m["Embedded"] = "EmbedImplementation"
}
func TestInspect(t *testing.T) {
Convey("We can parse a file and inspect it for its interfaces", t, func(){
f, data, err := File(path + string(os.PathSeparator) + "data.go")
So(err, ShouldBeNil)
signatures := Inspect(f, data)
So(err, ShouldBeNil)
interfaces := Interfaces(signatures, m)
for _, i := range interfaces {
fmt.Println(i.String())
}
})
}
func TestLowerFirstLetter(t *testing.T) {
Convey("We can get the first letter of a variable", t, func(){
l := LowerFirstLetterOfVar(localStruct)
So(l, ShouldEqual, "s")
p := LowerFirstLetterOfVar(packageStruct)
So(p, ShouldEqual, "b")
p = LowerFirstLetterOfVar(primitive)
So(p, ShouldEqual, "s")
e := LowerFirstLetterOfVar(errType)
So(e, ShouldEqual, "e")
})
}
func TestInterface_Save(t *testing.T) {
Convey("We can test saving an interface as a file", t, func(){
f, data, err := File(path + string(os.PathSeparator) + "data.go")
So(err, ShouldBeNil)
signatures := Inspect(f, data)
So(err, ShouldBeNil)
interfaces := Interfaces(signatures, m)
for _, i := range interfaces {
fmt.Println(i.Name)
d, err := ioutil.TempDir("/tmp", "")
So(err, ShouldBeNil)
err = i.Save(d)
So(err, ShouldBeNil)
data, err := ioutil.ReadFile(d + string(os.PathSeparator) + i.Name + ".go")
So(err, ShouldBeNil)
index := strings.Index(string(data), "\n")
data = data[index:]
So(string(data), ShouldEqual, customExpected)
err = os.RemoveAll(d)
So(err, ShouldBeNil)
}
})
}
func TestIOPackage(t *testing.T) {
Convey("We can read the io interface declarations and implement them.", t, func(){
f, data, err := File(iopath + string(os.PathSeparator) + "io.go")
So(err, ShouldBeNil)
signatures := Inspect(f, data)
So(err, ShouldBeNil)
ma := make(map[string]string)
ma["ReadWriter"] = "MyReadWriter"
interfaces := Interfaces(signatures, ma)
for _, i := range interfaces {
fmt.Println(i.Name)
fmt.Println(i.Implement())
}
})
}