Skip to content

Commit

Permalink
implementing a bunch of NodeSet elements. closes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed May 15, 2009
1 parent 9ce4595 commit 8188b10
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
* Nokogiri::XML::Node#accept implements Visitor pattern
* bin/nokogiri for easily examining documents (Thanks Yutaka HARA!)
* Nokogiri::XML::NodeSet now supports more Array and Enumerable operators:
index, delete, slice, - (difference), + (concatenation), and & (intersection).
index, delete, slice, - (difference), + (concatenation), & (intersection),
push, pop, shift, ==

* Bugfixes

Expand Down
29 changes: 29 additions & 0 deletions lib/nokogiri/xml/node_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,35 @@ def to_xml *args

alias :size :length
alias :to_ary :to_a

###
# Removes the last element from set and returns it, or +nil+ if
# the set is empty
def pop
return nil if length == 0
delete last
end

###
# Returns the first element of the NodeSet and removes it. Returns
# +nil+ if the set is empty.
def shift
return nil if length == 0
delete first
end

###
# Equality -- Two NodeSets are equal if the contain the same number
# of elements and if each element is equal to the corresponding
# element in the other NodeSet
def == other
return false unless other.is_a?(Nokogiri::XML::NodeSet)
return false unless length == other.length
each_with_index do |node, i|
return false unless node == other[i]
end
true
end
end
end
end
53 changes: 52 additions & 1 deletion test/xml/test_node_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,58 @@ module XML
class TestNodeSet < Nokogiri::TestCase
def setup
super
@xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
@xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
end

def test_double_equal
assert node_set_one = @xml.xpath('//employee')
assert node_set_two = @xml.xpath('//employee')

assert_not_equal node_set_one.object_id, node_set_two.object_id

assert_equal node_set_one, node_set_two
end

def test_node_set_not_equal_to_string
node_set_one = @xml.xpath('//employee')
assert_not_equal node_set_one, "asdfadsf"
end

def test_out_of_order_not_equal
one = @xml.xpath('//employee')
two = @xml.xpath('//employee')
two.push two.shift
assert_not_equal one, two
end

def test_shorter_is_not_equal
node_set_one = @xml.xpath('//employee')
node_set_two = @xml.xpath('//employee')
node_set_two.delete(node_set_two.first)

assert_not_equal node_set_one, node_set_two
end

def test_pop
set = @xml.xpath('//employee')
last = set.last
assert_equal last, set.pop
end

def test_shift
set = @xml.xpath('//employee')
first = set.first
assert_equal first, set.shift
end

def test_shift_empty
set = Nokogiri::XML::NodeSet.new(@xml)
assert_nil set.shift
end

def test_pop_empty
set = Nokogiri::XML::NodeSet.new(@xml)
assert_nil set.pop
end

def test_first_takes_arguments
Expand Down

0 comments on commit 8188b10

Please sign in to comment.