github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/type_test.go (about)

     1  package goquery
     2  
     3  import (
     4  	"bytes"
     5  	"code.google.com/p/go.net/html"
     6  	"fmt"
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  // Test helper functions and members
    12  var doc *Document
    13  var doc2 *Document
    14  var docB *Document
    15  var docW *Document
    16  
    17  func Doc() *Document {
    18  	if doc == nil {
    19  		doc = LoadDoc("page.html")
    20  	}
    21  	return doc
    22  }
    23  func Doc2() *Document {
    24  	if doc2 == nil {
    25  		doc2 = LoadDoc("page2.html")
    26  	}
    27  	return doc2
    28  }
    29  func DocB() *Document {
    30  	if docB == nil {
    31  		docB = LoadDoc("gotesting.html")
    32  	}
    33  	return docB
    34  }
    35  func DocW() *Document {
    36  	if docW == nil {
    37  		docW = LoadDoc("gowiki.html")
    38  	}
    39  	return docW
    40  }
    41  
    42  func AssertLength(t *testing.T, nodes []*html.Node, length int) {
    43  	if len(nodes) != length {
    44  		t.Errorf("Expected %d nodes, found %d.", length, len(nodes))
    45  		for i, n := range nodes {
    46  			t.Logf("Node %d: %+v.", i, n)
    47  		}
    48  	}
    49  }
    50  
    51  func AssertClass(t *testing.T, sel *Selection, class string) {
    52  	if !sel.HasClass(class) {
    53  		t.Errorf("Expected node to have class %s, found %+v.", class, sel.Get(0))
    54  	}
    55  }
    56  
    57  func AssertPanic(t *testing.T) {
    58  	if e := recover(); e == nil {
    59  		t.Error("Expected a panic.")
    60  	}
    61  }
    62  
    63  func AssertEqual(t *testing.T, s1 *Selection, s2 *Selection) {
    64  	if s1 != s2 {
    65  		t.Error("Expected selection objects to be the same.")
    66  	}
    67  }
    68  
    69  func AssertSelectionIs(t *testing.T, sel *Selection, is ...string) {
    70  	for i := 0; i < sel.Length(); i++ {
    71  		if !sel.Eq(i).Is(is[i]) {
    72  			t.Errorf("Expected node %d to be %s, found %+v", i, is[i], sel.Get(i))
    73  		}
    74  	}
    75  }
    76  
    77  func LoadDoc(page string) *Document {
    78  	if f, e := os.Open(fmt.Sprintf("./testdata/%s", page)); e != nil {
    79  		panic(e.Error())
    80  	} else {
    81  		defer f.Close()
    82  		if node, e := html.Parse(f); e != nil {
    83  			panic(e.Error())
    84  		} else {
    85  			return NewDocumentFromNode(node)
    86  		}
    87  	}
    88  	return nil
    89  }
    90  
    91  func TestNewDocument(t *testing.T) {
    92  	if f, e := os.Open("./testdata/page.html"); e != nil {
    93  		t.Error(e.Error())
    94  	} else {
    95  		defer f.Close()
    96  		if node, e := html.Parse(f); e != nil {
    97  			t.Error(e.Error())
    98  		} else {
    99  			doc = NewDocumentFromNode(node)
   100  		}
   101  	}
   102  }
   103  
   104  func TestNewDocumentFromReader(t *testing.T) {
   105  	cases := []struct {
   106  		src string
   107  		err bool
   108  		sel string
   109  		cnt int
   110  	}{
   111  		0: {
   112  			src: `
   113  <html>
   114  <head>
   115  <title>Test</title>
   116  <body>
   117  <h1>Hi</h1>
   118  </body>
   119  </html>`,
   120  			sel: "h1",
   121  			cnt: 1,
   122  		},
   123  		1: {
   124  			// Actually pretty hard to make html.Parse return an error
   125  			// based on content...
   126  			src: `<html><body><aef<eqf>>>qq></body></ht>`,
   127  		},
   128  	}
   129  	buf := bytes.NewBuffer(nil)
   130  
   131  	for i, c := range cases {
   132  		buf.Reset()
   133  		buf.WriteString(c.src)
   134  
   135  		d, e := NewDocumentFromReader(buf)
   136  		if (e != nil) != c.err {
   137  			if c.err {
   138  				t.Errorf("[%d] - expected error, got none", i)
   139  			} else {
   140  				t.Errorf("[%d] - expected no error, got %s", i, e)
   141  			}
   142  		}
   143  		if c.sel != "" {
   144  			s := d.Find(c.sel)
   145  			if s.Length() != c.cnt {
   146  				t.Errorf("[%d] - expected %d nodes, found %d", i, c.cnt, s.Length())
   147  			}
   148  		}
   149  	}
   150  }