layouts_test.rb 8.3 KB
Newer Older
J
Joshua Peek 已提交
1
require 'abstract_unit'
Y
Yehuda Katz and Carl Lerche 已提交
2 3 4 5 6 7

module AbstractControllerTests
  module Layouts

    # Base controller for these tests
    class Base < AbstractController::Base
8
      include AbstractController::Rendering
J
Joshua Peek 已提交
9 10
      include AbstractController::Layouts

11
      self.view_paths = [ActionView::FixtureResolver.new(
12 13 14 15 16 17
        "layouts/hello.erb"                     => "With String <%= yield %>",
        "layouts/hello_override.erb"            => "With Override <%= yield %>",
        "layouts/abstract_controller_tests/layouts/with_string_implied_child.erb" =>
                                                   "With Implied <%= yield %>",
        "layouts/omg.erb"                       => "OMGHI2U <%= yield %>",
        "layouts/with_false_layout.erb"         => "False Layout <%= yield %>"
Y
Yehuda Katz and Carl Lerche 已提交
18 19 20 21 22 23 24
      )]
    end
    
    class Blank < Base
      self.view_paths = []
      
      def index
C
Carlhuda 已提交
25
        render :_template => ActionView::Template::Text.new("Hello blank!")
Y
Yehuda Katz and Carl Lerche 已提交
26 27 28 29 30 31 32
      end
    end
    
    class WithString < Base
      layout "hello"
      
      def index
C
Carlhuda 已提交
33
        render :_template => ActionView::Template::Text.new("Hello string!")
Y
Yehuda Katz and Carl Lerche 已提交
34
      end
35 36

      def overwrite_default
C
Carlhuda 已提交
37
        render :_template => ActionView::Template::Text.new("Hello string!"), :layout => :default
38 39 40
      end

      def overwrite_false
C
Carlhuda 已提交
41
        render :_template => ActionView::Template::Text.new("Hello string!"), :layout => false
42 43 44
      end

      def overwrite_string
C
Carlhuda 已提交
45
        render :_template => ActionView::Template::Text.new("Hello string!"), :layout => "omg"
46 47 48 49 50
      end

      def overwrite_skip
        render :text => "Hello text!"
      end
Y
Yehuda Katz and Carl Lerche 已提交
51 52
    end
    
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    class WithStringChild < WithString
    end
    
    class WithStringOverriddenChild < WithString
      layout "hello_override"
    end
    
    class WithNilChild < WithString
      layout nil
    end    
    
    class WithStringImpliedChild < WithString
    end
    
    class WithChildOfImplied < WithStringImpliedChild
    end
    
70 71 72 73
    class WithSymbol < Base
      layout :hello
      
      def index
C
Carlhuda 已提交
74
        render :_template => ActionView::Template::Text.new("Hello symbol!")
75 76 77 78 79 80 81 82 83 84 85
      end
    private  
      def hello
        "omg"
      end
    end
    
    class WithSymbolReturningString < Base
      layout :no_hello
      
      def index
C
Carlhuda 已提交
86
        render :_template => ActionView::Template::Text.new("Hello missing symbol!")
87 88 89 90 91 92 93
      end
    private  
      def no_hello
        nil
      end
    end
    
94 95 96 97
    class WithSymbolReturningNil < Base
      layout :nilz
      
      def index
C
Carlhuda 已提交
98
        render :_template => ActionView::Template::Text.new("Hello nilz!")
99 100 101 102 103 104 105 106 107
      end
      
      def nilz() end
    end
    
    class WithSymbolReturningObj < Base
      layout :objekt
      
      def index
C
Carlhuda 已提交
108
        render :_template => ActionView::Template::Text.new("Hello nilz!")
109 110 111 112 113 114 115 116 117 118 119
      end
      
      def objekt
        Object.new
      end
    end    
    
    class WithSymbolAndNoMethod < Base
      layout :omg_no_method
      
      def index
C
Carlhuda 已提交
120
        render :_template => ActionView::Template::Text.new("Hello boom!")
121 122 123
      end
    end
    
Y
Yehuda Katz and Carl Lerche 已提交
124 125 126 127
    class WithMissingLayout < Base
      layout "missing"
      
      def index
C
Carlhuda 已提交
128
        render :_template => ActionView::Template::Text.new("Hello missing!")
Y
Yehuda Katz and Carl Lerche 已提交
129 130
      end
    end
131 132 133
    
    class WithFalseLayout < Base
      layout false
Y
Yehuda Katz and Carl Lerche 已提交
134
      
135
      def index
C
Carlhuda 已提交
136
        render :_template => ActionView::Template::Text.new("Hello false!")
137 138
      end
    end
Y
Yehuda Katz and Carl Lerche 已提交
139
    
140 141 142 143
    class WithNilLayout < Base
      layout nil
      
      def index
C
Carlhuda 已提交
144
        render :_template => ActionView::Template::Text.new("Hello nil!")
145 146 147
      end
    end
    
Y
Yehuda Katz and Carl Lerche 已提交
148 149
    class TestBase < ActiveSupport::TestCase
      test "when no layout is specified, and no default is available, render without a layout" do
150 151 152
        controller = Blank.new
        controller.process(:index)
        assert_equal "Hello blank!", controller.response_body
Y
Yehuda Katz and Carl Lerche 已提交
153 154 155
      end
      
      test "when layout is specified as a string, render with that layout" do
156 157 158
        controller = WithString.new
        controller.process(:index)
        assert_equal "With String Hello string!", controller.response_body
Y
Yehuda Katz and Carl Lerche 已提交
159
      end
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

      test "when layout is overwriten by :default in render, render default layout" do
        controller = WithString.new
        controller.process(:overwrite_default)
        assert_equal "With String Hello string!", controller.response_body
      end

      test "when layout is overwriten by string in render, render new layout" do
        controller = WithString.new
        controller.process(:overwrite_string)
        assert_equal "OMGHI2U Hello string!", controller.response_body
      end

      test "when layout is overwriten by false in render, render no layout" do
        controller = WithString.new
        controller.process(:overwrite_false)
        assert_equal "Hello string!", controller.response_body
      end

      test "when text is rendered, render no layout" do
        controller = WithString.new
        controller.process(:overwrite_skip)
        assert_equal "Hello text!", controller.response_body
      end

Y
Yehuda Katz and Carl Lerche 已提交
185
      test "when layout is specified as a string, but the layout is missing, raise an exception" do
186
        assert_raises(ActionView::MissingTemplate) { WithMissingLayout.new.process(:index) }
Y
Yehuda Katz and Carl Lerche 已提交
187
      end
188 189
      
      test "when layout is specified as false, do not use a layout" do
190 191 192
        controller = WithFalseLayout.new
        controller.process(:index)
        assert_equal "Hello false!", controller.response_body
193 194 195
      end
      
      test "when layout is specified as nil, do not use a layout" do
196 197 198
        controller = WithNilLayout.new
        controller.process(:index)
        assert_equal "Hello nil!", controller.response_body
199 200 201
      end
      
      test "when layout is specified as a symbol, call the requested method and use the layout returned" do
202 203 204
        controller = WithSymbol.new
        controller.process(:index)
        assert_equal "OMGHI2U Hello symbol!", controller.response_body
205 206 207
      end
      
      test "when layout is specified as a symbol and the method returns nil, don't use a layout" do
208 209 210
        controller = WithSymbolReturningNil.new
        controller.process(:index)
        assert_equal "Hello nilz!", controller.response_body
211 212 213
      end
      
      test "when the layout is specified as a symbol and the method doesn't exist, raise an exception" do
214
        assert_raises(NoMethodError) { WithSymbolAndNoMethod.new.process(:index) }
215 216 217
      end
      
      test "when the layout is specified as a symbol and the method returns something besides a string/false/nil, raise an exception" do
218
        assert_raises(ArgumentError) { WithSymbolReturningObj.new.process(:index) }
219 220 221
      end
      
      test "when a child controller does not have a layout, use the parent controller layout" do
222 223 224
        controller = WithStringChild.new
        controller.process(:index)
        assert_equal "With String Hello string!", controller.response_body
225 226 227
      end
      
      test "when a child controller has specified a layout, use that layout and not the parent controller layout" do
228 229 230
        controller = WithStringOverriddenChild.new
        controller.process(:index)
        assert_equal "With Override Hello string!", controller.response_body
231 232 233
      end
      
      test "when a child controller has an implied layout, use that layout and not the parent controller layout" do
234 235 236
        controller = WithStringImpliedChild.new
        controller.process(:index)
        assert_equal "With Implied Hello string!", controller.response_body
237 238 239
      end
      
      test "when a child controller specifies layout nil, do not use the parent layout" do
240 241 242
        controller = WithNilChild.new
        controller.process(:index)
        assert_equal "Hello string!", controller.response_body
243 244 245 246
      end
            
      test "when a grandchild has no layout specified, the child has an implied layout, and the " \
        "parent has specified a layout, use the child controller layout" do
247 248 249
          controller = WithChildOfImplied.new
          controller.process(:index)
          assert_equal "With Implied Hello string!", controller.response_body
250 251 252 253
      end
      
      test "raises an exception when specifying layout true" do
        assert_raises ArgumentError do
254
          Object.class_eval do
255 256 257
            class ::BadOmgFailLolLayout < AbstractControllerTests::Layouts::Base
              layout true
            end
258
          end
259 260
        end
      end
Y
Yehuda Katz and Carl Lerche 已提交
261 262
    end
  end
263
end