package tests_test import ( "os" "os/exec" "path/filepath" "strings" "testing" "git.akyoto.dev/cli/q/src/build" "git.akyoto.dev/cli/q/src/config" "git.akyoto.dev/go/assert" ) var programs = []struct { Name string ExitCode int }{ {"empty", 0}, {"assert", 1}, {"binary", 0}, {"octal", 0}, {"hexadecimal", 0}, {"variables", 0}, {"reassign", 0}, {"reuse", 0}, {"return", 0}, {"return-multi", 0}, {"math", 0}, {"precedence", 0}, {"operator-assign", 0}, {"const", 0}, {"escape-rune", 0}, {"escape-string", 0}, {"bitwise-and", 0}, {"bitwise-or", 0}, {"bitwise-xor", 0}, {"shift", 0}, {"modulo", 0}, {"modulo-assign", 0}, {"div-split", 0}, {"int64", 0}, {"negative", 0}, {"negation", 0}, {"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}, {"branch-save", 0}, {"jump-near", 0}, {"switch", 0}, {"loop-infinite", 0}, {"loop-lifetime", 0}, {"loop-for", 0}, {"memory-free", 0}, {"out-of-memory", 0}, {"index-static", 0}, {"index-dynamic", 0}, {"struct", 0}, {"len", 0}, } func TestPrograms(t *testing.T) { for _, test := range programs { file := filepath.Join("programs", test.Name+".q") t.Run(test.Name+"/debug", func(t *testing.T) { config.Optimize(false) run(t, file, "debug", "", "", test.ExitCode) }) t.Run(test.Name+"/release", func(t *testing.T) { config.Optimize(true) run(t, file, "release", "", "", test.ExitCode) }) } } func BenchmarkPrograms(b *testing.B) { for _, test := range programs { b.Run(test.Name, func(b *testing.B) { compiler := build.New(filepath.Join("programs", test.Name+".q")) for b.Loop() { _, err := compiler.Run() assert.Nil(b, err) } }) } } // run builds and runs the file to check if the output matches the expected output. func run(t *testing.T, path string, version string, input string, expectedOutput string, expectedExitCode int) { b := build.New(path) result, err := b.Run() assert.Nil(t, err) tmpDir := filepath.Join(os.TempDir(), "q", version) err = os.MkdirAll(tmpDir, 0755) assert.Nil(t, err) executable := b.Executable() executable = filepath.Join(tmpDir, filepath.Base(executable)) err = result.WriteFile(executable) assert.Nil(t, err) stat, err := os.Stat(executable) assert.Nil(t, err) assert.True(t, stat.Size() > 0) cmd := exec.Command(executable) cmd.Stdin = strings.NewReader(input) output, err := cmd.Output() exitCode := 0 if err != nil { exitError, ok := err.(*exec.ExitError) if !ok { t.Fatal(exitError) } exitCode = exitError.ExitCode() } assert.Equal(t, exitCode, expectedExitCode) assert.DeepEqual(t, string(output), expectedOutput) }