From 1b2ab3a269b020024bb3854b93247f8f76f171e1 Mon Sep 17 00:00:00 2001 From: Dylan Egan Date: Mon, 12 Oct 2009 17:22:01 +1100 Subject: [PATCH] ERubis support in to sinatra. With tests! Signed-off-by: Simon Rozet --- lib/sinatra/base.rb | 4 ++ test/erubis_test.rb | 82 +++++++++++++++++++++++++++++++++++++++ test/views/error.erubis | 3 ++ test/views/hello.erubis | 1 + test/views/layout2.erubis | 2 + 5 files changed, 92 insertions(+) create mode 100644 test/erubis_test.rb create mode 100644 test/views/error.erubis create mode 100644 test/views/hello.erubis create mode 100644 test/views/layout2.erubis diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index f9115cb8..098bf54b 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -288,6 +288,10 @@ module Sinatra render :erb, template, options, locals end + def erubis(template, options={}, locals={}) + render :erubis, template, options, locals + end + def haml(template, options={}, locals={}) render :haml, template, options, locals end diff --git a/test/erubis_test.rb b/test/erubis_test.rb new file mode 100644 index 00000000..447f25fa --- /dev/null +++ b/test/erubis_test.rb @@ -0,0 +1,82 @@ +require File.dirname(__FILE__) + '/helper' +require 'erubis' + +class ERubisTest < Test::Unit::TestCase + def erubis_app(&block) + mock_app { + set :views, File.dirname(__FILE__) + '/views' + get '/', &block + } + get '/' + end + + it 'renders inline ERubis strings' do + erubis_app { erubis '<%= 1 + 1 %>' } + assert ok? + assert_equal '2', body + end + + it 'renders .erubis files in views path' do + erubis_app { erubis :hello } + assert ok? + assert_equal "Hello World\n", body + end + + it 'takes a :locals option' do + erubis_app { + locals = {:foo => 'Bar'} + erubis '<%= foo %>', :locals => locals + } + assert ok? + assert_equal 'Bar', body + end + + it "renders with inline layouts" do + mock_app { + layout { 'THIS. IS. <%= yield.upcase %>!' } + get('/') { erubis 'Sparta' } + } + get '/' + assert ok? + assert_equal 'THIS. IS. SPARTA!', body + end + + it "renders with file layouts" do + erubis_app { + erubis 'Hello World', :layout => :layout2 + } + assert ok? + assert_equal "ERubis Layout!\nHello World\n", body + end + + it "renders erubis with blocks" do + mock_app { + def container + @_out_buf << "THIS." + yield + @_out_buf << "SPARTA!" + end + def is; "IS." end + get '/' do + erubis '<% 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) { "<%= 'hi' %>" } + template(:outer) { "<%= erubis :inner %>" } + get '/' do + erubis :outer + end + } + + get '/' + assert ok? + assert_equal 'hi', body + end +end diff --git a/test/views/error.erubis b/test/views/error.erubis new file mode 100644 index 00000000..b48d1f06 --- /dev/null +++ b/test/views/error.erubis @@ -0,0 +1,3 @@ +Hello <%= 'World' %> +<% raise 'Goodbye' unless defined?(french) && french %> +<% raise 'Au revoir' if defined?(french) && french %> diff --git a/test/views/hello.erubis b/test/views/hello.erubis new file mode 100644 index 00000000..bcbbc926 --- /dev/null +++ b/test/views/hello.erubis @@ -0,0 +1 @@ +Hello <%= 'World' %> diff --git a/test/views/layout2.erubis b/test/views/layout2.erubis new file mode 100644 index 00000000..b61db6d0 --- /dev/null +++ b/test/views/layout2.erubis @@ -0,0 +1,2 @@ +ERubis Layout! +<%= yield %>