forked from sinatra/sinatra
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for yajl templates, merges sinatra#450
Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
- Loading branch information
1 parent
4490a76
commit d255666
Showing
6 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json = { :yajl => "hello" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require File.expand_path('../helper', __FILE__) | ||
|
||
begin | ||
require 'yajl' | ||
|
||
class YajlTest < Test::Unit::TestCase | ||
def yajl_app(&block) | ||
mock_app { | ||
set :views, File.dirname(__FILE__) + '/views' | ||
get '/', &block | ||
} | ||
get '/' | ||
end | ||
|
||
it 'renders inline Yajl strings' do | ||
yajl_app { yajl 'json = { :foo => "bar" }' } | ||
assert ok? | ||
assert_body '{"foo":"bar"}' | ||
end | ||
|
||
it 'renders .yajl files in views path' do | ||
yajl_app { yajl :hello } | ||
assert ok? | ||
assert_body '{"yajl":"hello"}' | ||
end | ||
|
||
it 'raises error if template not found' do | ||
mock_app { | ||
get('/') { yajl :no_such_template } | ||
} | ||
assert_raise(Errno::ENOENT) { get('/') } | ||
end | ||
|
||
it 'accepts a :locals option' do | ||
yajl_app { | ||
locals = { :object => { :foo => 'bar' } } | ||
yajl 'json = object', :locals => locals | ||
} | ||
assert ok? | ||
assert_body '{"foo":"bar"}' | ||
end | ||
|
||
it 'accepts a :scope option' do | ||
yajl_app { | ||
scope = { :object => { :foo => 'bar' } } | ||
yajl 'json = self[:object]', :scope => scope | ||
} | ||
assert ok? | ||
assert_body '{"foo":"bar"}' | ||
end | ||
|
||
it 'decorates the json with a callback' do | ||
yajl_app { | ||
yajl 'json = { :foo => "bar" }', { :callback => 'baz' } | ||
} | ||
assert ok? | ||
assert_body 'baz({"foo":"bar"});' | ||
end | ||
|
||
it 'decorates the json with a variable' do | ||
yajl_app { | ||
yajl 'json = { :foo => "bar" }', { :variable => 'qux' } | ||
} | ||
assert ok? | ||
assert_body 'var qux = {"foo":"bar"};' | ||
end | ||
|
||
it 'decorates the json with a callback and a variable' do | ||
yajl_app { | ||
yajl 'json = { :foo => "bar" }', | ||
{ :callback => 'baz', :variable => 'qux' } | ||
} | ||
assert ok? | ||
assert_body 'var qux = {"foo":"bar"}; baz(qux);' | ||
end | ||
end | ||
|
||
rescue LoadError | ||
warn "#{$!.to_s}: skipping yajl tests" | ||
end |