github.com/MontFerret/ferret@v0.18.0/pkg/drivers/common/styles_test.go (about) 1 package common_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/MontFerret/ferret/pkg/drivers/common" 8 "github.com/MontFerret/ferret/pkg/runtime/core" 9 "github.com/MontFerret/ferret/pkg/runtime/values" 10 11 . "github.com/smartystreets/goconvey/convey" 12 ) 13 14 type style struct { 15 raw string 16 name values.String 17 value core.Value 18 } 19 20 func TestDeserializeStyles(t *testing.T) { 21 Convey("DeserializeStyles", t, func() { 22 styles := []style{ 23 { 24 raw: "min-height: 1.15", 25 name: "min-height", 26 value: values.NewFloat(1.15), 27 }, 28 { 29 raw: "background-color: #4A154B", 30 name: "background-color", 31 value: values.NewString("#4A154B"), 32 }, 33 { 34 raw: "font-size:26pt", 35 name: "font-size", 36 value: values.NewString("26pt"), 37 }, 38 { 39 raw: "page-break-after:avoid", 40 name: "page-break-after", 41 value: values.NewString("avoid"), 42 }, 43 { 44 raw: `font-family: Arial,"Helvetica Neue",Helvetica,sans-serif`, 45 name: "font-family", 46 value: values.NewString(`Arial,"Helvetica Neue",Helvetica,sans-serif`), 47 }, 48 { 49 raw: "color: black", 50 name: "color", 51 value: values.NewString("black"), 52 }, 53 { 54 raw: "display: inline-block", 55 name: "display", 56 value: values.NewString("inline-block"), 57 }, 58 { 59 raw: "min-width: 50", 60 name: "min-width", 61 value: values.NewFloat(50), 62 }, 63 } 64 65 Convey("Should parse a single style", func() { 66 for _, s := range styles { 67 out, err := common.DeserializeStyles(values.NewString(s.raw)) 68 69 So(err, ShouldBeNil) 70 So(out, ShouldNotBeNil) 71 72 value, exists := out.Get(s.name) 73 74 So(bool(exists), ShouldBeTrue) 75 76 So(value.Compare(s.value) == 0, ShouldBeTrue) 77 } 78 }) 79 80 Convey("Should parse multiple styles", func() { 81 var buff bytes.Buffer 82 83 for _, s := range styles { 84 buff.WriteString(s.raw) 85 buff.WriteString("; ") 86 } 87 88 out, err := common.DeserializeStyles(values.NewString(buff.String())) 89 90 So(err, ShouldBeNil) 91 So(out, ShouldNotBeNil) 92 So(int(out.Length()), ShouldEqual, len(styles)) 93 94 for _, s := range styles { 95 value, exists := out.Get(s.name) 96 97 So(bool(exists), ShouldBeTrue) 98 99 So(value.Compare(s.value) == 0, ShouldBeTrue) 100 } 101 }) 102 }) 103 }