/
main_test.go
49 lines (37 loc) · 987 Bytes
/
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
package main
import (
"io/ioutil"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/uiureo/jack/parser"
"github.com/uiureo/jack/tokenizer"
)
const toolPath = "/Users/zat/Downloads/nand2tetris/tools/TextComparer.sh"
func TestMain(t *testing.T) {
files, err := filepath.Glob("fixtures/*.jack")
if err != nil {
panic(err)
}
for _, jackFile := range files {
testMainOutput(t, jackFile)
}
}
func testMainOutput(t *testing.T, jackFile string) {
xmlFile := regexp.MustCompile(`\.jack$`).ReplaceAllString(jackFile, ".xml")
name := strings.Split(filepath.Base(jackFile), ".")[0]
code, err := ioutil.ReadFile(jackFile)
if err != nil {
t.Error(err)
return
}
parserOutput := parser.Parse(tokenizer.Tokenize(string(code))).ToXML()
file, _ := ioutil.TempFile("", "")
file.Write([]byte(parserOutput))
output, err := exec.Command(toolPath, file.Name(), xmlFile).CombinedOutput()
if err != nil {
t.Errorf("%s: %s %v", name, output, err)
}
}