forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
151 lines (135 loc) · 4.11 KB
/
testing.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
145
146
147
148
149
150
151
package codegen
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"goa.design/goa/eval"
"goa.design/goa/expr"
"github.com/sergi/go-diff/diffmatchpatch"
)
// RunDSL returns the DSL root resulting from running the given DSL.
func RunDSL(t *testing.T, dsl func()) *expr.RootExpr {
t.Helper()
eval.Reset()
expr.Root = new(expr.RootExpr)
expr.Root.GeneratedTypes = &expr.GeneratedRoot{}
eval.Register(expr.Root)
eval.Register(expr.Root.GeneratedTypes)
expr.Root.API = expr.NewAPIExpr("test api", func() {})
expr.Root.API.Servers = []*expr.ServerExpr{expr.Root.API.DefaultServer()}
if !eval.Execute(dsl, nil) {
t.Fatal(eval.Context.Error())
}
if err := eval.RunDSL(); err != nil {
t.Fatal(err)
}
return expr.Root
}
// RunDSLWithFunc returns the DSL root resulting from running the given DSL.
// It executes a function to add any top-level types to the design Root before
// running the DSL.
func RunDSLWithFunc(t *testing.T, dsl func(), fn func()) *expr.RootExpr {
t.Helper()
eval.Reset()
expr.Root = new(expr.RootExpr)
expr.Root.GeneratedTypes = &expr.GeneratedRoot{}
eval.Register(expr.Root)
eval.Register(expr.Root.GeneratedTypes)
expr.Root.API = expr.NewAPIExpr("test api", func() {})
expr.Root.API.Servers = []*expr.ServerExpr{expr.Root.API.DefaultServer()}
fn()
if !eval.Execute(dsl, nil) {
t.Fatal(eval.Context.Error())
}
if err := eval.RunDSL(); err != nil {
t.Fatal(err)
}
return expr.Root
}
// SectionCode generates and formats the code for the given section.
func SectionCode(t *testing.T, section *SectionTemplate) string {
return sectionCodeWithPrefix(t, section, "package foo\n")
}
// SectionsCode generates and formats the code for the given sections.
func SectionsCode(t *testing.T, sections []*SectionTemplate) string {
codes := make([]string, len(sections))
for i, section := range sections {
codes[i] = sectionCodeWithPrefix(t, section, "package foo\n")
}
return strings.Join(codes, "\n")
}
// SectionCodeFromImportsAndMethods generates and formats the code for given import and method definition sections.
func SectionCodeFromImportsAndMethods(t *testing.T, importSection *SectionTemplate, methodSection *SectionTemplate) string {
t.Helper()
var code bytes.Buffer
if err := importSection.Write(&code); err != nil {
t.Fatal(err)
}
return sectionCodeWithPrefix(t, methodSection, code.String())
}
func sectionCodeWithPrefix(t *testing.T, section *SectionTemplate, prefix string) string {
var code bytes.Buffer
if err := section.Write(&code); err != nil {
t.Fatal(err)
}
codestr := code.String()
if len(prefix) > 0 {
codestr = fmt.Sprintf("%s\n%s", prefix, codestr)
}
return FormatTestCode(t, codestr)
}
// FormatTestCode formats the given Go code. The code must correspond to the
// content of a valid Go source file (i.e. start with "package")
func FormatTestCode(t *testing.T, code string) string {
t.Helper()
tmp := CreateTempFile(t, code)
defer os.Remove(tmp)
if err := finalizeGoSource(tmp); err != nil {
t.Fatal(err)
}
content, err := ioutil.ReadFile(tmp)
if err != nil {
t.Fatal(err)
}
return strings.Join(strings.Split(string(content), "\n")[2:], "\n")
}
// Diff returns a diff between s1 and s2. It uses the diff tool if installed
// otherwise degrades to using the dmp package.
func Diff(t *testing.T, s1, s2 string) string {
_, err := exec.LookPath("diff")
supportsDiff := (err == nil)
if !supportsDiff {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(s1, s2, false)
return dmp.DiffPrettyText(diffs)
}
left := CreateTempFile(t, s1)
right := CreateTempFile(t, s2)
defer os.Remove(left)
defer os.Remove(right)
cmd := exec.Command("diff", left, right)
diffb, _ := cmd.CombinedOutput()
return strings.Replace(string(diffb), "\t", " ␉ ", -1)
}
// CreateTempFile creates a temporary file and writes the given content.
// It is used only for testing.
func CreateTempFile(t *testing.T, content string) string {
t.Helper()
f, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
_, err = f.WriteString(content)
if err != nil {
os.Remove(f.Name())
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
return f.Name()
}