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

     1  package goquery
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestIs(t *testing.T) {
     8  	sel := Doc().Find(".footer p:nth-child(1)")
     9  	if !sel.Is("p") {
    10  		t.Error("Expected .footer p:nth-child(1) to be p.")
    11  	}
    12  }
    13  
    14  func TestIsPositional(t *testing.T) {
    15  	sel := Doc().Find(".footer p:nth-child(2)")
    16  	if !sel.Is("p:nth-child(2)") {
    17  		t.Error("Expected .footer p:nth-child(2) to be p:nth-child(2).")
    18  	}
    19  }
    20  
    21  func TestIsPositionalNot(t *testing.T) {
    22  	sel := Doc().Find(".footer p:nth-child(1)")
    23  	if sel.Is("p:nth-child(2)") {
    24  		t.Error("Expected .footer p:nth-child(1) NOT to be p:nth-child(2).")
    25  	}
    26  }
    27  
    28  func TestIsFunction(t *testing.T) {
    29  	ok := Doc().Find("div").IsFunction(func(i int, s *Selection) bool {
    30  		return s.HasClass("container-fluid")
    31  	})
    32  
    33  	if !ok {
    34  		t.Error("Expected some div to have a container-fluid class.")
    35  	}
    36  }
    37  
    38  func TestIsFunctionRollback(t *testing.T) {
    39  	ok := Doc().Find("div").IsFunction(func(i int, s *Selection) bool {
    40  		return s.HasClass("container-fluid")
    41  	})
    42  
    43  	if !ok {
    44  		t.Error("Expected some div to have a container-fluid class.")
    45  	}
    46  }
    47  
    48  func TestIsSelection(t *testing.T) {
    49  	sel := Doc().Find("div")
    50  	sel2 := Doc().Find(".pvk-gutter")
    51  
    52  	if !sel.IsSelection(sel2) {
    53  		t.Error("Expected some div to have a pvk-gutter class.")
    54  	}
    55  }
    56  
    57  func TestIsSelectionNot(t *testing.T) {
    58  	sel := Doc().Find("div")
    59  	sel2 := Doc().Find("a")
    60  
    61  	if sel.IsSelection(sel2) {
    62  		t.Error("Expected some div NOT to be an anchor.")
    63  	}
    64  }
    65  
    66  func TestIsNodes(t *testing.T) {
    67  	sel := Doc().Find("div")
    68  	sel2 := Doc().Find(".footer")
    69  
    70  	if !sel.IsNodes(sel2.Nodes[0]) {
    71  		t.Error("Expected some div to have a footer class.")
    72  	}
    73  }
    74  
    75  func TestHasClass(t *testing.T) {
    76  	sel := Doc().Find("div")
    77  	if !sel.HasClass("span12") {
    78  		t.Error("Expected at least one div to have class span12.")
    79  	}
    80  }
    81  
    82  func TestHasClassNone(t *testing.T) {
    83  	sel := Doc().Find("h2")
    84  	if sel.HasClass("toto") {
    85  		t.Error("Expected h1 to have no class.")
    86  	}
    87  }
    88  
    89  func TestHasClassNotFirst(t *testing.T) {
    90  	sel := Doc().Find(".alert")
    91  	if !sel.HasClass("alert-error") {
    92  		t.Error("Expected .alert to also have class .alert-error.")
    93  	}
    94  }
    95  
    96  func TestDocContains(t *testing.T) {
    97  	sel := Doc().Find("h1")
    98  	if !Doc().Contains(sel.Nodes[0]) {
    99  		t.Error("Expected document to contain H1 tag.")
   100  	}
   101  }
   102  
   103  func TestSelContains(t *testing.T) {
   104  	sel := Doc().Find(".row-fluid")
   105  	sel2 := Doc().Find("a[ng-click]")
   106  	if !sel.Contains(sel2.Nodes[0]) {
   107  		t.Error("Expected .row-fluid to contain a[ng-click] tag.")
   108  	}
   109  }
   110  
   111  func TestSelNotContains(t *testing.T) {
   112  	sel := Doc().Find("a.link")
   113  	sel2 := Doc().Find("span")
   114  	if sel.Contains(sel2.Nodes[0]) {
   115  		t.Error("Expected a.link to NOT contain span tag.")
   116  	}
   117  }