From 1145db625fcf2ec47be6fed3534e80019c78caab Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Fri, 8 Nov 2024 13:27:26 -0500 Subject: [PATCH] Addedtional unit tests --- pkg/compiler/compiler_exec_test.go | 68 +++++++++-------------------- pkg/compiler/compiler_setup_test.go | 37 +++++++++++----- 2 files changed, 47 insertions(+), 58 deletions(-) diff --git a/pkg/compiler/compiler_exec_test.go b/pkg/compiler/compiler_exec_test.go index 9ac870f1..aa40ec32 100644 --- a/pkg/compiler/compiler_exec_test.go +++ b/pkg/compiler/compiler_exec_test.go @@ -78,6 +78,13 @@ RETURN %s func TestVariables(t *testing.T) { RunUseCases(t, []UseCase{ + CaseCompilationError(`RETURN foo`, "Should not compile if a variable not defined"), + CaseCompilationError(` + LET foo = "bar" + LET foo = "baz" + + RETURN foo + `, "Should not compile if a variable is not unique"), CaseNil(`LET i = NONE RETURN i`), Case(`LET a = TRUE RETURN a`, true), Case(`LET a = 1 RETURN a`, 1), @@ -127,6 +134,15 @@ func TestVariables(t *testing.T) { RETURN i `, []any{}, "Error handling in array comprehension"), + CaseCompilationError(` LET _ = (FOR i IN 1..100 RETURN NONE) + + RETURN _`, "Should not allow to use ignorable variable name"), + Case(` + LET _ = (FOR i IN 1..100 RETURN NONE) + LET _ = (FOR i IN 1..100 RETURN NONE) + + RETURN TRUE + `, true), }) Convey("Should compile LET i = (FOR i WHILE COUNTER() < 5 RETURN i) RETURN i", t, func() { @@ -175,40 +191,6 @@ func TestVariables(t *testing.T) { So(string(out), ShouldEqual, "true") }) - Convey("Should not compile FOR foo IN foo", t, func() { - c := compiler.New() - - _, err := c.Compile(` - FOR foo IN foo - RETURN foo - `) - - So(err, ShouldNotBeNil) - }) - - Convey("Should not compile if a variable not defined", t, func() { - c := compiler.New() - - _, err := c.Compile(` - RETURN foo - `) - - So(err, ShouldNotBeNil) - }) - - Convey("Should not compile if a variable is not unique", t, func() { - c := compiler.New() - - _, err := c.Compile(` - LET foo = "bar" - LET foo = "baz" - - RETURN foo - `) - - So(err, ShouldNotBeNil) - }) - //SkipConvey("Should use value returned from WAITFOR EVENT", t, func() { // out, err := newCompilerWithObservable().MustCompile(` // LET obj = X::VAL("event", ["data"]) @@ -248,18 +230,6 @@ func TestVariables(t *testing.T) { // So(string(out), ShouldEqual, `false`) //}) // - - Convey("Should not allow to use ignorable variable name", t, func() { - c := compiler.New() - - _, err := c.Compile(` - LET _ = (FOR i IN 1..100 RETURN NONE) - - RETURN _ - `) - - So(err, ShouldNotBeNil) - }) } func TestMathOperators(t *testing.T) { @@ -618,7 +588,7 @@ func TestMember(t *testing.T) { RETURN obj.attributes['data-index']`, 1), - CaseError(`LET obj = NONE RETURN obj.foo`), + CaseRuntimeError(`LET obj = NONE RETURN obj.foo`), CaseNil(`LET obj = NONE RETURN obj?.foo`), CaseObject(`RETURN {first: {second: "third"}}.first`, map[string]any{ @@ -750,6 +720,10 @@ func TestFor(t *testing.T) { // ShouldEqualJSON, //}, RunUseCases(t, []UseCase{ + CaseCompilationError(` + FOR foo IN foo + RETURN foo + `, "Should not compile FOR foo IN foo"), CaseArray("FOR i IN 1..5 RETURN i", []any{1, 2, 3, 4, 5}), CaseArray( ` diff --git a/pkg/compiler/compiler_setup_test.go b/pkg/compiler/compiler_setup_test.go index 858466f9..c8d7f279 100644 --- a/pkg/compiler/compiler_setup_test.go +++ b/pkg/compiler/compiler_setup_test.go @@ -53,12 +53,20 @@ func SkipCaseNil(expression string, desc ...string) UseCase { return Skip(CaseNil(expression, desc...)) } -func CaseError(expression string, desc ...string) UseCase { +func CaseRuntimeError(expression string, desc ...string) UseCase { return NewCase(expression, nil, ShouldBeError, desc...) } -func SkipCaseError(expression string, desc ...string) UseCase { - return Skip(CaseError(expression, desc...)) +func SkipCaseRuntimeError(expression string, desc ...string) UseCase { + return Skip(CaseRuntimeError(expression, desc...)) +} + +func CaseCompilationError(expression string, desc ...string) UseCase { + return NewCase(expression, nil, ShouldBeCompilationError, desc...) +} + +func SkipCompilationRuntimeError(expression string, desc ...string) UseCase { + return Skip(CaseRuntimeError(expression, desc...)) } func CaseObject(expression string, expected map[string]any, desc ...string) UseCase { @@ -168,6 +176,14 @@ func ShouldHaveSameItems(actual any, expected ...any) string { return "" } +func ShouldBeCompilationError(actual any, _ ...any) string { + // TODO: Expect a particular error message + + So(actual, ShouldBeError) + + return "" +} + func RunAsmUseCases(t *testing.T, useCases []ByteCodeUseCase) { c := compiler.New() for _, useCase := range useCases { @@ -218,16 +234,15 @@ func RunUseCasesWith(t *testing.T, c *compiler.Compiler, useCases []UseCase, opt t.Run(name, func(t *testing.T) { Convey(useCase.Expression, t, func() { - // catch panic - //defer func() { - // if r := recover(); r != nil { - // panic(fmt.Sprintf("%v,\nUse Case %d: - %s", r, idx+1, useCase.Expression)) - // } - //}() - prog, err := c.Compile(useCase.Expression) - So(err, ShouldBeNil) + if !ArePtrsEqual(useCase.Assertion, ShouldBeCompilationError) { + So(err, ShouldBeNil) + } else { + So(err, ShouldBeError) + + return + } options := []runtime.EnvironmentOption{ runtime.WithFunctions(c.Functions().Unwrap()),