Added tests for examples

This commit is contained in:
Eduard Urbach 2024-06-29 21:40:14 +02:00
parent 83640ba590
commit 6e1af81733
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 87 additions and 3 deletions

13
examples/write/write.q Normal file
View File

@ -0,0 +1,13 @@
main() {
address := 4194304 + 1
length := (0 + 50 - 20) * 10 / 100
print(address, length)
}
print(address, length) {
write(length-2, address, length)
}
write(fd, address, length) {
syscall(1, fd, address, length)
}

67
examples_test.go Normal file
View File

@ -0,0 +1,67 @@
package main_test
import (
"os"
"os/exec"
"testing"
"git.akyoto.dev/cli/q/src/build"
"git.akyoto.dev/go/assert"
)
func TestExamples(t *testing.T) {
var examples = []struct {
Name string
ExpectedOutput string
ExpectedExitCode int
}{
{"hello", "", 25},
{"write", "ELF", 0},
}
for _, example := range examples {
example := example
t.Run(example.Name, func(t *testing.T) {
runExample(t, example.Name, example.ExpectedOutput, example.ExpectedExitCode)
})
}
}
// runExample builds and runs the example to check if the output matches the expected output.
func runExample(t *testing.T, name string, expectedOutput string, expectedExitCode int) {
b := build.New("examples/" + name)
assert.True(t, len(b.Executable()) > 0)
defer os.Remove(b.Executable())
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)
})
}

View File

@ -63,3 +63,9 @@ func (r *Result) EachFunction(caller *Function, traversed map[*Function]bool, ca
r.EachFunction(callee, traversed, call) r.EachFunction(callee, traversed, call)
} }
} }
// Write write the final executable to disk.
func (r *Result) Write(path string) error {
code, data := r.Finalize()
return Write(path, code, data)
}

View File

@ -53,9 +53,7 @@ func Build(args []string) int {
return 0 return 0
} }
path := b.Executable() err = result.Write(b.Executable())
code, data := result.Finalize()
err = build.Write(path, code, data)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)