package tests_test import ( "os" "os/exec" "path/filepath" "strings" "testing" "git.akyoto.dev/cli/q/src/build" "git.akyoto.dev/cli/q/src/build/config" "git.akyoto.dev/go/assert" ) var programs = []struct { Name string Input string Output string ExitCode int }{ {"empty", "", "", 0}, {"assert", "", "", 1}, {"variables", "", "", 0}, {"reassign", "", "", 0}, {"reuse", "", "", 0}, {"return", "", "", 0}, {"math", "", "", 0}, {"precedence", "", "", 0}, {"op-assign", "", "", 0}, {"binary", "", "", 0}, {"octal", "", "", 0}, {"hexadecimal", "", "", 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}, {"64-bit", "", "", 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}, {"jump-near", "", "", 0}, {"loop", "", "", 0}, {"loop-lifetime", "", "", 0}, {"out-of-memory", "", "", 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.ConstantFold = false run(t, file, "debug", test.Name, test.Input, test.Output, test.ExitCode) }) t.Run(test.Name+"/release", func(t *testing.T) { config.ConstantFold = true run(t, file, "release", test.Name, test.Input, test.Output, 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 i := 0; i < b.N; i++ { _, 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, name string, input string, expectedOutput string, expectedExitCode int) { b := build.New(path) result, err := b.Run() assert.Nil(t, err) directory := filepath.Join(os.TempDir(), "q", version) err = os.MkdirAll(directory, 0755) assert.Nil(t, err) executable := filepath.Join(directory, name) 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) }