diff options
Diffstat (limited to 'actionpack/test/template')
-rw-r--r-- | actionpack/test/template/active_model_helper_test.rb | 18 | ||||
-rw-r--r-- | actionpack/test/template/body_parts_test.rb | 3 | ||||
-rw-r--r-- | actionpack/test/template/capture_helper_test.rb | 106 | ||||
-rw-r--r-- | actionpack/test/template/erb/form_for_test.rb | 11 | ||||
-rw-r--r-- | actionpack/test/template/erb/helper.rb | 30 | ||||
-rw-r--r-- | actionpack/test/template/erb/tag_helper_test.rb | 58 | ||||
-rw-r--r-- | actionpack/test/template/javascript_helper_test.rb | 31 | ||||
-rw-r--r-- | actionpack/test/template/lookup_context_test.rb | 45 | ||||
-rw-r--r-- | actionpack/test/template/number_helper_i18n_test.rb | 128 | ||||
-rw-r--r-- | actionpack/test/template/number_helper_test.rb | 295 | ||||
-rw-r--r-- | actionpack/test/template/output_buffer_test.rb | 19 | ||||
-rw-r--r-- | actionpack/test/template/render_test.rb | 10 | ||||
-rw-r--r-- | actionpack/test/template/translation_helper_test.rb | 15 | ||||
-rw-r--r-- | actionpack/test/template/url_helper_test.rb | 5 |
14 files changed, 614 insertions, 160 deletions
diff --git a/actionpack/test/template/active_model_helper_test.rb b/actionpack/test/template/active_model_helper_test.rb index 371aa53c68..7a665b00bc 100644 --- a/actionpack/test/template/active_model_helper_test.rb +++ b/actionpack/test/template/active_model_helper_test.rb @@ -147,6 +147,20 @@ class ActiveModelHelperTest < ActionView::TestCase ) end + def test_field_error_proc + old_proc = ActionView::Base.field_error_proc + ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| + %(<div class=\"fieldWithErrors\">#{html_tag} <span class="error">#{[instance.error_message].join(', ')}</span></div>).html_safe + end + + assert_dom_equal( + %(<div class="fieldWithErrors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /> <span class="error">can't be empty</span></div>), + text_field("post", "author_name") + ) + ensure + ActionView::Base.field_error_proc = old_proc if old_proc + end + def test_form_with_string assert_dom_equal( %(<form action="create" method="post"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>), @@ -252,6 +266,10 @@ class ActiveModelHelperTest < ActionView::TestCase assert_dom_equal "<div class=\"differentError\">beforecan't be emptyafter</div>", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after') end + def test_error_message_on_handles_empty_errors + assert_equal "", error_message_on(@post, :tag) + end + def test_error_messages_for_many_objects assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li><li>User email can't be empty</li></ul></div>), error_messages_for("post", "user") diff --git a/actionpack/test/template/body_parts_test.rb b/actionpack/test/template/body_parts_test.rb index defe85107e..69cf684083 100644 --- a/actionpack/test/template/body_parts_test.rb +++ b/actionpack/test/template/body_parts_test.rb @@ -8,9 +8,8 @@ class BodyPartsTest < ActionController::TestCase def index RENDERINGS.each do |rendering| - @template.punctuate_body! rendering + view_context.punctuate_body! rendering end - @performed_render = true end end diff --git a/actionpack/test/template/capture_helper_test.rb b/actionpack/test/template/capture_helper_test.rb index f887c9ab5b..bf541c17d3 100644 --- a/actionpack/test/template/capture_helper_test.rb +++ b/actionpack/test/template/capture_helper_test.rb @@ -3,9 +3,33 @@ require 'abstract_unit' class CaptureHelperTest < ActionView::TestCase def setup super + @av = ActionView::Base.new @_content_for = Hash.new {|h,k| h[k] = "" } end + def test_capture_captures_the_temporary_output_buffer_in_its_block + assert_nil @av.output_buffer + string = @av.capture do + @av.output_buffer << 'foo' + @av.output_buffer << 'bar' + end + assert_nil @av.output_buffer + assert_equal 'foobar', string + assert_kind_of ActionView::NonConcattingString, string + end + + def test_capture_captures_the_value_returned_by_the_block_if_the_temporary_buffer_is_blank + string = @av.capture('foo', 'bar') do |a, b| + a + b + end + assert_equal 'foobar', string + assert_kind_of ActionView::NonConcattingString, string + end + + def test_capture_returns_nil_if_the_returned_value_is_not_a_string + assert_nil @av.capture { 1 } + end + def test_content_for assert ! content_for?(:title) content_for :title, 'title' @@ -13,9 +37,85 @@ class CaptureHelperTest < ActionView::TestCase assert ! content_for?(:something_else) end + def test_with_output_buffer_swaps_the_output_buffer_given_no_argument + assert_nil @av.output_buffer + buffer = @av.with_output_buffer do + @av.output_buffer << '.' + end + assert_equal '.', buffer + assert_nil @av.output_buffer + end + + def test_with_output_buffer_swaps_the_output_buffer_with_an_argument + assert_nil @av.output_buffer + buffer = ActionView::OutputBuffer.new('.') + @av.with_output_buffer(buffer) do + @av.output_buffer << '.' + end + assert_equal '..', buffer + assert_nil @av.output_buffer + end + + def test_with_output_buffer_restores_the_output_buffer + buffer = ActionView::OutputBuffer.new + @av.output_buffer = buffer + @av.with_output_buffer do + @av.output_buffer << '.' + end + assert buffer.equal?(@av.output_buffer) + end + + unless RUBY_VERSION < '1.9' + def test_with_output_buffer_sets_proper_encoding + @av.output_buffer = ActionView::OutputBuffer.new + + # Ensure we set the output buffer to an encoding different than the default one. + alt_encoding = alt_encoding(@av.output_buffer) + @av.output_buffer.force_encoding(alt_encoding) + + @av.with_output_buffer do + assert alt_encoding, @av.output_buffer.encoding + end + end + end + def test_with_output_buffer_does_not_assume_there_is_an_output_buffer - av = ActionView::Base.new - assert_nil av.output_buffer - assert_equal "", av.with_output_buffer {} + assert_nil @av.output_buffer + assert_equal "", @av.with_output_buffer {} + end + + def test_flush_output_buffer_concats_output_buffer_to_response + view = view_with_controller + assert_equal [], view.response.body_parts + + view.output_buffer << 'OMG' + view.flush_output_buffer + assert_equal ['OMG'], view.response.body_parts + assert_equal '', view.output_buffer + + view.output_buffer << 'foobar' + view.flush_output_buffer + assert_equal ['OMG', 'foobar'], view.response.body_parts + assert_equal '', view.output_buffer + end + + unless RUBY_VERSION < '1.9' + def test_flush_output_buffer_preserves_the_encoding_of_the_output_buffer + view = view_with_controller + alt_encoding = alt_encoding(view.output_buffer) + view.output_buffer.force_encoding(alt_encoding) + flush_output_buffer + assert_equal alt_encoding, view.output_buffer.encoding + end + end + + def alt_encoding(output_buffer) + output_buffer.encoding == Encoding::US_ASCII ? Encoding::UTF_8 : Encoding::US_ASCII + end + + def view_with_controller + returning(TestController.new.view_context) do |view| + view.output_buffer = ActionView::OutputBuffer.new + end end end diff --git a/actionpack/test/template/erb/form_for_test.rb b/actionpack/test/template/erb/form_for_test.rb new file mode 100644 index 0000000000..482dbb0287 --- /dev/null +++ b/actionpack/test/template/erb/form_for_test.rb @@ -0,0 +1,11 @@ +require "abstract_unit" +require "template/erb/helper" + +module ERBTest + class TagHelperTest < BlockTestCase + test "form_for works" do + output = render_content "form_for(:staticpage, :url => {:controller => 'blah', :action => 'update'})", "" + assert_equal "<form action=\"/blah/update\" method=\"post\"></form>", output + end + end +end
\ No newline at end of file diff --git a/actionpack/test/template/erb/helper.rb b/actionpack/test/template/erb/helper.rb new file mode 100644 index 0000000000..7147178849 --- /dev/null +++ b/actionpack/test/template/erb/helper.rb @@ -0,0 +1,30 @@ +module ERBTest + class ViewContext + mock_controller = Class.new do + include SharedTestRoutes.url_helpers + end + + include ActionView::Helpers::TagHelper + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::FormHelper + + attr_accessor :output_buffer + + def protect_against_forgery?() false end + + define_method(:controller) do + mock_controller.new + end + end + + class BlockTestCase < ActiveSupport::TestCase + def render_content(start, inside) + template = block_helper(start, inside) + ActionView::Template::Handlers::Erubis.new(template).evaluate(ViewContext.new) + end + + def block_helper(str, rest) + "<%= #{str} do %>#{rest}<% end %>" + end + end +end
\ No newline at end of file diff --git a/actionpack/test/template/erb/tag_helper_test.rb b/actionpack/test/template/erb/tag_helper_test.rb index cc96a43901..64a88bde1d 100644 --- a/actionpack/test/template/erb/tag_helper_test.rb +++ b/actionpack/test/template/erb/tag_helper_test.rb @@ -1,66 +1,44 @@ require "abstract_unit" +require "template/erb/helper" module ERBTest - class ViewContext - mock_controller = Class.new do - include SharedTestRoutes.url_helpers - end - - include ActionView::Helpers::TagHelper - include ActionView::Helpers::JavaScriptHelper - include ActionView::Helpers::FormHelper - - attr_accessor :output_buffer - - def protect_against_forgery?() false end - - define_method(:controller) do - mock_controller.new - end - end - - class DeprecatedViewContext < ViewContext - include ActionView::Helpers::DeprecatedBlockHelpers - end - module SharedTagHelpers extend ActiveSupport::Testing::Declarative - def render_content(start, inside) - template = block_helper(start, inside) - ActionView::Template::Handlers::Erubis.new(template).evaluate(context.new) + def maybe_deprecated + if @deprecated + assert_deprecated { yield } + else + yield + end end test "percent equals works for content_tag and does not require parenthesis on method call" do - assert_equal "<div>Hello world</div>", render_content("content_tag :div", "Hello world") + maybe_deprecated { assert_equal "<div>Hello world</div>", render_content("content_tag :div", "Hello world") } end test "percent equals works for javascript_tag" do expected_output = "<script type=\"text/javascript\">\n//<![CDATA[\nalert('Hello')\n//]]>\n</script>" - assert_equal expected_output, render_content("javascript_tag", "alert('Hello')") + maybe_deprecated { assert_equal expected_output, render_content("javascript_tag", "alert('Hello')") } end test "percent equals works for javascript_tag with options" do expected_output = "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('Hello')\n//]]>\n</script>" - assert_equal expected_output, render_content("javascript_tag(:id => 'the_js_tag')", "alert('Hello')") + maybe_deprecated { assert_equal expected_output, render_content("javascript_tag(:id => 'the_js_tag')", "alert('Hello')") } end test "percent equals works with form tags" do expected_output = "<form action=\"foo\" method=\"post\">hello</form>" - assert_equal expected_output, render_content("form_tag('foo')", "<%= 'hello' %>") + maybe_deprecated { assert_equal expected_output, render_content("form_tag('foo')", "<%= 'hello' %>") } end test "percent equals works with fieldset tags" do expected_output = "<fieldset><legend>foo</legend>hello</fieldset>" - assert_equal expected_output, render_content("field_set_tag('foo')", "<%= 'hello' %>") + maybe_deprecated { assert_equal expected_output, render_content("field_set_tag('foo')", "<%= 'hello' %>") } end end - class TagHelperTest < ActiveSupport::TestCase - def context - ViewContext - end - + class TagHelperTest < BlockTestCase def block_helper(str, rest) "<%= #{str} do %>#{rest}<% end %>" end @@ -68,15 +46,15 @@ module ERBTest include SharedTagHelpers end - class DeprecatedTagHelperTest < ActiveSupport::TestCase - def context - DeprecatedViewContext - end - + class DeprecatedTagHelperTest < BlockTestCase def block_helper(str, rest) "<% __in_erb_template=true %><% #{str} do %>#{rest}<% end %>" end + def setup + @deprecated = true + end + include SharedTagHelpers end end
\ No newline at end of file diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index f49b763881..c5c2a6b952 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -17,7 +17,7 @@ class JavaScriptHelperTest < ActionView::TestCase ActiveSupport.escape_html_entities_in_json = true @template = self end - + def teardown ActiveSupport.escape_html_entities_in_json = false end @@ -60,6 +60,35 @@ class JavaScriptHelperTest < ActionView::TestCase button_to_function("Greeting") end + def test_link_to_function + assert_dom_equal %(<a href="#" onclick="alert('Hello world!'); return false;">Greeting</a>), + link_to_function("Greeting", "alert('Hello world!')") + end + + def test_link_to_function_with_existing_onclick + assert_dom_equal %(<a href="#" onclick="confirm('Sanity!'); alert('Hello world!'); return false;">Greeting</a>), + link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')") + end + + def test_link_to_function_with_rjs_block + html = link_to_function( "Greet me!" ) do |page| + page.replace_html 'header', "<h1>Greetings</h1>" + end + assert_dom_equal %(<a href="#" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E");; return false;">Greet me!</a>), html + end + + def test_link_to_function_with_rjs_block_and_options + html = link_to_function( "Greet me!", :class => "updater" ) do |page| + page.replace_html 'header', "<h1>Greetings</h1>" + end + assert_dom_equal %(<a href="#" class="updater" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E");; return false;">Greet me!</a>), html + end + + def test_link_to_function_with_href + assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>), + link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/') + end + def test_javascript_tag self.output_buffer = 'foo' diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index 697ebc694a..df1aa2edb2 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -22,34 +22,35 @@ class LookupContextTest < ActiveSupport::TestCase end test "normalizes details on initialization" do - formats = Mime::SET + [nil] - locale = [I18n.locale, nil] - assert_equal Hash[:formats => formats, :locale => locale], @lookup_context.details - end - - test "allows me to set details" do - @lookup_context.details = { :formats => [:html], :locale => :pt } - assert_equal Hash[:formats => [:html, nil], :locale => [:pt, nil]], @lookup_context.details + assert_equal Mime::SET, @lookup_context.formats + assert_equal :en, @lookup_context.locale end - test "does not allow details to be modified in place" do - assert @lookup_context.details.frozen? + test "allows me to update details" do + @lookup_context.update_details(:formats => [:html], :locale => :pt) + assert_equal [:html], @lookup_context.formats + assert_equal :pt, @lookup_context.locale end test "allows me to update an specific detail" do @lookup_context.update_details(:locale => :pt) assert_equal :pt, I18n.locale - formats = Mime::SET + [nil] - locale = [I18n.locale, nil] - assert_equal Hash[:formats => formats, :locale => locale], @lookup_context.details + assert_equal :pt, @lookup_context.locale + end + + test "allows me to freeze and retrieve frozen formats" do + @lookup_context.formats.freeze + assert @lookup_context.formats.frozen? end test "allows me to change some details to execute an specific block of code" do - formats = Mime::SET + [nil] + formats = Mime::SET @lookup_context.update_details(:locale => :pt) do - assert_equal Hash[:formats => formats, :locale => [:pt, nil]], @lookup_context.details + assert_equal formats, @lookup_context.formats + assert_equal :pt, @lookup_context.locale end - assert_equal Hash[:formats => formats, :locale => [:en, nil]], @lookup_context.details + assert_equal formats, @lookup_context.formats + assert_equal :en, @lookup_context.locale end test "provides getters and setters for formats" do @@ -62,6 +63,11 @@ class LookupContextTest < ActiveSupport::TestCase assert_equal Mime::SET, @lookup_context.formats end + test "adds :html fallback to :js formats" do + @lookup_context.formats = [:js] + assert_equal [:js, :html], @lookup_context.formats + end + test "provides getters and setters for locale" do @lookup_context.locale = :pt assert_equal :pt, @lookup_context.locale @@ -94,6 +100,13 @@ class LookupContextTest < ActiveSupport::TestCase assert_equal "Hey verden", template.source end + test "found templates respects given formats if one cannot be found from template or handler" do + ActionView::Template::Handlers::ERB.expects(:default_format).returns(nil) + @lookup_context.formats = [:text] + template = @lookup_context.find("hello_world", "test") + assert_equal [:text], template.formats + end + test "adds fallbacks to view paths when required" do assert_equal 1, @lookup_context.view_paths.size diff --git a/actionpack/test/template/number_helper_i18n_test.rb b/actionpack/test/template/number_helper_i18n_test.rb index bf5b81292f..f730a0d7f5 100644 --- a/actionpack/test/template/number_helper_i18n_test.rb +++ b/actionpack/test/template/number_helper_i18n_test.rb @@ -1,69 +1,95 @@ require 'abstract_unit' -class NumberHelperI18nTests < Test::Unit::TestCase - include ActionView::Helpers::NumberHelper - - attr_reader :request +class NumberHelperTest < ActionView::TestCase + tests ActionView::Helpers::NumberHelper def setup - @number_defaults = { :precision => 3, :delimiter => ',', :separator => '.' } - @currency_defaults = { :unit => '$', :format => '%u%n', :precision => 2 } - @human_defaults = { :precision => 1 } - @human_storage_units_format_default = "%n %u" - @human_storage_units_units_byte_other = "Bytes" - @human_storage_units_units_kb_other = "KB" - @percentage_defaults = { :delimiter => '' } - @precision_defaults = { :delimiter => '' } + I18n.backend.store_translations 'ts', + :number => { + :format => { :precision => 3, :delimiter => ',', :separator => '.', :significant => false, :strip_insignificant_zeros => false }, + :currency => { :format => { :unit => '&$', :format => '%u - %n', :precision => 2 } }, + :human => { + :format => { + :precision => 2, + :significant => true, + :strip_insignificant_zeros => true + }, + :storage_units => { + :format => "%n %u", + :units => { + :byte => "b", + :kb => "k" + } + }, + :decimal_units => { + :format => "%n %u", + :units => { + :deci => {:one => "Tenth", :other => "Tenths"}, + :unit => "u", + :ten => {:one => "Ten", :other => "Tens"}, + :thousand => "t", + :million => "m" , + :billion =>"b" , + :trillion =>"t" , + :quadrillion =>"q" + } + } + }, + :percentage => { :format => {:delimiter => '', :precision => 2, :strip_insignificant_zeros => true} }, + :precision => { :format => {:delimiter => '', :significant => true} } + }, + :custom_units_for_number_to_human => {:mili => "mm", :centi => "cm", :deci => "dm", :unit => "m", :ten => "dam", :hundred => "hm", :thousand => "km"} + end - I18n.backend.store_translations 'en', :number => { :format => @number_defaults, - :currency => { :format => @currency_defaults }, :human => @human_defaults } + def test_number_to_currency + assert_equal("&$ - 10.00", number_to_currency(10, :locale => 'ts')) end - def test_number_to_currency_translates_currency_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.currency.format', :locale => 'en', - :raise => true).returns(@currency_defaults) - number_to_currency(1, :locale => 'en') + def test_number_with_precision + #Delimiter was set to "" + assert_equal("10000", number_with_precision(10000, :locale => 'ts')) + + #Precision inherited and significant was set + assert_equal("1.00", number_with_precision(1.0, :locale => 'ts')) + end - def test_number_with_precision_translates_number_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.precision.format', :locale => 'en', - :raise => true).returns(@precision_defaults) - number_with_precision(1, :locale => 'en') + def test_number_with_delimiter + #Delimiter "," and separator "." + assert_equal("1,000,000.234", number_with_delimiter(1000000.234, :locale => 'ts')) end - def test_number_with_delimiter_translates_number_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - number_with_delimiter(1, :locale => 'en') + def test_number_to_percentage + # to see if strip_insignificant_zeros is true + assert_equal("1%", number_to_percentage(1, :locale => 'ts')) + # precision is 2, significant should be inherited + assert_equal("1.24%", number_to_percentage(1.2434, :locale => 'ts')) + # no delimiter + assert_equal("12434%", number_to_percentage(12434, :locale => 'ts')) end - def test_number_to_percentage_translates_number_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.percentage.format', :locale => 'en', - :raise => true).returns(@percentage_defaults) - number_to_percentage(1, :locale => 'en') + def test_number_to_human_size + #b for bytes and k for kbytes + assert_equal("2 k", number_to_human_size(2048, :locale => 'ts')) + assert_equal("42 b", number_to_human_size(42, :locale => 'ts')) end - def test_number_to_human_size_translates_human_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.human.format', :locale => 'en', - :raise => true).returns(@human_defaults) - I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en', - :raise => true).returns(@human_storage_units_format_default) - I18n.expects(:translate).with(:'number.human.storage_units.units.kb', :locale => 'en', :count => 2, - :raise => true).returns(@human_storage_units_units_kb_other) - # 2KB - number_to_human_size(2048, :locale => 'en') + def test_number_to_human_with_default_translation_scope + #Using t for thousand + assert_equal "2 t", number_to_human(2000, :locale => 'ts') + #Significant was set to true with precision 2, using b for billion + assert_equal "1.2 b", number_to_human(1234567890, :locale => 'ts') + #Using pluralization (Ten/Tens and Tenth/Tenths) + assert_equal "1 Tenth", number_to_human(0.1, :locale => 'ts') + assert_equal "1.3 Tenth", number_to_human(0.134, :locale => 'ts') + assert_equal "2 Tenths", number_to_human(0.2, :locale => 'ts') + assert_equal "1 Ten", number_to_human(10, :locale => 'ts') + assert_equal "1.2 Ten", number_to_human(12, :locale => 'ts') + assert_equal "2 Tens", number_to_human(20, :locale => 'ts') + end - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.human.format', :locale => 'en', - :raise => true).returns(@human_defaults) - I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en', - :raise => true).returns(@human_storage_units_format_default) - I18n.expects(:translate).with(:'number.human.storage_units.units.byte', :locale => 'en', :count => 42, - :raise => true).returns(@human_storage_units_units_byte_other) - # 42 Bytes - number_to_human_size(42, :locale => 'en') + def test_number_to_human_with_custom_translation_scope + #Significant was set to true with precision 2, with custom translated units + assert_equal "4.3 cm", number_to_human(0.0432, :locale => 'ts', :units => :custom_units_for_number_to_human) end end diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index 0a2b82bd89..50c57a5588 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -19,6 +19,15 @@ class NumberHelperTest < ActionView::TestCase gigabytes(number) * 1024 end + def silence_deprecation_warnings + @old_deprecatios_silenced = ActiveSupport::Deprecation.silenced + ActiveSupport::Deprecation.silenced = true + end + + def restore_deprecation_warnings + ActiveSupport::Deprecation.silenced = @old_deprecatios_silenced + end + def test_number_to_phone assert_equal("555-1234", number_to_phone(5551234)) assert_equal("800-555-1212", number_to_phone(8005551212)) @@ -31,8 +40,6 @@ class NumberHelperTest < ActionView::TestCase assert_equal("+18005551212", number_to_phone(8005551212, :country_code => 1, :delimiter => '')) assert_equal("22-555-1212", number_to_phone(225551212)) assert_equal("+45-22-555-1212", number_to_phone(225551212, :country_code => 45)) - assert_equal("x", number_to_phone("x")) - assert_nil number_to_phone(nil) end def test_number_to_currency @@ -43,9 +50,6 @@ class NumberHelperTest < ActionView::TestCase assert_equal("£1234567890,50", number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""})) assert_equal("$1,234,567,890.50", number_to_currency("1234567890.50")) assert_equal("1,234,567,890.50 Kč", number_to_currency("1234567890.50", {:unit => "Kč", :format => "%n %u"})) - #assert_equal("$x.", number_to_currency("x")) # fails due to API consolidation - assert_equal("$x", number_to_currency("x")) - assert_nil number_to_currency(nil) end def test_number_to_percentage @@ -54,9 +58,8 @@ class NumberHelperTest < ActionView::TestCase assert_equal("302.06%", number_to_percentage(302.0574, {:precision => 2})) assert_equal("100.000%", number_to_percentage("100")) assert_equal("1000.000%", number_to_percentage("1000")) - assert_equal("x%", number_to_percentage("x")) + assert_equal("123.4%", number_to_percentage(123.400, :precision => 3, :strip_insignificant_zeros => true)) assert_equal("1.000,000%", number_to_percentage(1000, :delimiter => '.', :separator => ',')) - assert_nil number_to_percentage(nil) end def test_number_with_delimiter @@ -70,8 +73,6 @@ class NumberHelperTest < ActionView::TestCase assert_equal("123,456,789.78901", number_with_delimiter(123456789.78901)) assert_equal("0.78901", number_with_delimiter(0.78901)) assert_equal("123,456.78", number_with_delimiter("123456.78")) - assert_equal("x", number_with_delimiter("x")) - assert_nil number_with_delimiter(nil) end def test_number_with_delimiter_with_options_hash @@ -81,6 +82,16 @@ class NumberHelperTest < ActionView::TestCase assert_equal '12.345.678,05', number_with_delimiter(12345678.05, :delimiter => '.', :separator => ',') end + def test_number_with_delimiter_old_api + silence_deprecation_warnings + assert_equal '12 345 678', number_with_delimiter(12345678, " ") + assert_equal '12-345-678.05', number_with_delimiter(12345678.05, '-') + assert_equal '12.345.678,05', number_with_delimiter(12345678.05, '.', ',') + assert_equal '12,345,678.05', number_with_delimiter(12345678.05, ',', '.') + assert_equal '12 345 678-05', number_with_delimiter(12345678.05, ',', '.', :delimiter => ' ', :separator => '-') + restore_deprecation_warnings + end + def test_number_with_precision assert_equal("111.235", number_with_precision(111.2346)) assert_equal("31.83", number_with_precision(31.825, :precision => 2)) @@ -91,10 +102,6 @@ class NumberHelperTest < ActionView::TestCase assert_equal("3268", number_with_precision((32.6751 * 100.00), :precision => 0)) assert_equal("112", number_with_precision(111.50, :precision => 0)) assert_equal("1234567892", number_with_precision(1234567891.50, :precision => 0)) - - # Return non-numeric params unchanged. - assert_equal("x", number_with_precision("x")) - assert_nil number_with_precision(nil) end def test_number_with_precision_with_custom_delimiter_and_separator @@ -102,48 +109,272 @@ class NumberHelperTest < ActionView::TestCase assert_equal '1.231,83', number_with_precision(1231.825, :precision => 2, :separator => ',', :delimiter => '.') end + def test_number_with_precision_with_significant_digits + assert_equal "124000", number_with_precision(123987, :precision => 3, :significant => true) + assert_equal "120000000", number_with_precision(123987876, :precision => 2, :significant => true ) + assert_equal "40000", number_with_precision("43523", :precision => 1, :significant => true ) + assert_equal "9775", number_with_precision(9775, :precision => 4, :significant => true ) + assert_equal "5.4", number_with_precision(5.3923, :precision => 2, :significant => true ) + assert_equal "5", number_with_precision(5.3923, :precision => 1, :significant => true ) + assert_equal "1", number_with_precision(1.232, :precision => 1, :significant => true ) + assert_equal "7", number_with_precision(7, :precision => 1, :significant => true ) + assert_equal "1", number_with_precision(1, :precision => 1, :significant => true ) + assert_equal "53", number_with_precision(52.7923, :precision => 2, :significant => true ) + assert_equal "9775.00", number_with_precision(9775, :precision => 6, :significant => true ) + assert_equal "5.392900", number_with_precision(5.3929, :precision => 7, :significant => true ) + end + + def test_number_with_precision_with_strip_insignificant_zeros + assert_equal "9775.43", number_with_precision(9775.43, :precision => 4, :strip_insignificant_zeros => true ) + assert_equal "9775.2", number_with_precision(9775.2, :precision => 6, :significant => true, :strip_insignificant_zeros => true ) + end + + def test_number_with_precision_with_significant_true_and_zero_precision + # Zero precision with significant is a mistake (would always return zero), + # so we treat it as if significant was false (increases backwards compatibily for number_to_human_size) + assert_equal "124", number_with_precision(123.987, :precision => 0, :significant => true) + assert_equal "12", number_with_precision(12, :precision => 0, :significant => true ) + assert_equal "12", number_with_precision("12.3", :precision => 0, :significant => true ) + end + + def test_number_with_precision_old_api + silence_deprecation_warnings + assert_equal("31.8250", number_with_precision(31.825, 4)) + assert_equal("111.235", number_with_precision(111.2346, 3)) + assert_equal("111.00", number_with_precision(111, 2)) + assert_equal("111.000", number_with_precision(111, 2, :precision =>3)) + restore_deprecation_warnings + end + def test_number_to_human_size assert_equal '0 Bytes', number_to_human_size(0) assert_equal '1 Byte', number_to_human_size(1) assert_equal '3 Bytes', number_to_human_size(3.14159265) assert_equal '123 Bytes', number_to_human_size(123.0) assert_equal '123 Bytes', number_to_human_size(123) - assert_equal '1.2 KB', number_to_human_size(1234) + assert_equal '1.21 KB', number_to_human_size(1234) assert_equal '12.1 KB', number_to_human_size(12345) - assert_equal '1.2 MB', number_to_human_size(1234567) - assert_equal '1.1 GB', number_to_human_size(1234567890) - assert_equal '1.1 TB', number_to_human_size(1234567890123) - assert_equal '1025 TB', number_to_human_size(terabytes(1025)) + assert_equal '1.18 MB', number_to_human_size(1234567) + assert_equal '1.15 GB', number_to_human_size(1234567890) + assert_equal '1.12 TB', number_to_human_size(1234567890123) + assert_equal '1030 TB', number_to_human_size(terabytes(1026)) assert_equal '444 KB', number_to_human_size(kilobytes(444)) - assert_equal '1023 MB', number_to_human_size(megabytes(1023)) + assert_equal '1020 MB', number_to_human_size(megabytes(1023)) assert_equal '3 TB', number_to_human_size(terabytes(3)) - assert_equal '1.18 MB', number_to_human_size(1234567, :precision => 2) + assert_equal '1.2 MB', number_to_human_size(1234567, :precision => 2) assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4) - assert_equal("123 Bytes", number_to_human_size("123")) - assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), :precision => 2) + assert_equal '123 Bytes', number_to_human_size('123') + assert_equal '1 KB', number_to_human_size(kilobytes(1.0123), :precision => 2) assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), :precision => 4) assert_equal '10 KB', number_to_human_size(kilobytes(10.000), :precision => 4) assert_equal '1 Byte', number_to_human_size(1.1) assert_equal '10 Bytes', number_to_human_size(10) - #assert_nil number_to_human_size('x') # fails due to API consolidation - assert_nil number_to_human_size(nil) end def test_number_to_human_size_with_options_hash - assert_equal '1.18 MB', number_to_human_size(1234567, :precision => 2) + assert_equal '1.2 MB', number_to_human_size(1234567, :precision => 2) assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4) - assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), :precision => 2) + assert_equal '1 KB', number_to_human_size(kilobytes(1.0123), :precision => 2) assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), :precision => 4) assert_equal '10 KB', number_to_human_size(kilobytes(10.000), :precision => 4) - assert_equal '1 TB', number_to_human_size(1234567890123, :precision => 0) - assert_equal '500 MB', number_to_human_size(524288000, :precision=>0) - assert_equal '40 KB', number_to_human_size(41010, :precision => 0) - assert_equal '40 KB', number_to_human_size(41100, :precision => 0) + assert_equal '1 TB', number_to_human_size(1234567890123, :precision => 1) + assert_equal '500 MB', number_to_human_size(524288000, :precision=>3) + assert_equal '40 KB', number_to_human_size(41010, :precision => 1) + assert_equal '40 KB', number_to_human_size(41100, :precision => 2) + assert_equal '1.0 KB', number_to_human_size(kilobytes(1.0123), :precision => 2, :strip_insignificant_zeros => false) + assert_equal '1.012 KB', number_to_human_size(kilobytes(1.0123), :precision => 3, :significant => false) + assert_equal '1 KB', number_to_human_size(kilobytes(1.0123), :precision => 0, :significant => true) #ignores significant it precision is 0 end def test_number_to_human_size_with_custom_delimiter_and_separator - assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0123), :precision => 2, :separator => ',') + assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0123), :precision => 3, :separator => ',') assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0100), :precision => 4, :separator => ',') - assert_equal '1.000,1 TB', number_to_human_size(terabytes(1000.1), :delimiter => '.', :separator => ',') + assert_equal '1.000,1 TB', number_to_human_size(terabytes(1000.1), :precision => 5, :delimiter => '.', :separator => ',') + end + + def test_number_to_human_size_old_api + silence_deprecation_warnings + assert_equal '1.3143 KB', number_to_human_size(kilobytes(1.3143), 4, :significant => false) + assert_equal '10.45 KB', number_to_human_size(kilobytes(10.453), 4) + assert_equal '10 KB', number_to_human_size(kilobytes(10.453), 4, :precision => 2) + restore_deprecation_warnings + end + + def test_number_to_human + assert_equal '123', number_to_human(123) + assert_equal '1.23 Thousand', number_to_human(1234) + assert_equal '12.3 Thousand', number_to_human(12345) + assert_equal '1.23 Million', number_to_human(1234567) + assert_equal '1.23 Billion', number_to_human(1234567890) + assert_equal '1.23 Trillion', number_to_human(1234567890123) + assert_equal '1.23 Quadrillion', number_to_human(1234567890123456) + assert_equal '1230 Quadrillion', number_to_human(1234567890123456789) + assert_equal '490 Thousand', number_to_human(489939, :precision => 2) + assert_equal '489.9 Thousand', number_to_human(489939, :precision => 4) + assert_equal '489 Thousand', number_to_human(489000, :precision => 4) + assert_equal '489.0 Thousand', number_to_human(489000, :precision => 4, :strip_insignificant_zeros => false) + assert_equal '1.2346 Million', number_to_human(1234567, :precision => 4, :significant => false) + assert_equal '1,2 Million', number_to_human(1234567, :precision => 1, :significant => false, :separator => ',') + assert_equal '1 Million', number_to_human(1234567, :precision => 0, :significant => true, :separator => ',') #significant forced to false + end + + def test_number_to_human_with_custom_units + #Only integers + volume = {:unit => "ml", :thousand => "lt", :million => "m3"} + assert_equal '123 lt', number_to_human(123456, :units => volume) + assert_equal '12 ml', number_to_human(12, :units => volume) + assert_equal '1.23 m3', number_to_human(1234567, :units => volume) + + #Including fractionals + distance = {:mili => "mm", :centi => "cm", :deci => "dm", :unit => "m", :ten => "dam", :hundred => "hm", :thousand => "km"} + assert_equal '1.23 mm', number_to_human(0.00123, :units => distance) + assert_equal '1.23 cm', number_to_human(0.0123, :units => distance) + assert_equal '1.23 dm', number_to_human(0.123, :units => distance) + assert_equal '1.23 m', number_to_human(1.23, :units => distance) + assert_equal '1.23 dam', number_to_human(12.3, :units => distance) + assert_equal '1.23 hm', number_to_human(123, :units => distance) + assert_equal '1.23 km', number_to_human(1230, :units => distance) + assert_equal '1.23 km', number_to_human(1230, :units => distance) + assert_equal '1.23 km', number_to_human(1230, :units => distance) + assert_equal '12.3 km', number_to_human(12300, :units => distance) + + #The quantifiers don't need to be a continuous sequence + gangster = {:hundred => "hundred bucks", :million => "thousand quids"} + assert_equal '1 hundred bucks', number_to_human(100, :units => gangster) + assert_equal '25 hundred bucks', number_to_human(2500, :units => gangster) + assert_equal '25 thousand quids', number_to_human(25000000, :units => gangster) + assert_equal '12300 thousand quids', number_to_human(12345000000, :units => gangster) + + #Spaces are stripped from the resulting string + assert_equal '4', number_to_human(4, :units => {:unit => "", :ten => 'tens '}) + assert_equal '4.5 tens', number_to_human(45, :units => {:unit => "", :ten => ' tens '}) end + + def test_number_to_human_with_custom_format + assert_equal '123 times Thousand', number_to_human(123456, :format => "%n times %u") + volume = {:unit => "ml", :thousand => "lt", :million => "m3"} + assert_equal '123.lt', number_to_human(123456, :units => volume, :format => "%n.%u") + end + + def test_number_helpers_should_return_nil_when_given_nil + assert_nil number_to_phone(nil) + assert_nil number_to_currency(nil) + assert_nil number_to_percentage(nil) + assert_nil number_with_delimiter(nil) + assert_nil number_with_precision(nil) + assert_nil number_to_human_size(nil) + assert_nil number_to_human(nil) + end + + def test_number_helpers_should_return_non_numeric_param_unchanged + assert_equal("+1-x x 123", number_to_phone("x", :country_code => 1, :extension => 123)) + assert_equal("x", number_to_phone("x")) + assert_equal("$x.", number_to_currency("x.")) + assert_equal("$x", number_to_currency("x")) + assert_equal("x%", number_to_percentage("x")) + assert_equal("x", number_with_delimiter("x")) + assert_equal("x.", number_with_precision("x.")) + assert_equal("x", number_with_precision("x")) + assert_equal "x", number_to_human_size('x') + assert_equal "x", number_to_human('x') + end + + def test_number_helpers_outputs_are_html_safe + assert number_to_human(1).html_safe? + assert !number_to_human("<script></script>").html_safe? + assert number_to_human("asdf".html_safe).html_safe? + + assert number_to_human_size(1).html_safe? + assert number_to_human_size(1000000).html_safe? + assert !number_to_human_size("<script></script>").html_safe? + assert number_to_human_size("asdf".html_safe).html_safe? + + assert number_with_precision(1, :strip_insignificant_zeros => false).html_safe? + assert number_with_precision(1, :strip_insignificant_zeros => true).html_safe? + assert !number_with_precision("<script></script>").html_safe? + assert number_with_precision("asdf".html_safe).html_safe? + + assert number_to_currency(1).html_safe? + assert !number_to_currency("<script></script>").html_safe? + assert number_to_currency("asdf".html_safe).html_safe? + + assert number_to_percentage(1).html_safe? + assert !number_to_percentage("<script></script>").html_safe? + assert number_to_percentage("asdf".html_safe).html_safe? + + assert number_to_phone(1).html_safe? + assert !number_to_phone("<script></script>").html_safe? + assert number_to_phone("asdf".html_safe).html_safe? + + assert number_with_delimiter(1).html_safe? + assert !number_with_delimiter("<script></script>").html_safe? + assert number_with_delimiter("asdf".html_safe).html_safe? + end + + def test_number_helpers_should_raise_error_if_invalid_when_specified + assert_raise InvalidNumberError do + number_to_human("x", :raise => true) + end + begin + number_to_human("x", :raise => true) + rescue InvalidNumberError => e + assert_equal "x", e.number + end + + assert_raise InvalidNumberError do + number_to_human_size("x", :raise => true) + end + begin + number_to_human_size("x", :raise => true) + rescue InvalidNumberError => e + assert_equal "x", e.number + end + + assert_raise InvalidNumberError do + number_with_precision("x", :raise => true) + end + begin + number_with_precision("x", :raise => true) + rescue InvalidNumberError => e + assert_equal "x", e.number + end + + assert_raise InvalidNumberError do + number_to_currency("x", :raise => true) + end + begin + number_with_precision("x", :raise => true) + rescue InvalidNumberError => e + assert_equal "x", e.number + end + + assert_raise InvalidNumberError do + number_to_percentage("x", :raise => true) + end + begin + number_to_percentage("x", :raise => true) + rescue InvalidNumberError => e + assert_equal "x", e.number + end + + assert_raise InvalidNumberError do + number_with_delimiter("x", :raise => true) + end + begin + number_with_delimiter("x", :raise => true) + rescue InvalidNumberError => e + assert_equal "x", e.number + end + + assert_raise InvalidNumberError do + number_to_phone("x", :raise => true) + end + begin + number_to_phone("x", :raise => true) + rescue InvalidNumberError => e + assert_equal "x", e.number + end + + end + end diff --git a/actionpack/test/template/output_buffer_test.rb b/actionpack/test/template/output_buffer_test.rb index 36bbaf9099..bd49a11af1 100644 --- a/actionpack/test/template/output_buffer_test.rb +++ b/actionpack/test/template/output_buffer_test.rb @@ -10,6 +10,7 @@ class OutputBufferTest < ActionController::TestCase tests TestController def setup + @vc = @controller.view_context get :index assert_equal ['foo'], body_parts end @@ -19,21 +20,21 @@ class OutputBufferTest < ActionController::TestCase end test 'flushing ignores nil output buffer' do - @controller.template.flush_output_buffer + @controller.view_context.flush_output_buffer assert_nil output_buffer assert_equal ['foo'], body_parts end test 'flushing ignores empty output buffer' do - @controller.template.output_buffer = '' - @controller.template.flush_output_buffer + @vc.output_buffer = '' + @vc.flush_output_buffer assert_equal '', output_buffer assert_equal ['foo'], body_parts end test 'flushing appends the output buffer to the body parts' do - @controller.template.output_buffer = 'bar' - @controller.template.flush_output_buffer + @vc.output_buffer = 'bar' + @vc.flush_output_buffer assert_equal '', output_buffer assert_equal ['foo', 'bar'], body_parts end @@ -41,8 +42,8 @@ class OutputBufferTest < ActionController::TestCase if '1.9'.respond_to?(:force_encoding) test 'flushing preserves output buffer encoding' do original_buffer = ' '.force_encoding(Encoding::EUC_JP) - @controller.template.output_buffer = original_buffer - @controller.template.flush_output_buffer + @vc.output_buffer = original_buffer + @vc.flush_output_buffer assert_equal ['foo', original_buffer], body_parts assert_not_equal original_buffer, output_buffer assert_equal Encoding::EUC_JP, output_buffer.encoding @@ -51,10 +52,10 @@ class OutputBufferTest < ActionController::TestCase protected def output_buffer - @controller.template.output_buffer + @vc.output_buffer end def body_parts - @controller.template.response.body_parts + @controller.response.body_parts end end diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index cea8ab1bce..e54ebfbf8d 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -9,7 +9,7 @@ module RenderTestCases def setup_view(paths) @assigns = { :secret => 'in the sauce' } @view = ActionView::Base.new(paths, @assigns) - @controller_view = ActionView::Base.for_controller(TestController.new) + @controller_view = TestController.new.view_context # Reload and register danish language for testing I18n.reload! @@ -228,6 +228,14 @@ module RenderTestCases @view.render(:file => "test/hello_world.erb", :layout => "layouts/yield") end + # TODO: Move to deprecated_tests.rb + def test_render_with_nested_layout_deprecated + assert_deprecated do + assert_equal %(<title>title</title>\n\n\n<div id="column">column</div>\n<div id="content">content</div>\n), + @view.render(:file => "test/deprecated_nested_layout.erb", :layout => "layouts/yield") + end + end + def test_render_with_nested_layout assert_equal %(<title>title</title>\n\n\n<div id="column">column</div>\n<div id="content">content</div>\n), @view.render(:file => "test/nested_layout.erb", :layout => "layouts/yield") diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index 699fb2f5bc..6782bf06d4 100644 --- a/actionpack/test/template/translation_helper_test.rb +++ b/actionpack/test/template/translation_helper_test.rb @@ -20,7 +20,14 @@ class TranslationHelperTest < ActiveSupport::TestCase def test_translation_of_an_array I18n.expects(:translate).with(["foo", "bar"], :raise => true).returns(["foo", "bar"]) - assert_equal ["foo", "bar"], translate(["foo", "bar"]) + assert_equal "foobar", translate(["foo", "bar"]) + end + + def test_translation_of_an_array_with_html + expected = '<a href="#">foo</a><a href="#">bar</a>' + I18n.expects(:translate).with(["foo", "bar"], :raise => true).returns(['<a href="#">foo</a>', '<a href="#">bar</a>']) + @view = ActionView::Base.new(ActionController::Base.view_paths, {}) + assert_equal expected, @view.render(:file => "test/array_translation") end def test_delegates_localize_to_i18n @@ -34,4 +41,10 @@ class TranslationHelperTest < ActiveSupport::TestCase @view = ActionView::Base.new(ActionController::Base.view_paths, {}) assert_equal "helper", @view.render(:file => "test/translation") end + + def test_scoping_by_partial_of_an_array + I18n.expects(:translate).with("test.scoped_array_translation.foo.bar", :raise => true).returns(["foo", "bar"]) + @view = ActionView::Base.new(ActionController::Base.view_paths, {}) + assert_equal "foobar", @view.render(:file => "test/scoped_array_translation") + end end diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 165cb655da..87b2e59255 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -238,10 +238,7 @@ class UrlHelperTest < ActionView::TestCase end def test_link_tag_using_block_in_erb - __in_erb_template = '' - - link_to("http://example.com") { concat("Example site") } - + output_buffer = link_to("http://example.com") { concat("Example site") } assert_equal '<a href="http://example.com">Example site</a>', output_buffer end |