github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/array_test.go (about) 1 package goquery 2 3 import ( 4 "testing" 5 ) 6 7 func TestFirst(t *testing.T) { 8 sel := Doc().Find(".pvk-content").First() 9 AssertLength(t, sel.Nodes, 1) 10 } 11 12 func TestFirstEmpty(t *testing.T) { 13 defer AssertPanic(t) 14 Doc().Find(".pvk-zzcontentzz").First() 15 } 16 17 func TestFirstRollback(t *testing.T) { 18 sel := Doc().Find(".pvk-content") 19 sel2 := sel.First().End() 20 AssertEqual(t, sel, sel2) 21 } 22 23 func TestLast(t *testing.T) { 24 sel := Doc().Find(".pvk-content").Last() 25 AssertLength(t, sel.Nodes, 1) 26 27 // Should contain Footer 28 foot := Doc().Find(".footer") 29 if !sel.Contains(foot.Nodes[0]) { 30 t.Error("Last .pvk-content should contain .footer.") 31 } 32 } 33 34 func TestLastRollback(t *testing.T) { 35 sel := Doc().Find(".pvk-content") 36 sel2 := sel.Last().End() 37 AssertEqual(t, sel, sel2) 38 } 39 40 func TestEq(t *testing.T) { 41 sel := Doc().Find(".pvk-content").Eq(1) 42 AssertLength(t, sel.Nodes, 1) 43 } 44 45 func TestEqNegative(t *testing.T) { 46 sel := Doc().Find(".pvk-content").Eq(-1) 47 AssertLength(t, sel.Nodes, 1) 48 49 // Should contain Footer 50 foot := Doc().Find(".footer") 51 if !sel.Contains(foot.Nodes[0]) { 52 t.Error("Index -1 of .pvk-content should contain .footer.") 53 } 54 } 55 56 func TestEqRollback(t *testing.T) { 57 sel := Doc().Find(".pvk-content") 58 sel2 := sel.Eq(1).End() 59 AssertEqual(t, sel, sel2) 60 } 61 62 func TestSlice(t *testing.T) { 63 sel := Doc().Find(".pvk-content").Slice(0, 2) 64 65 AssertLength(t, sel.Nodes, 2) 66 } 67 68 func TestSliceOutOfBounds(t *testing.T) { 69 defer AssertPanic(t) 70 Doc().Find(".pvk-content").Slice(2, 12) 71 } 72 73 func TestNegativeSliceStart(t *testing.T) { 74 sel := Doc().Find(".container-fluid").Slice(-2, 3) 75 AssertLength(t, sel.Nodes, 1) 76 AssertSelectionIs(t, sel.Eq(0), "#cf3") 77 } 78 79 func TestNegativeSliceEnd(t *testing.T) { 80 sel := Doc().Find(".container-fluid").Slice(1, -1) 81 AssertLength(t, sel.Nodes, 2) 82 AssertSelectionIs(t, sel.Eq(0), "#cf2") 83 AssertSelectionIs(t, sel.Eq(1), "#cf3") 84 } 85 86 func TestNegativeSliceBoth(t *testing.T) { 87 sel := Doc().Find(".container-fluid").Slice(-3, -1) 88 AssertLength(t, sel.Nodes, 2) 89 AssertSelectionIs(t, sel.Eq(0), "#cf2") 90 AssertSelectionIs(t, sel.Eq(1), "#cf3") 91 } 92 93 func TestNegativeSliceOutOfBounds(t *testing.T) { 94 defer AssertPanic(t) 95 Doc().Find(".container-fluid").Slice(-12, -7) 96 } 97 98 func TestSliceRollback(t *testing.T) { 99 sel := Doc().Find(".pvk-content") 100 sel2 := sel.Slice(0, 2).End() 101 AssertEqual(t, sel, sel2) 102 } 103 104 func TestGet(t *testing.T) { 105 sel := Doc().Find(".pvk-content") 106 node := sel.Get(1) 107 if sel.Nodes[1] != node { 108 t.Errorf("Expected node %v to be %v.", node, sel.Nodes[1]) 109 } 110 } 111 112 func TestGetNegative(t *testing.T) { 113 sel := Doc().Find(".pvk-content") 114 node := sel.Get(-3) 115 if sel.Nodes[0] != node { 116 t.Errorf("Expected node %v to be %v.", node, sel.Nodes[0]) 117 } 118 } 119 120 func TestGetInvalid(t *testing.T) { 121 defer AssertPanic(t) 122 sel := Doc().Find(".pvk-content") 123 sel.Get(129) 124 } 125 126 func TestIndex(t *testing.T) { 127 sel := Doc().Find(".pvk-content") 128 if i := sel.Index(); i != 1 { 129 t.Errorf("Expected index of 1, got %v.", i) 130 } 131 } 132 133 func TestIndexSelector(t *testing.T) { 134 sel := Doc().Find(".hero-unit") 135 if i := sel.IndexSelector("div"); i != 4 { 136 t.Errorf("Expected index of 4, got %v.", i) 137 } 138 } 139 140 func TestIndexOfNode(t *testing.T) { 141 sel := Doc().Find("div.pvk-gutter") 142 if i := sel.IndexOfNode(sel.Nodes[1]); i != 1 { 143 t.Errorf("Expected index of 1, got %v.", i) 144 } 145 } 146 147 func TestIndexOfNilNode(t *testing.T) { 148 sel := Doc().Find("div.pvk-gutter") 149 if i := sel.IndexOfNode(nil); i != -1 { 150 t.Errorf("Expected index of -1, got %v.", i) 151 } 152 } 153 154 func TestIndexOfSelection(t *testing.T) { 155 sel := Doc().Find("div") 156 sel2 := Doc().Find(".hero-unit") 157 if i := sel.IndexOfSelection(sel2); i != 4 { 158 t.Errorf("Expected index of 4, got %v.", i) 159 } 160 }