Revamp compiler "preceding" specs

This also includes some fixes to make this axis behave correctly when
evaluate relative to a document.
This commit is contained in:
Yorick Peterse 2015-08-28 16:49:59 +02:00
parent 6b2874c507
commit e8377b360a
2 changed files with 66 additions and 28 deletions

View File

@ -352,10 +352,10 @@ module Oga
node = node_literal node = node_literal
doc_node = literal(:doc_node) doc_node = literal(:doc_node)
orig_input.is_a?(XML::Node) input.is_a?(XML::Node).if_true do
.if_true { root.assign(orig_input.root_node) } root.assign(input.root_node)
.else { root.assign(orig_input) }
.followed_by do .followed_by do
document_or_node(root).if_true do
root.each_node.add_block(doc_node) do root.each_node.add_block(doc_node) do
doc_node.eq(input) doc_node.eq(input)
.if_true { self.break } .if_true { self.break }
@ -365,6 +365,8 @@ module Oga
end end
end end
end end
end
end
# @param [AST::Node] ast # @param [AST::Node] ast
# @param [Oga::Ruby::Node] input # @param [Oga::Ruby::Node] input

View File

@ -1,10 +1,9 @@
require 'spec_helper' require 'spec_helper'
describe Oga::XPath::Compiler do describe Oga::XPath::Compiler do
describe 'preceding axis' do
before do before do
@document = parse(<<-EOF.strip.gsub(/\s+/m, '')) @document = parse(<<-EOF.strip.gsub(/^\s+|\n/m, ''))
<root> <root foo="bar">
<foo> <foo>
<bar></bar> <bar></bar>
<baz> <baz>
@ -22,18 +21,55 @@ describe Oga::XPath::Compiler do
@baz3 = @document.children[0].children[1] @baz3 = @document.children[0].children[1]
end end
it 'returns a node set containing preceding nodes of root/foo/baz' do describe 'relative to a document' do
evaluate_xpath(@document, 'root/foo/baz/preceding::bar') describe 'preceding::*' do
.should == node_set(@bar1) it 'returns an empty NodeSet' do
evaluate_xpath(@document).should == node_set
end
end end
it 'returns a node set containing preceding nodes for root/baz' do describe 'root/foo/baz/preceding::bar' do
evaluate_xpath(@document, 'root/baz/preceding::baz') it 'returns a NodeSet' do
.should == node_set(@baz1, @baz2) evaluate_xpath(@document).should == node_set(@bar1)
end
end end
it 'returns a node set containing preceding nodes relative to root/baz' do describe 'root/baz/preceding::baz' do
evaluate_xpath(@baz3, 'preceding::baz').should == node_set(@baz1, @baz2) it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@baz1, @baz2)
end
end
end
describe 'relative to an element' do
describe 'preceding::root' do
it 'returns a NodeSet' do
evaluate_xpath(@foo1).should == @document.children
end
end
describe 'preceding::baz' do
it 'returns a NodeSet' do
evaluate_xpath(@baz3).should == node_set(@baz1, @baz2)
end
end
end
describe 'relative to the root element' do
describe 'preceding::*' do
it 'returns an empty NodeSet' do
evaluate_xpath(@document.children[0]).should == node_set
end
end
end
describe 'relative to an attribute' do
describe 'preceding::*' do
it 'returns an empty NodeSet' do
root = @document.children[0]
evaluate_xpath(root.attribute('foo')).should == node_set
end
end end
end end
end end