Froze rails gems
[depot.git] / vendor / rails / actionpack / test / template / render_test.rb
1 require 'abstract_unit'
2 require 'controller/fake_models'
3
4 class ViewRenderTest < Test::Unit::TestCase
5 def setup
6 @assigns = { :secret => 'in the sauce' }
7 @view = ActionView::Base.new(ActionController::Base.view_paths, @assigns)
8 end
9
10 def test_render_file
11 assert_deprecated do
12 assert_equal "Hello world!", @view.render("test/hello_world.erb")
13 end
14 end
15
16 def test_render_file_not_using_full_path
17 assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb")
18 end
19
20 def test_render_file_without_specific_extension
21 assert_deprecated do
22 assert_equal "Hello world!", @view.render("test/hello_world")
23 end
24 end
25
26 def test_render_file_at_top_level
27 assert_deprecated do
28 assert_equal 'Elastica', @view.render('/shared')
29 end
30 end
31
32 def test_render_file_with_full_path
33 template_path = File.join(File.dirname(__FILE__), '../fixtures/test/hello_world.erb')
34 assert_equal "Hello world!", @view.render(:file => template_path)
35 end
36
37 def test_render_file_with_instance_variables
38 assert_deprecated do
39 assert_equal "The secret is in the sauce\n", @view.render("test/render_file_with_ivar.erb")
40 end
41 end
42
43 def test_render_file_with_locals
44 locals = { :secret => 'in the sauce' }
45 assert_deprecated do
46 assert_equal "The secret is in the sauce\n", @view.render("test/render_file_with_locals.erb", locals)
47 end
48 end
49
50 def test_render_file_not_using_full_path_with_dot_in_path
51 assert_deprecated do
52 assert_equal "The secret is in the sauce\n", @view.render("test/dot.directory/render_file_with_ivar")
53 end
54 end
55
56 def test_render_has_access_current_template
57 assert_deprecated do
58 assert_equal "test/template.erb", @view.render("test/template.erb")
59 end
60 end
61
62 def test_render_update
63 # TODO: You should not have to stub out template because template is self!
64 @view.instance_variable_set(:@template, @view)
65 assert_equal 'alert("Hello, World!");', @view.render(:update) { |page| page.alert('Hello, World!') }
66 end
67
68 def test_render_partial
69 assert_equal "only partial", @view.render(:partial => "test/partial_only")
70 end
71
72 def test_render_partial_with_format
73 assert_equal 'partial html', @view.render(:partial => 'test/partial')
74 end
75
76 def test_render_partial_at_top_level
77 # file fixtures/_top_level_partial_only.erb (not fixtures/test)
78 assert_equal 'top level partial', @view.render(:partial => '/top_level_partial_only')
79 end
80
81 def test_render_partial_with_format_at_top_level
82 # file fixtures/_top_level_partial.html.erb (not fixtures/test, with format extension)
83 assert_equal 'top level partial html', @view.render(:partial => '/top_level_partial')
84 end
85
86 def test_render_partial_with_locals
87 assert_equal "5", @view.render(:partial => "test/counter", :locals => { :counter_counter => 5 })
88 end
89
90 def test_render_partial_with_errors
91 @view.render(:partial => "test/raise")
92 flunk "Render did not raise TemplateError"
93 rescue ActionView::TemplateError => e
94 assert_match "undefined local variable or method `doesnt_exist'", e.message
95 assert_equal "", e.sub_template_message
96 assert_equal "1", e.line_number
97 assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
98 end
99
100 def test_render_sub_template_with_errors
101 @view.render(:file => "test/sub_template_raise")
102 flunk "Render did not raise TemplateError"
103 rescue ActionView::TemplateError => e
104 assert_match "undefined local variable or method `doesnt_exist'", e.message
105 assert_equal "Trace of template inclusion: #{File.expand_path("#{FIXTURE_LOAD_PATH}/test/sub_template_raise.html.erb")}", e.sub_template_message
106 assert_equal "1", e.line_number
107 assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
108 end
109
110 def test_render_partial_collection
111 assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ])
112 end
113
114 def test_render_partial_collection_as
115 assert_equal "david david davidmary mary mary",
116 @view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer)
117 end
118
119 def test_render_partial_collection_without_as
120 assert_equal "local_inspector,local_inspector_counter,object",
121 @view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ])
122 end
123
124 def test_render_partial_with_empty_collection_should_return_nil
125 assert_nil @view.render(:partial => "test/customer", :collection => [])
126 end
127
128 def test_render_partial_with_nil_collection_should_return_nil
129 assert_nil @view.render(:partial => "test/customer", :collection => nil)
130 end
131
132 def test_render_partial_with_nil_values_in_collection
133 assert_equal "Hello: davidHello: Anonymous", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), nil ])
134 end
135
136 def test_render_partial_with_empty_array_should_return_nil
137 assert_nil @view.render(:partial => [])
138 end
139
140 # TODO: The reason for this test is unclear, improve documentation
141 def test_render_partial_and_fallback_to_layout
142 assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
143 end
144
145 # TODO: The reason for this test is unclear, improve documentation
146 def test_render_js_partial_and_fallback_to_erb_layout
147 @view.template_format = :js
148 assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
149 end
150
151 # TODO: The reason for this test is unclear, improve documentation
152 def test_render_missing_xml_partial_and_raise_missing_template
153 @view.template_format = :xml
154 assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") }
155 end
156
157 def test_render_inline
158 assert_equal "Hello, World!", @view.render(:inline => "Hello, World!")
159 end
160
161 def test_render_inline_with_locals
162 assert_equal "Hello, Josh!", @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" })
163 end
164
165 def test_render_fallbacks_to_erb_for_unknown_types
166 assert_equal "Hello, World!", @view.render(:inline => "Hello, World!", :type => :foo)
167 end
168
169 CustomHandler = lambda do |template|
170 "@output_buffer = ''\n" +
171 "@output_buffer << 'source: #{template.source.inspect}'\n"
172 end
173
174 def test_render_inline_with_compilable_custom_type
175 ActionView::Template.register_template_handler :foo, CustomHandler
176 assert_equal 'source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo)
177 end
178
179 def test_render_inline_with_locals_and_compilable_custom_type
180 ActionView::Template.register_template_handler :foo, CustomHandler
181 assert_equal 'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
182 end
183
184 def test_render_with_layout
185 assert_equal %(<title></title>\nHello world!\n),
186 @view.render(:file => "test/hello_world.erb", :layout => "layouts/yield")
187 end
188
189 def test_render_with_nested_layout
190 assert_equal %(<title>title</title>\n<div id="column">column</div>\n<div id="content">content</div>\n),
191 @view.render(:file => "test/nested_layout.erb", :layout => "layouts/yield")
192 end
193 end