package cascadia import ( "fmt" "strings" "testing" "golang.org/x/net/html" ) type testSpec struct { // html, css selector HTML, selector string // correct specificity spec Specificity } var testsSpecificity = []testSpec{ { HTML: `
`, selector: ":not(em, strong#foo)", spec: Specificity{1, 0, 1}, }, { HTML: `
`, selector: "*", spec: Specificity{0, 0, 0}, }, { HTML: `
`, selector: "ul", spec: Specificity{0, 0, 1}, }, { HTML: `
`, selector: "ul li", spec: Specificity{0, 0, 2}, }, { HTML: `
`, selector: "ul ol+li", spec: Specificity{0, 0, 3}, }, { HTML: `
`, selector: "H1 + *[REL=up] ", spec: Specificity{0, 1, 1}, }, { HTML: ``, selector: "UL OL LI.red", spec: Specificity{0, 1, 3}, }, { HTML: ``, selector: "LI.red.level", spec: Specificity{0, 2, 1}, }, { HTML: ``, selector: "#x34y", spec: Specificity{1, 0, 0}, }, { HTML: ``, selector: "#s12:not(FOO)", spec: Specificity{1, 0, 1}, }, { HTML: ``, selector: "#s12:not(FOO)", spec: Specificity{1, 0, 1}, }, { HTML: ``, selector: "#s12:empty", spec: Specificity{1, 1, 0}, }, { HTML: ``, selector: "#s12:only-child", spec: Specificity{1, 1, 0}, }, } func setupSel(selector, HTML string) (Sel, *html.Node, error) { s, err := Parse(selector) if err != nil { return nil, nil, fmt.Errorf("error compiling %q: %s", selector, err) } doc, err := html.Parse(strings.NewReader(HTML)) if err != nil { return nil, nil, fmt.Errorf("error parsing %q: %s", HTML, err) } return s, doc, nil } func TestSpecificity(t *testing.T) { for _, test := range testsSpecificity { s, doc, err := setupSel(test.selector, test.HTML) if err != nil { t.Fatal(err) } body := doc.FirstChild.LastChild testNode := body.FirstChild.FirstChild.LastChild if !s.Match(testNode) { t.Errorf("%s didn't match (html tree : \n %s) \n", test.selector, nodeString(doc)) continue } gotSpec := s.Specificity() if gotSpec != test.spec { t.Errorf("wrong specificity : expected %v, got %v", test.spec, gotSpec) } } } func TestCompareSpecificity(t *testing.T) { s1, s2 := Specificity{1, 1, 0}, Specificity{1, 0, 0} if s1.Less(s2) { t.Fatal() } if s1.Less(s1) { t.Fatal() } }