2011-05-11 09:44:02 +02:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2010-09-12 23:09:10 +02:00
|
|
|
|
|
|
|
begin
|
|
|
|
require 'coffee-script'
|
2011-04-11 12:46:21 +02:00
|
|
|
require 'execjs'
|
|
|
|
|
|
|
|
begin
|
|
|
|
ExecJS.compile '1'
|
|
|
|
rescue Exception
|
|
|
|
raise LoadError, 'unable to execute JavaScript'
|
|
|
|
end
|
2010-09-12 23:09:10 +02:00
|
|
|
|
2015-01-11 01:00:47 +05:30
|
|
|
class CoffeeTest < Minitest::Test
|
2010-09-19 17:57:48 +02:00
|
|
|
def coffee_app(options = {}, &block)
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2020-03-13 22:20:04 +01:00
|
|
|
set :views, __dir__ + '/views'
|
2010-09-19 17:57:48 +02:00
|
|
|
set(options)
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', &block)
|
|
|
|
end
|
2010-09-12 23:09:10 +02:00
|
|
|
get '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders inline Coffee strings' do
|
|
|
|
coffee_app { coffee "alert 'Aye!'\n" }
|
|
|
|
assert ok?
|
2011-04-13 14:53:40 +02:00
|
|
|
assert body.include?("alert('Aye!');")
|
2010-09-12 23:09:10 +02:00
|
|
|
end
|
|
|
|
|
2010-09-19 17:57:48 +02:00
|
|
|
it 'defaults content type to javascript' do
|
|
|
|
coffee_app { coffee "alert 'Aye!'\n" }
|
|
|
|
assert ok?
|
|
|
|
assert_equal "application/javascript;charset=utf-8", response['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults allows setting content type per route' do
|
|
|
|
coffee_app do
|
|
|
|
content_type :html
|
|
|
|
coffee "alert 'Aye!'\n"
|
|
|
|
end
|
|
|
|
assert ok?
|
|
|
|
assert_equal "text/html;charset=utf-8", response['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults allows setting content type globally' do
|
|
|
|
coffee_app(:coffee => { :content_type => 'html' }) do
|
|
|
|
coffee "alert 'Aye!'\n"
|
|
|
|
end
|
|
|
|
assert ok?
|
|
|
|
assert_equal "text/html;charset=utf-8", response['Content-Type']
|
|
|
|
end
|
|
|
|
|
2010-09-12 23:09:10 +02:00
|
|
|
it 'renders .coffee files in views path' do
|
|
|
|
coffee_app { coffee :hello }
|
|
|
|
assert ok?
|
2011-04-13 14:53:40 +02:00
|
|
|
assert_include body, "alert(\"Aye!\");"
|
2010-09-12 23:09:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores the layout option' do
|
|
|
|
coffee_app { coffee :hello, :layout => :layout2 }
|
|
|
|
assert ok?
|
2011-04-13 14:53:40 +02:00
|
|
|
assert_include body, "alert(\"Aye!\");"
|
2010-09-12 23:09:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises error if template not found" do
|
|
|
|
mock_app {
|
|
|
|
get('/') { coffee :no_such_template }
|
|
|
|
}
|
2015-01-11 01:00:47 +05:30
|
|
|
assert_raises(Errno::ENOENT) { get('/') }
|
2010-09-12 23:09:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes coffee options to the coffee engine" do
|
2011-04-13 14:53:40 +02:00
|
|
|
coffee_app { coffee "alert 'Aye!'\n", :no_wrap => true }
|
2010-09-12 23:09:10 +02:00
|
|
|
assert ok?
|
2011-11-09 20:05:51 +02:00
|
|
|
assert_body "alert('Aye!');"
|
2010-09-12 23:09:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes default coffee options to the coffee engine" do
|
2011-04-13 14:53:40 +02:00
|
|
|
mock_app do
|
2010-09-12 23:09:10 +02:00
|
|
|
set :coffee, :no_wrap => true # default coffee style is :nested
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { coffee "alert 'Aye!'\n" }
|
2011-04-13 14:53:40 +02:00
|
|
|
end
|
2010-09-12 23:09:10 +02:00
|
|
|
get '/'
|
|
|
|
assert ok?
|
2011-11-09 20:05:51 +02:00
|
|
|
assert_body "alert('Aye!');"
|
2010-09-12 23:09:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-25 00:27:15 -08:00
|
|
|
rescue LoadError
|
2017-06-25 11:19:22 +02:00
|
|
|
warn "#{$!}: skipping coffee tests"
|
2014-06-25 15:56:07 -07:00
|
|
|
rescue
|
|
|
|
if $!.class.name == 'ExecJS::RuntimeUnavailable'
|
2017-06-25 11:19:22 +02:00
|
|
|
warn "#{$!}: skipping coffee tests"
|
2014-06-25 15:56:07 -07:00
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
2010-09-12 23:09:10 +02:00
|
|
|
end
|