From 85568949a2488451b30a4fc64d69916b5463afe9 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 5 Feb 2025 11:11:15 +0100 Subject: [PATCH] Added more tests --- src/errors/Common.go | 1 + src/scanner/scanFunction.go | 6 +++++- src/token/List.go | 12 ++++++------ tests/errors/MissingParameter.q | 1 + tests/errors/MissingParameter2.q | 1 + tests/errors/MissingParameter3.q | 1 + tests/errors/MissingType.q | 1 + tests/errors_test.go | 4 ++++ 8 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 tests/errors/MissingParameter.q create mode 100644 tests/errors/MissingParameter2.q create mode 100644 tests/errors/MissingParameter3.q create mode 100644 tests/errors/MissingType.q diff --git a/src/errors/Common.go b/src/errors/Common.go index ab301d2..d395bdd 100644 --- a/src/errors/Common.go +++ b/src/errors/Common.go @@ -18,6 +18,7 @@ var ( MissingGroupEnd = &Base{"Missing ')'"} MissingMainFunction = &Base{"Missing main function"} MissingOperand = &Base{"Missing operand"} + MissingParameter = &Base{"Missing parameter"} MissingType = &Base{"Missing type"} NotImplemented = &Base{"Not implemented"} UnknownType = &Base{"Unknown type"} diff --git a/src/scanner/scanFunction.go b/src/scanner/scanFunction.go index f66c1bb..30cda18 100644 --- a/src/scanner/scanFunction.go +++ b/src/scanner/scanFunction.go @@ -164,7 +164,11 @@ func (s *Scanner) scanFunction(file *fs.File, tokens token.List, i int) (int, er count := 0 err := parameters.Split(func(tokens token.List) error { - if len(tokens) < 2 { + if len(tokens) == 0 { + return errors.New(errors.MissingParameter, file, parameters[0].Position) + } + + if len(tokens) == 1 { return errors.New(errors.MissingType, file, tokens[0].End()) } diff --git a/src/token/List.go b/src/token/List.go index c66dc27..218dcec 100644 --- a/src/token/List.go +++ b/src/token/List.go @@ -31,6 +31,10 @@ func (list List) LastIndexKind(kind Kind) int { // Split calls the callback function on each set of tokens in a comma separated list. func (list List) Split(call func(List) error) error { + if len(list) == 0 { + return nil + } + start := 0 groupLevel := 0 @@ -58,12 +62,8 @@ func (list List) Split(call func(List) error) error { } } - if start != len(list) { - parameter := list[start:] - return call(parameter) - } - - return nil + parameter := list[start:] + return call(parameter) } // Text returns the concatenated token text. diff --git a/tests/errors/MissingParameter.q b/tests/errors/MissingParameter.q new file mode 100644 index 0000000..a3247e3 --- /dev/null +++ b/tests/errors/MissingParameter.q @@ -0,0 +1 @@ +f(,) {} \ No newline at end of file diff --git a/tests/errors/MissingParameter2.q b/tests/errors/MissingParameter2.q new file mode 100644 index 0000000..fd8af14 --- /dev/null +++ b/tests/errors/MissingParameter2.q @@ -0,0 +1 @@ +f(a Int,) -> Int { return a } \ No newline at end of file diff --git a/tests/errors/MissingParameter3.q b/tests/errors/MissingParameter3.q new file mode 100644 index 0000000..6b4650b --- /dev/null +++ b/tests/errors/MissingParameter3.q @@ -0,0 +1 @@ +f(,a Int) {} \ No newline at end of file diff --git a/tests/errors/MissingType.q b/tests/errors/MissingType.q new file mode 100644 index 0000000..214c35e --- /dev/null +++ b/tests/errors/MissingType.q @@ -0,0 +1 @@ +f(a) {} \ No newline at end of file diff --git a/tests/errors_test.go b/tests/errors_test.go index 7afb488..20178a0 100644 --- a/tests/errors_test.go +++ b/tests/errors_test.go @@ -40,6 +40,10 @@ var errs = []struct { {"MissingMainFunction.q", errors.MissingMainFunction}, {"MissingOperand.q", errors.MissingOperand}, {"MissingOperand2.q", errors.MissingOperand}, + {"MissingParameter.q", errors.MissingParameter}, + {"MissingParameter2.q", errors.MissingParameter}, + {"MissingParameter3.q", errors.MissingParameter}, + {"MissingType.q", errors.MissingType}, {"ReturnCountMismatch.q", &errors.ReturnCountMismatch{Count: 1, ExpectedCount: 0}}, {"TypeMismatch.q", &errors.TypeMismatch{Expected: "Pointer", Encountered: "Int64", ParameterName: "p"}}, {"UnknownFunction.q", &errors.UnknownFunction{Name: "unknown"}},