65 lines
1.3 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:39:24 +00:00
func TestPrograms(t *testing.T) {
var tests = []struct {
2024-06-29 19:40:14 +00:00
Name string
ExpectedOutput string
ExpectedExitCode int
}{
2024-07-03 09:39:24 +00:00
{"successive-calls.q", "", 9},
2024-06-29 19:40:14 +00:00
}
2024-07-03 09:39:24 +00:00
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
run(t, filepath.Join("programs", test.Name), test.ExpectedOutput, test.ExpectedExitCode)
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)
})
}