Use the XPath compiler for XPath/CSS specs
This commit is contained in:
parent
3300a6df49
commit
a7744b7a5c
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'ancestor-or-self axis' do
|
||||
before do
|
||||
@document = parse('<a><b><c></c></b></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'ancestor axis' do
|
||||
before do
|
||||
@document = parse('<a><b><c></c></b></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'attribute axis' do
|
||||
before do
|
||||
@document = parse('<a foo="bar"></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'child axis' do
|
||||
before do
|
||||
@document = parse('<a><b></b></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'descendant-or-self axis' do
|
||||
before do
|
||||
@document = parse('<a><b><b><c class="x"></c></b></b></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'descendant axis' do
|
||||
before do
|
||||
@document = parse('<a><b><c></c></b><a></a></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'following-sibling axis' do
|
||||
before do
|
||||
# Strip whitespace so it's easier to retrieve/compare elements.
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'following axis' do
|
||||
before do
|
||||
# Strip whitespace so it's easier to retrieve/compare elements.
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'namespace axis' do
|
||||
before do
|
||||
@document = parse('<root xmlns:x="x"><foo xmlns:y="y"></foo></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'parent axis' do
|
||||
before do
|
||||
@document = parse('<a><b></b></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'preceding-sibling axis' do
|
||||
before do
|
||||
@document = parse(<<-EOF.strip.gsub(/\s+/m, ''))
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'preceding axis' do
|
||||
before do
|
||||
@document = parse(<<-EOF.strip.gsub(/\s+/m, ''))
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'self axis' do
|
||||
before do
|
||||
@document = parse('<a><b>foo</b><b>bar<c>test</c></b></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'boolean() function' do
|
||||
before do
|
||||
@document = parse('<root><a>foo</a></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'ceiling() function' do
|
||||
before do
|
||||
@document = parse('<root>10.123</root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'concat() function' do
|
||||
before do
|
||||
@document = parse('<root><a>foo</a><b>bar</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'contains() function' do
|
||||
before do
|
||||
@document = parse('<root><a>foo</a><b>foobar</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'count() function' do
|
||||
before do
|
||||
@document = parse('<root><a><b></b></a><a></a></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'false() function' do
|
||||
before do
|
||||
@document = parse('')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'floor() function' do
|
||||
before do
|
||||
@document = parse('<root>10.123</root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'id() function' do
|
||||
before do
|
||||
@document = parse('<root id="r1"><a id="a1"></a><a id="a2">a1</a></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'lang() function' do
|
||||
before do
|
||||
@document = parse('<root xml:lang="en"><a></a><a xml:lang="nl"></a></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'last() function' do
|
||||
before do
|
||||
@document = parse('<root><a>foo</a><a>bar</a></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'local-name() function' do
|
||||
before do
|
||||
@document = parse('<root xmlns:x="y"><x:a></x:a><b x:num="10"></b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'name() function' do
|
||||
before do
|
||||
@document = parse('<root xmlns:x="y"><x:a></x:a><b x:num="10"></b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'namespace-uri() function' do
|
||||
before do
|
||||
@document = parse('<root xmlns:x="y"><x:a></x:a><b x:num="10"></b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'normalize-space() function' do
|
||||
before do
|
||||
@document = parse('<root><a> fo o </a></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'not() function' do
|
||||
before do
|
||||
@document = parse('<root>foo</root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'number() function' do
|
||||
before do
|
||||
@document = parse('<root><a>10</a><b>10.5</b><!--10--></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'position() function' do
|
||||
before do
|
||||
@document = parse('<root><a>foo</a><a>bar</a></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'round() function' do
|
||||
before do
|
||||
@document = parse('<root>10.123</root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'starts-with() function' do
|
||||
before do
|
||||
@document = parse('<root><a>foo</a><b>foobar</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'string-length() function' do
|
||||
before do
|
||||
@document = parse('<root><a>x</a></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'string() function' do
|
||||
before do
|
||||
@document = parse(<<-EOF)
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'substring-after() function' do
|
||||
before do
|
||||
@document = parse('<root><a>-</a><b>a-b-c</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'substring-before() function' do
|
||||
before do
|
||||
@document = parse('<root><a>-</a><b>a-b-c</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'substring() function' do
|
||||
before do
|
||||
@document = parse('<root><a>foobar</a><b>3</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'sum() spec' do
|
||||
before do
|
||||
@document = parse('<root><a>1</a><b>2</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'translate() function' do
|
||||
before do
|
||||
@document = parse('<root><a>bar</a><b>abc</b><c>ABC</c></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'true() function' do
|
||||
before do
|
||||
@document = parse('')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'querying HTML documents' do
|
||||
before do
|
||||
@document = parse_html('<html xmlns="foo"><body></body></html>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'add operator' do
|
||||
before do
|
||||
@document = parse('<root><a>1</a><b>2</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'and operator' do
|
||||
before do
|
||||
@document = parse('<root><a>1</a><b>1</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'div operator' do
|
||||
before do
|
||||
@document = parse('<root><a>1</a><b>2</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'equal operator' do
|
||||
before do
|
||||
@document = parse('<root><a>10</a><b class="foo">10</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'greater-than operator' do
|
||||
before do
|
||||
@document = parse('<root><a>10</a><b>20</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'greater-than-or-equal operator' do
|
||||
before do
|
||||
@document = parse('<root><a>10</a><b>20</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'lower-than operator' do
|
||||
before do
|
||||
@document = parse('<root><a>10</a><b>20</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'lower-than-or-equal operator' do
|
||||
before do
|
||||
@document = parse('<root><a>10</a><b>20</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'mod operator' do
|
||||
before do
|
||||
@document = parse('<root><a>2</a><b>3</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'multiplication operator' do
|
||||
before do
|
||||
@document = parse('<root><a>2</a><b>3</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'not-equal operator' do
|
||||
before do
|
||||
@document = parse('<root><a>10</a><b>10</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'or operator' do
|
||||
before do
|
||||
@document = parse('<root><a>1</a><b>1</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'pipe operator' do
|
||||
before do
|
||||
@document = parse('<root><a></a><b></b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'subtraction operator' do
|
||||
before do
|
||||
@document = parse('<root><a>2</a><b>3</b></root>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'paths' do
|
||||
before do
|
||||
@document = parse('<a xmlns:ns1="x">Foo<b></b><b></b><ns1:c></ns1:c></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'predicates' do
|
||||
before do
|
||||
@document = parse(<<-EOF)
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'comment() tests' do
|
||||
before do
|
||||
@document = parse('<a><!--foo--><b><!--bar--></b></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'node() tests' do
|
||||
before do
|
||||
@document = parse('<a><b>foo</b><!--foo--><![CDATA[bar]]></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'processing-instruction() tests' do
|
||||
before do
|
||||
@document = parse('<a><?a foo ?><b><?b bar ?></b></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'text() tests' do
|
||||
before do
|
||||
@document = parse('<a>foo<b>bar</b></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'float types' do
|
||||
before do
|
||||
@document = parse('')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'integer types' do
|
||||
before do
|
||||
@document = parse('')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'string types' do
|
||||
before do
|
||||
@document = parse('')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'variable bindings' do
|
||||
before do
|
||||
@document = parse('<a></a>')
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
describe Oga::XPath::Compiler do
|
||||
describe 'wildcard paths' do
|
||||
before do
|
||||
@document = parse('<a xmlns:ns1="x"><b></b><b></b><ns1:c></ns1:c></a>')
|
|
@ -1,208 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
before do
|
||||
@document = parse('<a>Hello</a>')
|
||||
@evaluator = described_class.new(@document)
|
||||
end
|
||||
|
||||
describe '#function_node' do
|
||||
before do
|
||||
@document = parse('<root><a>Hello</a></root>')
|
||||
@context_set = @document.children
|
||||
end
|
||||
|
||||
it 'returns the first node in the expression' do
|
||||
exp = s(:axis, 'child', s(:test, nil, 'a'))
|
||||
node = @evaluator.function_node(@context_set, exp)
|
||||
|
||||
node.should == @context_set[0].children[0]
|
||||
end
|
||||
|
||||
it 'raises a TypeError if the expression did not return a node set' do
|
||||
exp = s(:call, 'false')
|
||||
block = lambda { @evaluator.function_node(@context_set, exp) }
|
||||
|
||||
block.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it 'uses the current context node if the expression is empty' do
|
||||
@evaluator.function_node(@context_set).should == @context_set[0]
|
||||
end
|
||||
end
|
||||
|
||||
describe '#first_node_text' do
|
||||
it 'returns the text of the first node' do
|
||||
@evaluator.first_node_text(@document.children).should == 'Hello'
|
||||
end
|
||||
|
||||
it 'returns an empty string if the node set is empty' do
|
||||
set = Oga::XML::NodeSet.new
|
||||
|
||||
@evaluator.first_node_text(set).should == ''
|
||||
end
|
||||
end
|
||||
|
||||
describe '#child_nodes' do
|
||||
before do
|
||||
@children = Oga::XML::NodeSet.new([
|
||||
Oga::XML::Element.new(:name => 'b'),
|
||||
Oga::XML::Element.new(:name => 'b')
|
||||
])
|
||||
|
||||
@parent = Oga::XML::Element.new(:name => 'a', :children => @children)
|
||||
end
|
||||
|
||||
it "returns a node's child nodes" do
|
||||
nodes = @evaluator.child_nodes([@parent])
|
||||
|
||||
nodes[0].should == @children[0]
|
||||
nodes[1].should == @children[1]
|
||||
end
|
||||
end
|
||||
|
||||
describe '#node_matches?' do
|
||||
describe 'without a namespace' do
|
||||
before do
|
||||
@node = Oga::XML::Element.new(:name => 'a')
|
||||
end
|
||||
|
||||
it 'returns true if a node is matched by its name' do
|
||||
@evaluator.node_matches?(@node, s(:test, nil, 'a')).should == true
|
||||
end
|
||||
|
||||
it 'returns true if a node is matched by a wildcard name' do
|
||||
@evaluator.node_matches?(@node, s(:test, nil, '*')).should == true
|
||||
end
|
||||
|
||||
it 'returns false if a node is not matched by its name' do
|
||||
@evaluator.node_matches?(@node, s(:test, nil, 'foo')).should == false
|
||||
end
|
||||
|
||||
it 'returns true if a node is matched without having a namespace' do
|
||||
@evaluator.node_matches?(@node, s(:test, '*', 'a')).should == true
|
||||
end
|
||||
|
||||
it 'returns true if the node type matches' do
|
||||
@evaluator.node_matches?(@node, s(:type_test, 'node')).should == true
|
||||
end
|
||||
|
||||
it 'returns false when trying to match an XML::Text instance' do
|
||||
text = Oga::XML::Text.new(:text => 'Foobar')
|
||||
|
||||
@evaluator.node_matches?(text, s(:test, nil, 'a')).should == false
|
||||
end
|
||||
|
||||
it 'returns true if a node is matched case insensitively' do
|
||||
node = Oga::XML::Element.new(:name => 'DiV')
|
||||
|
||||
@evaluator.node_matches?(node, s(:test, nil, 'div')).should == true
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with a custom namespace' do
|
||||
before do
|
||||
@node = Oga::XML::Element.new(:name => 'b', :namespace_name => 'x')
|
||||
|
||||
@node.register_namespace('x', 'y')
|
||||
end
|
||||
|
||||
it 'returns true if a node is matched by its name and namespace' do
|
||||
@evaluator.node_matches?(@node, s(:test, 'x', 'b')).should == true
|
||||
end
|
||||
|
||||
it 'returns false if a node is not matched by its namespace' do
|
||||
@evaluator.node_matches?(@node, s(:test, 'y', 'b')).should == false
|
||||
end
|
||||
|
||||
it 'returns true if a node is matched by a wildcard namespace' do
|
||||
@evaluator.node_matches?(@node, s(:test, '*', 'b')).should == true
|
||||
end
|
||||
|
||||
it 'returns true if a node is matched by a full wildcard search' do
|
||||
@evaluator.node_matches?(@node, s(:test, '*', '*')).should == true
|
||||
end
|
||||
|
||||
it 'returns false when the node has a namespace that is not given' do
|
||||
@evaluator.node_matches?(@node, s(:test, nil, 'b')).should == false
|
||||
end
|
||||
|
||||
it 'returns true if a node with a namespace is matched using a wildcard' do
|
||||
@evaluator.node_matches?(@node, s(:test, nil, '*')).should == true
|
||||
end
|
||||
end
|
||||
|
||||
describe 'using the default XML namespace' do
|
||||
before do
|
||||
@node = Oga::XML::Element.new(:name => 'a')
|
||||
ns = Oga::XML::DEFAULT_NAMESPACE
|
||||
|
||||
@node.register_namespace(ns.name, ns.uri)
|
||||
end
|
||||
|
||||
it 'returns true when the node name matches' do
|
||||
@evaluator.node_matches?(@node, s(:test, nil, 'a')).should == true
|
||||
end
|
||||
|
||||
it 'returns true when the namespace prefix and node name match' do
|
||||
@evaluator.node_matches?(@node, s(:test, 'xmlns', 'a')).should == true
|
||||
end
|
||||
|
||||
it 'returns false when the node name does not match' do
|
||||
@evaluator.node_matches?(@node, s(:test, nil, 'b')).should == false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#type_matches?' do
|
||||
before do
|
||||
@element = Oga::XML::Element.new(:name => 'a')
|
||||
@ns = Oga::XML::Namespace.new(:name => 'a')
|
||||
end
|
||||
|
||||
it 'returns true if the type matches' do
|
||||
@evaluator.type_matches?(@element, s(:type_test, 'node')).should == true
|
||||
end
|
||||
|
||||
it 'returns false if the type does not match' do
|
||||
@evaluator.type_matches?(@ns, s(:type_test, 'node')).should == false
|
||||
end
|
||||
end
|
||||
|
||||
describe '#has_parent?' do
|
||||
before do
|
||||
@parent = Oga::XML::Element.new
|
||||
@child = Oga::XML::Element.new
|
||||
|
||||
set = Oga::XML::NodeSet.new([@child], @parent)
|
||||
end
|
||||
|
||||
it 'returns true if a node has a parent node' do
|
||||
@evaluator.has_parent?(@child).should == true
|
||||
end
|
||||
|
||||
it 'returns false if a node has no parent node' do
|
||||
@evaluator.has_parent?(@parent).should == false
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_string' do
|
||||
it 'converts a float to a string' do
|
||||
@evaluator.to_string(10.5).should == '10.5'
|
||||
end
|
||||
|
||||
it 'converts a float without decimals to a string' do
|
||||
@evaluator.to_string(10.0).should == '10'
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_float' do
|
||||
it 'converts a string to a float' do
|
||||
@evaluator.to_float('10').should == 10.0
|
||||
end
|
||||
|
||||
it "returns NaN for values that can't be converted to floats" do
|
||||
@evaluator.to_float('a').should be_nan
|
||||
end
|
||||
end
|
||||
end
|
|
@ -6,7 +6,11 @@ module Oga
|
|||
# @return [Oga::XML::NodeSet]
|
||||
#
|
||||
def evaluate_xpath(document, xpath)
|
||||
return Oga::XPath::Evaluator.new(document).evaluate(xpath)
|
||||
ast = parse_xpath(xpath)
|
||||
compiler = Oga::XPath::Compiler.new
|
||||
block = compiler.compile(ast)
|
||||
|
||||
block.call(document)
|
||||
end
|
||||
|
||||
##
|
||||
|
@ -17,9 +21,11 @@ module Oga
|
|||
# @return [Oga::XML::NodeSet]
|
||||
#
|
||||
def evaluate_css(document, css)
|
||||
xpath = parse_css(css)
|
||||
ast = parse_css(css)
|
||||
compiler = Oga::XPath::Compiler.new
|
||||
block = compiler.compile(ast)
|
||||
|
||||
return Oga::XPath::Evaluator.new(document).evaluate_ast(xpath)
|
||||
block.call(document)
|
||||
end
|
||||
end # EvaluationHelpers
|
||||
end # Oga
|
||||
|
|
Loading…
Reference in New Issue