mirror of
https://github.com/MontFerret/ferret.git
synced 2025-05-17 22:32:59 +02:00
92 lines
1.8 KiB
Go
92 lines
1.8 KiB
Go
|
package compiler_test
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/MontFerret/ferret/pkg/compiler"
|
||
|
. "github.com/smartystreets/goconvey/convey"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestRangeOperator(t *testing.T) {
|
||
|
Convey("Should compile RETURN 1..10", t, func() {
|
||
|
c := compiler.New()
|
||
|
|
||
|
prog, err := c.Compile(`
|
||
|
RETURN 1..10
|
||
|
`)
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
|
||
|
out, err := prog.Run(context.Background())
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
|
||
|
So(string(out), ShouldEqual, `[1,2,3,4,5,6,7,8,9,10]`)
|
||
|
})
|
||
|
|
||
|
Convey("Should compile FOR i IN 1..10 RETURN i * 2", t, func() {
|
||
|
c := compiler.New()
|
||
|
|
||
|
prog, err := c.Compile(`
|
||
|
FOR i IN 1..10
|
||
|
RETURN i * 2
|
||
|
`)
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
|
||
|
out, err := prog.Run(context.Background())
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
|
||
|
So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||
|
})
|
||
|
|
||
|
Convey("Should compile LET arr = 1..10 FOR i IN arr RETURN i * 2", t, func() {
|
||
|
c := compiler.New()
|
||
|
|
||
|
prog, err := c.Compile(`
|
||
|
LET arr = 1..10
|
||
|
FOR i IN arr
|
||
|
RETURN i * 2
|
||
|
`)
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
|
||
|
out, err := prog.Run(context.Background())
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
|
||
|
So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||
|
})
|
||
|
|
||
|
Convey("Should use variables", t, func() {
|
||
|
out := compiler.New().MustCompile(`
|
||
|
LET max = 10
|
||
|
|
||
|
FOR i IN 1..max
|
||
|
RETURN i * 2
|
||
|
`).MustRun(context.Background())
|
||
|
|
||
|
So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||
|
|
||
|
out2 := compiler.New().MustCompile(`
|
||
|
LET min = 1
|
||
|
|
||
|
FOR i IN min..10
|
||
|
RETURN i * 2
|
||
|
`).MustRun(context.Background())
|
||
|
|
||
|
So(string(out2), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||
|
|
||
|
out3 := compiler.New().MustCompile(`
|
||
|
LET min = 1
|
||
|
LET max = 10
|
||
|
|
||
|
FOR i IN min..max
|
||
|
RETURN i * 2
|
||
|
`).MustRun(context.Background())
|
||
|
|
||
|
So(string(out3), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||
|
})
|
||
|
}
|