94 lines
2.0 KiB
Go
Raw Normal View History

2024-07-03 09:39:24 +00:00
package tests_test
2024-06-29 19:40:14 +00:00
import (
"os"
"os/exec"
2024-07-03 09:39:24 +00:00
"path/filepath"
2024-07-18 19:10:27 +00:00
"strings"
2024-06-29 19:40:14 +00:00
"testing"
"git.akyoto.dev/cli/q/src/build"
"git.akyoto.dev/go/assert"
)
2024-07-03 09:59:36 +00:00
var programs = []struct {
2024-07-18 19:10:27 +00:00
Name string
Input string
Output string
ExitCode int
2024-07-03 09:59:36 +00:00
}{
2024-07-18 19:10:27 +00:00
{"empty", "", "", 0},
{"math", "", "", 10},
{"precedence", "", "", 10},
{"square-sum", "", "", 25},
{"chained-calls", "", "", 9},
{"nested-calls", "", "", 4},
{"param", "", "", 3},
{"param-multi", "", "", 21},
{"reuse", "", "", 3},
{"return", "", "", 6},
{"reassign", "", "", 2},
{"branch", "", "", 0},
{"branch-and", "", "", 0},
{"branch-or", "", "", 0},
{"branch-both", "", "", 0},
{"jump-near", "", "", 0},
{"loop", "", "", 0},
{"loop-lifetime", "", "", 0},
2024-07-04 10:32:56 +00:00
}
func TestPrograms(t *testing.T) {
for _, test := range programs {
t.Run(test.Name, func(t *testing.T) {
2024-07-18 19:10:27 +00:00
run(t, filepath.Join("programs", test.Name+".q"), test.Input, test.Output, test.ExitCode)
2024-07-04 10:32:56 +00:00
})
}
2024-07-03 09:59:36 +00:00
}
func BenchmarkPrograms(b *testing.B) {
for _, test := range programs {
b.Run(test.Name, func(b *testing.B) {
2024-07-09 15:00:04 +00:00
compiler := build.New(filepath.Join("programs", test.Name+".q"))
2024-07-03 09:59:36 +00:00
for i := 0; i < b.N; i++ {
_, err := compiler.Run()
assert.Nil(b, err)
}
})
2024-06-29 19:40:14 +00:00
}
2024-07-03 09:59:36 +00:00
}
2024-06-29 19:40:14 +00:00
2024-07-03 09:39:24 +00:00
// run builds and runs the file to check if the output matches the expected output.
2024-07-18 19:10:27 +00:00
func run(t *testing.T, name string, input string, expectedOutput string, expectedExitCode int) {
2024-07-03 09:39:24 +00:00
b := build.New(name)
2024-06-29 19:40:14 +00:00
assert.True(t, len(b.Executable()) > 0)
2024-07-09 10:13:59 +00:00
result, err := b.Run()
assert.Nil(t, err)
2024-06-29 19:40:14 +00:00
2024-07-09 10:13:59 +00:00
err = result.Write(b.Executable())
assert.Nil(t, err)
2024-06-29 19:40:14 +00:00
2024-07-09 10:13:59 +00:00
stat, err := os.Stat(b.Executable())
assert.Nil(t, err)
assert.True(t, stat.Size() > 0)
2024-06-29 19:40:14 +00:00
2024-07-09 10:13:59 +00:00
cmd := exec.Command(b.Executable())
2024-07-18 19:10:27 +00:00
cmd.Stdin = strings.NewReader(input)
2024-07-09 10:13:59 +00:00
output, err := cmd.Output()
exitCode := 0
2024-06-29 19:40:14 +00:00
2024-07-09 10:13:59 +00:00
if err != nil {
exitError, ok := err.(*exec.ExitError)
2024-06-29 19:40:14 +00:00
2024-07-09 10:13:59 +00:00
if !ok {
t.Fatal(exitError)
2024-06-29 19:40:14 +00:00
}
2024-07-09 10:13:59 +00:00
exitCode = exitError.ExitCode()
}
assert.Equal(t, exitCode, expectedExitCode)
assert.DeepEqual(t, string(output), expectedOutput)
2024-06-29 19:40:14 +00:00
}