-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
60 lines (49 loc) · 1.47 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
59
60
package main
import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
)
func TestBentoFiles(t *testing.T) {
dir := "tests/"
fileInfos, err := ioutil.ReadDir(dir)
require.NoError(t, err)
for _, fileInfo := range fileInfos {
// This is useful for debugging a single file, so i'll leave it
// commented out.
//if fileInfo.Name() != "number.bento" {
// continue
//}
if !strings.HasSuffix(fileInfo.Name(), ".bento") {
continue
}
t.Run(fileInfo.Name(), func(t *testing.T) {
file, err := os.Open(dir + fileInfo.Name())
require.NoError(t, err)
parser := NewParser(file)
program, err := parser.Parse()
require.NoError(t, err)
compiler := NewCompiler(program)
compiledProgram := compiler.Compile()
vm := NewVirtualMachine(compiledProgram)
vm.out = bytes.NewBuffer(nil)
err = vm.Run()
require.NoError(t, err)
// There can be a specific expectation file depending on the OS.
expectedFilePath := dir + strings.Replace(fileInfo.Name(), ".bento", "."+runtime.GOOS+".txt", -1)
expectedData, err := ioutil.ReadFile(expectedFilePath)
if err != nil {
// Fallback to generic expectation file.
expectedFilePath = dir + strings.Replace(fileInfo.Name(), ".bento", ".txt", -1)
expectedData, err = ioutil.ReadFile(expectedFilePath)
require.NoError(t, err)
}
assert.Equal(t, string(expectedData), vm.out.(*bytes.Buffer).String())
})
}
}