-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
74 lines (69 loc) · 1.98 KB
/
main_test.go
File metadata and controls
74 lines (69 loc) · 1.98 KB
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
package nanocc
import (
"bufio"
"bytes"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func getOutput(f func()) []byte {
buf := new(bytes.Buffer)
out = buf
f()
out = os.Stdout
return buf.Bytes()
}
func outputEqual(t *testing.T, expected, actual io.Reader) {
addr := regexp.MustCompile(`(^( {4}|\d+> )(IMM|JMP|BZ|BNZ|JSR) +)(\d{2,12}|4|8)`)
expScan := bufio.NewScanner(expected)
actScan := bufio.NewScanner(actual)
line := 0
for expScan.Scan() && actScan.Scan() {
line++
expLine := addr.ReplaceAllString(expScan.Text(), "$1<addr>")
actLine := addr.ReplaceAllString(actScan.Text(), "$1<addr>")
require.Equal(t, expLine, actLine, "line : %d", line)
}
require.True(t, expScan.Scan() == actScan.Scan())
}
func Test_main(t *testing.T) {
tests := []struct {
file string
args string
}{
{"hello.c(comp)", "-s -d testdata/hello.c"},
{"hello.c(run)", "-d testdata/hello.c"},
{"c4.c(comp)", "-s -d testdata/c4.c"},
{"c4.c(run)", "-d testdata/c4.c"},
{"c4.c_hello.c(run)", "-d testdata/c4.c testdata/hello.c"},
{"c4.c_-d_hello.c(run)", "testdata/c4.c -d testdata/hello.c"},
{"c4.c_c4.c_hello.c(run)", "testdata/c4.c testdata/c4.c testdata/hello.c"},
{"arginc.c(comp)", "-s -d testdata/arginc.c"},
{"arginc.c_1_2(run)", "-d testdata/arginc.c 1 2"},
{"fib.c(comp)", "-s -d testdata/fib.c"},
{"fib.c_5(run)", "-d testdata/fib.c 5"},
{"inc.c(comp)", "-s -d testdata/inc.c"},
{"inc.c_1_2(run)", "-d testdata/inc.c 1 2"},
{"while.c(comp)", "-s -d testdata/while.c"},
{"while.c(run)", "-d testdata/while.c"},
}
for _, tt := range tests {
t.Run(tt.file, func(t *testing.T) {
file, err := os.Open(filepath.Join("testdata", tt.file+".txt"))
require.NoError(t, err)
defer file.Close()
src = false
debug = false
os.Args = strings.Split("nanocc "+tt.args, " ")
var output []byte
require.NotPanics(t, func() {
output = getOutput(main)
})
outputEqual(t, file, bytes.NewReader(output))
})
}
}