render_test.rb 7.2 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2
require File.dirname(__FILE__) + '/../abstract_unit'

3 4 5
unless defined?(Customer)
  Customer = Struct.new("Customer", :name)
end
D
Initial  
David Heinemeier Hansson 已提交
6

7 8
module Fun
  class GamesController < ActionController::Base
D
Initial  
David Heinemeier Hansson 已提交
9 10
    def hello_world
    end
11 12
  end
end
D
Initial  
David Heinemeier Hansson 已提交
13 14


15 16
class TestController < ActionController::Base
  layout :determine_layout
D
Initial  
David Heinemeier Hansson 已提交
17

18 19
  def hello_world
  end
D
Initial  
David Heinemeier Hansson 已提交
20

21 22 23
  def render_hello_world
    render "test/hello_world"
  end
D
Initial  
David Heinemeier Hansson 已提交
24

25 26 27 28
  def render_hello_world_from_variable
    @person = "david"
    render_text "hello #{@person}"
  end
D
Initial  
David Heinemeier Hansson 已提交
29

30 31 32
  def render_action_hello_world
    render_action "hello_world"
  end
33 34 35 36

  def render_action_hello_world_with_symbol
    render_action :hello_world
  end
J
Jeremy Kemper 已提交
37

38 39 40
  def render_text_hello_world
    render_text "hello world"
  end
D
Initial  
David Heinemeier Hansson 已提交
41

42 43 44
  def render_custom_code
    render_text "hello world", "404 Moved"
  end
J
Jeremy Kemper 已提交
45

46 47 48 49
  def render_xml_hello
    @name = "David"
    render "test/hello"
  end
50

51 52
  def greeting
    # let's just rely on the template
53 54
  end

55 56 57
  def layout_test
    render_action "hello_world"
  end
J
Jeremy Kemper 已提交
58

59 60 61 62 63
  def builder_layout_test
    render_action "hello"
  end

  def partials_list
64
    @test_unchanged = 'hello'
65 66 67 68
    @customers = [ Customer.new("david"), Customer.new("mary") ]
    render_action "list"
  end

69 70 71 72
  def partial_only
    render_partial
  end

73 74 75 76
  def hello_in_a_string
    @customers = [ Customer.new("david"), Customer.new("mary") ]
    render_text "How's there? #{render_to_string("test/list")}"
  end
J
Jeremy Kemper 已提交
77

78 79 80
  def accessing_params_in_template
    render_template "Hello: <%= params[:name] %>"
  end
81

82 83
  def accessing_local_assigns_in_inline_template
    name = params[:local_name]
84 85
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { :local_name => name }
86
  end
J
Jeremy Kemper 已提交
87

88 89
  def accessing_local_assigns_in_inline_template_with_string_keys
    name = params[:local_name]
90 91 92 93
    ActionView::Base.local_assigns_support_string_keys = true
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { "local_name" => name }
    ActionView::Base.local_assigns_support_string_keys = false
94
  end
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  def accessing_locals_hash_in_inline_template
    name = params[:local_name]
    render :inline => "<%= 'Goodbye, ' + locals[:local_name] %>",
           :locals => { :local_name => name }
  end
  
  def accessing_locals_hash_in_inline_template_setting_string_key
    name = params[:local_name]
    ActionView::Base.local_assigns_support_string_keys = true
    render :inline => "<%= 'Goodbye, ' + locals[:local_name] %>",
           :locals => { "local_name" => name }
    ActionView::Base.local_assigns_support_string_keys = false
  end
  
  def accessing_locals_hash_in_inline_template_getting_string_key
    name = params[:local_name]
    ActionView::Base.local_assigns_support_string_keys = true
    render :inline => "<%= 'Goodbye, ' + locals['local_name'] %>",
           :locals => { :local_name => name }
    ActionView::Base.local_assigns_support_string_keys = false
  end  

118 119 120 121
  def render_to_string_test
    @foo = render_to_string :inline => "this is a test"
  end

122
  def rescue_action(e) raise end
J
Jeremy Kemper 已提交
123

124 125
  private
    def determine_layout
J
Jeremy Kemper 已提交
126
      case action_name
127 128 129
        when "layout_test":         "layouts/standard"
        when "builder_layout_test": "layouts/builder"
      end
D
Initial  
David Heinemeier Hansson 已提交
130
    end
131
end
132

133
TestController.template_root = File.dirname(__FILE__) + "/../fixtures/"
134
Fun::GamesController.template_root = File.dirname(__FILE__) + "/../fixtures/"
135 136

class RenderTest < Test::Unit::TestCase
D
Initial  
David Heinemeier Hansson 已提交
137 138 139
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
140
    @controller = TestController.new
D
Initial  
David Heinemeier Hansson 已提交
141 142 143 144 145

    @request.host = "www.nextangle.com"
  end

  def test_simple_show
146
    get :hello_world
147 148
    assert_response 200
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
149 150 151
  end

  def test_do_with_render
J
Jeremy Kemper 已提交
152
    assert_deprecated_render { get :render_hello_world }
153
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
154 155 156
  end

  def test_do_with_render_from_variable
157
    get :render_hello_world_from_variable
158
    assert_equal "hello david", @response.body
D
Initial  
David Heinemeier Hansson 已提交
159 160 161
  end

  def test_do_with_render_action
162
    get :render_action_hello_world
163
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
164 165
  end

166
  def test_do_with_render_action_with_symbol
167
    get :render_action_hello_world_with_symbol
168
    assert_template "test/hello_world"
169 170
  end

D
Initial  
David Heinemeier Hansson 已提交
171
  def test_do_with_render_text
172
    get :render_text_hello_world
173
    assert_equal "hello world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
174 175 176
  end

  def test_do_with_render_custom_code
177
    get :render_custom_code
178
    assert_response 404
D
Initial  
David Heinemeier Hansson 已提交
179 180 181
  end

  def test_attempt_to_access_object_method
182
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
D
Initial  
David Heinemeier Hansson 已提交
183 184
  end

185
  def test_private_methods
186
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
187 188
  end

D
Initial  
David Heinemeier Hansson 已提交
189
  def test_access_to_request_in_view
190 191
    view_internals_old_value = ActionController::Base.view_controller_internals

D
Initial  
David Heinemeier Hansson 已提交
192
    ActionController::Base.view_controller_internals = false
193
    ActionController::Base.protected_variables_cache = nil
D
Initial  
David Heinemeier Hansson 已提交
194

195 196
    get :hello_world
    assert_nil assigns["request"]
D
Initial  
David Heinemeier Hansson 已提交
197 198

    ActionController::Base.view_controller_internals = true
199
    ActionController::Base.protected_variables_cache = nil
D
Initial  
David Heinemeier Hansson 已提交
200

201 202
    get :hello_world
    assert_kind_of ActionController::AbstractRequest,  assigns["request"]
203 204 205

    ActionController::Base.view_controller_internals = view_internals_old_value
    ActionController::Base.protected_variables_cache = nil
D
Initial  
David Heinemeier Hansson 已提交
206
  end
J
Jeremy Kemper 已提交
207

D
Initial  
David Heinemeier Hansson 已提交
208
  def test_render_xml
J
Jeremy Kemper 已提交
209
    assert_deprecated_render { get :render_xml_hello }
210
    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
211 212 213
  end

  def test_render_xml_with_default
214
    get :greeting
215
    assert_equal "<p>This is grand!</p>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
216 217 218
  end

  def test_layout_rendering
219
    get :layout_test
220
    assert_equal "<html>Hello world!</html>", @response.body
D
Initial  
David Heinemeier Hansson 已提交
221 222 223
  end

  def test_render_xml_with_layouts
224
    get :builder_layout_test
225
    assert_equal "<wrapper>\n<html>\n  <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
226 227
  end

228
  # def test_partials_list
229
  #   get :partials_list
230 231
  #   assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
  # end
D
Initial  
David Heinemeier Hansson 已提交
232

233
  def test_partial_only
234
    get :partial_only
235
    assert_equal "only partial", @response.body
236 237
  end

238
  def test_render_to_string
239
    get :hello_in_a_string
240
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
241 242 243
  end

  def test_render_to_string_resets_assigns
244
    get :render_to_string_test
245
    assert_equal "The value of foo is: ::this is a test::\n", @response.body
246 247
  end

248
  def test_nested_rendering
249 250 251
    @controller = Fun::GamesController.new
    get :hello_world
    assert_equal "Living in a nested world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
252 253
  end

254
  def test_accessing_params_in_template
255
    get :accessing_params_in_template, :name => "David"
256
    assert_equal "Hello: David", @response.body
257 258
  end

259
  def test_accessing_local_assigns_in_inline_template
260
    get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
261
    assert_equal "Goodbye, Local David", @response.body
262
  end
J
Jeremy Kemper 已提交
263

264
  def test_accessing_local_assigns_in_inline_template_with_string_keys
265
    get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
266
    assert_equal "Goodbye, Local David", @response.body
267
  end
J
Jeremy Kemper 已提交
268 269 270 271 272

  protected
    def assert_deprecated_render(&block)
      assert_deprecated(/render/, &block)
    end
273
end