mirror of
https://github.com/MontFerret/ferret.git
synced 2025-01-18 03:22:02 +02:00
add unit tests for runtime/core - value (#144)
This commit is contained in:
parent
6e06652a5a
commit
7da10c5a70
76
pkg/runtime/core/value_test.go
Normal file
76
pkg/runtime/core/value_test.go
Normal file
@ -0,0 +1,76 @@
|
||||
package core_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/html/static"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestTypeString(t *testing.T) {
|
||||
Convey("The string representation of the type should match this type", t, func() {
|
||||
So(core.Type(0).String(), ShouldEqual, "none")
|
||||
So(core.Type(1).String(), ShouldEqual, "boolean")
|
||||
So(core.Type(2).String(), ShouldEqual, "int")
|
||||
So(core.Type(3).String(), ShouldEqual, "float")
|
||||
So(core.Type(4).String(), ShouldEqual, "string")
|
||||
So(core.Type(5).String(), ShouldEqual, "datetime")
|
||||
So(core.Type(6).String(), ShouldEqual, "array")
|
||||
So(core.Type(7).String(), ShouldEqual, "object")
|
||||
So(core.Type(8).String(), ShouldEqual, "HTMLElement")
|
||||
So(core.Type(9).String(), ShouldEqual, "HTMLDocument")
|
||||
So(core.Type(10).String(), ShouldEqual, "BinaryType")
|
||||
})
|
||||
}
|
||||
|
||||
func TestIsTypeOf(t *testing.T) {
|
||||
Convey("Check type by value", t, func() {
|
||||
|
||||
So(core.IsTypeOf(values.None, core.NoneType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(values.NewBoolean(true), core.BooleanType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(values.NewInt(1), core.IntType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(values.NewFloat(1.1), core.FloatType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(values.NewString("test"), core.StringType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(values.NewDateTime(time.Now()), core.DateTimeType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(values.NewArray(1), core.ArrayType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(values.NewObject(), core.ObjectType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(&static.HTMLElement{}, core.HTMLElementType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(&static.HTMLDocument{}, core.HTMLDocumentType), ShouldBeTrue)
|
||||
So(core.IsTypeOf(values.NewBinary([]byte{}), core.BinaryType), ShouldBeTrue)
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidateType(t *testing.T) {
|
||||
Convey("Value should match type", t, func() {
|
||||
|
||||
So(core.ValidateType(values.None, core.NoneType), ShouldBeNil)
|
||||
So(core.ValidateType(values.NewBoolean(true), core.BooleanType), ShouldBeNil)
|
||||
So(core.ValidateType(values.NewInt(1), core.IntType), ShouldBeNil)
|
||||
So(core.ValidateType(values.NewFloat(1.1), core.FloatType), ShouldBeNil)
|
||||
So(core.ValidateType(values.NewString("test"), core.StringType), ShouldBeNil)
|
||||
So(core.ValidateType(values.NewDateTime(time.Now()), core.DateTimeType), ShouldBeNil)
|
||||
So(core.ValidateType(values.NewArray(1), core.ArrayType), ShouldBeNil)
|
||||
So(core.ValidateType(values.NewObject(), core.ObjectType), ShouldBeNil)
|
||||
So(core.ValidateType(&static.HTMLElement{}, core.HTMLElementType), ShouldBeNil)
|
||||
So(core.ValidateType(&static.HTMLDocument{}, core.HTMLDocumentType), ShouldBeNil)
|
||||
So(core.ValidateType(values.NewBinary([]byte{}), core.BinaryType), ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Value should not match type", t, func() {
|
||||
|
||||
So(core.ValidateType(values.None, core.BooleanType), ShouldBeError)
|
||||
So(core.ValidateType(values.NewBoolean(true), core.IntType, core.NoneType), ShouldBeError)
|
||||
So(core.ValidateType(values.NewInt(1), core.NoneType), ShouldBeError)
|
||||
So(core.ValidateType(values.NewFloat(1.1), core.StringType), ShouldBeError)
|
||||
So(core.ValidateType(values.NewString("test"), core.IntType, core.FloatType), ShouldBeError)
|
||||
So(core.ValidateType(values.NewDateTime(time.Now()), core.BooleanType), ShouldBeError)
|
||||
So(core.ValidateType(values.NewArray(1), core.StringType), ShouldBeError)
|
||||
So(core.ValidateType(values.NewObject(), core.BooleanType), ShouldBeError)
|
||||
So(core.ValidateType(&static.HTMLElement{}, core.ArrayType), ShouldBeError)
|
||||
So(core.ValidateType(&static.HTMLDocument{}, core.HTMLElementType), ShouldBeError)
|
||||
So(core.ValidateType(values.NewBinary([]byte{}), core.NoneType), ShouldBeError)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user