2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2011-02-08 16:43:04 -05:00
|
|
|
|
|
|
|
begin
|
2009-05-02 08:49:43 -04:00
|
|
|
require 'builder'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class BuilderTest < Minitest::Test
|
2010-09-19 11:57:48 -04:00
|
|
|
def builder_app(options = {}, &block)
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
set :views, File.dirname(__FILE__) + '/views'
|
2010-09-19 11:57:48 -04:00
|
|
|
set options
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', &block)
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
get '/'
|
2008-03-08 20:40:01 -05:00
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'renders inline Builder strings' do
|
|
|
|
builder_app { builder 'xml.instruct!' }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal %{<?xml version="1.0" encoding="UTF-8"?>\n}, body
|
2008-03-08 20:40:01 -05:00
|
|
|
end
|
|
|
|
|
2010-09-19 11:57:48 -04:00
|
|
|
it 'defaults content type to xml' do
|
|
|
|
builder_app { builder 'xml.instruct!' }
|
|
|
|
assert ok?
|
|
|
|
assert_equal "application/xml;charset=utf-8", response['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults allows setting content type per route' do
|
|
|
|
builder_app do
|
|
|
|
content_type :html
|
|
|
|
builder 'xml.instruct!'
|
|
|
|
end
|
|
|
|
assert ok?
|
|
|
|
assert_equal "text/html;charset=utf-8", response['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults allows setting content type globally' do
|
|
|
|
builder_app(:builder => { :content_type => 'html' }) do
|
|
|
|
builder 'xml.instruct!'
|
|
|
|
end
|
|
|
|
assert ok?
|
|
|
|
assert_equal "text/html;charset=utf-8", response['Content-Type']
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'renders inline blocks' do
|
2012-05-21 17:21:59 -04:00
|
|
|
builder_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
@name = "Frank & Mary"
|
2012-05-21 17:21:59 -04:00
|
|
|
builder { |xml| xml.couple @name }
|
|
|
|
end
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "<couple>Frank & Mary</couple>\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-03-08 20:40:01 -05:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'renders .builder files in views path' do
|
2012-05-21 17:21:59 -04:00
|
|
|
builder_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
@name = "Blue"
|
|
|
|
builder :hello
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal %(<exclaim>You're my boy, Blue!</exclaim>\n), body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-03-08 20:40:01 -05:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it "renders with inline layouts" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
|
|
|
layout { %(xml.layout { xml << yield }) }
|
2008-12-13 16:06:02 -05:00
|
|
|
get('/') { builder %(xml.em 'Hello World') }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
get '/'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "<layout>\n<em>Hello World</em>\n</layout>\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-03-08 20:40:01 -05:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it "renders with file layouts" do
|
2012-05-21 17:21:59 -04:00
|
|
|
builder_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
builder %(xml.em 'Hello World'), :layout => :layout2
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "<layout>\n<em>Hello World</em>\n</layout>\n", body
|
2008-03-08 20:40:01 -05:00
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it "raises error if template not found" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
get('/') { builder :no_such_template }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2015-01-10 14:30:47 -05:00
|
|
|
assert_raises(Errno::ENOENT) { get('/') }
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-03-08 20:40:01 -05:00
|
|
|
end
|
2011-02-08 16:43:04 -05:00
|
|
|
|
|
|
|
rescue LoadError
|
2011-02-27 03:45:26 -05:00
|
|
|
warn "#{$!.to_s}: skipping builder tests"
|
2011-02-08 16:43:04 -05:00
|
|
|
end
|