require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe Garterbelt::View do class BasicView < Garterbelt::View def content end def alt_content end end before do @view = BasicView.new end describe 'rendering' do it 'clears the output on start' do @view.output = 'Foo the magic output!' @view.render @view.output.should_not include 'Foo the magic output!' end describe 'argument parsing' do describe 'render method' do it 'finds in as the first argument' do @view.should_receive(:bar) @view.render(:bar) end it 'finds it in the options' do @view.should_receive(:baz) @view.render :method => :baz end end describe ':style option' do it 'is found in options as the second argument' do @view.stub(:foo) @view.render(:foo, {:style => :minified}) @view.render_style.should == :minified end it 'is found when options are the first argument' do @view.stub(:content) @view.render(:style => :text) @view.render_style.should == :text end end end describe 'methods' do it 'renders the :content method by default' do @view.should_receive(:content) @view.render end it 'renders an alternate method when requested' do @view.should_receive(:foo) @view.render(:foo) end end it 'returns the output' do @view.render.should === @view.output end describe "render_buffer" do before do @hr = Garterbelt::ContentTag.new(:view => @view, :type => :hr) @input = Garterbelt::ContentTag.new(:view => @view, :type => :input) @img = Garterbelt::ContentTag.new(:view => @view, :type => :img) end it 'will clear the buffer' do @view._buffer = [:foo] @view.render_buffer @view._buffer.should == [] end it 'will call render on each tag in the buffer' do @view._buffer << @hr << @input << @img @hr.should_receive(:render) @input.should_receive(:render) @img.should_receive(:render) @view.render_buffer end it 'will add non renderable items to the output as strings' do @view._buffer << "foo " << :bar @view.render_buffer @view.output.should include 'foo bar' end end describe 'tag nesting' do it 'should render correctly at one layer deep' do @view._buffer << Garterbelt::ClosedTag.new(:type => :hr, :view => @view) @view.render.should == "
\n" end describe 'second level' do before do @view._buffer << Garterbelt::ContentTag.new(:type => :p, :view => @view) do @view._buffer << Garterbelt::ClosedTag.new(:type => :hr, :view => @view) end @view.render end it 'should leave an empty buffer' do @view._buffer.should be_empty end it 'should include the content' do @view.output.should include "
" end it 'should puts the nested tag inside the parent tag' do @view.output.should match /

\W*


\W*<\/p>/ end end describe 'multi level' do before do @view._buffer << Garterbelt::ContentTag.new(:type => :form, :view => @view) do @view._buffer << Garterbelt::ContentTag.new(:type => :fieldset, :view => @view) do @view._buffer << Garterbelt::ContentTag.new(:type => :label, :view => @view, :attributes => {:for => 'email'}) do @view._buffer << Garterbelt::ClosedTag.new(:type => :input, :view => @view, :attributes => {:name => 'email', :type => 'text'}) end @view._buffer << Garterbelt::ContentTag.new(:type => :input, :view => @view, :attributes => {:type => 'submit', :value => 'Login or whatever'}) end end @view.render end it 'should include the deepest level content' do @view.output.should include "" end it 'should nest properly' do @view.output.should match /
\W*
\W*]*>\W*/ @view.output.should match /^
/ @view.output.should match /^