layouts_test.rb 8.7 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
        "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 %>",
16
        "layouts/overwrite.erb"                  => "Overwrite <%= yield %>",
17
        "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
45
        render :_template => ActionView::Template::Text.new("Hello string!"), :layout => "overwrite"
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
    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
69 70

    class WithProc < Base
71
      layout proc { |c| "overwrite" }
72 73 74 75 76 77

      def index
        render :_template => ActionView::Template::Text.new("Hello proc!")
      end
    end

78 79 80 81
    class WithSymbol < Base
      layout :hello
      
      def index
C
Carlhuda 已提交
82
        render :_template => ActionView::Template::Text.new("Hello symbol!")
83 84 85
      end
    private  
      def hello
86
        "overwrite"
87 88 89 90 91 92 93
      end
    end
    
    class WithSymbolReturningString < Base
      layout :no_hello
      
      def index
C
Carlhuda 已提交
94
        render :_template => ActionView::Template::Text.new("Hello missing symbol!")
95 96 97 98 99 100 101
      end
    private  
      def no_hello
        nil
      end
    end
    
102 103 104 105
    class WithSymbolReturningNil < Base
      layout :nilz
      
      def index
C
Carlhuda 已提交
106
        render :_template => ActionView::Template::Text.new("Hello nilz!")
107 108 109 110 111 112 113 114 115
      end
      
      def nilz() end
    end
    
    class WithSymbolReturningObj < Base
      layout :objekt
      
      def index
C
Carlhuda 已提交
116
        render :_template => ActionView::Template::Text.new("Hello nilz!")
117 118 119 120 121 122 123 124
      end
      
      def objekt
        Object.new
      end
    end    
    
    class WithSymbolAndNoMethod < Base
125
      layout :no_method
126 127
      
      def index
C
Carlhuda 已提交
128
        render :_template => ActionView::Template::Text.new("Hello boom!")
129 130 131
      end
    end
    
Y
Yehuda Katz and Carl Lerche 已提交
132 133 134 135
    class WithMissingLayout < Base
      layout "missing"
      
      def index
C
Carlhuda 已提交
136
        render :_template => ActionView::Template::Text.new("Hello missing!")
Y
Yehuda Katz and Carl Lerche 已提交
137 138
      end
    end
139 140 141
    
    class WithFalseLayout < Base
      layout false
Y
Yehuda Katz and Carl Lerche 已提交
142
      
143
      def index
C
Carlhuda 已提交
144
        render :_template => ActionView::Template::Text.new("Hello false!")
145 146
      end
    end
Y
Yehuda Katz and Carl Lerche 已提交
147
    
148 149 150 151
    class WithNilLayout < Base
      layout nil
      
      def index
C
Carlhuda 已提交
152
        render :_template => ActionView::Template::Text.new("Hello nil!")
153 154 155
      end
    end
    
Y
Yehuda Katz and Carl Lerche 已提交
156 157
    class TestBase < ActiveSupport::TestCase
      test "when no layout is specified, and no default is available, render without a layout" do
158 159 160
        controller = Blank.new
        controller.process(:index)
        assert_equal "Hello blank!", controller.response_body
Y
Yehuda Katz and Carl Lerche 已提交
161 162 163
      end
      
      test "when layout is specified as a string, render with that layout" do
164 165 166
        controller = WithString.new
        controller.process(:index)
        assert_equal "With String Hello string!", controller.response_body
Y
Yehuda Katz and Carl Lerche 已提交
167
      end
168 169 170 171 172 173 174 175 176 177

      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)
178
        assert_equal "Overwrite Hello string!", controller.response_body
179 180 181 182 183 184 185 186 187 188 189 190 191 192
      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 已提交
193
      test "when layout is specified as a string, but the layout is missing, raise an exception" do
194
        assert_raises(ActionView::MissingTemplate) { WithMissingLayout.new.process(:index) }
Y
Yehuda Katz and Carl Lerche 已提交
195
      end
196 197
      
      test "when layout is specified as false, do not use a layout" do
198 199 200
        controller = WithFalseLayout.new
        controller.process(:index)
        assert_equal "Hello false!", controller.response_body
201 202 203
      end
      
      test "when layout is specified as nil, do not use a layout" do
204 205 206
        controller = WithNilLayout.new
        controller.process(:index)
        assert_equal "Hello nil!", controller.response_body
207
      end
208 209 210 211

      test "when layout is specified as a proc, call it and use the layout returned" do
        controller = WithProc.new
        controller.process(:index)
212
        assert_equal "Overwrite Hello proc!", controller.response_body
213
      end
214 215
      
      test "when layout is specified as a symbol, call the requested method and use the layout returned" do
216 217
        controller = WithSymbol.new
        controller.process(:index)
218
        assert_equal "Overwrite Hello symbol!", controller.response_body
219 220 221
      end
      
      test "when layout is specified as a symbol and the method returns nil, don't use a layout" do
222 223 224
        controller = WithSymbolReturningNil.new
        controller.process(:index)
        assert_equal "Hello nilz!", controller.response_body
225 226 227
      end
      
      test "when the layout is specified as a symbol and the method doesn't exist, raise an exception" do
228
        assert_raises(NoMethodError) { WithSymbolAndNoMethod.new.process(:index) }
229 230 231
      end
      
      test "when the layout is specified as a symbol and the method returns something besides a string/false/nil, raise an exception" do
232
        assert_raises(ArgumentError) { WithSymbolReturningObj.new.process(:index) }
233 234 235
      end
      
      test "when a child controller does not have a layout, use the parent controller layout" do
236 237 238
        controller = WithStringChild.new
        controller.process(:index)
        assert_equal "With String Hello string!", controller.response_body
239 240 241
      end
      
      test "when a child controller has specified a layout, use that layout and not the parent controller layout" do
242 243 244
        controller = WithStringOverriddenChild.new
        controller.process(:index)
        assert_equal "With Override Hello string!", controller.response_body
245 246 247
      end
      
      test "when a child controller has an implied layout, use that layout and not the parent controller layout" do
248 249 250
        controller = WithStringImpliedChild.new
        controller.process(:index)
        assert_equal "With Implied Hello string!", controller.response_body
251 252 253
      end
      
      test "when a child controller specifies layout nil, do not use the parent layout" do
254 255 256
        controller = WithNilChild.new
        controller.process(:index)
        assert_equal "Hello string!", controller.response_body
257 258 259 260
      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
261 262 263
          controller = WithChildOfImplied.new
          controller.process(:index)
          assert_equal "With Implied Hello string!", controller.response_body
264 265 266 267
      end
      
      test "raises an exception when specifying layout true" do
        assert_raises ArgumentError do
268
          Object.class_eval do
269
            class ::BadFailLayout < AbstractControllerTests::Layouts::Base
270 271
              layout true
            end
272
          end
273 274
        end
      end
Y
Yehuda Katz and Carl Lerche 已提交
275 276
    end
  end
277
end