...

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

Documentation: github.com/PuerkitoBio/goquery

     1  package goquery
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestFilter(t *testing.T) {
     8  	sel := Doc().Find(".span12").Filter(".alert")
     9  	assertLength(t, sel.Nodes, 1)
    10  }
    11  
    12  func TestFilterNone(t *testing.T) {
    13  	sel := Doc().Find(".span12").Filter(".zzalert")
    14  	assertLength(t, sel.Nodes, 0)
    15  }
    16  
    17  func TestFilterInvalid(t *testing.T) {
    18  	sel := Doc().Find(".span12").Filter("")
    19  	assertLength(t, sel.Nodes, 0)
    20  }
    21  
    22  func TestFilterRollback(t *testing.T) {
    23  	sel := Doc().Find(".pvk-content")
    24  	sel2 := sel.Filter(".alert").End()
    25  	assertEqual(t, sel, sel2)
    26  }
    27  
    28  func TestFilterFunction(t *testing.T) {
    29  	sel := Doc().Find(".pvk-content").FilterFunction(func(i int, s *Selection) bool {
    30  		return i > 0
    31  	})
    32  	assertLength(t, sel.Nodes, 2)
    33  }
    34  
    35  func TestFilterFunctionRollback(t *testing.T) {
    36  	sel := Doc().Find(".pvk-content")
    37  	sel2 := sel.FilterFunction(func(i int, s *Selection) bool {
    38  		return i > 0
    39  	}).End()
    40  	assertEqual(t, sel, sel2)
    41  }
    42  
    43  func TestFilterNode(t *testing.T) {
    44  	sel := Doc().Find(".pvk-content")
    45  	sel2 := sel.FilterNodes(sel.Nodes[2])
    46  	assertLength(t, sel2.Nodes, 1)
    47  }
    48  
    49  func TestFilterNodeRollback(t *testing.T) {
    50  	sel := Doc().Find(".pvk-content")
    51  	sel2 := sel.FilterNodes(sel.Nodes[2]).End()
    52  	assertEqual(t, sel, sel2)
    53  }
    54  
    55  func TestFilterSelection(t *testing.T) {
    56  	sel := Doc().Find(".link")
    57  	sel2 := Doc().Find("a[ng-click]")
    58  	sel3 := sel.FilterSelection(sel2)
    59  	assertLength(t, sel3.Nodes, 1)
    60  }
    61  
    62  func TestFilterSelectionRollback(t *testing.T) {
    63  	sel := Doc().Find(".link")
    64  	sel2 := Doc().Find("a[ng-click]")
    65  	sel2 = sel.FilterSelection(sel2).End()
    66  	assertEqual(t, sel, sel2)
    67  }
    68  
    69  func TestFilterSelectionNil(t *testing.T) {
    70  	var sel2 *Selection
    71  
    72  	sel := Doc().Find(".link")
    73  	sel3 := sel.FilterSelection(sel2)
    74  	assertLength(t, sel3.Nodes, 0)
    75  }
    76  
    77  func TestNot(t *testing.T) {
    78  	sel := Doc().Find(".span12").Not(".alert")
    79  	assertLength(t, sel.Nodes, 1)
    80  }
    81  
    82  func TestNotInvalid(t *testing.T) {
    83  	sel := Doc().Find(".span12").Not("")
    84  	assertLength(t, sel.Nodes, 2)
    85  }
    86  
    87  func TestNotRollback(t *testing.T) {
    88  	sel := Doc().Find(".span12")
    89  	sel2 := sel.Not(".alert").End()
    90  	assertEqual(t, sel, sel2)
    91  }
    92  
    93  func TestNotNone(t *testing.T) {
    94  	sel := Doc().Find(".span12").Not(".zzalert")
    95  	assertLength(t, sel.Nodes, 2)
    96  }
    97  
    98  func TestNotFunction(t *testing.T) {
    99  	sel := Doc().Find(".pvk-content").NotFunction(func(i int, s *Selection) bool {
   100  		return i > 0
   101  	})
   102  	assertLength(t, sel.Nodes, 1)
   103  }
   104  
   105  func TestNotFunctionRollback(t *testing.T) {
   106  	sel := Doc().Find(".pvk-content")
   107  	sel2 := sel.NotFunction(func(i int, s *Selection) bool {
   108  		return i > 0
   109  	}).End()
   110  	assertEqual(t, sel, sel2)
   111  }
   112  
   113  func TestNotNode(t *testing.T) {
   114  	sel := Doc().Find(".pvk-content")
   115  	sel2 := sel.NotNodes(sel.Nodes[2])
   116  	assertLength(t, sel2.Nodes, 2)
   117  }
   118  
   119  func TestNotNodeRollback(t *testing.T) {
   120  	sel := Doc().Find(".pvk-content")
   121  	sel2 := sel.NotNodes(sel.Nodes[2]).End()
   122  	assertEqual(t, sel, sel2)
   123  }
   124  
   125  func TestNotSelection(t *testing.T) {
   126  	sel := Doc().Find(".link")
   127  	sel2 := Doc().Find("a[ng-click]")
   128  	sel3 := sel.NotSelection(sel2)
   129  	assertLength(t, sel3.Nodes, 6)
   130  }
   131  
   132  func TestNotSelectionRollback(t *testing.T) {
   133  	sel := Doc().Find(".link")
   134  	sel2 := Doc().Find("a[ng-click]")
   135  	sel2 = sel.NotSelection(sel2).End()
   136  	assertEqual(t, sel, sel2)
   137  }
   138  
   139  func TestIntersection(t *testing.T) {
   140  	sel := Doc().Find(".pvk-gutter")
   141  	sel2 := Doc().Find("div").Intersection(sel)
   142  	assertLength(t, sel2.Nodes, 6)
   143  }
   144  
   145  func TestIntersectionRollback(t *testing.T) {
   146  	sel := Doc().Find(".pvk-gutter")
   147  	sel2 := Doc().Find("div")
   148  	sel2 = sel.Intersection(sel2).End()
   149  	assertEqual(t, sel, sel2)
   150  }
   151  
   152  func TestHas(t *testing.T) {
   153  	sel := Doc().Find(".container-fluid").Has(".center-content")
   154  	assertLength(t, sel.Nodes, 2)
   155  	// Has() returns the high-level .container-fluid div, and the one that is the immediate parent of center-content
   156  }
   157  
   158  func TestHasInvalid(t *testing.T) {
   159  	sel := Doc().Find(".container-fluid").Has("")
   160  	assertLength(t, sel.Nodes, 0)
   161  }
   162  
   163  func TestHasRollback(t *testing.T) {
   164  	sel := Doc().Find(".container-fluid")
   165  	sel2 := sel.Has(".center-content").End()
   166  	assertEqual(t, sel, sel2)
   167  }
   168  
   169  func TestHasNodes(t *testing.T) {
   170  	sel := Doc().Find(".container-fluid")
   171  	sel2 := Doc().Find(".center-content")
   172  	sel = sel.HasNodes(sel2.Nodes...)
   173  	assertLength(t, sel.Nodes, 2)
   174  	// Has() returns the high-level .container-fluid div, and the one that is the immediate parent of center-content
   175  }
   176  
   177  func TestHasNodesRollback(t *testing.T) {
   178  	sel := Doc().Find(".container-fluid")
   179  	sel2 := Doc().Find(".center-content")
   180  	sel2 = sel.HasNodes(sel2.Nodes...).End()
   181  	assertEqual(t, sel, sel2)
   182  }
   183  
   184  func TestHasSelection(t *testing.T) {
   185  	sel := Doc().Find("p")
   186  	sel2 := Doc().Find("small")
   187  	sel = sel.HasSelection(sel2)
   188  	assertLength(t, sel.Nodes, 1)
   189  }
   190  
   191  func TestHasSelectionRollback(t *testing.T) {
   192  	sel := Doc().Find("p")
   193  	sel2 := Doc().Find("small")
   194  	sel2 = sel.HasSelection(sel2).End()
   195  	assertEqual(t, sel, sel2)
   196  }
   197  
   198  func TestEnd(t *testing.T) {
   199  	sel := Doc().Find("p").Has("small").End()
   200  	assertLength(t, sel.Nodes, 4)
   201  }
   202  
   203  func TestEndToTop(t *testing.T) {
   204  	sel := Doc().Find("p").Has("small").End().End().End()
   205  	assertLength(t, sel.Nodes, 0)
   206  }
   207  

View as plain text