Basic specs for the XPath compiler
This commit is contained in:
parent
f10b7aa065
commit
08c965bfbc
|
@ -1,7 +1,7 @@
|
||||||
module Oga
|
module Oga
|
||||||
module XPath
|
module XPath
|
||||||
##
|
##
|
||||||
# Compiling of XPath ASTs into Ruby source code.
|
# Compiling of XPath ASTs into Ruby code.
|
||||||
#
|
#
|
||||||
# The Compiler class can be used to turn an XPath AST into Ruby source code
|
# The Compiler class can be used to turn an XPath AST into Ruby source code
|
||||||
# that can be executed to match XML nodes in a given input document/element.
|
# that can be executed to match XML nodes in a given input document/element.
|
||||||
|
@ -9,6 +9,15 @@ module Oga
|
||||||
# recompiling the same expression over and over again.
|
# recompiling the same expression over and over again.
|
||||||
#
|
#
|
||||||
class Compiler
|
class Compiler
|
||||||
|
##
|
||||||
|
# Compiles an XPath AST into a Ruby Proc.
|
||||||
|
#
|
||||||
|
# @param [AST::Node] ast
|
||||||
|
# @return [Proc]
|
||||||
|
#
|
||||||
|
def compile(ast)
|
||||||
|
|
||||||
|
end
|
||||||
end # Compiler
|
end # Compiler
|
||||||
end # XPath
|
end # XPath
|
||||||
end # Oga
|
end # Oga
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Oga::XPath::Compiler do
|
||||||
|
before do
|
||||||
|
@compiler = described_class.new
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#compile' do
|
||||||
|
it 'returns a Proc as a lambda' do
|
||||||
|
ast = parse_xpath('foo')
|
||||||
|
block = @compiler.compile(ast)
|
||||||
|
|
||||||
|
block.should be_an_instance_of(Proc)
|
||||||
|
block.lambda?.should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a Proc with a single required argument' do
|
||||||
|
ast = parse_xpath('foo')
|
||||||
|
|
||||||
|
# Only the input document is required.
|
||||||
|
@compiler.compile(ast).arity.should == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
xit 'caches a compiled Proc for a given XPath AST' do
|
||||||
|
# TODO
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'calling the compiled Proc' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
doc = parse_xml('<foo></foo>')
|
||||||
|
ast = parse_xpath('foo')
|
||||||
|
block = @compiler.compile(ast)
|
||||||
|
|
||||||
|
block.call(doc).should be_an_instance_of(NodeSet)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue