Sha256: 20d6c92f30f59ec0c52386faeeee725808235ec28befd54e4097e3e1f0ce03ee

Contents?: true

Size: 1.82 KB

Versions: 17

Compression:

Stored size: 1.82 KB

Contents

require File.expand_path('../helper', __FILE__)

begin
require 'yajl'

class YajlTest < Test::Unit::TestCase
  def yajl_app(&block)
    mock_app do
      set :views, File.dirname(__FILE__) + '/views'
      get('/', &block)
    end
    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 do
      locals = { :object => { :foo => 'bar' } }
      yajl 'json = object', :locals => locals
    end
    assert ok?
    assert_body '{"foo":"bar"}'
  end

  it 'accepts a :scope option' do
    yajl_app do
      scope = { :object => { :foo => 'bar' } }
      yajl 'json = self[:object]', :scope => scope
    end
    assert ok?
    assert_body '{"foo":"bar"}'
  end

  it 'decorates the json with a callback' do
    yajl_app do
      yajl(
        'json = { :foo => "bar" }',
        { :callback => 'baz' }
      )
    end
    assert ok?
    assert_body 'baz({"foo":"bar"});'
  end

  it 'decorates the json with a variable' do
    yajl_app do
      yajl(
        'json = { :foo => "bar" }',
        { :variable => 'qux' }
      )
    end
    assert ok?
    assert_body 'var qux = {"foo":"bar"};'
  end

  it 'decorates the json with a callback and a variable' do
    yajl_app do
      yajl(
        'json = { :foo => "bar" }',
        { :callback => 'baz', :variable => 'qux' }
      )
    end
    assert ok?
    assert_body 'var qux = {"foo":"bar"}; baz(qux);'
  end
end

rescue LoadError
  warn "#{$!.to_s}: skipping yajl tests"
end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
sinatra-acd-1.4.5 test/yajl_test.rb
sinatra-1.4.5 test/yajl_test.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/sinatra-1.4.4/test/yajl_test.rb
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/sinatra-1.4.4/test/yajl_test.rb
mango-0.7.0 vendor/bundler/ruby/2.0.0/gems/sinatra-1.4.4/test/yajl_test.rb
sinatra-1.4.4 test/yajl_test.rb
sinatra-1.4.3 test/yajl_test.rb
sinatra-1.4.2 test/yajl_test.rb
sinatra-1.4.1 test/yajl_test.rb
sinatra-1.4.0 test/yajl_test.rb
sinatra-1.4.0.d test/yajl_test.rb
sinatra-1.4.0.c test/yajl_test.rb
sinatra-1.4.0.b test/yajl_test.rb
sinatra-1.4.0.a test/yajl_test.rb
devcenter-0.0.3 vendor/sinatra/test/yajl_test.rb
devcenter-0.0.2 vendor/sinatra/test/yajl_test.rb
devcenter-0.0.1 vendor/sinatra/test/yajl_test.rb