Refactored code structure

This commit is contained in:
2024-07-03 11:39:24 +02:00
parent ed03f6a802
commit feebfe65bb
54 changed files with 583 additions and 450 deletions

26
tests/benchmarks_test.go Normal file
View File

@ -0,0 +1,26 @@
package tests_test
import (
"testing"
"git.akyoto.dev/cli/q/src/build"
"git.akyoto.dev/go/assert"
)
func BenchmarkEmpty(b *testing.B) {
compiler := build.New("benchmarks/empty.q")
for i := 0; i < b.N; i++ {
_, err := compiler.Run()
assert.Nil(b, err)
}
}
func BenchmarkExpressions(b *testing.B) {
compiler := build.New("benchmarks/expressions.q")
for i := 0; i < b.N; i++ {
_, err := compiler.Run()
assert.Nil(b, err)
}
}

50
tests/errors_test.go Normal file
View File

@ -0,0 +1,50 @@
package tests_test
import (
"path/filepath"
"strings"
"testing"
"git.akyoto.dev/cli/q/src/build"
"git.akyoto.dev/cli/q/src/build/errors"
"git.akyoto.dev/go/assert"
)
func TestErrors(t *testing.T) {
tests := []struct {
File string
ExpectedError error
}{
{"ExpectedFunctionDefinition.q", errors.ExpectedFunctionDefinition},
{"ExpectedFunctionName.q", errors.ExpectedFunctionName},
{"ExpectedFunctionParameters.q", errors.ExpectedFunctionParameters},
{"InvalidInstructionIdentifier.q", &errors.InvalidInstruction{Instruction: "abc"}},
{"InvalidInstructionNumber.q", &errors.InvalidInstruction{Instruction: "123"}},
{"InvalidExpression.q", errors.InvalidExpression},
{"InvalidOperator.q", &errors.InvalidOperator{Operator: "+++"}},
{"InvalidCharacter.q", &errors.InvalidCharacter{Character: "@"}},
{"InvalidCharacter2.q", &errors.InvalidCharacter{Character: "@"}},
{"InvalidCharacter3.q", &errors.InvalidCharacter{Character: "@"}},
{"MissingAssignValue.q", errors.MissingAssignValue},
{"MissingBlockEnd.q", errors.MissingBlockEnd},
{"MissingBlockStart.q", errors.MissingBlockStart},
{"MissingGroupEnd.q", errors.MissingGroupEnd},
{"MissingGroupStart.q", errors.MissingGroupStart},
{"MissingMainFunction.q", errors.MissingMainFunction},
{"VariableAlreadyExists.q", &errors.VariableAlreadyExists{Name: "x"}},
{"UnknownIdentifier.q", &errors.UnknownIdentifier{Name: "x"}},
{"UnknownIdentifier2.q", &errors.UnknownIdentifier{Name: "x"}},
{"UnusedVariable.q", &errors.UnusedVariable{Name: "x"}},
}
for _, test := range tests {
name := strings.TrimSuffix(test.File, ".q")
t.Run(name, func(t *testing.T) {
b := build.New(filepath.Join("errors", test.File))
_, err := b.Run()
assert.NotNil(t, err)
assert.Contains(t, err.Error(), test.ExpectedError.Error())
})
}
}

23
tests/examples_test.go Normal file
View File

@ -0,0 +1,23 @@
package tests_test
import (
"path/filepath"
"testing"
)
func TestExamples(t *testing.T) {
var tests = []struct {
Name string
ExpectedOutput string
ExpectedExitCode int
}{
{"hello", "", 9},
{"write", "ELF", 0},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
run(t, filepath.Join("..", "examples", test.Name), test.ExpectedOutput, test.ExpectedExitCode)
})
}
}

View File

@ -0,0 +1,7 @@
main() {
syscall(60, f(1) + f(2) + f(3))
}
f(x) {
return x + 1
}

64
tests/programs_test.go Normal file
View File

@ -0,0 +1,64 @@
package tests_test
import (
"os"
"os/exec"
"path/filepath"
"testing"
"git.akyoto.dev/cli/q/src/build"
"git.akyoto.dev/go/assert"
)
func TestPrograms(t *testing.T) {
var tests = []struct {
Name string
ExpectedOutput string
ExpectedExitCode int
}{
{"successive-calls.q", "", 9},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
run(t, filepath.Join("programs", test.Name), test.ExpectedOutput, test.ExpectedExitCode)
})
}
}
// 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)
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)
})
}