github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_range_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  )
    11  
    12  func TestRangeOperator(t *testing.T) {
    13  	Convey("Should compile RETURN 1..10", t, func() {
    14  		c := compiler.New()
    15  
    16  		p, err := c.Compile(`
    17  				RETURN 1..10
    18  			`)
    19  
    20  		So(err, ShouldBeNil)
    21  
    22  		out, err := p.Run(context.Background())
    23  
    24  		So(err, ShouldBeNil)
    25  
    26  		So(string(out), ShouldEqual, `[1,2,3,4,5,6,7,8,9,10]`)
    27  	})
    28  
    29  	Convey("Should compile FOR i IN 1..10 RETURN i * 2", t, func() {
    30  		c := compiler.New()
    31  
    32  		p, err := c.Compile(`
    33  				FOR i IN 1..10
    34  					RETURN i * 2
    35  			`)
    36  
    37  		So(err, ShouldBeNil)
    38  
    39  		out, err := p.Run(context.Background())
    40  
    41  		So(err, ShouldBeNil)
    42  
    43  		So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
    44  	})
    45  
    46  	Convey("Should compile LET arr = 1..10 FOR i IN arr RETURN i * 2", t, func() {
    47  		c := compiler.New()
    48  
    49  		p, err := c.Compile(`
    50  				LET arr = 1..10
    51  				FOR i IN arr
    52  					RETURN i * 2
    53  			`)
    54  
    55  		So(err, ShouldBeNil)
    56  
    57  		out, err := p.Run(context.Background())
    58  
    59  		So(err, ShouldBeNil)
    60  
    61  		So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
    62  	})
    63  
    64  	Convey("Should use variables", t, func() {
    65  		out := compiler.New().MustCompile(`
    66  				LET max = 10
    67  				
    68  				FOR i IN 1..max
    69  					RETURN i * 2
    70  		`).MustRun(context.Background())
    71  
    72  		So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
    73  
    74  		out2 := compiler.New().MustCompile(`
    75  				LET min = 1
    76  				
    77  				FOR i IN min..10
    78  					RETURN i * 2
    79  		`).MustRun(context.Background())
    80  
    81  		So(string(out2), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
    82  
    83  		out3 := compiler.New().MustCompile(`
    84  				LET min = 1
    85  				LET max = 10
    86  				
    87  				FOR i IN min..max
    88  					RETURN i * 2
    89  		`).MustRun(context.Background())
    90  
    91  		So(string(out3), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
    92  	})
    93  }
    94  
    95  func BenchmarkRangeOperator(b *testing.B) {
    96  	p := compiler.New().MustCompile(`
    97  				RETURN 1..10
    98  			`)
    99  
   100  	for n := 0; n < b.N; n++ {
   101  		p.Run(context.Background())
   102  	}
   103  }