1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
package cascadia
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"reflect"
"testing"
"golang.org/x/net/html"
)
func TestInvalidSelectors(t *testing.T) {
c, err := ioutil.ReadFile("test_resources/invalid_selectors.json")
if err != nil {
t.Fatal(err)
}
var tests []invalidSelector
if err = json.Unmarshal(c, &tests); err != nil {
t.Fatal(err)
}
for _, test := range tests {
_, err := ParseGroupWithPseudoElements(test.Selector)
if err == nil {
t.Fatalf("%s -> expected error on invalid selector : %s", test.Name, test.Selector)
}
}
}
func parseReference(filename string) *html.Node {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
node, err := html.Parse(f)
if err != nil {
log.Fatal(err)
}
return node
}
func getId(n *html.Node) string {
for _, attr := range n.Attr {
if attr.Key == "id" {
return attr.Val
}
}
return ""
}
func isEqual(m map[string]int, l []string) bool {
expected := map[string]int{}
for _, s := range l {
expected[s]++
}
return reflect.DeepEqual(m, expected)
}
func loadValidSelectors(t *testing.T) []validSelector {
c, err := ioutil.ReadFile("test_resources/valid_selectors.json")
if err != nil {
t.Fatal(err)
}
var tests []validSelector
if err = json.Unmarshal(c, &tests); err != nil {
t.Fatal(err)
}
return tests
}
func TestValidSelectors(t *testing.T) {
tests := loadValidSelectors(t)
doc := parseReference("test_resources/content.xhtml")
for i, test := range tests {
if test.Xfail {
t.Logf("skiped test %s", test.Name)
continue
}
sels, err := ParseGroupWithPseudoElements(test.Selector)
if err != nil {
t.Fatalf("%s -> unable to parse valid selector : %s : %s", test.Name, test.Selector, err)
}
matchingNodes := map[*html.Node]bool{}
for _, sel := range sels {
if sel.PseudoElement() != "" {
continue // pseudo element doesn't count as a match in this test since they are not part of the document
}
for _, node := range Selector(sel.Match).MatchAll(doc) {
matchingNodes[node] = true
}
}
matchingIds := map[string]int{}
for node := range matchingNodes {
matchingIds[getId(node)]++
}
if !isEqual(matchingIds, test.Expect) {
t.Fatalf("test %d %s : expected %v got %v", i, test.Name, test.Expect, matchingIds)
}
}
}
|