Skip to content

Commit

Permalink
Addedtional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Nov 8, 2024
1 parent 866fb9d commit 1145db6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 58 deletions.
68 changes: 21 additions & 47 deletions pkg/compiler/compiler_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ RETURN %s<!DOCTYPE html>

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),
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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(
`
Expand Down
37 changes: 26 additions & 11 deletions pkg/compiler/compiler_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()),
Expand Down

0 comments on commit 1145db6

Please sign in to comment.