-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
79 lines (66 loc) · 1.67 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"bytes"
"interingo/repl"
"os"
"strings"
"testing"
)
func isIigFile(fullName string) (string, bool) {
splited := strings.Split(fullName, ".")
if len(splited) < 2 {
return "", false
}
extension := splited[len(splited)-1]
if extension != "iig" {
return "", false
}
fileName := strings.Join(splited[0:len(splited)-1], "")
return fileName, true
}
// Hack so that testing flag can be init first
var _ = func() bool {
testing.Init()
return true
}()
// This evaluating all `test/*.iig` file and compare it with the desier
// `test/result/*.out` file content
func TestMain(t *testing.T) {
f, err := os.Open("test")
if err != nil {
t.Errorf("Test directory read error, is it available, error code: %v\n", err)
}
files, err := f.Readdir(0)
if err != nil {
t.Errorf("Test directory read error, is it available, error code: %v\n", err)
}
for _, v := range files {
buf := new(bytes.Buffer)
if v.IsDir() {
continue
}
fileName, ok := isIigFile(v.Name())
if !ok {
continue
}
inputFileContent, err := os.ReadFile("test/" + v.Name())
if err != nil {
t.Errorf("File read error, error code: %v\n", err)
}
repl.Handle(string(inputFileContent), buf)
outputFileName := fileName + ".out"
outputFileContent, err := os.ReadFile("test/result/" + outputFileName)
if err != nil {
t.Errorf("File read error, recheck output file %s location\n", outputFileName)
}
for i, outByte := range outputFileContent {
b, err := buf.ReadByte()
if err != nil {
break
}
if outByte != b {
t.Errorf("Result of %s not match output file, expect \"%c\" at %d'th output but got \"%c\" instead", v.Name(), outByte, i, b)
}
}
}
}