1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/test/rabl_test.rb

90 lines
1.9 KiB
Ruby
Raw Normal View History

2012-05-31 22:22:06 -04:00
require File.expand_path('../helper', __FILE__)
begin
require 'rabl'
require 'ostruct'
require 'json'
require 'active_support/core_ext/hash/conversions'
class RablTest < Minitest::Test
2012-05-31 22:22:06 -04:00
def rabl_app(&block)
mock_app {
set :views, __dir__ + '/views'
2012-05-31 22:22:06 -04:00
get '/', &block
}
get '/'
end
it 'renders inline rabl strings' do
rabl_app do
@foo = OpenStruct.new(:baz => 'w00t')
rabl %q{
object @foo
attributes :baz
}
end
assert ok?
assert_equal '{"openstruct":{"baz":"w00t"}}', body
end
it 'renders .rabl files in views path' do
rabl_app do
@foo = OpenStruct.new(:bar => 'baz')
rabl :hello
end
assert ok?
assert_equal '{"openstruct":{"bar":"baz"}}', body
end
2012-07-18 15:07:24 -04:00
2012-05-31 22:22:06 -04:00
it "renders with file layouts" do
rabl_app {
@foo = OpenStruct.new(:bar => 'baz')
rabl :hello, :layout => :layout2
}
assert ok?
assert_equal '{"qux":{"openstruct":{"bar":"baz"}}}', body
end
2012-07-18 15:07:24 -04:00
2012-05-31 22:22:06 -04:00
it "raises error if template not found" do
mock_app {
get('/') { rabl :no_such_template }
}
assert_raises(Errno::ENOENT) { get('/') }
2012-05-31 22:22:06 -04:00
end
2012-07-18 15:07:24 -04:00
2012-05-31 22:22:06 -04:00
it "passes rabl options to the rabl engine" do
mock_app do
get('/') do
@foo = OpenStruct.new(:bar => 'baz')
rabl %q{
object @foo
attributes :bar
}, :format => 'xml'
end
end
get '/'
assert ok?
assert_body '<?xml version="1.0" encoding="UTF-8"?><openstruct><bar>baz</bar></openstruct>'
end
2012-07-18 15:07:24 -04:00
2012-05-31 22:22:06 -04:00
it "passes default rabl options to the rabl engine" do
mock_app do
set :rabl, :format => 'xml'
get('/') do
@foo = OpenStruct.new(:bar => 'baz')
rabl %q{
object @foo
attributes :bar
}
end
end
get '/'
assert ok?
assert_body '<?xml version="1.0" encoding="UTF-8"?><openstruct><bar>baz</bar></openstruct>'
end
2012-07-18 15:07:24 -04:00
2012-05-31 22:22:06 -04:00
end
rescue LoadError
2017-06-25 05:19:22 -04:00
warn "#{$!}: skipping rabl tests"
2012-05-31 22:22:06 -04:00
end