Sha256: bb591ed579e4a0dd86f02514ed82e532f959b364a44c719f5347c45f930fd73b

Contents?: true

Size: 1.69 KB

Versions: 53

Compression:

Stored size: 1.69 KB

Contents

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

Version data entries

53 entries across 52 versions & 9 rubygems

Version Path
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/sinatra-1.0/test/erb_test.rb
adamwiggins-sinatra-0.10.1 test/erb_test.rb
sinatra-sinatra-0.10.0 test/erb_test.rb
sinatra-sinatra-0.10.1 test/erb_test.rb
sinatra-sinatra-0.9.1.3 test/erb_test.rb
sinatra-sinatra-0.9.2 test/erb_test.rb
sinatra-1.2.9 test/erb_test.rb
sinatra-base-1.0 test/erb_test.rb
sinatra-1.2.8 test/erb_test.rb
sinatra-1.2.7 test/erb_test.rb
sinatra-1.2.6 test/erb_test.rb
sinatra-1.3.0.c test/erb_test.rb
sinatra-1.2.3 test/erb_test.rb
sinatra-1.1.4 test/erb_test.rb
sinatra-1.2.2 test/erb_test.rb
sinatra-1.3.0.b test/erb_test.rb
sinatra-1.3.0.a test/erb_test.rb
sinatra-1.2.1 test/erb_test.rb
sinatra-1.2.0 test/erb_test.rb
sinatra-1.2.0.d test/erb_test.rb