mirror of
https://github.com/MontFerret/ferret.git
synced 2025-08-13 19:52:52 +02:00
Add integration tests for FOR-WHILE
loops with LIMIT
scenarios and custom function usage
This commit is contained in:
66
test/integration/vm/vm_for_while_limit_test.go
Normal file
66
test/integration/vm/vm_for_while_limit_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package vm_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime"
|
||||
"github.com/MontFerret/ferret/pkg/vm"
|
||||
)
|
||||
|
||||
func TestForWhileLimit(t *testing.T) {
|
||||
RunUseCases(t, []UseCase{
|
||||
CaseArray(
|
||||
`
|
||||
FOR i WHILE UNTIL(5)
|
||||
LIMIT 2
|
||||
RETURN i
|
||||
`,
|
||||
[]any{0, 1}),
|
||||
CaseArray(`
|
||||
FOR i WHILE UNTIL(8)
|
||||
LIMIT 4, 2
|
||||
RETURN i
|
||||
`, []any{4, 5}),
|
||||
CaseArray(`
|
||||
FOR i WHILE UNTIL(8)
|
||||
LET x = i
|
||||
LIMIT 2
|
||||
RETURN i*x
|
||||
`, []any{0, 1},
|
||||
"Should be able to reuse values from a source"),
|
||||
CaseArray(`
|
||||
FOR i WHILE UNTIL(8)
|
||||
LET x = "foo"
|
||||
TYPENAME(x)
|
||||
LIMIT 2
|
||||
RETURN i
|
||||
`, []any{0, 1}, "Should define variables and call functions"),
|
||||
CaseArray(`
|
||||
FOR i WHILE UNTIL(8)
|
||||
LIMIT LIMIT_VALUE()
|
||||
RETURN i
|
||||
`, []any{0, 1}, "Should be able to use function call"),
|
||||
CaseArray(`
|
||||
LET o = {
|
||||
limit: 2
|
||||
}
|
||||
FOR i WHILE UNTIL(8)
|
||||
LIMIT o.limit
|
||||
RETURN i
|
||||
`, []any{0, 1}, "Should be able to use object property"),
|
||||
CaseArray(`
|
||||
LET o = [1,2]
|
||||
|
||||
FOR i WHILE UNTIL(8)
|
||||
LIMIT o[1]
|
||||
RETURN i
|
||||
`, []any{0, 1}, "Should be able to use array element"),
|
||||
}, vm.WithFunctionSetter(func(fns runtime.Functions) {
|
||||
fns.F().Set("LIMIT_VALUE", func(ctx context.Context, args ...runtime.Value) (runtime.Value, error) {
|
||||
return runtime.NewInt(2), nil
|
||||
})
|
||||
|
||||
fns.SetAll(ForWhileHelpers())
|
||||
}))
|
||||
}
|
Reference in New Issue
Block a user