From 5608755eb5b17bf7a131efb2b60bbf83041b5cff Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Thu, 30 Aug 2012 15:42:36 -0400 Subject: [PATCH] reorganize positional methods in array.go, tests. --- first.go => array.go | 11 ++++--- array_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++ doc.go | 18 ++++++------ first_test.go | 19 ------------- 4 files changed, 84 insertions(+), 32 deletions(-) rename first.go => array.go (71%) create mode 100644 array_test.go delete mode 100644 first_test.go diff --git a/first.go b/array.go similarity index 71% rename from first.go rename to array.go index 4babb05..3511713 100644 --- a/first.go +++ b/array.go @@ -21,8 +21,11 @@ func (this *Selection) Eq(index int) *Selection { if index < 0 { index += l } - if index > -1 && index < l { - return newSingleSelection(this.Nodes[index], this.document) - } - return newEmptySelection(this.document) + return this.Slice(index, index+1) +} + +// Slice() reduces the set of matched elements to a subset specified by a range of indices. +// At the moment, negative indices are not supported. +func (this *Selection) Slice(start int, end int) *Selection { + return pushStack(this, this.Nodes[start:end]) } diff --git a/array_test.go b/array_test.go new file mode 100644 index 0000000..2b417b4 --- /dev/null +++ b/array_test.go @@ -0,0 +1,68 @@ +package goquery + +import ( + "testing" +) + +func TestFirst(t *testing.T) { + sel := doc.Find(".pvk-content").First() + if len(sel.Nodes) != 1 { + t.Errorf("Expected 1 node, found %v.", len(sel.Nodes)) + } +} + +func TestFirstEmpty(t *testing.T) { + defer func() { + if e := recover(); e == nil { + t.Error("Expected a panic, First() called on empty Selection.") + } + }() + doc.Find(".pvk-zzcontentzz").First() +} + +func TestLast(t *testing.T) { + sel := doc.Find(".pvk-content").Last() + if len(sel.Nodes) != 1 { + t.Errorf("Expected 1 node, found %v.", len(sel.Nodes)) + } + // Should contain Footer + foot := doc.Find(".footer") + if !sel.Contains(foot.Nodes[0]) { + t.Error("Last .pvk-content should contain .footer.") + } +} + +func TestEq(t *testing.T) { + sel := doc.Find(".pvk-content").Eq(1) + if len(sel.Nodes) != 1 { + t.Errorf("Expected 1 node, found %v.", len(sel.Nodes)) + } +} + +func TestEqNegative(t *testing.T) { + sel := doc.Find(".pvk-content").Eq(-1) + if len(sel.Nodes) != 1 { + t.Errorf("Expected 1 node, found %v.", len(sel.Nodes)) + } + // Should contain Footer + foot := doc.Find(".footer") + if !sel.Contains(foot.Nodes[0]) { + t.Error("Index -1 of .pvk-content should contain .footer.") + } +} + +func TestSlice(t *testing.T) { + sel := doc.Find(".pvk-content").Slice(0, 2) + if len(sel.Nodes) != 2 { + t.Errorf("Expected 2 nodes, found %v.", len(sel.Nodes)) + } +} + +func TestSliceOutOfBounds(t *testing.T) { + defer func() { + if e := recover(); e == nil { + t.Error("Expected a panic, Slice() called with out of bounds indices.") + } + }() + doc.Find(".pvk-content").Slice(2, 12) +} diff --git a/doc.go b/doc.go index 3143eb6..32ce86a 100644 --- a/doc.go +++ b/doc.go @@ -37,14 +37,14 @@ necessary since multiple return values cannot be used to allow a chainable inter */ package goquery -// Positional Manipulation: First(), Last(), Eq(), Get(), Index(), Slice() -// Filtering: Filter(), Not(), Has(), End() -// "Expanding": Add(), AndSelf() -// Reflect (query) node: Is(), Contains(), HasClass() -// Inspect node: Contents(), Html(), Text(), Attr(), Val() -// Selection "properties": Length(), Size() -// Traversal: Find(), Children(), Parents...(), Next...(), Prev...(), Closest(), Siblings() -// Iteration: Each(), Map() +// array.go : Positional Manipulation: First(), Last(), Eq(), Get(), Index(), Slice() +// filter.go : Filtering: Filter(), Not(), Has(), End() +// expand.go : "Expanding": Add(), AndSelf() +// query.go : Reflect (query) node: Is(), Contains(), HasClass() +// property.go : Inspect node: Contents(), Html(), Text(), Attr(), Val(), Length(), Size() +// traversal.go : Traversal: Find(), Children(), Parents...(), Next...(), Prev...(), Closest(), Siblings() +// iteration.go : Iteration: Each(), Map() +// type.go : Selection and Document // TODO : Benchmarks @@ -85,7 +85,7 @@ package goquery // - PrevUntil() - Tree traversal // x PushStack() ? - Internals // - Siblings() - Tree traversal -// - Slice() - Filtering +// x Slice() - Filtering // - Text() - DOM Manipulation // x ToArray() Is not implemented, is Selection.Nodes // x Unique() ? Or internally only, to remove duplicates and maintain node order? - Utilities diff --git a/first_test.go b/first_test.go deleted file mode 100644 index c164164..0000000 --- a/first_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package goquery - -import ( - "testing" -) - -func TestFirst(t *testing.T) { - sel := doc.Find(".pvk-content").First() - if len(sel.Nodes) != 1 { - t.Errorf("Expected 1 node, found %v.", len(sel.Nodes)) - } -} - -func TestFirstEmpty(t *testing.T) { - sel := doc.Find(".pvk-zzcontentzz").First() - if len(sel.Nodes) != 0 { - t.Errorf("Expected 0 node, found %v.", len(sel.Nodes)) - } -}