91 lines
1.9 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-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 {
Name string
ExpectedOutput string
ExpectedExitCode int
}{
{"empty.q", "", 0},
2024-07-06 11:06:39 +00:00
{"math.q", "", 10},
2024-07-05 08:10:09 +00:00
{"precedence.q", "", 10},
{"square-sum.q", "", 25},
2024-07-05 09:56:57 +00:00
{"chained-calls.q", "", 9},
{"nested-calls.q", "", 4},
2024-07-09 09:43:33 +00:00
{"overwrite.q", "", 3},
2024-07-09 08:39:29 +00:00
{"reuse.q", "", 3},
2024-07-05 14:31:33 +00:00
{"return.q", "", 6},
2024-07-06 13:20:52 +00:00
{"reassign.q", "", 2},
2024-07-08 11:38:44 +00:00
{"branch.q", "", 0},
{"branch-and.q", "", 0},
{"branch-or.q", "", 0},
{"branch-combined.q", "", 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) {
run(t, filepath.Join("programs", test.Name), test.ExpectedOutput, test.ExpectedExitCode)
})
}
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) {
compiler := build.New(filepath.Join("programs", test.Name))
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.
func run(t *testing.T, name string, expectedOutput string, expectedExitCode int) {
b := build.New(name)
2024-06-29 19:40:14 +00:00
assert.True(t, len(b.Executable()) > 0)
t.Run("Compile", func(t *testing.T) {
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)
})
t.Run("Output", func(t *testing.T) {
cmd := exec.Command(b.Executable())
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)
})
}