...

Source file src/github.com/PuerkitoBio/goquery/type_test.go

Documentation: github.com/PuerkitoBio/goquery

     1  package goquery
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/andybalholm/cascadia"
    11  	"golang.org/x/net/html"
    12  )
    13  
    14  // Test helper functions and members
    15  var doc *Document
    16  var doc2 *Document
    17  var doc3 *Document
    18  var docB *Document
    19  var docW *Document
    20  
    21  func Doc() *Document {
    22  	if doc == nil {
    23  		doc = loadDoc("page.html")
    24  	}
    25  	return doc
    26  }
    27  
    28  func Doc2() *Document {
    29  	if doc2 == nil {
    30  		doc2 = loadDoc("page2.html")
    31  	}
    32  	return doc2
    33  }
    34  
    35  func Doc2Clone() *Document {
    36  	return CloneDocument(Doc2())
    37  }
    38  
    39  func Doc3() *Document {
    40  	if doc3 == nil {
    41  		doc3 = loadDoc("page3.html")
    42  	}
    43  	return doc3
    44  }
    45  
    46  func Doc3Clone() *Document {
    47  	return CloneDocument(Doc3())
    48  }
    49  
    50  func DocB() *Document {
    51  	if docB == nil {
    52  		docB = loadDoc("gotesting.html")
    53  	}
    54  	return docB
    55  }
    56  
    57  func DocW() *Document {
    58  	if docW == nil {
    59  		docW = loadDoc("gowiki.html")
    60  	}
    61  	return docW
    62  }
    63  
    64  func assertLength(t *testing.T, nodes []*html.Node, length int) {
    65  	if len(nodes) != length {
    66  		t.Errorf("Expected %d nodes, found %d.", length, len(nodes))
    67  		for i, n := range nodes {
    68  			t.Logf("Node %d: %+v.", i, n)
    69  		}
    70  	}
    71  }
    72  
    73  func assertClass(t *testing.T, sel *Selection, class string) {
    74  	if !sel.HasClass(class) {
    75  		t.Errorf("Expected node to have class %s, found %+v.", class, sel.Get(0))
    76  	}
    77  }
    78  
    79  func assertPanic(t *testing.T) {
    80  	if e := recover(); e == nil {
    81  		t.Error("Expected a panic.")
    82  	}
    83  }
    84  
    85  func assertEqual(t *testing.T, s1 *Selection, s2 *Selection) {
    86  	if s1 != s2 {
    87  		t.Error("Expected selection objects to be the same.")
    88  	}
    89  }
    90  
    91  func assertSelectionIs(t *testing.T, sel *Selection, is ...string) {
    92  	for i := 0; i < sel.Length(); i++ {
    93  		if !sel.Eq(i).Is(is[i]) {
    94  			t.Errorf("Expected node %d to be %s, found %+v", i, is[i], sel.Get(i))
    95  		}
    96  	}
    97  }
    98  
    99  func printSel(t *testing.T, sel *Selection) {
   100  	if testing.Verbose() {
   101  		h, err := sel.Html()
   102  		if err != nil {
   103  			t.Fatal(err)
   104  		}
   105  		t.Log(h)
   106  	}
   107  }
   108  
   109  func loadDoc(page string) *Document {
   110  	var f *os.File
   111  	var e error
   112  
   113  	if f, e = os.Open(fmt.Sprintf("./testdata/%s", page)); e != nil {
   114  		panic(e.Error())
   115  	}
   116  	defer f.Close()
   117  
   118  	var node *html.Node
   119  	if node, e = html.Parse(f); e != nil {
   120  		panic(e.Error())
   121  	}
   122  	return NewDocumentFromNode(node)
   123  }
   124  
   125  func loadString(t *testing.T, doc string) *Document {
   126  	d, err := NewDocumentFromReader(strings.NewReader(doc))
   127  	if err != nil {
   128  		t.Error("Failed to parse test document")
   129  	}
   130  	return d
   131  }
   132  
   133  func TestNewDocument(t *testing.T) {
   134  	if f, e := os.Open("./testdata/page.html"); e != nil {
   135  		t.Error(e.Error())
   136  	} else {
   137  		defer f.Close()
   138  		if node, e := html.Parse(f); e != nil {
   139  			t.Error(e.Error())
   140  		} else {
   141  			doc = NewDocumentFromNode(node)
   142  		}
   143  	}
   144  }
   145  
   146  func TestNewDocumentFromReader(t *testing.T) {
   147  	cases := []struct {
   148  		src string
   149  		err bool
   150  		sel string
   151  		cnt int
   152  	}{
   153  		0: {
   154  			src: `
   155  <html>
   156  <head>
   157  <title>Test</title>
   158  <body>
   159  <h1>Hi</h1>
   160  </body>
   161  </html>`,
   162  			sel: "h1",
   163  			cnt: 1,
   164  		},
   165  		1: {
   166  			// Actually pretty hard to make html.Parse return an error
   167  			// based on content...
   168  			src: `<html><body><aef<eqf>>>qq></body></ht>`,
   169  		},
   170  	}
   171  	buf := bytes.NewBuffer(nil)
   172  
   173  	for i, c := range cases {
   174  		buf.Reset()
   175  		buf.WriteString(c.src)
   176  
   177  		d, e := NewDocumentFromReader(buf)
   178  		if (e != nil) != c.err {
   179  			if c.err {
   180  				t.Errorf("[%d] - expected error, got none", i)
   181  			} else {
   182  				t.Errorf("[%d] - expected no error, got %s", i, e)
   183  			}
   184  		}
   185  		if c.sel != "" {
   186  			s := d.Find(c.sel)
   187  			if s.Length() != c.cnt {
   188  				t.Errorf("[%d] - expected %d nodes, found %d", i, c.cnt, s.Length())
   189  			}
   190  		}
   191  	}
   192  }
   193  
   194  func TestNewDocumentFromResponseNil(t *testing.T) {
   195  	_, e := NewDocumentFromResponse(nil)
   196  	if e == nil {
   197  		t.Error("Expected error, got none")
   198  	}
   199  }
   200  
   201  func TestIssue103(t *testing.T) {
   202  	d, err := NewDocumentFromReader(strings.NewReader("<html><title>Scientists Stored These Images in DNA—Then Flawlessly Retrieved Them</title></html>"))
   203  	if err != nil {
   204  		t.Error(err)
   205  	}
   206  	text := d.Find("title").Text()
   207  	for i, r := range text {
   208  		t.Logf("%d: %d - %q\n", i, r, string(r))
   209  	}
   210  	t.Log(text)
   211  }
   212  
   213  func TestSingle(t *testing.T) {
   214  	data := `
   215  <html>
   216    <body>
   217      <div class="b">1</div>
   218      <div class="a">2</div>
   219      <div class="a">3</div>
   220      <p class="b">4</p>
   221    </body>
   222  </html>
   223  `
   224  	doc, err := NewDocumentFromReader(strings.NewReader(data))
   225  	if err != nil {
   226  		t.Fatal(err)
   227  	}
   228  
   229  	text := doc.FindMatcher(Single("div")).Text()
   230  	if text != "1" {
   231  		t.Fatalf("want %q, got %q", "1", text)
   232  	}
   233  
   234  	// Verify semantic equivalence
   235  	sel1 := doc.Find("div").First()
   236  	sel2 := doc.FindMatcher(Single("div"))
   237  	if sel1.Text() != sel2.Text() {
   238  		t.Fatalf("want sel1 to equal sel2")
   239  	}
   240  
   241  	// Here, the Single has no effect as the selector is used to filter
   242  	// from the existing selection, not to find nodes in the document.
   243  	divs := doc.Find("div")
   244  	text = divs.FilterMatcher(Single(".a")).Text()
   245  	if text != "23" {
   246  		t.Fatalf("want %q, got %q", "23", text)
   247  	}
   248  
   249  	classA := cascadia.MustCompile(".a")
   250  	classB := cascadia.MustCompile(".b")
   251  	text = doc.FindMatcher(classB).AddMatcher(SingleMatcher(classA)).Text()
   252  	if text != "142" {
   253  		t.Fatalf("want %q, got %q", "142", text)
   254  	}
   255  }
   256  

View as plain text