diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index 507381a..e5565a1 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -1,7 +1,7 @@ module Oga 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 # 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. # class Compiler + ## + # Compiles an XPath AST into a Ruby Proc. + # + # @param [AST::Node] ast + # @return [Proc] + # + def compile(ast) + + end end # Compiler end # XPath end # Oga diff --git a/spec/oga/xpath/compiler_spec.rb b/spec/oga/xpath/compiler_spec.rb new file mode 100644 index 0000000..61e6218 --- /dev/null +++ b/spec/oga/xpath/compiler_spec.rb @@ -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('') + ast = parse_xpath('foo') + block = @compiler.compile(ast) + + block.call(doc).should be_an_instance_of(NodeSet) + end + end + end +end