From fe36d7039a5969315ec2dab0962f417d750db1ec Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Thu, 30 Aug 2012 15:52:41 -0400 Subject: [PATCH] add Get() to array.go, with tests --- array.go | 18 +++++++++++++++--- array_test.go | 27 +++++++++++++++++++++++++++ get.go | 18 ------------------ get_test.go | 21 --------------------- 4 files changed, 42 insertions(+), 42 deletions(-) delete mode 100644 get.go delete mode 100644 get_test.go diff --git a/array.go b/array.go index 3511713..990ee27 100644 --- a/array.go +++ b/array.go @@ -1,5 +1,9 @@ package goquery +import ( + "exp/html" +) + // First() reduces the set of matched elements to the first in the set. // It returns a new Selection object. func (this *Selection) First() *Selection { @@ -16,10 +20,8 @@ func (this *Selection) Last() *Selection { // If a negative index is given, it counts backwards starting at the end of the set. // It returns a new Selection object, and an empty Selection object if the index is invalid. func (this *Selection) Eq(index int) *Selection { - var l = len(this.Nodes) - if index < 0 { - index += l + index += len(this.Nodes) } return this.Slice(index, index+1) } @@ -29,3 +31,13 @@ func (this *Selection) Eq(index int) *Selection { func (this *Selection) Slice(start int, end int) *Selection { return pushStack(this, this.Nodes[start:end]) } + +// Get() retrieves the underlying node at the specified index. +// Get() without parameter is not implemented, since the node array is available +// on the Selection object. +func (this *Selection) Get(index int) *html.Node { + if index < 0 { + index += len(this.Nodes) // Negative index gets from the end + } + return this.Nodes[index] +} diff --git a/array_test.go b/array_test.go index 2b417b4..5ab8749 100644 --- a/array_test.go +++ b/array_test.go @@ -66,3 +66,30 @@ func TestSliceOutOfBounds(t *testing.T) { }() doc.Find(".pvk-content").Slice(2, 12) } + +func TestGet(t *testing.T) { + sel := doc.Find(".pvk-content") + node := sel.Get(1) + if sel.Nodes[1] != node { + t.Errorf("Expected node %v to be %v.", node, sel.Nodes[1]) + } +} + +func TestGetNegative(t *testing.T) { + sel := doc.Find(".pvk-content") + node := sel.Get(-3) + if sel.Nodes[0] != node { + t.Errorf("Expected node %v to be %v.", node, sel.Nodes[0]) + } +} + +func TestGetInvalid(t *testing.T) { + defer func() { + if e := recover(); e == nil { + t.Error("Expected a panic, Get() called with out of bounds index.") + } + }() + + sel := doc.Find(".pvk-content") + sel.Get(129) +} diff --git a/get.go b/get.go deleted file mode 100644 index 69e830a..0000000 --- a/get.go +++ /dev/null @@ -1,18 +0,0 @@ -package goquery - -import ( - "exp/html" -) - -// Get() without parameter is not implemented, its behaviour would be exactly the same as getting selection.Nodes -func (this *Selection) Get(index int) *html.Node { - var l = len(this.Nodes) - - if index < 0 { - index += l // Negative index gets from the end - } - if index >= 0 && index < l { - return this.Nodes[index] - } - return nil -} diff --git a/get_test.go b/get_test.go deleted file mode 100644 index 9c84d0f..0000000 --- a/get_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package goquery - -import ( - "testing" -) - -func TestGet(t *testing.T) { - sel := doc.Find(".pvk-content") - node := sel.Get(1) - if sel.Nodes[1] != node { - t.Errorf("Expected node %v to be %v.", node, sel.Nodes[1]) - } - node = sel.Get(-3) - if sel.Nodes[0] != node { - t.Errorf("Expected node %v to be %v.", node, sel.Nodes[0]) - } - node = sel.Get(129) - if node != nil { - t.Error("Expected node to be nil.") - } -}