2018-10-16 00:50:55 +02:00
|
|
|
package compiler_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/MontFerret/ferret/pkg/compiler"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMathOperators(t *testing.T) {
|
|
|
|
Convey("Integers", t, func() {
|
|
|
|
Convey("Should compile RETURN 1 + 1", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 1 + 1
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "2")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should compile RETURN 1 - 1", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 1 - 1
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "0")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should compile RETURN 2*2", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 2*2
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "4")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should compile RETURN 4/2", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 4/2
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "2")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should compile RETURN 5 % 2", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 5 % 2
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "1")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Floats", t, func() {
|
|
|
|
Convey("Should compile RETURN 1.2 + 1", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 1.2 + 1
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "2.2")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should compile RETURN 1.1 - 1", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 1.1 - 1
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "0.10000000000000009")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should compile RETURN 2.1*2", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 2.1*2
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "4.2")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should compile RETURN 4.4/2", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 4.4/2
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "2.2")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should compile RETURN 5.5 % 2", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 5.5 % 2
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, "1")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Strings", t, func() {
|
|
|
|
Convey("Should concat two strings RETURN 'Foo' + 'Bar'", func() {
|
|
|
|
c := compiler.New()
|
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
p, err := c.Compile(`
|
2018-10-16 00:50:55 +02:00
|
|
|
RETURN 'Foo' + 'Bar'
|
|
|
|
`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 01:40:57 +02:00
|
|
|
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
|
2018-10-16 00:50:55 +02:00
|
|
|
|
2018-10-25 01:40:57 +02:00
|
|
|
out, err := p.Run(context.Background())
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(out), ShouldEqual, `"FooBar"`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|