-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.go
129 lines (113 loc) · 2.54 KB
/
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
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
var update = flag.Bool("update", false, "update golden files")
func main() {
flag.Parse()
for d, args := range map[string][]string{
"typecheck": {"-typecheck"},
"parse": {"-parse"},
"eval": {},
} {
n, err := run(d, args)
if err != nil {
fmt.Fprintf(os.Stderr, "Test failed: %v\n", err)
switch x := err.(type) {
case *Error:
fmt.Fprint(os.Stderr, x.stderr)
}
os.Exit(1)
}
fmt.Printf("Test(%s) succeeded: %d cases passed\n", d, n)
}
if err := check_parser_conflicts(); err != nil {
fmt.Fprintf(os.Stderr, "Test failed: %v\n", err)
switch x := err.(type) {
case *Error:
fmt.Fprint(os.Stderr, x.stderr)
}
os.Exit(1)
}
fmt.Println("Test(grammar) succeeded: unambiguous grammar")
}
type Error struct {
err error
stderr string
filename string
}
func (e *Error) Error() string {
if e.filename != "" {
return fmt.Sprintf("%s: %s", e.filename, e.err.Error())
}
return e.err.Error()
}
func run(d string, args []string) (n int, err error) {
pwd, err := os.Getwd()
if err != nil {
return n, err
}
dir := filepath.Join("tests", d)
f, err := os.Open(dir)
if err != nil {
return n, err
}
defer f.Close()
names, err := f.Readdirnames(0)
if err != nil {
return n, err
}
for _, name := range names {
if !strings.HasSuffix(name, ".bml") {
continue
}
var buf bytes.Buffer
var output bytes.Buffer
cmd := exec.Command("./bright-ml", append(args, filepath.Join(dir, name))...)
cmd.Stderr = &buf
cmd.Stdout = &output
cmd.Env = append(os.Environ(), "BML_PATH="+pwd)
if err := cmd.Run(); err != nil {
return n, &Error{err: err, filename: name, stderr: buf.String()}
}
if d == "eval" {
golden := filepath.Join(dir, strings.ReplaceAll(name, ".bml", ".golden"))
if *update {
err := ioutil.WriteFile(golden, output.Bytes(), 0644)
if err != nil {
return n, err
}
}
expected, err := ioutil.ReadFile(golden)
if err != nil {
return n, err
}
got := output.String()
if got != string(expected) {
return n, fmt.Errorf("%s: evaluation result differs from the golden file", filepath.Join(dir, name))
}
}
n++
}
return n, nil
}
func check_parser_conflicts() error {
word := "conflict"
cmd := exec.Command("make", "-B", "make_parser.sml")
bs, err := cmd.Output()
if err != nil {
return err
}
if strings.Contains(string(bs), word) {
return &Error{err: errors.New("ambiguous grammar"), stderr: string(bs)}
}
return nil
}