From b3970fbb43ad3188299b26ca8f139646befa851b Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Wed, 2 Jul 2025 20:02:37 -0400 Subject: [PATCH] Add integration tests for `FOR-WHILE` loops with `LIMIT` scenarios and custom function usage --- .../integration/vm/vm_for_while_limit_test.go | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/integration/vm/vm_for_while_limit_test.go diff --git a/test/integration/vm/vm_for_while_limit_test.go b/test/integration/vm/vm_for_while_limit_test.go new file mode 100644 index 00000000..546feeea --- /dev/null +++ b/test/integration/vm/vm_for_while_limit_test.go @@ -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()) + })) +}