github.com/MontFerret/ferret@v0.18.0/pkg/runtime/values/string_test.go (about) 1 package values_test 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 . "github.com/smartystreets/goconvey/convey" 9 10 "github.com/MontFerret/ferret/pkg/runtime/values" 11 ) 12 13 func TestString(t *testing.T) { 14 Convey(".Hash", t, func() { 15 Convey("It should calculate hash", func() { 16 v := values.NewString("a") 17 18 h := v.Hash() 19 20 So(h, ShouldBeGreaterThan, 0) 21 22 v2 := values.NewString("b") 23 24 So(h, ShouldNotEqual, v2.Hash()) 25 }) 26 27 Convey("Hash sum should be consistent", func() { 28 v := values.NewString("foobar") 29 30 So(v.Hash(), ShouldEqual, v.Hash()) 31 }) 32 }) 33 34 Convey(".Length", t, func() { 35 Convey("Should return unicode length", func() { 36 str := values.NewString("Спутник") 37 38 So(str.Length(), ShouldEqual, 7) 39 }) 40 }) 41 42 Convey(".MarshalJSON", t, func() { 43 Convey("It should correctly serialize value", func() { 44 value := "foobar" 45 46 json1, err := json.Marshal(value) 47 So(err, ShouldBeNil) 48 49 json2, err := values.NewString(value).MarshalJSON() 50 So(err, ShouldBeNil) 51 52 So(json1, ShouldResemble, json2) 53 }) 54 55 Convey("It should NOT escape HTML", func() { 56 value := "<div><span>Foobar</span></div>" 57 58 json1, err := json.Marshal(value) 59 So(err, ShouldBeNil) 60 61 json2, err := values.NewString(value).MarshalJSON() 62 So(err, ShouldBeNil) 63 64 So(json1, ShouldNotResemble, json2) 65 So(string(json2), ShouldEqual, fmt.Sprintf(`"%s"`, value)) 66 }) 67 }) 68 Convey(".At", t, func() { 69 Convey("It should return a character", func() { 70 v := values.NewString("abcdefg") 71 c := v.At(2) 72 73 So(string(c), ShouldEqual, "c") 74 }) 75 }) 76 }