Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/napsy/go-css

go 1.19

require github.com/stretchr/testify v1.10.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
226 changes: 99 additions & 127 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,160 +3,132 @@ package css
import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseSimple(t *testing.T) {
ex1 := `rule {
style1: value1;
style2: value2;
}`
ex2 := `{
style1: value1;
}`
/*
ex3 := `rule {
style1: value1
style2: value2;
}`
*/
ex4 := `rule {
style1: value1;
style2:;
}`
ex5 := `}
rule {
style1: value1;
style2:;
}`

t.Run("GoodCSS", func(t *testing.T) {
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
rule, ok := css["rule"]
if !ok {
t.Fatal("rule 'rule' doesn't exist")
}

if value, ok := rule["style1"]; !ok {
t.Fatal("syle 'style1' doesn't exist")
} else if value != "value1" {
t.Fatalf("incorrect value for 'style1', got '%v', expected 'value1'", value)
}

if value, ok := rule["style2"]; !ok {
t.Fatal("style 'style2' doesn't exist")
} else if value != "value2" {
t.Fatalf("incorrect value for 'style2', got '%v', expected 'value2'", value)
}
})

t.Run("MissingRule", func(t *testing.T) {
if _, err := Unmarshal([]byte(ex2)); err == nil {
t.Fatal("should error out")
}
})

/*
t.Run("StatementMissingSemicolon", func(t *testing.T) {
if css, err := Unmarshal([]byte(ex3)); err == nil {
for k, v := range css {
t.Logf("k: %v, v: %v\n", k, v)
}
t.Fatal("should error out")
}
})
*/
t.Run("StyleWithoutValue", func(t *testing.T) {
if _, err := Unmarshal([]byte(ex4)); err == nil {
t.Fatal("should error out")
}
})
t.Run("BlockEndsWithoutBeginning", func(t *testing.T) {
if _, err := Unmarshal([]byte(ex5)); err == nil {
t.Fatal("should error out")
}
})
}

func TestParseHarder(t *testing.T) {
t.Run("MultiRules", func(t *testing.T) {
ex1 := `rule1 {
ex2 := `rule1 {
style1: value1;
style2: value2;
}
rule2 {
style3: value3;
}`
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
if len(css) != 2 {
t.Fatalf("expected 2 rules, got %d", len(css))
}

if _, ok := css["rule1"]; !ok {
t.Fatal("missing rule 'rule1'")
}
if _, ok := css["rule2"]; !ok {
t.Fatal("missing rule 'rule2'")
}

if len(css["rule1"]) != 2 {
t.Fatalf("expected 2 styles for rule 'rule1', got %d", len(css["rule1"]))
}

if len(css["rule2"]) != 1 {
t.Fatalf("expected 1 style for rule 'rule2', got %d", len(css["rule2"]))
}
})
t.Run("PropertyWithSpace", func(t *testing.T) {
ex1 := `body {
ex3 := `body {
font-family: 'Zil', serif;
}`
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}

if css["body"]["font-family"] != "'Zil', serif" {
t.Fatalf("invalid rule 'font-family', got %q", css["body"]["font-family"])
}
})
t.Run("MergedRules", func(t *testing.T) {
ex1 := `rule1 {
ex4 := `rule1 {
style1: value1;
style2: value2;
}
rule1 {
style1: value3;
}`
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
if len(css) != 1 {
t.Fatalf("there should be only one rule, got %d", len(css))
}
if len(css["rule1"]) != 2 {
t.Fatalf("there should be two styles for rule 'rule1', got %d", len(css["rule1"]))
}
if css["rule1"]["style1"] != "value3" {
t.Fatalf("value of 'style1' should be 'value3' but got '%v'", css["rule1"]["style1"])
}
})
t.Run("RealWorldCSS", func(t *testing.T) {
ex1 := `body {

ex5 := `body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}`
_, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
})

cases := []struct {
name string
CSS string
expected map[Rule]map[string]string
}{
{"Single rule (simple)", ex1, map[Rule]map[string]string{
"rule": {
"style1": "value1",
"style2": "value2",
},
}},
{"Multiple rules", ex2, map[Rule]map[string]string{
"rule1": {
"style1": "value1",
"style2": "value2",
},
"rule2": {
"style3": "value3",
},
}},
{"Property with spaces", ex3, map[Rule]map[string]string{
"body": {
"font-family": "'Zil', serif",
},
}},
{"Merged rules", ex4, map[Rule]map[string]string{
"rule1": {
"style1": "value3",
"style2": "value2",
},
}},
{"Real world css", ex5, map[Rule]map[string]string{
"body": {
"background-image": "url(\"gradient_bg.png\")",
"background-repeat": "repeat-x",
},
}},
}

for _, tt := range cases {
t.Run("GoodCSS", func(t *testing.T) {
css, err := Unmarshal([]byte(tt.CSS))
if err != nil {
t.Fatal(err)
}

assert.Equal(t, tt.expected, css, "Expected CSS to be equal")
})
}
}

func TestParseError(t *testing.T) {
ex1 := `{
style1: value1;
}`

ex2 := `rule {
style1: value1;
style2:;
}`

ex3 := `rule {
style1: value1
style2: value2;
}`

ex4 := `}
rule {
style1: value1;
style2:;
}`
_ = ex3

cases := []struct {
name string
CSS string
}{
{"Missing rule", ex1},
{"Missing style", ex2},
// TODO: this hsould not crash
//{"Statement Missing Semicolon", ex3},
{"BlockEndsWithoutBeginning", ex4},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if _, err := Unmarshal([]byte(tt.CSS)); err == nil {
t.Fatal("Should return error!")
}
})
}
}

func TestParseSelectors(t *testing.T) {
Expand Down