This commit is contained in:
parent
5fd38282b6
commit
7a294061d6
3 changed files with 78 additions and 50 deletions
|
@ -1,31 +1,21 @@
|
||||||
package tests_test
|
package tests_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.urbach.dev/cli/q/src/build"
|
"git.urbach.dev/cli/q/src/build"
|
||||||
"git.urbach.dev/cli/q/src/compiler"
|
"git.urbach.dev/cli/q/src/compiler"
|
||||||
"git.urbach.dev/cli/q/src/linker"
|
|
||||||
"git.urbach.dev/go/assert"
|
"git.urbach.dev/go/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var examples = []struct {
|
var examples = []testRun{
|
||||||
Name string
|
|
||||||
Input string
|
|
||||||
Output string
|
|
||||||
ExitCode int
|
|
||||||
}{
|
|
||||||
{"hello", "", "Hello\n", 0},
|
{"hello", "", "Hello\n", 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExamples(t *testing.T) {
|
func TestExamples(t *testing.T) {
|
||||||
for _, test := range examples {
|
for _, test := range examples {
|
||||||
directory := filepath.Join("..", "examples", test.Name)
|
test.Run(t, filepath.Join("..", "examples", test.Name))
|
||||||
run(t, directory, test.Input, test.Output, test.ExitCode)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,41 +31,3 @@ func BenchmarkExamples(b *testing.B) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// run builds and runs the file to check if the output matches the expected output.
|
|
||||||
func run(t *testing.T, path string, input string, expectedOutput string, expectedExitCode int) {
|
|
||||||
b := build.New(path)
|
|
||||||
env, err := compiler.Compile(b)
|
|
||||||
assert.Nil(t, err)
|
|
||||||
|
|
||||||
tmpDir := filepath.Join(os.TempDir(), "q", "tests")
|
|
||||||
err = os.MkdirAll(tmpDir, 0o755)
|
|
||||||
assert.Nil(t, err)
|
|
||||||
|
|
||||||
executable := b.Executable()
|
|
||||||
executable = filepath.Join(tmpDir, filepath.Base(executable))
|
|
||||||
err = linker.WriteFile(executable, b, env)
|
|
||||||
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)
|
|
||||||
}
|
|
61
tests/testRun_test.go
Normal file
61
tests/testRun_test.go
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
package tests_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.urbach.dev/cli/q/src/build"
|
||||||
|
"git.urbach.dev/cli/q/src/compiler"
|
||||||
|
"git.urbach.dev/cli/q/src/linker"
|
||||||
|
"git.urbach.dev/go/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type testRun struct {
|
||||||
|
Name string
|
||||||
|
Input string
|
||||||
|
Output string
|
||||||
|
ExitCode int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run builds and runs the file to check if the output matches the expected output.
|
||||||
|
func (test *testRun) Run(t *testing.T, path string) {
|
||||||
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
|
b := build.New(path)
|
||||||
|
env, err := compiler.Compile(b)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
tmpDir := filepath.Join(os.TempDir(), "q", "tests")
|
||||||
|
err = os.MkdirAll(tmpDir, 0o755)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
executable := b.Executable()
|
||||||
|
executable = filepath.Join(tmpDir, filepath.Base(executable))
|
||||||
|
err = linker.WriteFile(executable, b, env)
|
||||||
|
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(test.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, test.ExitCode)
|
||||||
|
assert.DeepEqual(t, string(output), test.Output)
|
||||||
|
})
|
||||||
|
}
|
15
tests/tests_test.go
Normal file
15
tests/tests_test.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package tests_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var tests = []testRun{
|
||||||
|
{"sum", "", "", 10},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTests(t *testing.T) {
|
||||||
|
for _, test := range tests {
|
||||||
|
test.Run(t, test.Name+".q")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue