From ae8e46de4db81b566d98cf9170aa822e22c7b1ad Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 29 Jul 2024 18:03:15 +0200 Subject: [PATCH] Improved tests --- src/build/compiler/Result.go | 30 +++++++++++++++++++++--------- src/cli/Build.go | 2 +- tests/examples_test.go | 14 ++++++++++++-- tests/programs_test.go | 35 ++++++++++++++++++----------------- 4 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/build/compiler/Result.go b/src/build/compiler/Result.go index ee7c8c1..68c37ca 100644 --- a/src/build/compiler/Result.go +++ b/src/build/compiler/Result.go @@ -2,6 +2,7 @@ package compiler import ( "bufio" + "io" "os" "git.akyoto.dev/cli/q/src/build/arch/x64" @@ -83,24 +84,27 @@ func (r *Result) PrintInstructions() { }) } -// Write writes an executable file to disk. -func (r *Result) Write(path string) error { +// Write writes the executable to the given writer. +func (r *Result) Write(writer io.Writer) error { code, data := r.finalize() - return write(path, code, data) + return write(writer, code, data) } -// write writes an executable file to disk. -func write(path string, code []byte, data []byte) error { +// Write writes an executable file to disk. +func (r *Result) WriteFile(path string) error { file, err := os.Create(path) if err != nil { return err } - buffer := bufio.NewWriter(file) - executable := elf.New(code, data) - executable.Write(buffer) - buffer.Flush() + err = r.Write(file) + + if err != nil { + file.Close() + return err + } + err = file.Close() if err != nil { @@ -109,3 +113,11 @@ func write(path string, code []byte, data []byte) error { return os.Chmod(path, 0755) } + +// write writes an executable file to the given writer. +func write(writer io.Writer, code []byte, data []byte) error { + buffer := bufio.NewWriter(writer) + executable := elf.New(code, data) + executable.Write(buffer) + return buffer.Flush() +} diff --git a/src/cli/Build.go b/src/cli/Build.go index 9f54a16..ba79ec6 100644 --- a/src/cli/Build.go +++ b/src/cli/Build.go @@ -74,5 +74,5 @@ func makeExecutable(b *build.Build) error { return nil } - return result.Write(b.Executable()) + return result.WriteFile(b.Executable()) } diff --git a/tests/examples_test.go b/tests/examples_test.go index 2a5b41b..5c2ee12 100644 --- a/tests/examples_test.go +++ b/tests/examples_test.go @@ -5,6 +5,7 @@ import ( "testing" "git.akyoto.dev/cli/q/src/build" + "git.akyoto.dev/cli/q/src/build/config" "git.akyoto.dev/go/assert" ) @@ -18,13 +19,22 @@ var examples = []struct { {"factorial", "", "", 120}, {"fibonacci", "", "", 55}, {"array", "", "Hello", 0}, + {"echo", "Echo", "Echo", 0}, {"itoa", "", "9223372036854775807", 0}, } func TestExamples(t *testing.T) { for _, test := range examples { - t.Run(test.Name, func(t *testing.T) { - run(t, filepath.Join("..", "examples", test.Name), test.Input, test.Output, test.ExitCode) + directory := filepath.Join("..", "examples", test.Name) + + t.Run(test.Name+"/debug", func(t *testing.T) { + config.ConstantFold = false + run(t, directory, "debug", test.Name, test.Input, test.Output, test.ExitCode) + }) + + t.Run(test.Name+"/release", func(t *testing.T) { + config.ConstantFold = true + run(t, directory, "release", test.Name, test.Input, test.Output, test.ExitCode) }) } } diff --git a/tests/programs_test.go b/tests/programs_test.go index 429695f..5dfce4e 100644 --- a/tests/programs_test.go +++ b/tests/programs_test.go @@ -53,19 +53,17 @@ var programs = []struct { } func TestPrograms(t *testing.T) { - config.ConstantFold = false - for _, test := range programs { - t.Run(test.Name, func(t *testing.T) { - run(t, filepath.Join("programs", test.Name+".q"), test.Input, test.Output, test.ExitCode) + file := filepath.Join("programs", test.Name+".q") + + t.Run(test.Name+"/debug", func(t *testing.T) { + config.ConstantFold = false + run(t, file, "debug", test.Name, test.Input, test.Output, test.ExitCode) }) - } - config.ConstantFold = true - - for _, test := range programs { - t.Run(test.Name+" (optimized)", func(t *testing.T) { - run(t, filepath.Join("programs", test.Name+".q"), test.Input, test.Output, test.ExitCode) + t.Run(test.Name+"/release", func(t *testing.T) { + config.ConstantFold = true + run(t, file, "release", test.Name, test.Input, test.Output, test.ExitCode) }) } } @@ -84,21 +82,24 @@ func BenchmarkPrograms(b *testing.B) { } // run builds and runs the file to check if the output matches the expected output. -func run(t *testing.T, name string, input string, expectedOutput string, expectedExitCode int) { - b := build.New(name) - assert.True(t, len(b.Executable()) > 0) - +func run(t *testing.T, path string, version string, name string, input string, expectedOutput string, expectedExitCode int) { + b := build.New(path) result, err := b.Run() assert.Nil(t, err) - err = result.Write(b.Executable()) + directory := filepath.Join(os.TempDir(), "q", version) + err = os.MkdirAll(directory, 0755) assert.Nil(t, err) - stat, err := os.Stat(b.Executable()) + executable := filepath.Join(directory, name) + err = result.WriteFile(executable) + assert.Nil(t, err) + + stat, err := os.Stat(executable) assert.Nil(t, err) assert.True(t, stat.Size() > 0) - cmd := exec.Command(b.Executable()) + cmd := exec.Command(executable) cmd.Stdin = strings.NewReader(input) output, err := cmd.Output() exitCode := 0