aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2008-06-25 05:47:01 -0500
committerJoshua Peek <josh@joshpeek.com>2008-06-25 05:49:38 -0500
commit6f5327013d6353c50cadfe2160d1b526ad687633 (patch)
tree61e61cd49bbad6e43ca98995884a2fc329c33d8e /actionpack
parent670e22e3724791f51d639f409930fba5af920081 (diff)
downloadrails-6f5327013d6353c50cadfe2160d1b526ad687633.tar.gz
rails-6f5327013d6353c50cadfe2160d1b526ad687633.tar.bz2
rails-6f5327013d6353c50cadfe2160d1b526ad687633.zip
Consolidate CustomHandlerTest, TemplateFileTest, and TemplateObjectTest and test them at a higher level of abstraction in ViewRenderTest.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_view/view_load_paths.rb10
-rw-r--r--actionpack/test/controller/custom_handler_test.rb45
-rw-r--r--actionpack/test/controller/view_paths_test.rb30
-rw-r--r--actionpack/test/template/render_test.rb124
-rw-r--r--actionpack/test/template/template_file_test.rb95
-rw-r--r--actionpack/test/template/template_object_test.rb92
6 files changed, 142 insertions, 254 deletions
diff --git a/actionpack/lib/action_view/view_load_paths.rb b/actionpack/lib/action_view/view_load_paths.rb
index e873d96aa0..f23ac665f1 100644
--- a/actionpack/lib/action_view/view_load_paths.rb
+++ b/actionpack/lib/action_view/view_load_paths.rb
@@ -6,19 +6,15 @@ module ActionView #:nodoc:
class LoadPath #:nodoc:
attr_reader :path, :paths
- delegate :to_s, :inspect, :to => :path
+ delegate :to_s, :to_str, :inspect, :to => :path
def initialize(path)
@path = path.freeze
reload!
end
- def eql?(view_path)
- view_path.is_a?(ViewPath) && @path == view_path.path
- end
-
- def hash
- @path.hash
+ def ==(path)
+ to_str == path.to_str
end
# Rebuild load path directory cache
diff --git a/actionpack/test/controller/custom_handler_test.rb b/actionpack/test/controller/custom_handler_test.rb
deleted file mode 100644
index ac484ae17e..0000000000
--- a/actionpack/test/controller/custom_handler_test.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-require 'abstract_unit'
-
-class CustomHandler < ActionView::TemplateHandler
- def initialize( view )
- @view = view
- end
-
- def render( template )
- [ template.source,
- template.locals,
- @view ]
- end
-end
-
-class CustomHandlerTest < Test::Unit::TestCase
- def setup
- ActionView::Template.register_template_handler "foo", CustomHandler
- ActionView::Template.register_template_handler :foo2, CustomHandler
- @view = ActionView::Base.new
- end
-
- def test_custom_render
- template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo")
-
- result = @view.render_template(template)
- assert_equal(
- [ "hello <%= one %>", { :one => "two" }, @view ],
- result )
- end
-
- def test_custom_render2
- template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo2")
- result = @view.render_template(template)
- assert_equal(
- [ "hello <%= one %>", { :one => "two" }, @view ],
- result )
- end
-
- def test_unhandled_extension
- # uses the ERb handler by default if the extension isn't recognized
- template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "bar")
- result = @view.render_template(template)
- assert_equal "hello two", result
- end
-end
diff --git a/actionpack/test/controller/view_paths_test.rb b/actionpack/test/controller/view_paths_test.rb
index 1b4c1fae2f..8203061028 100644
--- a/actionpack/test/controller/view_paths_test.rb
+++ b/actionpack/test/controller/view_paths_test.rb
@@ -47,35 +47,35 @@ class ViewLoadPathsTest < Test::Unit::TestCase
end
def test_template_load_path_was_set_correctly
- assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths.map(&:to_s)
+ assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths
end
def test_controller_appends_view_path_correctly
@controller.append_view_path 'foo'
- assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths.map(&:to_s)
+ assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths
@controller.append_view_path(%w(bar baz))
- assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
+ assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
@controller.append_view_path(LOAD_PATH_ROOT)
- assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+ assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
@controller.append_view_path([LOAD_PATH_ROOT])
- assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+ assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
end
def test_controller_prepends_view_path_correctly
@controller.prepend_view_path 'baz'
- assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+ assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths
@controller.prepend_view_path(%w(foo bar))
- assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+ assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
@controller.prepend_view_path(LOAD_PATH_ROOT)
- assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
+ assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
@controller.prepend_view_path([LOAD_PATH_ROOT])
- assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
+ assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
end
def test_template_appends_view_path_correctly
@@ -83,10 +83,10 @@ class ViewLoadPathsTest < Test::Unit::TestCase
class_view_paths = TestController.view_paths
@controller.append_view_path 'foo'
- assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths.map(&:to_s)
+ assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths
@controller.append_view_path(%w(bar baz))
- assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
+ assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
assert_equal class_view_paths, TestController.view_paths
end
@@ -95,10 +95,10 @@ class ViewLoadPathsTest < Test::Unit::TestCase
class_view_paths = TestController.view_paths
@controller.prepend_view_path 'baz'
- assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+ assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths
@controller.prepend_view_path(%w(foo bar))
- assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+ assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
assert_equal class_view_paths, TestController.view_paths
end
@@ -140,13 +140,13 @@ class ViewLoadPathsTest < Test::Unit::TestCase
A.view_paths = ['a/path']
- assert_equal ['a/path'], A.view_paths.map(&:to_s)
+ assert_equal ['a/path'], A.view_paths
assert_equal A.view_paths, B.view_paths
assert_equal original_load_paths, C.view_paths
C.view_paths = []
assert_nothing_raised { C.view_paths << 'c/path' }
- assert_equal ['c/path'], C.view_paths.map(&:to_s)
+ assert_equal ['c/path'], C.view_paths
end
def test_find_template_file_for_path
diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb
new file mode 100644
index 0000000000..bdabe07121
--- /dev/null
+++ b/actionpack/test/template/render_test.rb
@@ -0,0 +1,124 @@
+require 'abstract_unit'
+require 'controller/fake_models'
+
+class ViewRenderTest < Test::Unit::TestCase
+ FIXTURE_LOAD_PATHS = ActionView::ViewLoadPaths.new([File.join(File.dirname(__FILE__), '..', 'fixtures')])
+
+ def setup
+ @assigns = { :secret => 'in the sauce' }
+ @view = ActionView::Base.new(FIXTURE_LOAD_PATHS, @assigns)
+ end
+
+ def test_render_file
+ assert_equal "Hello world!", @view.render("test/hello_world.erb")
+ end
+
+ def test_render_file_not_using_full_path
+ assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb", :use_full_path => true)
+ end
+
+ def test_render_file_without_specific_extension
+ assert_equal "Hello world!", @view.render("test/hello_world")
+ end
+
+ def test_render_file_with_full_path
+ template_path = File.join(File.dirname(__FILE__), '../fixtures/test/hello_world.erb')
+ assert_equal "Hello world!", @view.render(:file => template_path, :use_full_path => false)
+ end
+
+ def test_render_file_with_instance_variables
+ assert_equal "The secret is in the sauce\n", @view.render("test/render_file_with_ivar.erb")
+ end
+
+ def test_render_file_with_locals
+ locals = { :secret => 'in the sauce' }
+ assert_equal "The secret is in the sauce\n", @view.render("test/render_file_with_locals.erb", locals)
+ end
+
+ def test_render_file_not_using_full_path_with_dot_in_path
+ assert_equal "The secret is in the sauce\n", @view.render("test/dot.directory/render_file_with_ivar")
+ end
+
+ def test_render_update
+ # TODO: You should not have to stub out template because template is self!
+ @view.instance_variable_set(:@template, @view)
+ assert_equal 'alert("Hello, World!");', @view.render(:update) { |page| page.alert('Hello, World!') }
+ end
+
+ def test_render_partial
+ assert_equal "only partial", @view.render(:partial => "test/partial_only")
+ end
+
+ def test_render_partial_with_errors
+ assert_raise(ActionView::TemplateError) { @view.render(:partial => "test/raise") }
+ end
+
+ def test_render_partial_collection
+ assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ])
+ end
+
+ # TODO: The reason for this test is unclear, improve documentation
+ def test_render_partial_and_fallback_to_layout
+ assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
+ end
+
+ # TODO: The reason for this test is unclear, improve documentation
+ def test_render_js_partial_and_fallback_to_erb_layout
+ @view.template_format = :js
+ assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
+ end
+
+ # TODO: The reason for this test is unclear, improve documentation
+ def test_render_missing_xml_partial_and_raise_missing_template
+ @view.template_format = :xml
+ assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") }
+ end
+
+ def test_render_inline
+ assert_equal "Hello, World!", @view.render(:inline => "Hello, World!")
+ end
+
+ def test_render_inline_with_locals
+ assert_equal "Hello, Josh!", @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" })
+ end
+
+ def test_render_fallbacks_to_erb_for_unknown_types
+ assert_equal "Hello, World!", @view.render(:inline => "Hello, World!", :type => :foo)
+ end
+
+ class CustomHandler < ActionView::TemplateHandler
+ def render(template)
+ [template.source, template.locals].inspect
+ end
+ end
+
+ def test_render_inline_with_custom_type
+ ActionView::Template.register_template_handler :foo, CustomHandler
+ assert_equal '["Hello, World!", {}]', @view.render(:inline => "Hello, World!", :type => :foo)
+ end
+
+ def test_render_inline_with_locals_and_custom_type
+ ActionView::Template.register_template_handler :foo, CustomHandler
+ assert_equal '["Hello, <%= name %>!", {:name=>"Josh"}]', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
+ end
+
+ class CompilableCustomHandler < ActionView::TemplateHandler
+ include ActionView::TemplateHandlers::Compilable
+
+ def compile(template)
+ "@output_buffer = ''\n" +
+ "@output_buffer << 'locals: #{template.locals.inspect}, '\n" +
+ "@output_buffer << 'source: #{template.source.inspect}'\n"
+ end
+ end
+
+ def test_render_inline_with_compilable_custom_type
+ ActionView::Template.register_template_handler :foo, CompilableCustomHandler
+ assert_equal 'locals: {}, source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo)
+ end
+
+ def test_render_inline_with_locals_and_compilable_custom_type
+ ActionView::Template.register_template_handler :foo, CompilableCustomHandler
+ assert_equal 'locals: {:name=>"Josh"}, source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
+ end
+end
diff --git a/actionpack/test/template/template_file_test.rb b/actionpack/test/template/template_file_test.rb
deleted file mode 100644
index d14a966c1c..0000000000
--- a/actionpack/test/template/template_file_test.rb
+++ /dev/null
@@ -1,95 +0,0 @@
-require 'abstract_unit'
-
-class TemplateFileTest < Test::Unit::TestCase
- LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures')
-
- def setup
- @template = ActionView::TemplateFile.new("test/hello_world.html.erb")
- @another_template = ActionView::TemplateFile.new("test/hello_world.erb")
- @file_only = ActionView::TemplateFile.new("hello_world.erb")
- @full_path = ActionView::TemplateFile.new("/u/app/scales/config/../app/views/test/hello_world.erb", true)
- @layout = ActionView::TemplateFile.new("layouts/hello")
- @multipart = ActionView::TemplateFile.new("test_mailer/implicitly_multipart_example.text.html.erb")
- end
-
- def test_path
- assert_equal "test/hello_world.html.erb", @template.path
- assert_equal "test/hello_world.erb", @another_template.path
- assert_equal "hello_world.erb", @file_only.path
- assert_equal "/u/app/scales/config/../app/views/test/hello_world.erb", @full_path.path
- assert_equal "layouts/hello", @layout.path
- assert_equal "test_mailer/implicitly_multipart_example.text.html.erb", @multipart.path
- end
-
- def test_path_without_extension
- assert_equal "test/hello_world.html", @template.path_without_extension
- assert_equal "test/hello_world", @another_template.path_without_extension
- assert_equal "hello_world", @file_only.path_without_extension
- assert_equal "layouts/hello", @layout.path_without_extension
- assert_equal "test_mailer/implicitly_multipart_example.text.html", @multipart.path_without_extension
- end
-
- def test_path_without_format_and_extension
- assert_equal "test/hello_world", @template.path_without_format_and_extension
- assert_equal "test/hello_world", @another_template.path_without_format_and_extension
- assert_equal "hello_world", @file_only.path_without_format_and_extension
- assert_equal "layouts/hello", @layout.path_without_format_and_extension
- assert_equal "test_mailer/implicitly_multipart_example", @multipart.path_without_format_and_extension
- end
-
- def test_name
- assert_equal "hello_world", @template.name
- assert_equal "hello_world", @another_template.name
- assert_equal "hello_world", @file_only.name
- assert_equal "hello_world", @full_path.name
- assert_equal "hello", @layout.name
- assert_equal "implicitly_multipart_example", @multipart.name
- end
-
- def test_format
- assert_equal "html", @template.format
- assert_equal nil, @another_template.format
- assert_equal nil, @layout.format
- assert_equal "text.html", @multipart.format
- end
-
- def test_extension
- assert_equal "erb", @template.extension
- assert_equal "erb", @another_template.extension
- assert_equal nil, @layout.extension
- assert_equal "erb", @multipart.extension
- end
-
- def test_format_and_extension
- assert_equal "html.erb", @template.format_and_extension
- assert_equal "erb", @another_template.format_and_extension
- assert_equal nil, @layout.format_and_extension
- assert_equal "text.html.erb", @multipart.format_and_extension
- end
-
- def test_new_file_with_extension
- file = @template.dup_with_extension(:haml)
- assert_equal "test/hello_world.html", file.path_without_extension
- assert_equal "haml", file.extension
- assert_equal "test/hello_world.html.haml", file.path
-
- file = @another_template.dup_with_extension(:haml)
- assert_equal "test/hello_world", file.path_without_extension
- assert_equal "haml", file.extension
- assert_equal "test/hello_world.haml", file.path
-
- file = @another_template.dup_with_extension(nil)
- assert_equal "test/hello_world", file.path_without_extension
- assert_equal nil, file.extension
- assert_equal "test/hello_world", file.path
- end
-
- def test_freezes_entire_contents
- @template.freeze
- assert @template.frozen?
- assert @template.base_path.frozen?
- assert @template.name.frozen?
- assert @template.format.frozen?
- assert @template.extension.frozen?
- end
-end
diff --git a/actionpack/test/template/template_object_test.rb b/actionpack/test/template/template_object_test.rb
deleted file mode 100644
index 2cfc4523c6..0000000000
--- a/actionpack/test/template/template_object_test.rb
+++ /dev/null
@@ -1,92 +0,0 @@
-require 'abstract_unit'
-
-class TemplateObjectTest < Test::Unit::TestCase
- LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures')
-
- class TemplateTest < Test::Unit::TestCase
- def setup
- @view = ActionView::Base.new(LOAD_PATH_ROOT)
- @path = "test/hello_world.erb"
- end
-
- def test_should_create_valid_template
- template = ActionView::Template.new(@view, @path, true)
-
- assert_kind_of ActionView::TemplateHandlers::ERB, template.handler
- assert_equal "test/hello_world.erb", template.path.to_s
- assert_nil template.instance_variable_get(:"@source")
- assert_equal "erb", template.extension
- end
-
- uses_mocha 'Template preparation tests' do
- def test_should_prepare_template_properly
- template = ActionView::Template.new(@view, @path, true)
- view = template.instance_variable_get(:"@view")
-
- view.expects(:evaluate_assigns)
- template.handler.expects(:compile_template).with(template)
- view.expects(:method_names).returns({})
-
- template.prepare!
- end
- end
- end
-
- class PartialTemplateTest < Test::Unit::TestCase
- def setup
- @view = ActionView::Base.new(LOAD_PATH_ROOT)
- @path = "test/partial_only"
- end
-
- def test_should_create_valid_partial_template
- template = ActionView::PartialTemplate.new(@view, @path, nil)
-
- assert_equal "test/_partial_only", template.path.path_without_format_and_extension
- assert_equal :partial_only, template.variable_name
-
- assert template.locals.has_key?(:object)
- assert template.locals.has_key?(:partial_only)
- end
-
- def test_partial_with_errors
- template = ActionView::PartialTemplate.new(@view, 'test/raise', nil)
- assert_raise(ActionView::TemplateError) { template.render_template }
- end
-
- uses_mocha 'Partial template preparation tests' do
- def test_should_prepare_on_initialization
- ActionView::PartialTemplate.any_instance.expects(:prepare!)
- template = ActionView::PartialTemplate.new(@view, @path, 1)
- end
- end
- end
-
- class PartialTemplateFallbackTest < Test::Unit::TestCase
- def setup
- @view = ActionView::Base.new(LOAD_PATH_ROOT)
- @path = 'test/layout_for_partial'
- end
-
- def test_default
- template = ActionView::PartialTemplate.new(@view, @path, nil)
- assert_equal 'test/_layout_for_partial', template.path.path_without_format_and_extension
- assert_equal 'erb', template.extension
- assert_equal :html, @view.template_format
- end
-
- def test_js
- @view.template_format = :js
- template = ActionView::PartialTemplate.new(@view, @path, nil)
- assert_equal 'test/_layout_for_partial', template.path.path_without_format_and_extension
- assert_equal 'erb', template.extension
- assert_equal :html, @view.template_format
- end
-
- def test_xml
- @view.template_format = :xml
- assert_raise ActionView::MissingTemplate do
- ActionView::PartialTemplate.new(@view, @path, nil)
- end
- end
- end
-end