-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_test.go
117 lines (98 loc) · 2.41 KB
/
generator_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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/matryer/is"
)
type GeneratorTest struct {
name string
input string // input
want string // expected output
}
var tests = []GeneratorTest{
{"Args", args_in, args_out},
{"Results", results_in, results_out},
{"NamedResults", named_results_in, named_results_out},
{"Receiver", receiver_in, receiver_out},
{"PtrReceiver", ptr_receiver_in, ptr_receiver_out},
}
func TestGenerator(t *testing.T) {
is := is.New(t)
dir, err := ioutil.TempDir("", "muster")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)
for _, test := range tests {
is := is.NewRelaxed(t)
// Some setup
g := Generator{}
input := "package test\n" + test.input
file := test.name + ".go"
absFile := filepath.Join(dir, file)
is.NoErr(ioutil.WriteFile(absFile, []byte(input), 0644)) // create file without error please
// Run the generator
g.parsePackage([]string{absFile}, nil)
g.generate(test.name)
// Comparisons
got := string(g.format())
is.Equal(got, test.want)
}
}
const args_in = `func Args(str string, n int) error {
return nil
}`
const args_out = `// MustArgs is like Args except it panics if Args errors.
func MustArgs(str string, n int) {
err := Args(str, n)
if err != nil {
panic(err)
}
return
}`
const results_in = `func Results() (string, int, error) {
return "", 0, nil
}`
const results_out = `// MustResults is like Results except it panics if Results errors.
func MustResults() (string, int) {
r0, r1, err := Results()
if err != nil {
panic(err)
}
return r0, r1
}`
const named_results_in = `func NamedResults() (str string, n int, err error) {
return "", 0, nil
}`
const named_results_out = `// MustNamedResults is like NamedResults except it panics if NamedResults errors.
func MustNamedResults() (str string, n int) {
str, n, err := NamedResults()
if err != nil {
panic(err)
}
return str, n
}`
const receiver_in = `func (t T) Receiver() error {
return nil
}`
const receiver_out = `// MustReceiver is like Receiver except it panics if Receiver errors.
func (t T) MustReceiver() {
err := t.Receiver()
if err != nil {
panic(err)
}
return
}`
const ptr_receiver_in = `func (t *T) PtrReceiver() error {
return nil
}`
const ptr_receiver_out = `// MustPtrReceiver is like PtrReceiver except it panics if PtrReceiver errors.
func (t *T) MustPtrReceiver() {
err := t.PtrReceiver()
if err != nil {
panic(err)
}
return
}`