Add the hello world test case
This commit is contained in:
parent
da434f00b6
commit
c65a307f4a
111
compiler/compiler_test.go
Normal file
111
compiler/compiler_test.go
Normal file
@ -0,0 +1,111 @@
|
||||
package compiler
|
||||
|
||||
import "os"
|
||||
import "io/fs"
|
||||
import "embed"
|
||||
import "strings"
|
||||
import "testing"
|
||||
import "os/exec"
|
||||
import "path/filepath"
|
||||
import "git.tebibyte.media/fspl/fspl/entity"
|
||||
import "git.tebibyte.media/fspl/fspl/testcommon"
|
||||
|
||||
//go:embed all:test-data/*
|
||||
var testData embed.FS
|
||||
|
||||
func testUnit (
|
||||
test *testing.T,
|
||||
address entity.Address,
|
||||
clangArgs []string,
|
||||
stdin, stdout string,
|
||||
exit int,
|
||||
args ...string,
|
||||
) {
|
||||
// create temporary directory
|
||||
temp := test.TempDir()
|
||||
|
||||
// instantiate and configure the compiler
|
||||
comp := new(Compiler)
|
||||
comp.Writer = os.Stderr
|
||||
comp.Resolver = NewResolver (
|
||||
"/usr/local/src/fspl",
|
||||
"/usr/src/fspl",
|
||||
"/usr/local/incude/fspl",
|
||||
"/usr/include/fspl")
|
||||
comp.Resolver.FS = testData
|
||||
comp.Output = filepath.Join(temp, "output.o")
|
||||
comp.Format = ".o"
|
||||
comp.Debug = true
|
||||
|
||||
// compile to object file
|
||||
err := comp.CompileUnit(address)
|
||||
if err != nil {
|
||||
test.Error("compiler returned error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// link the object file into an executable
|
||||
executablePath := filepath.Join(temp, "output")
|
||||
linkCommand := exec.Command("clang", append (
|
||||
clangArgs,
|
||||
comp.Output,
|
||||
"-o",
|
||||
executablePath)...)
|
||||
linkCommand.Stdout = os.Stderr
|
||||
linkCommand.Stderr = os.Stderr
|
||||
test.Log("running link command: ", linkCommand)
|
||||
err = linkCommand.Run()
|
||||
if err != nil {
|
||||
test.Error("error linking executable:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// run the executable file and check its output
|
||||
executableCommand := exec.Command(executablePath, args...)
|
||||
stdoutBuilder := new(strings.Builder)
|
||||
executableCommand.Stdin = strings.NewReader(stdin)
|
||||
executableCommand.Stdout = stdoutBuilder
|
||||
test.Log("running executable command: ", executableCommand)
|
||||
err = executableCommand.Run()
|
||||
|
||||
// check error, compare exit code
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
code := exitErr.ExitCode()
|
||||
if code != exit {
|
||||
test.Errorf (
|
||||
"expecting exit code %d, got %d",
|
||||
exit, code)
|
||||
}
|
||||
} else if err != nil {
|
||||
test.Errorf("error running %s: %v", executablePath, err)
|
||||
return
|
||||
} else {
|
||||
if 0 != exit {
|
||||
test.Errorf("expecting exit code %d, got 0", exit)
|
||||
}
|
||||
}
|
||||
|
||||
// compare stdout
|
||||
gotStdout := stdoutBuilder.String()
|
||||
if gotStdout != stdout {
|
||||
testcommon.Compare(test, stdout, gotStdout)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmbedOk (test *testing.T) {
|
||||
err := fs.WalkDir(testData, ".", func (path string, d fs.DirEntry, err error) error {
|
||||
test.Log("file:", path)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
test.Error("walk dir failed: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelloWorld (test *testing.T) {
|
||||
testUnit (test,
|
||||
"/test-data/data/hello", nil,
|
||||
"",
|
||||
"Hello, world!\n",
|
||||
0,
|
||||
)}
|
Loading…
Reference in New Issue
Block a user