aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/test')
-rw-r--r--actionpack/test/controller/view_paths_test.rb111
-rw-r--r--actionpack/test/template/template_file_test.rb95
-rw-r--r--actionpack/test/template/template_finder_test.rb60
-rw-r--r--actionpack/test/template/template_object_test.rb37
4 files changed, 180 insertions, 123 deletions
diff --git a/actionpack/test/controller/view_paths_test.rb b/actionpack/test/controller/view_paths_test.rb
index 0516da32d8..1b4c1fae2f 100644
--- a/actionpack/test/controller/view_paths_test.rb
+++ b/actionpack/test/controller/view_paths_test.rb
@@ -1,30 +1,30 @@
require 'abstract_unit'
class ViewLoadPathsTest < Test::Unit::TestCase
-
LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures')
- ActionController::Base.view_paths = [ LOAD_PATH_ROOT ]
+ ActionController::Base.view_paths = [LOAD_PATH_ROOT]
class TestController < ActionController::Base
def self.controller_path() "test" end
def rescue_action(e) raise end
-
+
before_filter :add_view_path, :only => :hello_world_at_request_time
-
+
def hello_world() end
def hello_world_at_request_time() render(:action => 'hello_world') end
+
private
- def add_view_path
- prepend_view_path "#{LOAD_PATH_ROOT}/override"
- end
+ def add_view_path
+ prepend_view_path "#{LOAD_PATH_ROOT}/override"
+ end
end
-
+
class Test::SubController < ActionController::Base
layout 'test/sub'
def hello_world; render(:template => 'test/hello_world'); end
end
-
+
def setup
TestController.view_paths = nil
@@ -41,68 +41,80 @@ class ViewLoadPathsTest < Test::Unit::TestCase
@last_message = nil
ActiveSupport::Deprecation.behavior = Proc.new { |message, callback| @last_message = message }
end
-
+
def teardown
ActiveSupport::Deprecation.behavior = @old_behavior
end
-
+
def test_template_load_path_was_set_correctly
- assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths
+ assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths.map(&:to_s)
end
-
+
def test_controller_appends_view_path_correctly
@controller.append_view_path 'foo'
- assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths
-
+ assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths.map(&:to_s)
+
@controller.append_view_path(%w(bar baz))
- assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
+ assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
+
+ @controller.append_view_path(LOAD_PATH_ROOT)
+ assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+
+ @controller.append_view_path([LOAD_PATH_ROOT])
+ assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
end
-
+
def test_controller_prepends_view_path_correctly
@controller.prepend_view_path 'baz'
- assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths
-
+ assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+
@controller.prepend_view_path(%w(foo bar))
- assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
+ assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+
+ @controller.prepend_view_path(LOAD_PATH_ROOT)
+ assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
+
+ @controller.prepend_view_path([LOAD_PATH_ROOT])
+ assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
end
-
+
def test_template_appends_view_path_correctly
@controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller)
class_view_paths = TestController.view_paths
@controller.append_view_path 'foo'
- assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths
-
+ assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths.map(&:to_s)
+
@controller.append_view_path(%w(bar baz))
- assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
+ assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
assert_equal class_view_paths, TestController.view_paths
end
-
+
def test_template_prepends_view_path_correctly
@controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller)
class_view_paths = TestController.view_paths
-
+
@controller.prepend_view_path 'baz'
- assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths
-
+ assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
+
@controller.prepend_view_path(%w(foo bar))
- assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
+ assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s)
assert_equal class_view_paths, TestController.view_paths
end
-
+
def test_view_paths
get :hello_world
assert_response :success
assert_equal "Hello world!", @response.body
end
-
+
def test_view_paths_override
TestController.prepend_view_path "#{LOAD_PATH_ROOT}/override"
get :hello_world
assert_response :success
assert_equal "Hello overridden world!", @response.body
end
-
+
def test_view_paths_override_for_layouts_in_controllers_with_a_module
@controller = Test::SubController.new
Test::SubController.view_paths = [ "#{LOAD_PATH_ROOT}/override", LOAD_PATH_ROOT, "#{LOAD_PATH_ROOT}/override2" ]
@@ -110,31 +122,44 @@ class ViewLoadPathsTest < Test::Unit::TestCase
assert_response :success
assert_equal "layout: Hello overridden world!", @response.body
end
-
+
def test_view_paths_override_at_request_time
get :hello_world_at_request_time
assert_response :success
assert_equal "Hello overridden world!", @response.body
end
-
+
def test_inheritance
original_load_paths = ActionController::Base.view_paths
-
+
self.class.class_eval %{
class A < ActionController::Base; end
class B < A; end
class C < ActionController::Base; end
}
-
- A.view_paths = [ 'a/path' ]
-
- assert_equal [ 'a/path' ], A.view_paths
- assert_equal A.view_paths, B.view_paths
+
+ A.view_paths = ['a/path']
+
+ assert_equal ['a/path'], A.view_paths.map(&:to_s)
+ 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
+ assert_equal ['c/path'], C.view_paths.map(&:to_s)
+ end
+
+ def test_find_template_file_for_path
+ assert_equal "test/hello_world.erb", @controller.view_paths.find_template_file_for_path("test/hello_world.erb").to_s
+ assert_equal "test/hello.builder", @controller.view_paths.find_template_file_for_path("test/hello.builder").to_s
+ assert_equal nil, @controller.view_paths.find_template_file_for_path("test/missing.erb")
+ end
+
+ def test_view_paths_find_template_file_for_path
+ assert_equal "test/formatted_html_erb.html.erb", @controller.view_paths.find_template_file_for_path("test/formatted_html_erb.html").to_s
+ assert_equal "test/formatted_xml_erb.xml.erb", @controller.view_paths.find_template_file_for_path("test/formatted_xml_erb.xml").to_s
+ assert_equal "test/hello_world.erb", @controller.view_paths.find_template_file_for_path("test/hello_world.html").to_s
+ assert_equal "test/hello_world.erb", @controller.view_paths.find_template_file_for_path("test/hello_world.xml").to_s
+ assert_equal nil, @controller.view_paths.find_template_file_for_path("test/missing.html")
end
-
end
diff --git a/actionpack/test/template/template_file_test.rb b/actionpack/test/template/template_file_test.rb
new file mode 100644
index 0000000000..d14a966c1c
--- /dev/null
+++ b/actionpack/test/template/template_file_test.rb
@@ -0,0 +1,95 @@
+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_finder_test.rb b/actionpack/test/template/template_finder_test.rb
deleted file mode 100644
index 07fc4b8c56..0000000000
--- a/actionpack/test/template/template_finder_test.rb
+++ /dev/null
@@ -1,60 +0,0 @@
-require 'abstract_unit'
-
-class TemplateFinderTest < Test::Unit::TestCase
-
- LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures')
-
- def setup
- ActionView::TemplateFinder.process_view_paths(LOAD_PATH_ROOT)
- ActionView::Template::register_template_handler :mab, Class.new(ActionView::TemplateHandler)
- @template = ActionView::Base.new
- @finder = ActionView::TemplateFinder.new(@template, LOAD_PATH_ROOT)
- end
-
- def test_should_cache_file_extension_properly
- assert_equal ["builder", "erb", "rhtml", "rjs", "rxml", "mab"].sort,
- ActionView::TemplateFinder.file_extension_cache[LOAD_PATH_ROOT].values.flatten.uniq.sort
-
- assert_equal (Dir.glob("#{LOAD_PATH_ROOT}/**/*/*.{erb,rjs,rhtml,builder,rxml,mab}") |
- Dir.glob("#{LOAD_PATH_ROOT}/**.{erb,rjs,rhtml,builder,rxml,mab}")).size,
- ActionView::TemplateFinder.file_extension_cache[LOAD_PATH_ROOT].keys.size
- end
-
- def test_should_cache_dir_content_properly
- assert ActionView::TemplateFinder.processed_view_paths[LOAD_PATH_ROOT]
- assert_equal (Dir.glob("#{LOAD_PATH_ROOT}/**/*/**") | Dir.glob("#{LOAD_PATH_ROOT}/**")).find_all {|f| !File.directory?(f) }.size,
- ActionView::TemplateFinder.processed_view_paths[LOAD_PATH_ROOT].size
- end
-
- def test_find_template_extension_from_first_render
- assert_nil @finder.send(:find_template_extension_from_first_render)
-
- {
- nil => nil,
- '' => nil,
- 'foo' => nil,
- '/foo' => nil,
- 'foo.rb' => 'rb',
- 'foo.bar.rb' => 'bar.rb',
- 'baz/foo.rb' => 'rb',
- 'baz/foo.bar.rb' => 'bar.rb',
- 'baz/foo.o/foo.rb' => 'rb',
- 'baz/foo.o/foo.bar.rb' => 'bar.rb',
- }.each do |input,expectation|
- @template.instance_variable_set('@first_render', input)
- assert_equal expectation, @finder.send(:find_template_extension_from_first_render)
- end
- end
-
- def test_should_report_file_exists_correctly
- assert_nil @finder.send(:find_template_extension_from_first_render)
- assert_equal false, @finder.send(:file_exists?, 'test.rhtml')
- assert_equal false, @finder.send(:file_exists?, 'test.rb')
-
- @template.instance_variable_set('@first_render', 'foo.rb')
-
- assert_equal 'rb', @finder.send(:find_template_extension_from_first_render)
- assert_equal false, @finder.send(:file_exists?, 'baz')
- assert_equal false, @finder.send(:file_exists?, 'baz.rb')
- end
-end
diff --git a/actionpack/test/template/template_object_test.rb b/actionpack/test/template/template_object_test.rb
index afb5c5cc16..2cfc4523c6 100644
--- a/actionpack/test/template/template_object_test.rb
+++ b/actionpack/test/template/template_object_test.rb
@@ -2,60 +2,57 @@ require 'abstract_unit'
class TemplateObjectTest < Test::Unit::TestCase
LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures')
- ActionView::TemplateFinder.process_view_paths(LOAD_PATH_ROOT)
-
+
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
+ 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
+
+ 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!)
@@ -63,7 +60,7 @@ class TemplateObjectTest < Test::Unit::TestCase
end
end
end
-
+
class PartialTemplateFallbackTest < Test::Unit::TestCase
def setup
@view = ActionView::Base.new(LOAD_PATH_ROOT)
@@ -72,7 +69,7 @@ class TemplateObjectTest < Test::Unit::TestCase
def test_default
template = ActionView::PartialTemplate.new(@view, @path, nil)
- assert_equal 'test/_layout_for_partial', template.path
+ 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
@@ -80,7 +77,7 @@ class TemplateObjectTest < Test::Unit::TestCase
def test_js
@view.template_format = :js
template = ActionView::PartialTemplate.new(@view, @path, nil)
- assert_equal 'test/_layout_for_partial', template.path
+ 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