package tests_test import ( "os" "os/exec" "path/filepath" "strings" "testing" "git.akyoto.dev/cli/q/src/build" "git.akyoto.dev/go/assert" ) var programs = []struct { Name string Input string Output string ExitCode int }{ {"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}, {"remainder", "", "", 0}, } func TestPrograms(t *testing.T) { for _, test := range programs { t.Run(test.Name, func(t *testing.T) { run(t, filepath.Join("programs", test.Name+".q"), 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, name string, input string, expectedOutput string, expectedExitCode int) { b := build.New(name) assert.True(t, len(b.Executable()) > 0) result, err := b.Run() assert.Nil(t, err) err = result.Write(b.Executable()) assert.Nil(t, err) stat, err := os.Stat(b.Executable()) assert.Nil(t, err) assert.True(t, stat.Size() > 0) cmd := exec.Command(b.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) }