Added Attribute#each_ancestor

This commit is contained in:
Yorick Peterse 2015-08-26 22:26:46 +02:00
parent 083d048e63
commit 9899a419b7
2 changed files with 34 additions and 4 deletions

View File

@ -125,6 +125,15 @@ module Oga
"Attribute(#{segments.join(' ')})"
end
# @see [Oga::XML::Node#each_ancestor]
def each_ancestor(&block)
return unless element
yield element
element.each_ancestor(&block)
end
private
##

View File

@ -168,17 +168,38 @@ EOF
describe '#expanded_name' do
describe 'with a namespace' do
it 'returns a String' do
element = described_class.new(:namespace_name => 'foo', :name => 'bar')
attr = described_class.new(:namespace_name => 'foo', :name => 'bar')
element.expanded_name.should == 'foo:bar'
attr.expanded_name.should == 'foo:bar'
end
end
describe 'without a namespace' do
it 'returns a String' do
element = described_class.new(:name => 'bar')
attr = described_class.new(:name => 'bar')
element.expanded_name.should == 'bar'
attr.expanded_name.should == 'bar'
end
end
end
describe '#each_ancestor' do
describe 'without an element' do
it 'simply returns' do
attr = described_class.new(:name => 'class')
expect { |b| attr.each_ancestor(&b) }.to_not yield_control
end
end
describe 'with an element' do
it 'yields the supplied block for every ancestor' do
child = Oga::XML::Element.new(:name => 'b')
parent = Oga::XML::Element.new(:name => 'a', :children => [child])
attr = described_class.new(:name => 'class', :element => child)
expect { |b| attr.each_ancestor(&b) }
.to yield_successive_args(child, parent)
end
end
end