Added XML::Node#before and XML::Node#after

These methods can be used to insert nodes before/after other nodes without
manually having to mess around with node sets.

This fixes #28.
This commit is contained in:
Yorick Peterse committed 2014-09-09 22:48:31 +02:00
1 parent e0f8965616
commit e2dab952d0
4 files changed
+111

No files matched your search

+43
View File
@@ -196,6 +196,49 @@ describe Oga::XML::NodeSet do
end
end
context '#insert' do
before do
@set = described_class.new
@owned_set = described_class.new([], Oga::XML::Node.new)
end
example 'insert a node into an empty node set' do
node = Oga::XML::Node.new
@set.insert(0, node)
@set[0].should == node
end
example 'do not insert a node that is already in the set' do
node = Oga::XML::Node.new
@set.insert(0, node)
@set.insert(0, node)
@set.length.should == 1
end
example 'insert a node before another node' do
node1 = Oga::XML::Node.new
node2 = Oga::XML::Node.new
@set.insert(0, node1)
@set.insert(0, node2)
@set[0].should == node2
@set[1].should == node1
end
example 'take ownership of a node when inserting into an owned set' do
node = Oga::XML::Node.new
@owned_set.insert(0, node)
node.node_set.should == @owned_set
end
end
context '#[]' do
before do
@n1 = Oga::XML::Element.new(:name => 'a')
+32
View File
@@ -178,4 +178,36 @@ describe Oga::XML::Node do
@n1.node_set.nil?.should == true
end
end
context '#before' do
before do
@node = described_class.new
@container = described_class.new(:children => [@node])
end
example 'insert a node before another node' do
other = described_class.new
@node.before(other)
@container.children[0].should == other
@container.children[1].should == @node
end
end
context '#after' do
before do
@node = described_class.new
@container = described_class.new(:children => [@node])
end
example 'insert a node after another node' do
other = described_class.new
@node.after(other)
@container.children[0].should == @node
@container.children[1].should == other
end
end
end