Skip to content

Commit

Permalink
Refactored testing helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Mar 26, 2024
1 parent 72bbfcf commit 7e4de7a
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 31 deletions.
40 changes: 37 additions & 3 deletions pkg/compiler/compiler_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"

. "github.com/smartystreets/goconvey/convey"

"github.com/MontFerret/ferret/pkg/compiler"
)

func TestFor(t *testing.T) {
Expand All @@ -16,7 +14,7 @@ func TestFor(t *testing.T) {
// []any{},
// ShouldEqualJSON,
//},
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
{
"FOR i IN 1..5 RETURN i",
[]any{1, 2, 3, 4, 5},
Expand Down Expand Up @@ -137,3 +135,39 @@ func TestFor(t *testing.T) {
},
})
}

func BenchmarkForEmpty(b *testing.B) {
RunBenchmark(b, `
FOR i IN []
RETURN i
`)
}

func BenchmarkForStaticArray(b *testing.B) {
RunBenchmark(b, `
FOR i IN [1,2,3,4,5,6,7,8,9,10]
RETURN i
`)
}

func BenchmarkForRange(b *testing.B) {
RunBenchmark(b, `
FOR i IN 1..10
RETURN i
`)
}

func BenchmarkForObject(b *testing.B) {
RunBenchmark(b, `
FOR i IN {"1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9":9, "10":10}
RETURN i
`)
}

func BenchmarkForNested(b *testing.B) {
RunBenchmark(b, `
FOR prop IN ["a"]
FOR val IN [1, 2, 3]
RETURN {[prop]: val}
`)
}
4 changes: 1 addition & 3 deletions pkg/compiler/compiler_for_while_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (
"github.com/MontFerret/ferret/pkg/runtime/values"

. "github.com/smartystreets/goconvey/convey"

"github.com/MontFerret/ferret/pkg/compiler"
)

func TestForWhile(t *testing.T) {
var counter int64
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
//{
// "FOR i WHILE false RETURN i",
// []any{},
Expand Down
4 changes: 1 addition & 3 deletions pkg/compiler/compiler_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package compiler_test

import (
"testing"

"github.com/MontFerret/ferret/pkg/compiler"
)

func TestFunctionCall(t *testing.T) {
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
{
"RETURN TYPENAME(1)",
"int",
Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/compiler_let_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestLet(t *testing.T) {
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
{
`LET i = NONE RETURN i`,
nil,
Expand Down
4 changes: 1 addition & 3 deletions pkg/compiler/compiler_like_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package compiler_test

import (
"testing"

"github.com/MontFerret/ferret/pkg/compiler"
)

func TestLikeOperator(t *testing.T) {
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
{`RETURN "foo" LIKE "f*"`, true, nil},
{`RETURN "foo" LIKE "b*"`, false, nil},
{`RETURN "foo" NOT LIKE "f*"`, false, nil},
Expand Down
4 changes: 1 addition & 3 deletions pkg/compiler/compiler_logical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package compiler_test

import (
"testing"

"github.com/MontFerret/ferret/pkg/compiler"
)

func TestLogicalOperators(t *testing.T) {
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
{"RETURN 1 AND 0", 0, nil},
{"RETURN 1 AND 1", 1, nil},
{"RETURN 2 > 1 AND 1 > 0", true, nil},
Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/compiler_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func TestMember(t *testing.T) {
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
{
"LET arr = [1,2,3,4] RETURN arr[10]",
nil,
Expand Down
3 changes: 1 addition & 2 deletions pkg/compiler/compiler_range_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package compiler_test

import (
"github.com/MontFerret/ferret/pkg/compiler"
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func TestRange(t *testing.T) {
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
{
"RETURN 1..10",
[]any{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
Expand Down
4 changes: 1 addition & 3 deletions pkg/compiler/compiler_ternary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package compiler_test

import (
"testing"

"github.com/MontFerret/ferret/pkg/compiler"
)

func TestTernaryOperator(t *testing.T) {
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
{"RETURN 1 < 2 ? 3 : 4", 3, nil},
{"RETURN 1 > 2 ? 3 : 4", 4, nil},
{"RETURN 2 ? : 4", 2, nil},
Expand Down
4 changes: 1 addition & 3 deletions pkg/compiler/compiler_unary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package compiler_test

import (
"testing"

"github.com/MontFerret/ferret/pkg/compiler"
)

func TestUnaryOperator(t *testing.T) {
RunUseCases(t, compiler.New(), []UseCase{
RunUseCases(t, []UseCase{
{"RETURN !TRUE", false, nil},
{"RETURN !FALSE", true, nil},
{"RETURN -1", -1, nil},
Expand Down
39 changes: 36 additions & 3 deletions pkg/compiler/test_test.go → pkg/compiler/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ type UseCase struct {
Assertion Assertion
}

func Exec(p *runtime.Program, raw bool, opts ...runtime.EnvironmentOption) (any, error) {
func Run(p *runtime.Program, opts ...runtime.EnvironmentOption) ([]byte, error) {
vm := runtime.NewVM(opts...)

out, err := vm.Run(context.Background(), p)
return vm.Run(context.Background(), p)
}

func Exec(p *runtime.Program, raw bool, opts ...runtime.EnvironmentOption) (any, error) {
out, err := Run(p, opts...)

if err != nil {
return 0, err
Expand Down Expand Up @@ -65,7 +69,7 @@ func ShouldHaveSameItems(actual any, expected ...any) string {
return ""
}

func RunUseCases(t *testing.T, c *compiler.Compiler, useCases []UseCase, opts ...runtime.EnvironmentOption) {
func RunUseCasesWith(t *testing.T, c *compiler.Compiler, useCases []UseCase, opts ...runtime.EnvironmentOption) {
for _, useCase := range useCases {
Convey(useCase.Expression, t, func() {
prog, err := c.Compile(useCase.Expression)
Expand Down Expand Up @@ -101,3 +105,32 @@ func RunUseCases(t *testing.T, c *compiler.Compiler, useCases []UseCase, opts ..
})
}
}

func RunUseCases(t *testing.T, useCases []UseCase, opts ...runtime.EnvironmentOption) {
RunUseCasesWith(t, compiler.New(), useCases, opts...)
}

func RunBenchmarkWith(b *testing.B, c *compiler.Compiler, expression string, opts ...runtime.EnvironmentOption) {
prog, err := c.Compile(expression)

if err != nil {
panic(err)
}

options := []runtime.EnvironmentOption{
runtime.WithFunctions(c.Functions().Unwrap()),
}
options = append(options, opts...)

for n := 0; n < b.N; n++ {
_, e := Run(prog, opts...)

if e != nil {
panic(e)
}
}
}

func RunBenchmark(b *testing.B, expression string, opts ...runtime.EnvironmentOption) {
RunBenchmarkWith(b, compiler.New(), expression, opts...)
}
7 changes: 4 additions & 3 deletions pkg/runtime/values/array_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
)

type ArrayIterator struct {
pos int
values *Array
length int
pos int
}

func NewArrayIterator(values *Array) core.Iterator {
return &ArrayIterator{values: values, pos: 0}
return &ArrayIterator{values: values, length: int(values.Length()), pos: 0}
}

func (iterator *ArrayIterator) HasNext(_ context.Context) (bool, error) {
return int(iterator.values.Length()) > iterator.pos, nil
return iterator.length > iterator.pos, nil
}

func (iterator *ArrayIterator) Next(_ context.Context) (value core.Value, key core.Value, err error) {
Expand Down

0 comments on commit 7e4de7a

Please sign in to comment.