Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 82 lines (72 sloc) 1.727 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
require File.dirname(__FILE__) + '/helper'

class ERBTest < Test::Unit::TestCase
  def erb_app(&block)
    mock_app {
      set :views, File.dirname(__FILE__) + '/views'
      get '/', &block
    }
    get '/'
  end

  it 'renders inline ERB strings' do
    erb_app { erb '<%= 1 + 1 %>' }
    assert ok?
    assert_equal '2', body
  end

  it 'renders .erb files in views path' do
    erb_app { erb :hello }
    assert ok?
    assert_equal "Hello World\n", body
  end

  it 'takes a :locals option' do
    erb_app {
      locals = {:foo => 'Bar'}
      erb '<%= foo %>', :locals => locals
    }
    assert ok?
    assert_equal 'Bar', body
  end

  it "renders with inline layouts" do
    mock_app {
      layout { 'THIS. IS. <%= yield.upcase %>!' }
      get('/') { erb 'Sparta' }
    }
    get '/'
    assert ok?
    assert_equal 'THIS. IS. SPARTA!', body
  end

  it "renders with file layouts" do
    erb_app {
      erb 'Hello World', :layout => :layout2
    }
    assert ok?
    assert_equal "ERB Layout!\nHello World\n", body
  end

  it "renders erb with blocks" do
    mock_app {
      def container
        @_out_buf << "THIS."
        yield
        @_out_buf << "SPARTA!"
      end
      def is; "IS." end
      get '/' do
        erb '<% container do %> <%= is %> <% end %>'
      end
    }
    get '/'
    assert ok?
    assert_equal 'THIS. IS. SPARTA!', body
  end

  it "can be used in a nested fashion for partials and whatnot" do
    mock_app {
      template(:inner) { "<inner><%= 'hi' %></inner>" }
      template(:outer) { "<outer><%= erb :inner %></outer>" }
      get '/' do
        erb :outer
      end
    }

    get '/'
    assert ok?
    assert_equal '<outer><inner>hi</inner></outer>', body
  end
end
Something went wrong with that request. Please try again.