From 453b4d89565a00e6e73b17d22cababa14662518f Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 4 Jun 2024 15:51:31 +0200 Subject: [PATCH] Added more tests --- examples/fibonacci/fibonacci.q | 19 ------------------- src/asm/Data.go | 3 ++- main_test.go => src/cli/Main_test.go | 21 ++++++++------------- src/directory/Walk_test.go | 19 +++++++++++++++++++ src/elf/ELF_test.go | 13 +++++++++++++ 5 files changed, 42 insertions(+), 33 deletions(-) delete mode 100644 examples/fibonacci/fibonacci.q rename main_test.go => src/cli/Main_test.go (62%) create mode 100644 src/directory/Walk_test.go create mode 100644 src/elf/ELF_test.go diff --git a/examples/fibonacci/fibonacci.q b/examples/fibonacci/fibonacci.q deleted file mode 100644 index c188e38..0000000 --- a/examples/fibonacci/fibonacci.q +++ /dev/null @@ -1,19 +0,0 @@ -import sys - -main() { - let f = fibonacci(11) - sys.exit(f) -} - -fibonacci(n Int) -> Int { - mut b = 0 - mut c = 1 - - for 0..n { - let a = b - b = c - c = a + b - } - - return b -} diff --git a/src/asm/Data.go b/src/asm/Data.go index 9268c99..418a898 100644 --- a/src/asm/Data.go +++ b/src/asm/Data.go @@ -5,7 +5,8 @@ import "bytes" // Data represents the static read-only data. type Data []byte -// Add adds the given bytes to the data block and returns the address relative to the start of the data section. +// Add adds the given bytes to the data block if this sequence of bytes doesn't exist yet. +// It returns the address relative to the start of the data section. func (data *Data) Add(block []byte) Address { position := bytes.Index(*data, block) diff --git a/main_test.go b/src/cli/Main_test.go similarity index 62% rename from main_test.go rename to src/cli/Main_test.go index ab4ac62..95fcf55 100644 --- a/main_test.go +++ b/src/cli/Main_test.go @@ -1,6 +1,7 @@ -package main_test +package cli_test import ( + "fmt" "io" "os" "testing" @@ -27,23 +28,17 @@ func TestCLI(t *testing.T) { {[]string{"invalid"}, 2}, {[]string{"system"}, 0}, {[]string{"build", "non-existing-directory"}, 1}, - {[]string{"build", "examples/hello/hello.q"}, 1}, - {[]string{"build", "examples/hello", "--invalid"}, 2}, - {[]string{"build", "examples/hello", "--dry"}, 0}, - {[]string{"build", "examples/hello", "--dry", "--verbose"}, 0}, + {[]string{"build", "../../examples/hello/hello.q"}, 1}, + {[]string{"build", "../../examples/hello", "--invalid"}, 2}, + {[]string{"build", "../../examples/hello", "--dry"}, 0}, + {[]string{"build", "../../examples/hello", "--dry", "--verbose"}, 0}, } for _, test := range tests { t.Log(test.arguments) + directory, _ := os.Getwd() + fmt.Println(directory) exitCode := cli.Main(test.arguments) assert.Equal(t, exitCode, test.expectedExitCode) } } - -func BenchmarkBuild(b *testing.B) { - args := []string{"build", "examples/hello", "--dry"} - - for i := 0; i < b.N; i++ { - cli.Main(args) - } -} diff --git a/src/directory/Walk_test.go b/src/directory/Walk_test.go new file mode 100644 index 0000000..a4fcda8 --- /dev/null +++ b/src/directory/Walk_test.go @@ -0,0 +1,19 @@ +package directory_test + +import ( + "testing" + + "git.akyoto.dev/cli/q/src/directory" + "git.akyoto.dev/go/assert" +) + +func TestWalk(t *testing.T) { + var files []string + + directory.Walk(".", func(file string) { + files = append(files, file) + }) + + assert.Contains(t, files, "Walk.go") + assert.Contains(t, files, "Walk_test.go") +} diff --git a/src/elf/ELF_test.go b/src/elf/ELF_test.go new file mode 100644 index 0000000..45a9aa2 --- /dev/null +++ b/src/elf/ELF_test.go @@ -0,0 +1,13 @@ +package elf_test + +import ( + "io" + "testing" + + "git.akyoto.dev/cli/q/src/elf" +) + +func TestELF(t *testing.T) { + exe := elf.New(nil, nil) + exe.Write(io.Discard) +}