-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
144 lines (138 loc) · 3.11 KB
/
main_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
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
package main
import (
"bytes"
"log"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestMacro(t *testing.T) {
// Setup start
testDir := filepath.Join(os.TempDir(), "gpp-test-macro")
goPathTest := filepath.Join(testDir, "go")
moduleName, err := getModuleName(".")
if err != nil {
t.Fatalf("getModuleName error %+v", err)
}
src := filepath.Join(goPathTest, "src", moduleName)
// clean before running
os.RemoveAll(src)
err = os.MkdirAll(src, 0700)
if err != nil {
t.Fatalf("MkdirAll error %+v", err)
}
cmd := exec.Command("cp", "-r", ".", src)
err = cmd.Run()
if err != nil {
t.Fatalf("cp error %+v", err)
}
curDir, err := os.Getwd()
if err != nil {
log.Fatalf("getwd error %+v", err)
}
err = os.Chdir(src)
if err != nil {
log.Fatalf("chdir %+v", err)
}
// Setup end
defer func() {
err = os.Chdir(curDir)
if err != nil {
log.Fatalf("chdir %+v", err)
}
if !t.Failed() {
// let check directory on fail
os.RemoveAll(src)
}
}()
cases := []struct {
desc string
srcDir string
output string
err error
}{
{
desc: "Test NewSeq M/F/R fluent api",
srcDir: filepath.Join(src, "testdata", "newseq"),
output: `
NewSeq Map/Filter [{strLen:3} {strLen:4}]
NewSeq res [2] sum even 12 mult even 48
`,
err: nil,
},
{
desc: "Test try_μ",
srcDir: filepath.Join(src, "testdata", "try"),
output: `
(result, err) = (1, fErr: fErr error)
(result, err) = (1, <nil>)
`,
err: nil,
},
{
desc: "Test log_μ",
srcDir: filepath.Join(src, "testdata", "log"),
output: `
/main.go:16 result before result=0
/main.go:18 result after result=10
/main.go:22 err, slice and index err=<nil> a=[][2]int{[2]int{1, 2}} a[0]=[2]int{1, 2}
/main.go:23 func calls sl(10)[0]=10 strr('hello')="hello"
/lib/lib.go:8 LogLibFunc val=20
/main.go:25 lib calls lib.LogLibFuncA(20)=20
`,
err: nil,
},
}
var buf bytes.Buffer
for i, tc := range cases {
buf.Reset()
err = parseDir(tc.srcDir, moduleName, nil)
if isUnexpectedErr(t, i, tc.desc, tc.err, err) {
continue
}
err = os.Chdir(tc.srcDir)
if err != nil {
log.Fatalf("chdir %+v", err)
}
cmd = exec.Command("go", "build", "main.go")
cmd.Env = append(os.Environ(), "GOPATH="+goPathTest)
cmd.Stdout = &buf
cmd.Stderr = &buf
err = cmd.Run()
output := buf.String()
if isUnexpectedErr(t, i, tc.desc, nil, err) {
t.Errorf("cmd args %v\n%s", cmd.Args, output)
continue
}
buf.Reset()
// run binary
cmd = exec.Command("./main")
cmd.Stdout = &buf
cmd.Stderr = &buf
err = cmd.Run()
output = buf.String()
if isUnexpectedErr(t, i, tc.desc, nil, err) {
t.Errorf("cmd args %v\n%s", cmd.Args, output)
continue
}
if output != tc.output {
t.Errorf("case [%d] %s\nexpected %s, got %s", i, tc.desc, tc.output, output)
}
}
}
func isUnexpectedErr(t *testing.T, caseID int, desc string, expectedErr, goterr error) bool {
t.Helper()
var eStr, gotStr string
if expectedErr != nil {
eStr = expectedErr.Error()
}
if goterr != nil {
gotStr = goterr.Error()
}
if eStr != gotStr {
t.Errorf("case [%d] %s\nexpected error \"%s\"\ngot \"%s\"", caseID, desc, eStr, gotStr)
return true
}
return false
}