2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2010-10-26 14:50:07 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
require 'slim'
|
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class SlimTest < Minitest::Test
|
2010-10-26 14:50:07 -04:00
|
|
|
def slim_app(&block)
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2020-03-13 17:20:04 -04:00
|
|
|
set :views, __dir__ + '/views'
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', &block)
|
|
|
|
end
|
2010-10-26 14:50:07 -04:00
|
|
|
get '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders inline slim strings' do
|
|
|
|
slim_app { slim "h1 Hiya\n" }
|
|
|
|
assert ok?
|
|
|
|
assert_equal "<h1>Hiya</h1>", body
|
|
|
|
end
|
2012-07-18 15:07:24 -04:00
|
|
|
|
2010-10-26 14:50:07 -04:00
|
|
|
it 'renders .slim files in views path' do
|
|
|
|
slim_app { slim :hello }
|
|
|
|
assert ok?
|
|
|
|
assert_equal "<h1>Hello From Slim</h1>", body
|
|
|
|
end
|
2012-07-18 15:07:24 -04:00
|
|
|
|
2010-10-26 14:50:07 -04:00
|
|
|
it "renders with inline layouts" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2010-10-26 14:50:07 -04:00
|
|
|
layout { %(h1\n | THIS. IS. \n == yield.upcase ) }
|
2010-11-05 06:41:54 -04:00
|
|
|
get('/') { slim 'em Sparta' }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2010-10-26 14:50:07 -04:00
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal "<h1>THIS. IS. <EM>SPARTA</EM></h1>", body
|
|
|
|
end
|
2012-07-18 15:07:24 -04:00
|
|
|
|
2010-10-26 14:50:07 -04:00
|
|
|
it "renders with file layouts" do
|
2012-05-21 17:21:59 -04:00
|
|
|
slim_app { slim('| Hello World', :layout => :layout2) }
|
2010-10-26 14:50:07 -04:00
|
|
|
assert ok?
|
|
|
|
assert_equal "<h1>Slim Layout!</h1><p>Hello World</p>", body
|
|
|
|
end
|
2012-07-18 15:07:24 -04:00
|
|
|
|
2010-10-26 14:50:07 -04:00
|
|
|
it "raises error if template not found" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app { get('/') { slim(:no_such_template) } }
|
2015-01-10 14:30:47 -05:00
|
|
|
assert_raises(Errno::ENOENT) { get('/') }
|
2010-10-26 14:50:07 -04:00
|
|
|
end
|
2012-07-18 15:07:24 -04:00
|
|
|
|
2010-10-26 14:50:07 -04:00
|
|
|
HTML4_DOCTYPE = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
|
2012-07-18 15:07:24 -04:00
|
|
|
|
2010-10-26 14:50:07 -04:00
|
|
|
it "passes slim options to the slim engine" do
|
2013-09-15 14:55:01 -04:00
|
|
|
mock_app { get('/') { slim("x foo='bar'", :attr_quote => "'") }}
|
2010-10-26 14:50:07 -04:00
|
|
|
get '/'
|
|
|
|
assert ok?
|
2011-09-01 13:54:50 -04:00
|
|
|
assert_body "<x foo='bar'></x>"
|
2010-10-26 14:50:07 -04:00
|
|
|
end
|
2012-07-18 15:07:24 -04:00
|
|
|
|
2010-10-26 14:50:07 -04:00
|
|
|
it "passes default slim options to the slim engine" do
|
2011-09-01 13:54:50 -04:00
|
|
|
mock_app do
|
2013-09-15 14:55:01 -04:00
|
|
|
set :slim, :attr_quote => "'"
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { slim("x foo='bar'") }
|
2011-09-01 13:54:50 -04:00
|
|
|
end
|
2010-10-26 14:50:07 -04:00
|
|
|
get '/'
|
|
|
|
assert ok?
|
2011-09-01 13:54:50 -04:00
|
|
|
assert_body "<x foo='bar'></x>"
|
2010-10-26 14:50:07 -04:00
|
|
|
end
|
2012-07-18 15:07:24 -04:00
|
|
|
|
2010-10-26 14:50:07 -04:00
|
|
|
it "merges the default slim options with the overrides and passes them to the slim engine" do
|
2011-09-01 13:54:50 -04:00
|
|
|
mock_app do
|
2013-09-15 14:55:01 -04:00
|
|
|
set :slim, :attr_quote => "'"
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { slim("x foo='bar'") }
|
2013-09-15 14:55:01 -04:00
|
|
|
get('/other') { slim("x foo='bar'", :attr_quote => '"') }
|
2011-09-01 13:54:50 -04:00
|
|
|
end
|
2010-10-26 14:50:07 -04:00
|
|
|
get '/'
|
|
|
|
assert ok?
|
2011-09-01 13:54:50 -04:00
|
|
|
assert_body "<x foo='bar'></x>"
|
|
|
|
get '/other'
|
2010-11-05 12:00:08 -04:00
|
|
|
assert ok?
|
2011-09-01 13:54:50 -04:00
|
|
|
assert_body '<x foo="bar"></x>'
|
2010-10-26 14:50:07 -04:00
|
|
|
end
|
2013-02-17 06:53:34 -05:00
|
|
|
|
2013-03-14 14:20:51 -04:00
|
|
|
it "can render truly nested layouts by accepting a layout and a block with the contents" do
|
2013-02-17 06:53:34 -05:00
|
|
|
mock_app do
|
|
|
|
template(:main_outer_layout) { "h1 Title\n== yield" }
|
|
|
|
template(:an_inner_layout) { "h2 Subtitle\n== yield" }
|
|
|
|
template(:a_page) { "p Contents." }
|
|
|
|
get('/') do
|
|
|
|
slim :main_outer_layout, :layout => false do
|
|
|
|
slim :an_inner_layout do
|
|
|
|
slim :a_page
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_body "<h1>Title</h1>\n<h2>Subtitle</h2>\n<p>Contents.</p>\n"
|
|
|
|
end
|
2010-10-26 14:50:07 -04:00
|
|
|
end
|
2011-01-25 03:27:15 -05:00
|
|
|
|
|
|
|
rescue LoadError
|
2017-06-25 05:19:22 -04:00
|
|
|
warn "#{$!}: skipping slim tests"
|
2010-10-26 14:50:07 -04:00
|
|
|
end
|