github.com/MontFerret/ferret@v0.18.0/pkg/runtime/values/boolean_test.go (about)

     1  package values_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/smartystreets/goconvey/convey"
     7  
     8  	"github.com/MontFerret/ferret/pkg/runtime/core"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values"
    10  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    11  )
    12  
    13  func TestBoolean(t *testing.T) {
    14  	Convey(".MarshalJSON", t, func() {
    15  		Convey("Should serialize a boolean items", func() {
    16  			b := values.True
    17  			marshaled, err := b.MarshalJSON()
    18  
    19  			So(err, ShouldBeNil)
    20  
    21  			So(string(marshaled), ShouldEqual, "true")
    22  		})
    23  	})
    24  
    25  	Convey(".Type", t, func() {
    26  		Convey("Should return a type", func() {
    27  			So(values.True.Type().Equals(types.Boolean), ShouldBeTrue)
    28  		})
    29  	})
    30  
    31  	Convey(".Unwrap", t, func() {
    32  		Convey("Should return an unwrapped items", func() {
    33  			So(values.True.Unwrap(), ShouldHaveSameTypeAs, true)
    34  		})
    35  	})
    36  
    37  	Convey(".String", t, func() {
    38  		Convey("Should return a string representation ", func() {
    39  			So(values.True.String(), ShouldEqual, "true")
    40  		})
    41  	})
    42  
    43  	Convey(".Compare", t, func() {
    44  		Convey("It should return 1 when compared to None", func() {
    45  			So(values.True.Compare(values.None), ShouldEqual, 1)
    46  		})
    47  
    48  		Convey("It should return -1 for all non-boolean values", func() {
    49  			vals := []core.Value{
    50  				values.NewString("foo"),
    51  				values.NewInt(1),
    52  				values.NewFloat(1.1),
    53  				values.NewArray(10),
    54  				values.NewObject(),
    55  			}
    56  
    57  			for _, v := range vals {
    58  				So(values.True.Compare(v), ShouldEqual, -1)
    59  				So(values.False.Compare(v), ShouldEqual, -1)
    60  			}
    61  		})
    62  
    63  		Convey("It should return 0 when both are True or False", func() {
    64  			So(values.True.Compare(values.True), ShouldEqual, 0)
    65  			So(values.False.Compare(values.False), ShouldEqual, 0)
    66  		})
    67  
    68  		Convey("It should return 1 when other is false", func() {
    69  			So(values.True.Compare(values.False), ShouldEqual, 1)
    70  		})
    71  
    72  		Convey("It should return -1 when other are true", func() {
    73  			So(values.False.Compare(values.True), ShouldEqual, -1)
    74  		})
    75  	})
    76  
    77  	Convey(".Hash", t, func() {
    78  		Convey("It should calculate hash", func() {
    79  			So(values.True.Hash(), ShouldBeGreaterThan, 0)
    80  			So(values.False.Hash(), ShouldBeGreaterThan, 0)
    81  		})
    82  
    83  		Convey("Hash sum should be consistent", func() {
    84  			So(values.True.Hash(), ShouldEqual, values.True.Hash())
    85  			So(values.False.Hash(), ShouldEqual, values.False.Hash())
    86  		})
    87  	})
    88  }