require 'action_tree' describe ActionTree::Basic::Node do # PATH PARSING AND DESCENDING describe 'descend' do it 'returns self when descending to nil, "", "/" or []' do [nil, '', '/', []].each do |location| subject.descend(location).should == subject end end it 'returns a new node when descending one level' do n = subject.descend('/the') n.should be_a(ActionTree::Basic::Node) n.should_not == subject end it 'returns a new node when descending two levels' do n = subject.descend('/the/world') n.should be_a(ActionTree::Basic::Node) n.should_not == subject n.should_not == subject.descend('/the') end it 'gives same node when descending two times, one level' do subject.descend('/the').should == subject.descend('/the') end it 'gives same node when descending two times, three levels' do subject.descend('/the/world/is').should == subject.descend('/the/world/is') end it 'gives birth recursively and selectively when descending' do n = subject.descend('/the/world/is/good') n.token.should == 'good' n.should == subject.descend('the').descend('world/is').descend('good') end end # TOKEN DIFFERENTIATION describe 'with string token' do subject { ActionTree::Basic::Node.new('noodles') } it 'matches the same token' do subject.match?('noodles').should be_true end it 'does not match another token' do subject.match?('naddles').should be_false end it 'names no captures' do subject.capture_names.should == [] end end describe 'with string token with four/five captures' do subject do ActionTree::Basic::Node.new(':year-:month-:day-:word-and-:word') end it 'matches a valid match' do subject.match?('2007-08-20-widgets-and-gadgets').should be_true end it 'does not match invalid match' do subject.match?('2007-08-20-widgets-gadgets').should be_false end it 'names the four captures' do subject.capture_names.should == %w{year month day word word} end end describe 'with regexp capture' do subject { ActionTree::Basic::Node.new(/[0-9]+\.[0-9]+/) } it 'matches the same token' do subject.match?('65.2').should be_true end it 'does not match another token' do subject.match?('beef').should be_false subject.match?('abba.beatles').should be_false end it 'captures to "match"' do subject.capture_names.should == ['match'] end end describe 'with grouped regexp capture' do subject { ActionTree::Basic::Node.new(/([0-9]+)\.([0-9]+)/) } it 'matches the same token' do subject.match?('65.2').should be_true end it 'does not match another token' do subject.match?('beef').should be_false subject.match?('abba.beatles').should be_false end it 'captures to "match"' do subject.capture_names.should == ['match'] end end describe 'with symbol capture' do subject { ActionTree::Basic::Node.new(:hometown) } it 'matches any word' do %w{Osaka Oslo Baden-Baden}.each do |town| subject.match?(town).should be_true end end it 'names the capture' do subject.capture_names.should == ['hometown'] end end describe 'dsl' do it 'mounts' do pending end it 'defines helpers' do subject.helpers { def guru; 'Ramana'; end } o = Object.new o.extend(subject.helper_scope) o.guru.should == 'Ramana' end end end