github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_for_while_ternary_test.go (about)

     1  package compiler_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  
     9  	"github.com/MontFerret/ferret/pkg/compiler"
    10  	"github.com/MontFerret/ferret/pkg/runtime/core"
    11  	"github.com/MontFerret/ferret/pkg/runtime/values"
    12  )
    13  
    14  func TestForTernaryWhileExpression(t *testing.T) {
    15  	Convey("RETURN foo ? TRUE : (FOR i WHILE false RETURN i*2)", t, func() {
    16  		c := compiler.New()
    17  
    18  		out1, err := c.MustCompile(`
    19  			LET foo = FALSE
    20  			RETURN foo ? TRUE : (FOR i WHILE false RETURN i*2)
    21  		`).Run(context.Background())
    22  
    23  		So(err, ShouldBeNil)
    24  		So(string(out1), ShouldEqual, `[]`)
    25  	})
    26  
    27  	Convey("RETURN foo ? TRUE : (FOR i WHILE T::FAIL() RETURN i*2)?", t, func() {
    28  		c := compiler.New()
    29  
    30  		out1, err := c.MustCompile(`
    31  			LET foo = FALSE
    32  			RETURN foo ? TRUE : (FOR i WHILE T::FAIL() RETURN i*2)?
    33  		`).Run(context.Background())
    34  
    35  		So(err, ShouldBeNil)
    36  		So(string(out1), ShouldEqual, `null`)
    37  	})
    38  
    39  	Convey("RETURN foo ? TRUE : (FOR i WHILE F() < 10 RETURN i*2)", t, func() {
    40  		c := compiler.New()
    41  
    42  		counter := -1
    43  		c.MustRegisterFunction("F", func(ctx context.Context, args ...core.Value) (core.Value, error) {
    44  			counter++
    45  			return values.NewInt(counter), nil
    46  		})
    47  
    48  		out1, err := c.MustCompile(`
    49  			LET foo = FALSE
    50  			RETURN foo ? TRUE : (FOR i WHILE F() < 10 RETURN i*2)
    51  		`).Run(context.Background())
    52  
    53  		So(err, ShouldBeNil)
    54  		So(string(out1), ShouldEqual, `[0,2,4,6,8,10,12,14,16,18]`)
    55  	})
    56  }