-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgoimpl_test.go
More file actions
118 lines (101 loc) · 2.71 KB
/
goimpl_test.go
File metadata and controls
118 lines (101 loc) · 2.71 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
package goimpl
import (
"bytes"
"errors"
"fmt"
"io"
"net/rpc"
"reflect"
"strings"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
)
func TestMethods(t *testing.T) {
tc := []struct {
opts GenOpts
expected string
shouldError bool
}{
{
opts: GenOpts{
PkgName: "pkg",
ImplName: "*Impl",
Inter: reflect.TypeOf((*io.ReadCloser)(nil)).Elem(),
NoNamedReturnValues: true,
},
expected: `package pkg
import (
"errors"
)
type Impl struct{}
func (i *Impl) Close() error {
panic(errors.New("*Impl.Close not implemented"))
}
func (i *Impl) Read(u []uint8) (int, error) {
panic(errors.New("*Impl.Read not implemented"))
}
`,
}, {
opts: GenOpts{
Existing: AlmostClientCodec{},
Inter: reflect.TypeOf((*rpc.ClientCodec)(nil)).Elem(),
},
expected: strings.Replace(`package goimpl
import (
"errors"
"net/rpc"
)
type AlmostClientCodec struct{}
// number of inputs: had 1, want 0
func (a AlmostClientCodec) Close() (err error) {
panic(errors.New("AlmostClientCodec.Close not implemented"))
}
func (a AlmostClientCodec) ReadResponseBody(i interface{}) (err error) {
panic(errors.New("AlmostClientCodec.ReadResponseBody not implemented"))
}
// inputs[0]: had 'interface {}' want '*rpc.Request'; inputs[1]: had '*rpc.Request' want 'interface {}'
func (a AlmostClientCodec) WriteRequest(r *rpc.Request, i interface{}) (err error) {
panic(errors.New("AlmostClientCodec.WriteRequest not implemented"))
}
`, "'", "`", -1),
},
{
opts: GenOpts{
PkgName: "pkg",
ImplName: "-bad name",
Inter: reflect.TypeOf((*fmt.Stringer)(nil)).Elem(),
},
shouldError: true,
},
}
for _, c := range tc {
var buf bytes.Buffer
err := Generate(&c.opts, &buf)
gen := buf.String()
if gen != c.expected {
d := diffmatchpatch.New()
diff := d.DiffToDelta(d.DiffMain(gen, c.expected, false))
t.Errorf("opts: %#v. expected:\n%s\n-----\nGot:\n%s\n-----\nDiff:\n%v", c.opts, c.expected, gen, diff)
}
if c.shouldError && err == nil || !c.shouldError && err != nil {
t.Errorf("opts: %#v. err: %v", c.opts, err)
}
}
}
type AlmostClientCodec struct{}
// Wrong number of inputs.
func (a AlmostClientCodec) Close(int) error {
panic(errors.New("AlmostClientCodec.Close not implemented"))
}
// missing.
// func (a AlmostClientCodec) ReadResponseBody(interface{}) error {
// panic(errors.New("Not implemented"))
// }
// As it should be.
func (a AlmostClientCodec) ReadResponseHeader(*rpc.Response) error {
panic(errors.New("AlmostClientCodec.ReadResponseHeader not implemented"))
}
// Inputs swapped.
func (a AlmostClientCodec) WriteRequest(interface{}, *rpc.Request) error {
panic(errors.New("AlmostClientCodec.WriteReque not implemented"))
}