Skip to content

Commit

Permalink
add unit tests for runtime/core - value (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
agneum authored and ziflex committed Oct 28, 2018
1 parent 6e06652 commit 7da10c5
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions pkg/runtime/core/value_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 7da10c5

Please sign in to comment.