2012-01-10 03:15:06 -05:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'yajl'
|
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class YajlTest < Minitest::Test
|
2012-01-10 03:15:06 -05:00
|
|
|
def yajl_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
|
2012-01-10 03:15:06 -05:00
|
|
|
get '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders inline Yajl strings' do
|
2012-05-21 17:21:59 -04:00
|
|
|
yajl_app { yajl('json = { :foo => "bar" }') }
|
2012-01-10 03:15:06 -05:00
|
|
|
assert ok?
|
|
|
|
assert_body '{"foo":"bar"}'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders .yajl files in views path' do
|
2012-05-21 17:21:59 -04:00
|
|
|
yajl_app { yajl(:hello) }
|
2012-01-10 03:15:06 -05:00
|
|
|
assert ok?
|
|
|
|
assert_body '{"yajl":"hello"}'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises error if template not found' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app { get('/') { yajl(:no_such_template) } }
|
2015-01-10 14:30:47 -05:00
|
|
|
assert_raises(Errno::ENOENT) { get('/') }
|
2012-01-10 03:15:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts a :locals option' do
|
2012-05-21 17:21:59 -04:00
|
|
|
yajl_app do
|
2012-01-10 03:15:06 -05:00
|
|
|
locals = { :object => { :foo => 'bar' } }
|
|
|
|
yajl 'json = object', :locals => locals
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2012-01-10 03:15:06 -05:00
|
|
|
assert ok?
|
|
|
|
assert_body '{"foo":"bar"}'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts a :scope option' do
|
2012-05-21 17:21:59 -04:00
|
|
|
yajl_app do
|
2012-01-10 03:15:06 -05:00
|
|
|
scope = { :object => { :foo => 'bar' } }
|
|
|
|
yajl 'json = self[:object]', :scope => scope
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2012-01-10 03:15:06 -05:00
|
|
|
assert ok?
|
|
|
|
assert_body '{"foo":"bar"}'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'decorates the json with a callback' do
|
2012-05-21 17:21:59 -04:00
|
|
|
yajl_app do
|
|
|
|
yajl(
|
2012-07-18 15:07:24 -04:00
|
|
|
'json = { :foo => "bar" }',
|
2012-05-21 17:21:59 -04:00
|
|
|
{ :callback => 'baz' }
|
|
|
|
)
|
|
|
|
end
|
2012-01-10 03:15:06 -05:00
|
|
|
assert ok?
|
|
|
|
assert_body 'baz({"foo":"bar"});'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'decorates the json with a variable' do
|
2012-05-21 17:21:59 -04:00
|
|
|
yajl_app do
|
|
|
|
yajl(
|
|
|
|
'json = { :foo => "bar" }',
|
|
|
|
{ :variable => 'qux' }
|
|
|
|
)
|
|
|
|
end
|
2012-01-10 03:15:06 -05:00
|
|
|
assert ok?
|
|
|
|
assert_body 'var qux = {"foo":"bar"};'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'decorates the json with a callback and a variable' do
|
2012-05-21 17:21:59 -04:00
|
|
|
yajl_app do
|
|
|
|
yajl(
|
|
|
|
'json = { :foo => "bar" }',
|
2012-01-10 03:15:06 -05:00
|
|
|
{ :callback => 'baz', :variable => 'qux' }
|
2012-05-21 17:21:59 -04:00
|
|
|
)
|
|
|
|
end
|
2012-01-10 03:15:06 -05:00
|
|
|
assert ok?
|
|
|
|
assert_body 'var qux = {"foo":"bar"}; baz(qux);'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue LoadError
|
2017-06-25 05:19:22 -04:00
|
|
|
warn "#{$!}: skipping yajl tests"
|
2012-01-10 03:15:06 -05:00
|
|
|
end
|