github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_math_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 "github.com/MontFerret/ferret/pkg/runtime" 11 ) 12 13 func TestMathOperators(t *testing.T) { 14 Convey("Integers", t, func() { 15 Convey("Should compile RETURN 1 + 1", func() { 16 c := compiler.New() 17 18 p, err := c.Compile(` 19 RETURN 1 + 1 20 `) 21 22 So(err, ShouldBeNil) 23 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 24 25 out, err := p.Run(context.Background()) 26 27 So(err, ShouldBeNil) 28 So(string(out), ShouldEqual, "2") 29 }) 30 31 Convey("Should compile RETURN 1 - 1", func() { 32 c := compiler.New() 33 34 p, err := c.Compile(` 35 RETURN 1 - 1 36 `) 37 38 So(err, ShouldBeNil) 39 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 40 41 out, err := p.Run(context.Background()) 42 43 So(err, ShouldBeNil) 44 So(string(out), ShouldEqual, "0") 45 }) 46 47 Convey("Should compile RETURN 2*2", func() { 48 c := compiler.New() 49 50 p, err := c.Compile(` 51 RETURN 2*2 52 `) 53 54 So(err, ShouldBeNil) 55 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 56 57 out, err := p.Run(context.Background()) 58 59 So(err, ShouldBeNil) 60 So(string(out), ShouldEqual, "4") 61 }) 62 63 Convey("Should compile RETURN 4/2", func() { 64 c := compiler.New() 65 66 p, err := c.Compile(` 67 RETURN 4/2 68 `) 69 70 So(err, ShouldBeNil) 71 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 72 73 out, err := p.Run(context.Background()) 74 75 So(err, ShouldBeNil) 76 So(string(out), ShouldEqual, "2") 77 }) 78 79 Convey("Should compile RETURN 5 % 2", func() { 80 c := compiler.New() 81 82 p, err := c.Compile(` 83 RETURN 5 % 2 84 `) 85 86 So(err, ShouldBeNil) 87 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 88 89 out, err := p.Run(context.Background()) 90 91 So(err, ShouldBeNil) 92 So(string(out), ShouldEqual, "1") 93 }) 94 }) 95 96 Convey("Floats", t, func() { 97 Convey("Should compile RETURN 1.2 + 1", func() { 98 c := compiler.New() 99 100 p, err := c.Compile(` 101 RETURN 1.2 + 1 102 `) 103 104 So(err, ShouldBeNil) 105 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 106 107 out, err := p.Run(context.Background()) 108 109 So(err, ShouldBeNil) 110 So(string(out), ShouldEqual, "2.2") 111 }) 112 113 Convey("Should compile RETURN 1.1 - 1", func() { 114 c := compiler.New() 115 116 p, err := c.Compile(` 117 RETURN 1.1 - 1 118 `) 119 120 So(err, ShouldBeNil) 121 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 122 123 out, err := p.Run(context.Background()) 124 125 So(err, ShouldBeNil) 126 So(string(out), ShouldEqual, "0.10000000000000009") 127 }) 128 129 Convey("Should compile RETURN 2.1*2", func() { 130 c := compiler.New() 131 132 p, err := c.Compile(` 133 RETURN 2.1*2 134 `) 135 136 So(err, ShouldBeNil) 137 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 138 139 out, err := p.Run(context.Background()) 140 141 So(err, ShouldBeNil) 142 So(string(out), ShouldEqual, "4.2") 143 }) 144 145 Convey("Should compile RETURN 4.4/2", func() { 146 c := compiler.New() 147 148 p, err := c.Compile(` 149 RETURN 4.4/2 150 `) 151 152 So(err, ShouldBeNil) 153 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 154 155 out, err := p.Run(context.Background()) 156 157 So(err, ShouldBeNil) 158 So(string(out), ShouldEqual, "2.2") 159 }) 160 161 Convey("Should compile RETURN 5.5 % 2", func() { 162 c := compiler.New() 163 164 p, err := c.Compile(` 165 RETURN 5.5 % 2 166 `) 167 168 So(err, ShouldBeNil) 169 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 170 171 out, err := p.Run(context.Background()) 172 173 So(err, ShouldBeNil) 174 So(string(out), ShouldEqual, "1") 175 }) 176 }) 177 178 Convey("Strings", t, func() { 179 Convey("Should concat two strings RETURN 'Foo' + 'Bar'", func() { 180 c := compiler.New() 181 182 p, err := c.Compile(` 183 RETURN 'Foo' + 'Bar' 184 `) 185 186 So(err, ShouldBeNil) 187 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 188 189 out, err := p.Run(context.Background()) 190 191 So(err, ShouldBeNil) 192 So(string(out), ShouldEqual, `"FooBar"`) 193 }) 194 }) 195 }