1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/compiler/compiler_ternary_test.go

115 lines
2.0 KiB
Go
Raw Normal View History

package compiler_test
import (
"context"
"fmt"
"github.com/MontFerret/ferret/pkg/compiler"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestTernaryOperator(t *testing.T) {
Convey("Should compile ternary operator", t, func() {
c := compiler.New()
2018-10-25 01:40:57 +02:00
p, err := c.Compile(`
FOR i IN [1, 2, 3, 4, 5, 6]
RETURN i < 3 ? i * 3 : i * 2;
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `[3,6,6,8,10,12]`)
})
Convey("Should compile ternary operator with shortcut", t, func() {
c := compiler.New()
2018-10-25 01:40:57 +02:00
p, err := c.Compile(`
FOR i IN [1, 2, 3, 4, 5, 6]
RETURN i < 3 ? : i * 2
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `[true,true,6,8,10,12]`)
})
Convey("Should compile ternary operator with shortcut with nones", t, func() {
c := compiler.New()
2018-10-25 01:40:57 +02:00
p, err := c.Compile(`
FOR i IN [NONE, 2, 3, 4, 5, 6]
RETURN i ? : i
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `[null,2,3,4,5,6]`)
})
Convey("Should compile ternary operator with default values", t, func() {
vals := []string{
"0",
"0.0",
"''",
"NONE",
"FALSE",
}
c := compiler.New()
for _, val := range vals {
2018-10-25 01:40:57 +02:00
p, err := c.Compile(fmt.Sprintf(`
FOR i IN [%s, 1, 2, 3]
RETURN i ? i * 2 : 'no value'
`, val))
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `["no value",2,4,6]`)
}
})
}
2018-10-25 01:40:57 +02:00
func BenchmarkTernaryOperator(b *testing.B) {
p := compiler.New().MustCompile(`
LET a = "a"
LET b = "b"
LET c = FALSE
RETURN c ? a : b;
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkTernaryOperatorDef(b *testing.B) {
p := compiler.New().MustCompile(`
LET a = "a"
LET b = "b"
LET c = FALSE
RETURN c ? : a;
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}