Added initial CSS evaluation tests.
This commit is contained in:
parent
f6319ed0c7
commit
1f3b4cb2fb
spec
|
@ -0,0 +1,127 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'CSS selector evaluation' do
|
||||
before do
|
||||
@document = parse('<a xmlns:ns1="x"><b></b><b></b><ns1:c></ns1:c></a>')
|
||||
end
|
||||
|
||||
context 'regular paths' do
|
||||
before do
|
||||
@set = evaluate_css(@document, 'a')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'return the <a> node' do
|
||||
@set[0].should == @document.children[0]
|
||||
end
|
||||
end
|
||||
|
||||
context 'nested paths' do
|
||||
before do
|
||||
@set = evaluate_css(@document, 'a b')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 2
|
||||
|
||||
example 'include the first <b> node' do
|
||||
@set[0].should == @document.children[0].children[0]
|
||||
end
|
||||
|
||||
example 'include the second <b> node' do
|
||||
@set[1].should == @document.children[0].children[1]
|
||||
end
|
||||
end
|
||||
|
||||
context 'paths with namespaces' do
|
||||
before do
|
||||
@set = evaluate_css(@document, 'a ns1|c')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'include the <n1:c> node' do
|
||||
@set[0].should == @document.children[0].children[2]
|
||||
end
|
||||
end
|
||||
|
||||
context 'paths with name wildcards' do
|
||||
before do
|
||||
@set = evaluate_css(@document, 'a *')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 3
|
||||
|
||||
example 'include the first <b> node' do
|
||||
@set[0].should == @document.children[0].children[0]
|
||||
end
|
||||
|
||||
example 'include the second <b> node' do
|
||||
@set[1].should == @document.children[0].children[1]
|
||||
end
|
||||
|
||||
example 'include the second <ns1:c> node' do
|
||||
@set[2].should == @document.children[0].children[2]
|
||||
end
|
||||
end
|
||||
|
||||
context 'paths with namespace wildcards' do
|
||||
before do
|
||||
@set = evaluate_css(@document, 'a *|c')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'include the <ns1:c> node' do
|
||||
@set[0].should == @document.children[0].children[2]
|
||||
end
|
||||
end
|
||||
|
||||
context 'paths with namespaces and name wildcards' do
|
||||
before do
|
||||
@set = evaluate_css(@document, 'a ns1|*')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'include the <ns1:c> node' do
|
||||
@set[0].should == @document.children[0].children[2]
|
||||
end
|
||||
end
|
||||
|
||||
context 'paths with full wildcards' do
|
||||
before do
|
||||
@set = evaluate_css(@document, 'a *|*')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 3
|
||||
|
||||
example 'include the first <b> node' do
|
||||
@set[0].should == @document.children[0].children[0]
|
||||
end
|
||||
|
||||
example 'include the second <b> node' do
|
||||
@set[1].should == @document.children[0].children[1]
|
||||
end
|
||||
|
||||
example 'include the second <ns1:c> node' do
|
||||
@set[2].should == @document.children[0].children[2]
|
||||
end
|
||||
end
|
||||
|
||||
context 'paths without namespaces and with name wildcards' do
|
||||
before do
|
||||
@set = evaluate_css(@document, 'a |*')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 2
|
||||
|
||||
example 'include the first <b> node' do
|
||||
@set[0].should == @document.children[0].children[0]
|
||||
end
|
||||
|
||||
example 'include the second <b> node' do
|
||||
@set[1].should == @document.children[0].children[1]
|
||||
end
|
||||
end
|
||||
end
|
|
@ -7,11 +7,14 @@ end
|
|||
|
||||
require_relative '../lib/oga'
|
||||
require_relative 'support/parsing'
|
||||
require_relative 'support/evaluation'
|
||||
require_relative 'support/shared_examples'
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.color = true
|
||||
|
||||
config.include Oga::ParsingHelpers
|
||||
config.include Oga::EvaluationHelpers
|
||||
|
||||
config.expect_with :rspec do |c|
|
||||
c.syntax = [:should, :expect]
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
module Oga
|
||||
module EvaluationHelpers
|
||||
##
|
||||
# Parses and evaluates a CSS expression.
|
||||
#
|
||||
# @param [Oga::XML::Document] document
|
||||
# @param [String] css
|
||||
# @return [Oga::XML::NodeSet]
|
||||
#
|
||||
def evaluate_css(document, css)
|
||||
xpath = parse_css(css)
|
||||
|
||||
return Oga::XPath::Evaluator.new(document).evaluate_ast(xpath)
|
||||
end
|
||||
end # EvaluationHelpers
|
||||
end # Oga
|
Loading…
Reference in New Issue