forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
103 lines (89 loc) · 2.27 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
package expr
import (
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
"goa.design/goa/eval"
)
// RunDSL returns the DSL root resulting from running the given DSL.
// Used only in tests.
func RunDSL(t *testing.T, dsl func()) *RootExpr {
t.Helper()
setupDSLRun()
// run DSL (first pass)
if !eval.Execute(dsl, nil) {
t.Fatal(eval.Context.Error())
}
// run DSL (second pass)
if err := eval.RunDSL(); err != nil {
t.Fatal(err)
}
// return generated root
return Root
}
// RunInvalidDSL returns the error resulting from running the given DSL.
// It is used only in tests.
func RunInvalidDSL(t *testing.T, dsl func()) error {
t.Helper()
setupDSLRun()
// run DSL (first pass)
if !eval.Execute(dsl, nil) {
return eval.Context.Errors
}
// run DSL (second pass)
if err := eval.RunDSL(); err != nil {
return err
}
// expected an error - didn't get one
t.Fatal("expected a DSL evaluation error - got none")
return nil
}
// 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()
}
func setupDSLRun() {
// reset all roots and codegen data structures
eval.Reset()
Root = new(RootExpr)
Root.GeneratedTypes = &GeneratedRoot{}
eval.Register(Root)
eval.Register(Root.GeneratedTypes)
Root.API = NewAPIExpr("test api", func() {})
Root.API.Servers = []*ServerExpr{Root.API.DefaultServer()}
}