require 'action_tree' require 'fileutils' describe ActionTree do describe 'integration' do subject do ActionTree.new do helpers { def h1; 'bob'; end } helpers { def h1; '1'; end } helpers { def h2; h1; end } helpers { def h4; '6'; end } before { @v = h2 + '2' } before { @v = @v + '3' } before { @v2 = 'sleigh' } a { @v } helpers('/a') {def h3; @v2; end} before('/a') { @v2 = '4' } before('/a') { @v3 = @v + h3 } a('/a') { @v3 } before('/a/b') { @v4 = @v3 + '5' } before('/a/b') { @v5 = @v4 } a('/a/b') { @v5 } not_found('a/b') { @v5 + h4 } end end it 'works, if only just a bit' do subject.match('/').run.should == '123' end it 'works' do subject.match('a/b').run.should == '12345' end it 'works with not_found' do subject.match('a/b/y').run.should == '123456' end end describe 'integration scenario' do subject do ActionTree.new do before { @msg = 'root hook ok' } with('houses') do action { 'house index' } action('create') {'newly built house'} with(:id) do action { "showing house #{@id}" } action('edit') { "editing house #{@id}" } action('destroy') { "destroying house #{@id}" } action(/[0-9]+/) {'number match'} end end before('houses/:id') { @msg = 'house hook ok' } action('long/action/weird/url') { 'long' } action(/[0-9]+/) { 'root number match' } end end it 'matches "" => self' do subject.match('').node.should == subject end it 'matches "/" => self' do subject.match('/').node.should == subject end it 'returns nil for nonexistant level one node' do subject.match('/nothing').found?.should be_false end it 'returns nil for nonexistant level two node' do subject.match('/long/nothing').found?.should be_false end it 'matches a first level request' do subject.match('/houses').node.should == subject.descend('houses') end it 'matches a second level request' do subject.match('/houses/create').node.should == subject.descend('houses/create') end it 'matches a second level capture' do subject.match('/houses/4').node.should == subject.descend('houses/:id') end it 'matches beneath second level capture' do subject.match('/houses/4/edit').node.should == subject.descend('houses/:id/edit') end it 'returns nil for nonexistant node beneath capture' do subject.match('/houses/4/nothing_here').found?.should be_false end it 'gets the path right' do subject.match('/long/action/weird/url').path.should == '/long/action/weird/url' end it 'gets the path right with captures' do subject.match('/houses/53/edit').path.should == '/houses/53/edit' end it 'ignores trailing slashes' do subject.match('/long/action').node.should == subject.match('long/action/').node end it 'matches regexp in root scope' do subject.match('/346').node.should == subject.descend([/[0-9]+/]) end it 'fills in path for regexp in root scope' do subject.match('/346').path.should == '/346' end it 'matches regexp in capture scope' do subject.match('/houses/5/4').node.should == subject.descend(['houses', :id, /[0-9]+/]) end it 'fills in path for regexp in capture scope' do subject.match('/houses/5/4').path.should == '/houses/5/4' end end end