-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
60 lines (51 loc) · 1.25 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 (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)
func TestRocketlangCode(t *testing.T) {
origStdout := os.Stdout
defer func() {
os.Stdout = origStdout
}()
testDir := "tests"
matches, err := fs.Glob(os.DirFS(testDir), "*.rl")
if err != nil {
t.Errorf("failed to read test dir 'tests/': %s", err)
}
for _, match := range matches {
filename := filepath.Join(testDir, match)
rlCode, err := os.ReadFile(filename)
if err != nil {
t.Errorf("%s: %s", match, err)
continue
}
expectedStdout, err := os.ReadFile(strings.ReplaceAll(filename, ".rl", ".expected"))
if err != nil {
t.Errorf("%s: %s", match, err)
continue
}
fakeStdout, err := os.CreateTemp("", strings.TrimSuffix(match, ".rl"))
if err != nil {
t.Errorf("%s: %s", match, err)
continue
}
defer os.Remove(fakeStdout.Name())
os.Stdout = fakeStdout
runProgram(string(rlCode), "test")
os.Stdout = origStdout
resultStdout, err := os.ReadFile(fakeStdout.Name())
if err != nil {
t.Errorf("%s: %s", match, err)
continue
}
if string(resultStdout) != string(expectedStdout) {
fmt.Printf("--- stdout ---\n%s--- expected ---\n%s", resultStdout, expectedStdout)
t.Errorf("%s: stdout does not match expected", match)
}
}
}