From a735e34c1939d0889c196c0db531de414fa8985a Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Wed, 13 Apr 2011 14:53:40 +0200 Subject: [PATCH] prepare for Tilt 1.3 (while remaining compatible with 1.2) --- lib/sinatra/base.rb | 6 ++++-- test/coffee_test.rb | 17 +++++++---------- test/helper.rb | 4 ++++ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index ca82ea4e..1b6b7cdf 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -523,8 +523,9 @@ module Sinatra # Calls the given block for every possible template file in views, # named name.ext, where ext is registered on engine. def find_template(views, name, engine) - Tilt.mappings.each do |ext, klass| - next unless klass == engine + yield ::File.join(views, "#{name}.#{@preferred_extension}") + Tilt.mappings.each do |ext, engines| + next unless ext != @preferred_extension and Array(engines).include? engine yield ::File.join(views, "#{name}.#{ext}") end end @@ -585,6 +586,7 @@ module Sinatra template.new(path, line.to_i, options) { body } else found = false + @preferred_extension = engine.to_s find_template(views, data, template) do |file| path ||= file # keep the initial path rather than the last one if found = File.exists?(file) diff --git a/test/coffee_test.rb b/test/coffee_test.rb index 99c3ebe8..84cdd106 100644 --- a/test/coffee_test.rb +++ b/test/coffee_test.rb @@ -23,7 +23,7 @@ class CoffeeTest < Test::Unit::TestCase it 'renders inline Coffee strings' do coffee_app { coffee "alert 'Aye!'\n" } assert ok? - assert_equal "(function() {\n alert('Aye!');\n}).call(this);\n", body + assert body.include?("alert('Aye!');") end it 'defaults content type to javascript' do @@ -52,38 +52,35 @@ class CoffeeTest < Test::Unit::TestCase it 'renders .coffee files in views path' do coffee_app { coffee :hello } assert ok? - assert_equal "(function() {\n alert(\"Aye!\");\n}).call(this);\n", body + assert_include body, "alert(\"Aye!\");" end it 'ignores the layout option' do coffee_app { coffee :hello, :layout => :layout2 } assert ok? - assert_equal "(function() {\n alert(\"Aye!\");\n}).call(this);\n", body + assert_include body, "alert(\"Aye!\");" end it "raises error if template not found" do mock_app { get('/') { coffee :no_such_template } } - assert_raise(Errno::ENOENT) { get('/') } + assert_raise(Errno::ENOENT, ArgumentError) { get('/') } end it "passes coffee options to the coffee engine" do - coffee_app { - coffee "alert 'Aye!'\n", - :no_wrap => true - } + coffee_app { coffee "alert 'Aye!'\n", :no_wrap => true } assert ok? assert_equal "alert('Aye!');", body end it "passes default coffee options to the coffee engine" do - mock_app { + mock_app do set :coffee, :no_wrap => true # default coffee style is :nested get '/' do coffee "alert 'Aye!'\n" end - } + end get '/' assert ok? assert_equal "alert('Aye!');", body diff --git a/test/helper.rb b/test/helper.rb index b75b0301..99ec8245 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -65,6 +65,10 @@ class Test::Unit::TestCase assert_equal value.lstrip.gsub(/\s*\n\s*/, ""), body.lstrip.gsub(/\s*\n\s*/, "") end + def assert_include(str, substr) + assert str.include?(substr), "expected #{str.inspect} to include #{substr.inspect}" + end + # Delegate other missing methods to response. def method_missing(name, *args, &block) if response && response.respond_to?(name)