From 795935ddfbd9f38a90a60db8d03c2ef3430f1414 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 3 Jul 2024 11:59:36 +0200 Subject: [PATCH] Added more tests --- tests/benchmarks_test.go | 26 --------- tests/errors_test.go | 54 +++++++++---------- tests/examples_test.go | 20 +++---- tests/{benchmarks => programs}/empty.q | 0 .../{successive-calls.q => multi-calls.q} | 0 .../expressions.q => programs/square-sum.q} | 0 tests/programs_test.go | 33 ++++++++---- 7 files changed, 61 insertions(+), 72 deletions(-) delete mode 100644 tests/benchmarks_test.go rename tests/{benchmarks => programs}/empty.q (100%) rename tests/programs/{successive-calls.q => multi-calls.q} (100%) rename tests/{benchmarks/expressions.q => programs/square-sum.q} (100%) diff --git a/tests/benchmarks_test.go b/tests/benchmarks_test.go deleted file mode 100644 index 6ff03a7..0000000 --- a/tests/benchmarks_test.go +++ /dev/null @@ -1,26 +0,0 @@ -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) - } -} diff --git a/tests/errors_test.go b/tests/errors_test.go index 9de3370..6299be8 100644 --- a/tests/errors_test.go +++ b/tests/errors_test.go @@ -10,34 +10,34 @@ import ( "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"}}, - } +var errs = []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 { +func TestErrors(t *testing.T) { + for _, test := range errs { name := strings.TrimSuffix(test.File, ".q") t.Run(name, func(t *testing.T) { diff --git a/tests/examples_test.go b/tests/examples_test.go index a8d9954..ab00191 100644 --- a/tests/examples_test.go +++ b/tests/examples_test.go @@ -5,17 +5,17 @@ import ( "testing" ) -func TestExamples(t *testing.T) { - var tests = []struct { - Name string - ExpectedOutput string - ExpectedExitCode int - }{ - {"hello", "", 9}, - {"write", "ELF", 0}, - } +var examples = []struct { + Name string + ExpectedOutput string + ExpectedExitCode int +}{ + {"hello", "", 9}, + {"write", "ELF", 0}, +} - for _, test := range tests { +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.ExpectedOutput, test.ExpectedExitCode) }) diff --git a/tests/benchmarks/empty.q b/tests/programs/empty.q similarity index 100% rename from tests/benchmarks/empty.q rename to tests/programs/empty.q diff --git a/tests/programs/successive-calls.q b/tests/programs/multi-calls.q similarity index 100% rename from tests/programs/successive-calls.q rename to tests/programs/multi-calls.q diff --git a/tests/benchmarks/expressions.q b/tests/programs/square-sum.q similarity index 100% rename from tests/benchmarks/expressions.q rename to tests/programs/square-sum.q diff --git a/tests/programs_test.go b/tests/programs_test.go index e671fd0..a5ee862 100644 --- a/tests/programs_test.go +++ b/tests/programs_test.go @@ -10,16 +10,31 @@ import ( "git.akyoto.dev/go/assert" ) -func TestPrograms(t *testing.T) { - var tests = []struct { - Name string - ExpectedOutput string - ExpectedExitCode int - }{ - {"successive-calls.q", "", 9}, - } +var programs = []struct { + Name string + ExpectedOutput string + ExpectedExitCode int +}{ + {"empty.q", "", 0}, + {"square-sum.q", "", 25}, + {"multi-calls.q", "", 9}, +} - for _, test := range tests { +func BenchmarkPrograms(b *testing.B) { + for _, test := range programs { + b.Run(test.Name, func(b *testing.B) { + compiler := build.New(filepath.Join("programs", test.Name)) + + for i := 0; i < b.N; i++ { + _, err := compiler.Run() + assert.Nil(b, err) + } + }) + } +} + +func TestPrograms(t *testing.T) { + for _, test := range programs { t.Run(test.Name, func(t *testing.T) { run(t, filepath.Join("programs", test.Name), test.ExpectedOutput, test.ExpectedExitCode) })