Revamped compiler type test specs

This commit is contained in:
Yorick Peterse 2015-08-30 01:45:51 +02:00
parent 001c57e0ad
commit 1b62dd3256
5 changed files with 71 additions and 50 deletions

View File

@ -1276,7 +1276,7 @@ module Oga
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
# #
def on_type_test(ast, input, &block) def on_type_test(ast, input, &block)
name, followng = *ast name, following = *ast
handler = name.gsub('-', '_') handler = name.gsub('-', '_')

View File

@ -1,20 +1,24 @@
require 'spec_helper' require 'spec_helper'
describe Oga::XPath::Compiler do describe Oga::XPath::Compiler do
describe 'comment() tests' do before do
before do @document = parse('<a><!--foo--><b><!--bar--></b></a>')
@document = parse('<a><!--foo--><b><!--bar--></b></a>')
@comment1 = @document.children[0].children[0] @comment1 = @document.children[0].children[0]
@comment2 = @document.children[0].children[1].children[0] @comment2 = @document.children[0].children[1].children[0]
end
describe 'relative to a document' do
describe 'a/comment()' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@comment1)
end
end end
it 'returns a node set containing comment nodes' do describe 'a/b/comment()' do
evaluate_xpath(@document, 'a/comment()').should == node_set(@comment1) it 'returns a NodeSet' do
end evaluate_xpath(@document).should == node_set(@comment2)
end
it 'returns a node set containing nested comments' do
evaluate_xpath(@document, 'a/b/comment()').should == node_set(@comment2)
end end
end end
end end

View File

@ -1,27 +1,38 @@
require 'spec_helper' require 'spec_helper'
describe Oga::XPath::Compiler do describe Oga::XPath::Compiler do
describe 'node() tests' do before do
before do @document = parse('<a><b>foo</b><!--foo--><![CDATA[bar]]></a>')
@document = parse('<a><b>foo</b><!--foo--><![CDATA[bar]]></a>')
@a1 = @document.children[0] @a1 = @document.children[0]
@b1 = @a1.children[0] @b1 = @a1.children[0]
@comment1 = @a1.children[1] @comment1 = @a1.children[1]
@cdata1 = @a1.children[2] @cdata1 = @a1.children[2]
end
describe 'relative to a document' do
describe 'node()' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@a1)
end
end end
it 'returns a node set containing elements' do describe 'a/node()' do
evaluate_xpath(@document, 'node()').should == node_set(@a1) it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@b1, @comment1, @cdata1)
end
end end
it 'returns a node set containing elements, comments and CDATA tags' do describe 'a/b/node()' do
evaluate_xpath(@document, 'a/node()') it 'returns NodeSet' do
.should == node_set(@b1, @comment1, @cdata1) evaluate_xpath(@document).should == node_set(@b1.children[0])
end
end end
it 'returns a node set containing text nodes' do describe 'node()/b' do
evaluate_xpath(@document, 'a/b/node()').should == node_set(@b1.children[0]) it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@b1)
end
end end
end end
end end

View File

@ -1,22 +1,24 @@
require 'spec_helper' require 'spec_helper'
describe Oga::XPath::Compiler do describe Oga::XPath::Compiler do
describe 'processing-instruction() tests' do before do
before do @document = parse('<a><?a foo ?><b><?b bar ?></b></a>')
@document = parse('<a><?a foo ?><b><?b bar ?></b></a>')
@proc_ins1 = @document.children[0].children[0] @proc_ins1 = @document.children[0].children[0]
@proc_ins2 = @document.children[0].children[1].children[0] @proc_ins2 = @document.children[0].children[1].children[0]
end
describe 'relative to a document' do
describe 'a/processing-instruction()' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@proc_ins1)
end
end end
it 'returns a node set containing processing instructions' do describe 'a/b/processing-instruction()' do
evaluate_xpath(@document, 'a/processing-instruction()') it 'returns a NodeSet' do
.should == node_set(@proc_ins1) evaluate_xpath(@document).should == node_set(@proc_ins2)
end end
it 'returns a node set containing nested processing instructions' do
evaluate_xpath(@document, 'a/b/processing-instruction()')
.should == node_set(@proc_ins2)
end end
end end
end end

View File

@ -1,20 +1,24 @@
require 'spec_helper' require 'spec_helper'
describe Oga::XPath::Compiler do describe Oga::XPath::Compiler do
describe 'text() tests' do before do
before do @document = parse('<a>foo<b>bar</b></a>')
@document = parse('<a>foo<b>bar</b></a>')
@text1 = @document.children[0].children[0] @text1 = @document.children[0].children[0]
@text2 = @document.children[0].children[1].children[0] @text2 = @document.children[0].children[1].children[0]
end
describe 'relative to a document' do
describe 'a/text()' do
it 'returns a NodeSet' do
evaluate_xpath(@document).should == node_set(@text1)
end
end end
it 'returns a node set containing text nodes' do describe 'a/b/text()' do
evaluate_xpath(@document, 'a/text()').should == node_set(@text1) it 'returns a NodeSet' do
end evaluate_xpath(@document).should == node_set(@text2)
end
it 'returns a node set containing nested text nodes' do
evaluate_xpath(@document, 'a/b/text()').should == node_set(@text2)
end end
end end
end end