diff options
Diffstat (limited to 'actionpack/test/template')
-rw-r--r-- | actionpack/test/template/date_helper_i18n_test.rb | 15 | ||||
-rw-r--r-- | actionpack/test/template/date_helper_test.rb | 31 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 27 | ||||
-rw-r--r-- | actionpack/test/template/record_tag_helper_test.rb | 8 | ||||
-rw-r--r-- | actionpack/test/template/render_test.rb | 7 | ||||
-rw-r--r-- | actionpack/test/template/sprockets_helper_test.rb | 14 | ||||
-rw-r--r-- | actionpack/test/template/sprockets_helper_with_routes_test.rb | 4 | ||||
-rw-r--r-- | actionpack/test/template/template_test.rb | 16 |
8 files changed, 111 insertions, 11 deletions
diff --git a/actionpack/test/template/date_helper_i18n_test.rb b/actionpack/test/template/date_helper_i18n_test.rb index d45215acfd..b0b407b473 100644 --- a/actionpack/test/template/date_helper_i18n_test.rb +++ b/actionpack/test/template/date_helper_i18n_test.rb @@ -36,16 +36,25 @@ class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase end end - def assert_distance_of_time_in_words_translates_key(passed, expected) + def test_distance_of_time_in_words_calls_i18n_with_custom_scope + { + [30.days, false] => [:'about_x_months', 1], + [60.days, false] => [:'x_months', 2], + }.each do |passed, expected| + assert_distance_of_time_in_words_translates_key(passed, expected, {:scope => :'datetime.distance_in_words_ago'}) + end + end + + def assert_distance_of_time_in_words_translates_key(passed, expected, options = {}) diff, include_seconds = *passed key, count = *expected to = @from + diff - options = {:locale => 'en', :scope => :'datetime.distance_in_words'} + options = {:locale => 'en', :scope => :'datetime.distance_in_words'}.merge(options) options[:count] = count if count I18n.expects(:t).with(key, options) - distance_of_time_in_words(@from, to, include_seconds, :locale => 'en') + distance_of_time_in_words(@from, to, include_seconds, options) end def test_distance_of_time_pluralizations diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index cf707e1de5..e4f84f8dd7 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -128,12 +128,39 @@ class DateHelperTest < ActionView::TestCase end def test_distance_in_words_with_integers - assert_equal "less than a minute", distance_of_time_in_words(59) + assert_equal "1 minute", distance_of_time_in_words(59) assert_equal "about 1 hour", distance_of_time_in_words(60*60) - assert_equal "less than a minute", distance_of_time_in_words(0, 59) + assert_equal "1 minute", distance_of_time_in_words(0, 59) assert_equal "about 1 hour", distance_of_time_in_words(60*60, 0) end + def test_distance_in_words_with_times + assert_equal "1 minute", distance_of_time_in_words(30.seconds) + assert_equal "1 minute", distance_of_time_in_words(59.seconds) + assert_equal "2 minutes", distance_of_time_in_words(119.seconds) + assert_equal "2 minutes", distance_of_time_in_words(1.minute + 59.seconds) + assert_equal "3 minutes", distance_of_time_in_words(2.minute + 30.seconds) + assert_equal "44 minutes", distance_of_time_in_words(44.minutes + 29.seconds) + assert_equal "about 1 hour", distance_of_time_in_words(44.minutes + 30.seconds) + assert_equal "about 1 hour", distance_of_time_in_words(60.minutes) + + # include seconds + assert_equal "half a minute", distance_of_time_in_words(39.seconds, 0, true) + assert_equal "less than a minute", distance_of_time_in_words(40.seconds, 0, true) + assert_equal "less than a minute", distance_of_time_in_words(59.seconds, 0, true) + assert_equal "1 minute", distance_of_time_in_words(60.seconds, 0, true) + end + + def test_distance_in_words_with_offset_datetimes + start_date = DateTime.new 1975, 1, 31, 0, 0, 0, '+6' + end_date = DateTime.new 1977, 1, 31, 0, 0, 0, '+6' + assert_equal("about 2 years", distance_of_time_in_words(start_date, end_date)) + + start_date = DateTime.new 1982, 12, 3, 0, 0, 0, '+6' + end_date = DateTime.new 2010, 11, 30, 0, 0, 0, '+6' + assert_equal("almost 28 years", distance_of_time_in_words(start_date, end_date)) + end + def test_time_ago_in_words assert_equal "about 1 year", time_ago_in_words(1.year.ago - 1.day) end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 19af01e2c8..7b35424ec7 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -393,6 +393,19 @@ class FormHelperTest < ActionView::TestCase ) end + def test_check_box_with_multiple_behavior_and_index + @post.comment_ids = [2,3] + assert_dom_equal( + '<input name="post[foo][comment_ids][]" type="hidden" value="0" /><input id="post_foo_comment_ids_1" name="post[foo][comment_ids][]" type="checkbox" value="1" />', + check_box("post", "comment_ids", { :multiple => true, :index => "foo" }, 1) + ) + assert_dom_equal( + '<input name="post[bar][comment_ids][]" type="hidden" value="0" /><input checked="checked" id="post_bar_comment_ids_3" name="post[bar][comment_ids][]" type="checkbox" value="3" />', + check_box("post", "comment_ids", { :multiple => true, :index => "bar" }, 3) + ) + + end + def test_checkbox_disabled_disables_hidden_field assert_dom_equal( '<input name="post[secret]" type="hidden" value="0" disabled="disabled"/><input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" />', @@ -742,7 +755,6 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end - def test_form_for_with_format form_for(@post, :format => :json, :html => { :id => "edit_post_123", :class => "edit_post" }) do |f| concat f.label(:title) @@ -2204,6 +2216,19 @@ class FormHelperTest < ActionView::TestCase assert_equal "fields", output end + def test_form_for_only_instantiates_builder_once + initialization_count = 0 + builder_class = Class.new(ActionView::Helpers::FormBuilder) do + define_method :initialize do |*args| + super(*args) + initialization_count += 1 + end + end + + form_for(@post, :builder => builder_class) { } + assert_equal 1, initialization_count, 'form builder instantiated more than once' + end + protected def protect_against_forgery? false diff --git a/actionpack/test/template/record_tag_helper_test.rb b/actionpack/test/template/record_tag_helper_test.rb index f33471ab28..3cb5997a83 100644 --- a/actionpack/test/template/record_tag_helper_test.rb +++ b/actionpack/test/template/record_tag_helper_test.rb @@ -81,6 +81,14 @@ class RecordTagHelperTest < ActionView::TestCase assert_dom_equal expected, actual end + def test_content_tag_for_collection_without_given_block + post_1 = RecordTagPost.new.tap { |post| post.id = 101; post.body = "Hello!"; post.persisted = true } + post_2 = RecordTagPost.new.tap { |post| post.id = 102; post.body = "World!"; post.persisted = true } + expected = %(<li class="record_tag_post" id="record_tag_post_101"></li>\n<li class="record_tag_post" id="record_tag_post_102"></li>) + actual = content_tag_for(:li, [post_1, post_2]) + assert_dom_equal expected, actual + end + def test_div_for_collection post_1 = RecordTagPost.new.tap { |post| post.id = 101; post.body = "Hello!"; post.persisted = true } post_2 = RecordTagPost.new.tap { |post| post.id = 102; post.body = "World!"; post.persisted = true } diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index bfeb6aee1a..b907e3297b 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -336,7 +336,7 @@ module RenderTestCases ActionView::Template.register_template_handler :foo, CustomHandler assert_equal 'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo) end - + def test_render_knows_about_types_registered_when_extensions_are_checked_earlier_in_initialization ActionView::Template::Handlers.extensions ActionView::Template.register_template_handler :foo, CustomHandler @@ -406,6 +406,11 @@ module RenderTestCases @view.render(:partial => 'test/partial_with_layout_block_content', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'}) end + def test_render_partial_with_layout_raises_descriptive_error + e = assert_raises(ActionView::MissingTemplate) { @view.render(:partial => 'test/partial', :layout => true) } + assert_match "Missing partial /true with", e.message + end + def test_render_with_nested_layout assert_equal %(<title>title</title>\n\n<div id="column">column</div>\n<div id="content">content</div>\n), @view.render(:file => "test/nested_layout", :layout => "layouts/yield") diff --git a/actionpack/test/template/sprockets_helper_test.rb b/actionpack/test/template/sprockets_helper_test.rb index 72d03e43e9..93832c7bd9 100644 --- a/actionpack/test/template/sprockets_helper_test.rb +++ b/actionpack/test/template/sprockets_helper_test.rb @@ -1,7 +1,7 @@ require 'abstract_unit' require 'sprockets' require 'sprockets/helpers/rails_helper' -require 'mocha' +require 'mocha/setup' class SprocketsHelperTest < ActionView::TestCase include Sprockets::Helpers::RailsHelper @@ -266,6 +266,15 @@ class SprocketsHelperTest < ActionView::TestCase javascript_include_tag('/javascripts/application') assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1" type="text/javascript"></script>}, javascript_include_tag(:application) + assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/extra-[0-9a-f]+.js\?body=1" type="text/javascript"></script>}, + javascript_include_tag(:application, :extra) + end + + test "precompiled assets with an extension when no JS runtime is available" do + @config.assets.compile = false + @config.assets.digests = {'foo.min.js' => 'foo.min-f00.js'} + Sprockets::Index.any_instance.stubs(:build_asset).raises + assert_nothing_raised { javascript_include_tag('foo.min') } end test "stylesheet path through asset_path" do @@ -324,6 +333,9 @@ class SprocketsHelperTest < ActionView::TestCase assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />}, stylesheet_link_tag(:application) + assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/extra-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />}, + stylesheet_link_tag(:application, :extra) + assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" type="text/css" />}, stylesheet_link_tag(:application, :media => "print") end diff --git a/actionpack/test/template/sprockets_helper_with_routes_test.rb b/actionpack/test/template/sprockets_helper_with_routes_test.rb index bcbd81a7dd..bc253ea0fd 100644 --- a/actionpack/test/template/sprockets_helper_with_routes_test.rb +++ b/actionpack/test/template/sprockets_helper_with_routes_test.rb @@ -1,7 +1,7 @@ require 'abstract_unit' require 'sprockets' require 'sprockets/helpers/rails_helper' -require 'mocha' +require 'mocha/setup' class SprocketsHelperWithRoutesTest < ActionView::TestCase include Sprockets::Helpers::RailsHelper @@ -54,4 +54,4 @@ class SprocketsHelperWithRoutesTest < ActionView::TestCase stylesheet_link_tag(:application) end -end
\ No newline at end of file +end diff --git a/actionpack/test/template/template_test.rb b/actionpack/test/template/template_test.rb index 5880eb2bd4..56943381d8 100644 --- a/actionpack/test/template/template_test.rb +++ b/actionpack/test/template/template_test.rb @@ -25,6 +25,10 @@ class TestERBTemplate < ActiveSupport::TestCase "Hello" end + def apostrophe + "l'apostrophe" + end + def partial ActionView::Template.new( "<%= @virtual_path %>", @@ -47,7 +51,7 @@ class TestERBTemplate < ActiveSupport::TestCase end end - def new_template(body = "<%= hello %>", details = {}) + def new_template(body = "<%= hello %>", details = { :format => :html }) ActionView::Template.new(body, "hello template", ERBHandler, {:virtual_path => "hello"}.merge!(details)) end @@ -64,6 +68,16 @@ class TestERBTemplate < ActiveSupport::TestCase assert_equal "Hello", render end + def test_basic_template_does_html_escape + @template = new_template("<%= apostrophe %>") + assert_equal "l'apostrophe", render + end + + def test_text_template_does_not_html_escape + @template = new_template("<%= apostrophe %> <%== apostrophe %>", :format => :text) + assert_equal "l'apostrophe l'apostrophe", render + end + def test_template_loses_its_source_after_rendering @template = new_template render |