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

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