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

194 lines
3.7 KiB
Go
Raw Normal View History

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(`
RETURN 1 + 1
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 1 - 1
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 2*2
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 4/2
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 5 % 2
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 1.2 + 1
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 1.1 - 1
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 2.1*2
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 4.4/2
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 5.5 % 2
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
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(`
RETURN 'Foo' + 'Bar'
`)
So(err, ShouldBeNil)
2018-10-25 01:40:57 +02:00
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
2018-10-25 01:40:57 +02:00
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"FooBar"`)
})
})
}