130 lines
3.0 KiB
Go
Raw Normal View History

2024-07-03 11:39:24 +02:00
package tests_test
2024-06-29 21:40:14 +02:00
import (
"os"
"os/exec"
2024-07-03 11:39:24 +02:00
"path/filepath"
2024-07-18 21:10:27 +02:00
"strings"
2024-06-29 21:40:14 +02:00
"testing"
"git.akyoto.dev/cli/q/src/build"
2024-08-07 19:39:10 +02:00
"git.akyoto.dev/cli/q/src/config"
2024-06-29 21:40:14 +02:00
"git.akyoto.dev/go/assert"
)
2024-07-03 11:59:36 +02:00
var programs = []struct {
2024-07-18 21:10:27 +02:00
Name string
Input string
Output string
ExitCode int
2024-07-03 11:59:36 +02:00
}{
2024-07-18 21:10:27 +02:00
{"empty", "", "", 0},
2024-07-28 18:47:13 +02:00
{"assert", "", "", 1},
2024-07-30 15:46:47 +02:00
{"variables", "", "", 0},
2024-07-28 18:47:13 +02:00
{"reassign", "", "", 0},
2024-07-30 15:46:47 +02:00
{"reuse", "", "", 0},
2024-07-28 18:47:13 +02:00
{"return", "", "", 0},
2024-08-05 12:39:07 +02:00
{"return-multi", "", "", 0},
2024-07-31 17:50:31 +02:00
{"math", "", "", 0},
{"precedence", "", "", 0},
2024-08-02 12:55:25 +02:00
{"op-assign", "", "", 0},
{"binary", "", "", 0},
{"octal", "", "", 0},
{"hexadecimal", "", "", 0},
2025-01-24 22:47:28 +01:00
{"array", "", "", 0},
2024-07-31 17:50:31 +02:00
{"escape-rune", "", "", 0},
{"escape-string", "", "", 0},
2024-07-25 14:17:51 +02:00
{"bitwise-and", "", "", 0},
{"bitwise-or", "", "", 0},
{"bitwise-xor", "", "", 0},
2024-07-25 15:47:19 +02:00
{"shift", "", "", 0},
2024-07-28 18:12:42 +02:00
{"modulo", "", "", 0},
{"modulo-assign", "", "", 0},
{"div-split", "", "", 0},
2024-08-05 18:47:24 +02:00
{"int64", "", "", 0},
2024-07-28 21:09:42 +02:00
{"negative", "", "", 0},
{"negation", "", "", 0},
2024-07-28 18:47:13 +02:00
{"square-sum", "", "", 0},
{"chained-calls", "", "", 0},
{"nested-calls", "", "", 0},
{"param", "", "", 0},
{"param-multi", "", "", 0},
{"param-order", "", "", 0},
{"branch", "", "", 0},
{"branch-and", "", "", 0},
{"branch-or", "", "", 0},
{"branch-both", "", "", 0},
{"jump-near", "", "", 0},
2024-08-03 23:05:09 +02:00
{"switch", "", "", 0},
2024-07-28 18:47:13 +02:00
{"loop", "", "", 0},
{"loop-lifetime", "", "", 0},
2024-07-31 15:24:21 +02:00
{"out-of-memory", "", "", 0},
2024-07-04 12:32:56 +02:00
}
func TestPrograms(t *testing.T) {
for _, test := range programs {
2024-07-29 18:03:15 +02:00
file := filepath.Join("programs", test.Name+".q")
2024-07-29 14:44:16 +02:00
2024-07-29 18:03:15 +02:00
t.Run(test.Name+"/debug", func(t *testing.T) {
config.ConstantFold = false
2024-08-14 13:41:22 +02:00
run(t, file, "debug", test.Input, test.Output, test.ExitCode)
2024-07-29 18:03:15 +02:00
})
2024-07-29 14:44:16 +02:00
2024-07-29 18:03:15 +02:00
t.Run(test.Name+"/release", func(t *testing.T) {
config.ConstantFold = true
2024-08-14 13:41:22 +02:00
run(t, file, "release", test.Input, test.Output, test.ExitCode)
2024-07-29 14:44:16 +02:00
})
}
2024-07-03 11:59:36 +02:00
}
func BenchmarkPrograms(b *testing.B) {
for _, test := range programs {
b.Run(test.Name, func(b *testing.B) {
2024-07-09 17:00:04 +02:00
compiler := build.New(filepath.Join("programs", test.Name+".q"))
2024-07-03 11:59:36 +02:00
2025-01-30 22:23:38 +01:00
for range b.N {
2024-07-03 11:59:36 +02:00
_, err := compiler.Run()
assert.Nil(b, err)
}
})
2024-06-29 21:40:14 +02:00
}
2024-07-03 11:59:36 +02:00
}
2024-06-29 21:40:14 +02:00
2024-07-03 11:39:24 +02:00
// run builds and runs the file to check if the output matches the expected output.
2024-08-14 13:41:22 +02:00
func run(t *testing.T, path string, version string, input string, expectedOutput string, expectedExitCode int) {
2024-07-29 18:03:15 +02:00
b := build.New(path)
2024-07-09 12:13:59 +02:00
result, err := b.Run()
assert.Nil(t, err)
2024-06-29 21:40:14 +02:00
2024-08-14 13:41:22 +02:00
tmpDir := filepath.Join(os.TempDir(), "q", version)
err = os.MkdirAll(tmpDir, 0755)
2024-07-29 18:03:15 +02:00
assert.Nil(t, err)
2024-08-14 13:41:22 +02:00
executable := b.Executable()
executable = filepath.Join(tmpDir, filepath.Base(executable))
2024-07-29 18:03:15 +02:00
err = result.WriteFile(executable)
2024-07-09 12:13:59 +02:00
assert.Nil(t, err)
2024-06-29 21:40:14 +02:00
2024-07-29 18:03:15 +02:00
stat, err := os.Stat(executable)
2024-07-09 12:13:59 +02:00
assert.Nil(t, err)
assert.True(t, stat.Size() > 0)
2024-06-29 21:40:14 +02:00
2024-07-29 18:03:15 +02:00
cmd := exec.Command(executable)
2024-07-18 21:10:27 +02:00
cmd.Stdin = strings.NewReader(input)
2024-07-09 12:13:59 +02:00
output, err := cmd.Output()
exitCode := 0
2024-06-29 21:40:14 +02:00
2024-07-09 12:13:59 +02:00
if err != nil {
exitError, ok := err.(*exec.ExitError)
2024-06-29 21:40:14 +02:00
2024-07-09 12:13:59 +02:00
if !ok {
t.Fatal(exitError)
2024-06-29 21:40:14 +02:00
}
2024-07-09 12:13:59 +02:00
exitCode = exitError.ExitCode()
}
assert.Equal(t, exitCode, expectedExitCode)
assert.DeepEqual(t, string(output), expectedOutput)
2024-06-29 21:40:14 +02:00
}