1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-04 10:35:08 +02:00

Add more unit tests for operators - ToNumberOrString, ToNumberOnly (#781)

* WIP TestToNumberOrString

* add boolean check

* add array type

* WIP add TestToNumberOnly

* add ToNumberOnly test
This commit is contained in:
tsukasa-ino 2023-03-28 06:04:16 +09:00 committed by GitHub
parent fb6b29b29e
commit 82f0e5f044
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package operators_test
import (
"testing"
"time"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/expressions/operators"
@ -1280,3 +1281,146 @@ func TestNot(t *testing.T) {
})
})
}
func TestToNumberOrString(t *testing.T) {
Convey("Invert to numeric value", t, func() {
Convey("0 turns 0", func() {
So(operators.ToNumberOrString(values.NewInt(0)), ShouldEqual, 0)
})
Convey("1 turns 1", func() {
So(operators.ToNumberOrString(values.NewInt(1)), ShouldEqual, 1)
})
Convey("-1 turns -1", func() {
So(operators.ToNumberOrString(values.NewInt(-1)), ShouldEqual, -1)
})
Convey("0.0 turns 0.0", func() {
So(operators.ToNumberOrString(values.NewFloat(0.0)), ShouldEqual, 0.0)
})
Convey("-1.0 turns -1.0", func() {
So(operators.ToNumberOrString(values.NewFloat(-1.0)), ShouldEqual, -1.0)
})
Convey("1.0 turns 1.0", func() {
So(operators.ToNumberOrString(values.NewFloat(1.0)), ShouldEqual, 1.0)
})
Convey("string type 0 turns string type 0", func() {
So(operators.ToNumberOrString(values.NewString("0")), ShouldEqual, "0")
})
Convey("string type 1 turns string type 1", func() {
So(operators.ToNumberOrString(values.NewString("1")), ShouldEqual, "1")
})
Convey("string type -1 turns string type -1", func() {
So(operators.ToNumberOrString(values.NewString("-1")), ShouldEqual, "-1")
})
Convey("string type 0.0 turns string type 0.0", func() {
So(operators.ToNumberOrString(values.NewString("0.0")), ShouldEqual, "0.0")
})
Convey("a turns a", func() {
So(operators.ToNumberOrString(values.NewString("a")), ShouldEqual, "a")
})
Convey("true turns 1", func() {
So(operators.ToNumberOrString(values.NewBoolean(true)), ShouldEqual, 1)
})
Convey("false turns 0", func() {
So(operators.ToNumberOrString(values.NewBoolean(false)), ShouldEqual, 0)
})
Convey("current DateTime turns unix", func() {
So(operators.ToNumberOrString(values.NewDateTime(time.Now())), ShouldEqual, time.Now().Unix())
})
Convey("zero value DateTime turns 0", func() {
var zero time.Time
So(operators.ToNumberOrString(values.NewDateTime(zero)), ShouldEqual, 0)
})
Convey("[1] turns 1", func() {
arg := values.NewArrayWith(values.NewInt(1))
So(operators.ToNumberOrString(arg), ShouldEqual, 1)
})
Convey("[1, 2] turns 3", func() {
arg := values.NewArrayWith(values.NewInt(1), values.NewInt(2))
So(operators.ToNumberOrString(arg), ShouldEqual, 3)
})
Convey("[a] turns 0", func() {
arg := values.NewArrayWith(values.NewString("a"))
So(operators.ToNumberOrString(arg), ShouldEqual, 0)
})
Convey("[true] turns 1", func() {
arg := values.NewArrayWith(values.NewBoolean(true))
So(operators.ToNumberOrString(arg), ShouldEqual, 1)
})
Convey("[false] turns 0", func() {
arg := values.NewArrayWith(values.NewBoolean(false))
So(operators.ToNumberOrString(arg), ShouldEqual, 0)
})
})
}
func TestToNumberOnly(t *testing.T) {
Convey("Invert to int", t, func() {
Convey("0 turns 0", func() {
So(operators.ToNumberOnly(values.NewInt(0)), ShouldEqual, 0)
})
Convey("1 turns 1", func() {
So(operators.ToNumberOnly(values.NewInt(1)), ShouldEqual, 1)
})
Convey("-1 turns -1", func() {
So(operators.ToNumberOnly(values.NewInt(-1)), ShouldEqual, -1)
})
Convey("0.0 turns 0.0", func() {
So(operators.ToNumberOnly(values.NewFloat(0.0)), ShouldEqual, 0.0)
})
Convey("-1.0 turns -1.0", func() {
So(operators.ToNumberOnly(values.NewFloat(-1.0)), ShouldEqual, -1.0)
})
Convey("1.0 turns 1.0", func() {
So(operators.ToNumberOnly(values.NewFloat(1.0)), ShouldEqual, 1.0)
})
Convey("string type 0 turns 0", func() {
So(operators.ToNumberOnly(values.NewString("0")), ShouldEqual, 0)
})
Convey("string type 1 turns 1", func() {
So(operators.ToNumberOnly(values.NewString("1")), ShouldEqual, 1)
})
Convey("string type -1 turns -1", func() {
So(operators.ToNumberOnly(values.NewString("-1")), ShouldEqual, -1)
})
Convey("string type 0.0 turns 0.0", func() {
So(operators.ToNumberOnly(values.NewString("0.0")), ShouldEqual, 0.0)
})
Convey("a turns 0", func() {
So(operators.ToNumberOnly(values.NewString("a")), ShouldEqual, 0)
})
Convey("true turns 1", func() {
So(operators.ToNumberOnly(values.NewBoolean(true)), ShouldEqual, 1)
})
Convey("false turns 0", func() {
So(operators.ToNumberOnly(values.NewBoolean(false)), ShouldEqual, 0)
})
Convey("current DateTime turns unix", func() {
So(operators.ToNumberOnly(values.NewDateTime(time.Now())), ShouldEqual, time.Now().Unix())
})
Convey("zero value DateTime turns 0", func() {
var zero time.Time
So(operators.ToNumberOnly(values.NewDateTime(zero)), ShouldEqual, 0)
})
Convey("[1] turns 1", func() {
arg := values.NewArrayWith(values.NewInt(1))
So(operators.ToNumberOnly(arg), ShouldEqual, 1)
})
Convey("[1, 2] turns 3", func() {
arg := values.NewArrayWith(values.NewInt(1), values.NewInt(2))
So(operators.ToNumberOnly(arg), ShouldEqual, 3)
})
Convey("[a] turns 0", func() {
arg := values.NewArrayWith(values.NewString("a"))
So(operators.ToNumberOnly(arg), ShouldEqual, 0)
})
Convey("[true] turns 1", func() {
arg := values.NewArrayWith(values.NewBoolean(true))
So(operators.ToNumberOnly(arg), ShouldEqual, 1)
})
Convey("[false] turns 0", func() {
arg := values.NewArrayWith(values.NewBoolean(false))
So(operators.ToNumberOnly(arg), ShouldEqual, 0)
})
})
}