File: rabl_test.rb

package info (click to toggle)
ruby-sinatra 1.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,524 kB
  • ctags: 483
  • sloc: ruby: 9,521; makefile: 5
file content (89 lines) | stat: -rw-r--r-- 1,996 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require File.expand_path('../helper', __FILE__)

begin
require 'rabl'
require 'ostruct'
require 'json'
require 'active_support/core_ext/hash/conversions'

class RablTest < Test::Unit::TestCase
  def rabl_app(&block)
    mock_app {
      set :views, File.dirname(__FILE__) + '/views'
      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

  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

  it "raises error if template not found" do
    mock_app {
      get('/') { rabl :no_such_template }
    }
    assert_raise(Errno::ENOENT) { get('/') }
  end

  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

  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

end

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