-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
58 lines (53 loc) · 1.19 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
package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/mibk/hawk/compiler"
)
func TestRun(t *testing.T) {
files, err := filepath.Glob("testdata/*.hawk")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
name := strings.TrimSuffix(filepath.Base(file), ".hawk")
t.Run(name, func(t *testing.T) {
f, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
prog, err := compiler.Compile(file, f)
if err != nil {
t.Errorf("%s.hawk:%v", name, err)
return
}
var out bytes.Buffer
fin, err := os.Open(strings.TrimSuffix(file, ".hawk") + ".in")
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
if err := prog.Run(&out, fin); err != nil {
t.Errorf("unexpected runtime err: %v", err)
return
}
var want []byte
fout, err := os.Open(strings.TrimSuffix(file, ".hawk") + ".out")
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
if fout != nil {
want, err = ioutil.ReadAll(fout)
if err != nil {
t.Fatal(err)
}
}
if got := out.Bytes(); !bytes.Equal(got, want) {
t.Errorf("%s.out:\n got:\n`%s`\nwant:\n`%s`", name, string(got), string(want))
}
})
}
}