2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
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
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class BuilderTest < Test::Unit::TestCase
|
2010-09-19 11:57:48 -04:00
|
|
|
def builder_app(options = {}, &block)
|
2008-12-13 16:06:02 -05:00
|
|
|
mock_app {
|
|
|
|
set :views, File.dirname(__FILE__) + '/views'
|
2010-09-19 11:57:48 -04:00
|
|
|
set options
|
2008-12-13 16:06:02 -05:00
|
|
|
get '/', &block
|
|
|
|
}
|
|
|
|
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
|
|
|
|
builder_app {
|
|
|
|
@name = "Frank & Mary"
|
|
|
|
builder do |xml|
|
|
|
|
xml.couple @name
|
2008-03-08 20:40:01 -05:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
}
|
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
|
|
|
|
builder_app {
|
|
|
|
@name = "Blue"
|
|
|
|
builder :hello
|
|
|
|
}
|
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
|
|
|
|
mock_app {
|
|
|
|
layout do
|
|
|
|
%(xml.layout { xml << yield })
|
2008-03-08 20:40:01 -05:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
get('/') { builder %(xml.em 'Hello World') }
|
|
|
|
}
|
|
|
|
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
|
|
|
|
builder_app {
|
|
|
|
builder %(xml.em 'Hello World'), :layout => :layout2
|
|
|
|
}
|
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
|
|
|
|
mock_app {
|
|
|
|
get('/') { builder :no_such_template }
|
|
|
|
}
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_raise(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
|