From f557aaef761b2b9bd9020a2459cd37382b1e708d Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 13 Jun 2014 00:16:40 +0200 Subject: [PATCH] Lexer specs for the various XPath axes. --- spec/oga/xpath/lexer/axes_spec.rb | 140 ++++++++++++++++++++++++++++-- 1 file changed, 131 insertions(+), 9 deletions(-) diff --git a/spec/oga/xpath/lexer/axes_spec.rb b/spec/oga/xpath/lexer/axes_spec.rb index daf3ed5..6e2c471 100644 --- a/spec/oga/xpath/lexer/axes_spec.rb +++ b/spec/oga/xpath/lexer/axes_spec.rb @@ -1,19 +1,141 @@ require 'spec_helper' describe Oga::XPath::Lexer do - context 'axes' do - example 'lex an axis using the full syntax form' do - lex_xpath('/parent::node()').should == [ + context 'full axes' do + example 'lex the ancestor axis' do + lex_xpath('/ancestor::A').should == [ [:T_SLASH, nil], - [:T_AXIS, 'parent'], - [:T_IDENT, 'node'], - [:T_LPAREN, nil], - [:T_RPAREN, nil] + [:T_AXIS, 'ancestor'], + [:T_IDENT, 'A'] ] end - example 'lex an axis using the short syntax form' do - lex_xpath('/..').should == [[:T_SLASH, nil], [:T_AXIS, 'parent']] + example 'lex the ancestor-or-self axis' do + lex_xpath('/ancestor-or-self::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'ancestor-or-self'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the attribute axis' do + lex_xpath('/attribute::class').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'attribute'], + [:T_IDENT, 'class'], + ] + end + + example 'lex the child axis' do + lex_xpath('/child::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'child'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the descendant axis' do + lex_xpath('/descendant::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'descendant'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the descendant-or-self axis' do + lex_xpath('/descendant-or-self::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'descendant-or-self'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the following axis' do + lex_xpath('/following::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'following'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the follow-sibling axis' do + lex_xpath('/following-sibling::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'following-sibling'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the namespace axis' do + lex_xpath('/namespace::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'namespace'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the parent axis' do + lex_xpath('/parent::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'parent'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the preceding axis' do + lex_xpath('/preceding::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'preceding'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the preceding-sibling axis' do + lex_xpath('/preceding-sibling::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'preceding-sibling'], + [:T_IDENT, 'A'], + ] + end + + example 'lex the self axis' do + lex_xpath('/self::A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'self'], + [:T_IDENT, 'A'], + ] + end + end + + context 'short axes' do + example 'lex the @attribute axis' do + lex_xpath('/@A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'attribute'], + [:T_IDENT, 'A'] + ] + end + + example 'lex the // axis' do + lex_xpath('//A').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'descendant-or-self'], + [:T_IDENT, 'A'] + ] + end + + example 'lex the .. axis' do + lex_xpath('/..').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'parent'] + ] + end + + example 'lex the . axis' do + lex_xpath('/.').should == [ + [:T_SLASH, nil], + [:T_AXIS, 'self'] + ] end end end