1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-02-13 13:58:32 +02:00
ferret/pkg/compiler/compiler_for_while_ternary_test.go
Tim Voronov bd07b84736
Feature/#262 while loop (#567)
* Added new syntax and iterator

* Added FOR-WHILE loop

* Added 'FOR-DO-WHILE loop'
2020-11-10 19:16:22 -05:00

43 lines
1.1 KiB
Go

package compiler_test
import (
"context"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestForTernaryWhileExpression(t *testing.T) {
Convey("RETURN foo ? TRUE : (FOR i WHILE false RETURN i*2)", t, func() {
c := compiler.New()
out1, err := c.MustCompile(`
LET foo = FALSE
RETURN foo ? TRUE : (FOR i WHILE false RETURN i*2)
`).Run(context.Background())
So(err, ShouldBeNil)
So(string(out1), ShouldEqual, `[]`)
})
Convey("RETURN foo ? TRUE : (FOR i WHILE F() < 10 RETURN i*2)", t, func() {
c := compiler.New()
counter := -1
c.MustRegisterFunction("F", func(ctx context.Context, args ...core.Value) (core.Value, error) {
counter++
return values.NewInt(counter), nil
})
out1, err := c.MustCompile(`
LET foo = FALSE
RETURN foo ? TRUE : (FOR i WHILE F() < 10 RETURN i*2)
`).Run(context.Background())
So(err, ShouldBeNil)
So(string(out1), ShouldEqual, `[0,2,4,6,8,10,12,14,16,18]`)
})
}