From 6c027443b044276fd2a85b387b67a8dee940b320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 11 Mar 2010 12:45:05 +0100 Subject: Add tests for lookup context. --- .../test/controller/new_base/render_rjs_test.rb | 2 +- actionpack/test/lib/fixture_template.rb | 2 + actionpack/test/template/lookup_context_test.rb | 167 +++++++++++++++++++++ 3 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 actionpack/test/template/lookup_context_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/new_base/render_rjs_test.rb b/actionpack/test/controller/new_base/render_rjs_test.rb index 8c47b38ab6..f4516ade63 100644 --- a/actionpack/test/controller/new_base/render_rjs_test.rb +++ b/actionpack/test/controller/new_base/render_rjs_test.rb @@ -17,7 +17,7 @@ module RenderRjs end def index_locale - old_locale, I18n.locale = I18n.locale, :da + self.locale = :da end end diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb index d287fae470..eda76ddfd0 100644 --- a/actionpack/test/lib/fixture_template.rb +++ b/actionpack/test/lib/fixture_template.rb @@ -1,5 +1,7 @@ module ActionView #:nodoc: class FixtureResolver < PathResolver + attr_reader :hash + def initialize(hash = {}) super() @hash = hash diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb new file mode 100644 index 0000000000..bf07735724 --- /dev/null +++ b/actionpack/test/template/lookup_context_test.rb @@ -0,0 +1,167 @@ +require "abstract_unit" +require "abstract_controller/rendering" + +ActionView::LookupContext::DetailsKey.class_eval do + def self.details_keys + @details_keys + end +end + +class LookupContextTest < ActiveSupport::TestCase + def setup + @lookup_context = ActionView::LookupContext.new(FIXTURE_LOAD_PATH, {}) + end + + def teardown + I18n.locale = :en + ActionView::LookupContext::DetailsKey.details_keys.clear + end + + test "process view paths on initialization" do + assert_kind_of ActionView::PathSet, @lookup_context.view_paths + 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 + end + + test "does not allow details to be modified in place" do + assert_raise TypeError do + @lookup_context.details.clear + end + 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 + end + + test "allows me to change some details to execute an specific block of code" do + formats = Mime::SET + [nil] + @lookup_context.update_details(:locale => :pt) do + assert_equal Hash[:formats => formats, :locale => [:pt, nil]], @lookup_context.details + end + assert_equal Hash[:formats => formats, :locale => [:en, nil]], @lookup_context.details + end + + test "provides getters and setters for formats" do + @lookup_context.formats = :html + assert_equal [:html], @lookup_context.formats + end + + test "handles */* formats" do + @lookup_context.formats = [:"*/*"] + assert_equal Mime::SET, @lookup_context.formats + end + + test "provides getters and setters for locale" do + @lookup_context.locale = :pt + assert_equal :pt, @lookup_context.locale + end + + test "changing lookup_context locale, changes I18n.locale" do + @lookup_context.locale = :pt + assert_equal :pt, I18n.locale + end + + test "delegates changing the locale to the I18n configuration object if it contains a lookup_context object" do + begin + I18n.config = AbstractController::I18nProxy.new(I18n.config, @lookup_context) + @lookup_context.locale = :pt + assert_equal :pt, I18n.locale + assert_equal :pt, @lookup_context.locale + ensure + I18n.config = I18n.config.i18n_config + end + + assert_equal :pt, I18n.locale + end + + test "find templates using the given view paths and configured details" do + template = @lookup_context.find("hello_world", "test") + assert_equal "Hello world!", template.source + + @lookup_context.locale = :da + template = @lookup_context.find("hello_world", "test") + assert_equal "Hey verden", template.source + end + + test "adds fallbacks to view paths when required" do + assert_equal 1, @lookup_context.view_paths.size + + @lookup_context.with_fallbacks do + assert_equal 3, @lookup_context.view_paths.size + assert @lookup_context.view_paths.include?(ActionView::FileSystemResolver.new("")) + assert @lookup_context.view_paths.include?(ActionView::FileSystemResolver.new("/")) + end + end + + test "add fallbacks just once in nested fallbacks calls" do + @lookup_context.with_fallbacks do + @lookup_context.with_fallbacks do + assert_equal 3, @lookup_context.view_paths.size + end + end + end + + test "generates a new details key for each details hash" do + keys = [] + keys << @lookup_context.details_key + assert_equal 1, keys.uniq.size + + @lookup_context.locale = :da + keys << @lookup_context.details_key + assert_equal 2, keys.uniq.size + + @lookup_context.locale = :en + keys << @lookup_context.details_key + assert_equal 2, keys.uniq.size + + @lookup_context.formats = :html + keys << @lookup_context.details_key + assert_equal 3, keys.uniq.size + + @lookup_context.formats = nil + keys << @lookup_context.details_key + assert_equal 3, keys.uniq.size + end + + test "gives the key forward to the resolver, so it can be used as cache key" do + @lookup_context.view_paths = ActionView::FixtureResolver.new("test/_foo.erb" => "Foo") + template = @lookup_context.find("foo", "test", true) + assert_equal "Foo", template.source + + # Now we are going to change the template, but it won't change the returned template + # since we will hit the cache. + @lookup_context.view_paths.first.hash["test/_foo.erb"] = "Bar" + template = @lookup_context.find("foo", "test", true) + assert_equal "Foo", template.source + + # This time we will change the locale. The updated template should be picked since + # lookup_context generated a new key after we changed the locale. + @lookup_context.locale = :da + template = @lookup_context.find("foo", "test", true) + assert_equal "Bar", template.source + + # Now we will change back the locale and it will still pick the old template. + # This is expected because lookup_context will reuse the previous key for :en locale. + @lookup_context.locale = :en + template = @lookup_context.find("foo", "test", true) + assert_equal "Foo", template.source + + # Finally, we can expire the cache. And the expected template will be used. + @lookup_context.view_paths.first.clear_cache + template = @lookup_context.find("foo", "test", true) + assert_equal "Bar", template.source + end +end \ No newline at end of file -- cgit v1.2.3 From b27376773e8f51b03bd4cb2678764cd392455870 Mon Sep 17 00:00:00 2001 From: Eaden McKee Date: Fri, 12 Mar 2010 14:13:10 +1300 Subject: simplify alt tag generation for images [#2837 state:committed] Signed-off-by: Jeremy Kemper --- actionpack/test/template/asset_tag_helper_test.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 50c3ab0b60..fe255246e0 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -141,13 +141,16 @@ class AssetTagHelperTest < ActionView::TestCase ImageLinkToTag = { %(image_tag("xml.png")) => %(Xml), - %(image_tag("..jpg")) => %(), + %(image_tag("..jpg")) => %(.), %(image_tag("rss.gif", :alt => "rss syndication")) => %(rss syndication), %(image_tag("gold.png", :size => "45x70")) => %(Gold), %(image_tag("gold.png", "size" => "45x70")) => %(Gold), %(image_tag("error.png", "size" => "45")) => %(Error), %(image_tag("error.png", "size" => "45 x 70")) => %(Error), %(image_tag("error.png", "size" => "x")) => %(Error), + %(image_tag("google.com.png")) => %(Google.com), + %(image_tag("slash..png")) => %(Slash.), + %(image_tag(".pdf.png")) => %(.pdf), %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(Rails), %(image_tag("mouse.png", :mouseover => "/images/mouse_over.png")) => %(Mouse), %(image_tag("mouse.png", :mouseover => image_path("mouse_over.png"))) => %(Mouse) -- cgit v1.2.3 From 5a7f7928a60749095dd890abfceff45a9912b8d2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 11 Mar 2010 17:49:17 -0800 Subject: Fix test --- actionpack/test/template/asset_tag_helper_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index fe255246e0..d9a89959da 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -141,7 +141,7 @@ class AssetTagHelperTest < ActionView::TestCase ImageLinkToTag = { %(image_tag("xml.png")) => %(Xml), - %(image_tag("..jpg")) => %(.), + %(image_tag("..jpg")) => %(..jpg), %(image_tag("rss.gif", :alt => "rss syndication")) => %(rss syndication), %(image_tag("gold.png", :size => "45x70")) => %(Gold), %(image_tag("gold.png", "size" => "45x70")) => %(Gold), -- cgit v1.2.3 From 839362fa07de3f7bdf1fc1a361ff456cd02efc4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 12 Mar 2010 11:50:45 +0100 Subject: Make all AP tests pass for Ruby 1.9.1. --- actionpack/test/dispatch/mount_test.rb | 8 ++++---- actionpack/test/lib/fixture_template.rb | 5 +++-- actionpack/test/template/lookup_context_test.rb | 4 +--- 3 files changed, 8 insertions(+), 9 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/mount_test.rb b/actionpack/test/dispatch/mount_test.rb index f89e5fda07..00ca5ec9dc 100644 --- a/actionpack/test/dispatch/mount_test.rb +++ b/actionpack/test/dispatch/mount_test.rb @@ -1,12 +1,12 @@ require 'abstract_unit' class TestRoutingMount < ActionDispatch::IntegrationTest - SprocketsApp = lambda { |env| - [200, {"Content-Type" => "text/html"}, ["#{env["SCRIPT_NAME"]} -- #{env["PATH_INFO"]}"]] - } - Router = ActionDispatch::Routing::RouteSet.new Router.draw do + SprocketsApp = lambda { |env| + [200, {"Content-Type" => "text/html"}, ["#{env["SCRIPT_NAME"]} -- #{env["PATH_INFO"]}"]] + } + mount SprocketsApp, :at => "/sprockets" mount SprocketsApp => "/shorthand" diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb index eda76ddfd0..02248d84ad 100644 --- a/actionpack/test/lib/fixture_template.rb +++ b/actionpack/test/lib/fixture_template.rb @@ -12,7 +12,7 @@ module ActionView #:nodoc: def query(partial, path, exts) query = Regexp.escape(path) exts.each do |ext| - query << '(?:' << ext.map {|e| e && Regexp.escape(".#{e}") }.join('|') << ')' + query << '(' << ext.map {|e| e && Regexp.escape(".#{e}") }.join('|') << ')' end templates = [] @@ -21,7 +21,8 @@ module ActionView #:nodoc: templates << Template.new(source, path, handler, :partial => partial, :virtual_path => path, :format => format) end - templates.sort_by {|t| -t.formats.size } + + templates.sort_by {|t| -t.identifier.match(/^#{query}$/).captures.reject(&:blank?).size } end end diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index bf07735724..697ebc694a 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -33,9 +33,7 @@ class LookupContextTest < ActiveSupport::TestCase end test "does not allow details to be modified in place" do - assert_raise TypeError do - @lookup_context.details.clear - end + assert @lookup_context.details.frozen? end test "allows me to update an specific detail" do -- cgit v1.2.3 From 4840acd485a4a6bcdd73338447af7e1340587e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 12 Mar 2010 12:08:23 +0100 Subject: %= works for content_tag and does not require parenthesis on method call --- actionpack/test/template/erb/tag_helper_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/erb/tag_helper_test.rb b/actionpack/test/template/erb/tag_helper_test.rb index b91539ef0b..cc96a43901 100644 --- a/actionpack/test/template/erb/tag_helper_test.rb +++ b/actionpack/test/template/erb/tag_helper_test.rb @@ -31,8 +31,8 @@ module ERBTest ActionView::Template::Handlers::Erubis.new(template).evaluate(context.new) end - test "percent equals works for content_tag" do - assert_equal "
Hello world
", render_content("content_tag(:div)", "Hello world") + test "percent equals works for content_tag and does not require parenthesis on method call" do + assert_equal "
Hello world
", render_content("content_tag :div", "Hello world") end test "percent equals works for javascript_tag" do -- cgit v1.2.3 From 2a12686832fbcf0566454904a5d733998506bf56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 12 Mar 2010 14:25:10 +0100 Subject: Allow anything that responds to render to be given as :template and use find_template instead of find in views. --- actionpack/test/abstract/layouts_test.rb | 156 +++++++++++-------------------- 1 file changed, 52 insertions(+), 104 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/abstract/layouts_test.rb b/actionpack/test/abstract/layouts_test.rb index 65a50807fd..f580ad40f7 100644 --- a/actionpack/test/abstract/layouts_test.rb +++ b/actionpack/test/abstract/layouts_test.rb @@ -8,210 +8,158 @@ module AbstractControllerTests include AbstractController::Rendering include AbstractController::Layouts - def _prefix - "template" - end - self.view_paths = [ActionView::FixtureResolver.new( - "abstract_controller_tests/layouts/with_string_implied_child.erb" => - "With Implied <%= yield %>", "layouts/hello.erb" => "With String <%= yield %>", "layouts/hello_override.erb" => "With Override <%= yield %>", "layouts/overwrite.erb" => "Overwrite <%= yield %>", - "layouts/with_false_layout.erb" => "False Layout <%= yield %>" + "layouts/with_false_layout.erb" => "False Layout <%= yield %>", + "abstract_controller_tests/layouts/with_string_implied_child.erb" => + "With Implied <%= yield %>" )] end - + class Blank < Base - self.view_paths = ActionView::FixtureResolver.new("template/index.erb" => "Hello blank!") + self.view_paths = [] def index - render + render :template => ActionView::Template::Text.new("Hello blank!") end end - + class WithString < Base layout "hello" - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello string!", - "template/overwrite_default.erb" => "Hello string!", - "template/overwrite_false.erb" => "Hello string!", - "template/overwrite_string.erb" => "Hello string!" - ) - def index - render + render :template => ActionView::Template::Text.new("Hello string!") end def overwrite_default - render :layout => :default + render :template => ActionView::Template::Text.new("Hello string!"), :layout => :default end def overwrite_false - render :layout => false + render :template => ActionView::Template::Text.new("Hello string!"), :layout => false end def overwrite_string - render :layout => "overwrite" + render :template => ActionView::Template::Text.new("Hello string!"), :layout => "overwrite" end def overwrite_skip render :text => "Hello text!" end end - + class WithStringChild < WithString end - + class WithStringOverriddenChild < WithString layout "hello_override" end - + class WithNilChild < WithString layout nil - end - + end + class WithStringImpliedChild < WithString end - + class WithChildOfImplied < WithStringImpliedChild end class WithProc < Base layout proc { |c| "overwrite" } - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello proc!" - ) - def index - render + render :template => ActionView::Template::Text.new("Hello proc!") end end class WithSymbol < Base layout :hello - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello symbol!" - ) - def index - render + render :template => ActionView::Template::Text.new("Hello symbol!") end - private - def hello "overwrite" end end - + class WithSymbolReturningString < Base layout :no_hello - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello missing symbol!" - ) - def index - render + render :template => ActionView::Template::Text.new("Hello missing symbol!") end - private - def no_hello nil end end - + class WithSymbolReturningNil < Base layout :nilz - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello nilz!" - ) - def index - render - end - - def nilz + render :template => ActionView::Template::Text.new("Hello nilz!") end + + def nilz() end end - + class WithSymbolReturningObj < Base layout :objekt - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello object!" - ) - def index - render + render :template => ActionView::Template::Text.new("Hello nilz!") end - + def objekt Object.new end - end - + end + class WithSymbolAndNoMethod < Base layout :no_method - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello boom!" - ) - def index - render + render :template => ActionView::Template::Text.new("Hello boom!") end end - + class WithMissingLayout < Base layout "missing" - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello missing!" - ) - def index - render + render :template => ActionView::Template::Text.new("Hello missing!") end end - + class WithFalseLayout < Base layout false - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello false!" - ) - def index - render + render :template => ActionView::Template::Text.new("Hello false!") end end - + class WithNilLayout < Base layout nil - append_view_path ActionView::FixtureResolver.new( - "template/index.erb" => "Hello nil!" - ) - def index - render + render :template => ActionView::Template::Text.new("Hello nil!") end end - + class TestBase < ActiveSupport::TestCase test "when no layout is specified, and no default is available, render without a layout" do controller = Blank.new controller.process(:index) assert_equal "Hello blank!", controller.response_body end - + test "when layout is specified as a string, render with that layout" do controller = WithString.new controller.process(:index) @@ -245,13 +193,13 @@ module AbstractControllerTests test "when layout is specified as a string, but the layout is missing, raise an exception" do assert_raises(ActionView::MissingTemplate) { WithMissingLayout.new.process(:index) } end - + test "when layout is specified as false, do not use a layout" do controller = WithFalseLayout.new controller.process(:index) assert_equal "Hello false!", controller.response_body end - + test "when layout is specified as nil, do not use a layout" do controller = WithNilLayout.new controller.process(:index) @@ -263,58 +211,58 @@ module AbstractControllerTests controller.process(:index) assert_equal "Overwrite Hello proc!", controller.response_body end - + test "when layout is specified as a symbol, call the requested method and use the layout returned" do controller = WithSymbol.new controller.process(:index) assert_equal "Overwrite Hello symbol!", controller.response_body end - + test "when layout is specified as a symbol and the method returns nil, don't use a layout" do controller = WithSymbolReturningNil.new controller.process(:index) assert_equal "Hello nilz!", controller.response_body end - + test "when the layout is specified as a symbol and the method doesn't exist, raise an exception" do assert_raises(NoMethodError) { WithSymbolAndNoMethod.new.process(:index) } end - + test "when the layout is specified as a symbol and the method returns something besides a string/false/nil, raise an exception" do assert_raises(ArgumentError) { WithSymbolReturningObj.new.process(:index) } end - + test "when a child controller does not have a layout, use the parent controller layout" do controller = WithStringChild.new controller.process(:index) assert_equal "With String Hello string!", controller.response_body end - + test "when a child controller has specified a layout, use that layout and not the parent controller layout" do controller = WithStringOverriddenChild.new controller.process(:index) assert_equal "With Override Hello string!", controller.response_body end - + test "when a child controller has an implied layout, use that layout and not the parent controller layout" do controller = WithStringImpliedChild.new controller.process(:index) assert_equal "With Implied Hello string!", controller.response_body end - + test "when a child controller specifies layout nil, do not use the parent layout" do controller = WithNilChild.new controller.process(:index) assert_equal "Hello string!", controller.response_body end - + test "when a grandchild has no layout specified, the child has an implied layout, and the " \ "parent has specified a layout, use the child controller layout" do controller = WithChildOfImplied.new controller.process(:index) assert_equal "With Implied Hello string!", controller.response_body end - + test "raises an exception when specifying layout true" do assert_raises ArgumentError do Object.class_eval do @@ -326,4 +274,4 @@ module AbstractControllerTests end end end -end +end \ No newline at end of file -- cgit v1.2.3 From e484d4ae5684b9ca49b27a844bf48c91c945814e Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Thu, 11 Mar 2010 00:47:10 -0500 Subject: Made asset_tag_helper use config.perform_caching instead of ActionController::Base.perform_caching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/test/template/asset_tag_helper_test.rb | 36 +++++++++++------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index d9a89959da..d36a763876 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -50,7 +50,7 @@ class AssetTagHelperTest < ActionView::TestCase end def teardown - ActionController::Base.perform_caching = false + config.perform_caching = false ENV.delete('RAILS_ASSET_ID') end @@ -422,7 +422,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_javascript_include_tag_when_caching_on ENV["RAILS_ASSET_ID"] = "" @controller.config.asset_host = 'http://a0.example.com' - ActionController::Base.perform_caching = true + config.perform_caching = true assert_dom_equal( %(), @@ -454,7 +454,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_javascript_include_tag_when_caching_on_with_proc_asset_host ENV['RAILS_ASSET_ID'] = '' @controller.config.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" } - ActionController::Base.perform_caching = true + config.perform_caching = true assert_equal '/javascripts/scripts.js'.length, 23 assert_dom_equal( @@ -477,7 +477,7 @@ class AssetTagHelperTest < ActionView::TestCase "#{request.protocol}assets#{source.length}.example.com" end } - ActionController::Base.perform_caching = true + config.perform_caching = true assert_equal '/javascripts/vanilla.js'.length, 23 assert_dom_equal( @@ -517,7 +517,7 @@ class AssetTagHelperTest < ActionView::TestCase end end.new - ActionController::Base.perform_caching = true + config.perform_caching = true assert_equal '/javascripts/vanilla.js'.length, 23 assert_dom_equal( @@ -548,7 +548,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_javascript_include_tag_when_caching_on_and_using_subdirectory ENV["RAILS_ASSET_ID"] = "" @controller.config.asset_host = 'http://a%d.example.com' - ActionController::Base.perform_caching = true + config.perform_caching = true hash = '/javascripts/cache/money.js'.hash % 4 assert_dom_equal( @@ -564,7 +564,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_javascript_include_tag_with_all_and_recursive_puts_defaults_at_the_start_of_the_file ENV["RAILS_ASSET_ID"] = "" @controller.config.asset_host = 'http://a0.example.com' - ActionController::Base.perform_caching = true + config.perform_caching = true assert_dom_equal( %(), @@ -585,7 +585,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_javascript_include_tag_with_all_puts_defaults_at_the_start_of_the_file ENV["RAILS_ASSET_ID"] = "" @controller.config.asset_host = 'http://a0.example.com' - ActionController::Base.perform_caching = true + config.perform_caching = true assert_dom_equal( %(), @@ -606,7 +606,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_javascript_include_tag_with_relative_url_root ENV["RAILS_ASSET_ID"] = "" @controller.config.relative_url_root = "/collaboration/hieraki" - ActionController::Base.perform_caching = true + config.perform_caching = true assert_dom_equal( %(), @@ -629,7 +629,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_javascript_include_tag_when_caching_off ENV["RAILS_ASSET_ID"] = "" - ActionController::Base.perform_caching = false + config.perform_caching = false assert_dom_equal( %(\n\n\n\n\n\n\n), @@ -658,7 +658,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_javascript_include_tag_when_caching_on_and_missing_javascript_file ENV["RAILS_ASSET_ID"] = "" - ActionController::Base.perform_caching = true + config.perform_caching = true assert_raise(Errno::ENOENT) { javascript_include_tag('bank', 'robber', 'missing_security_guard', :cache => true) @@ -675,7 +675,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_javascript_include_tag_when_caching_off_and_missing_javascript_file ENV["RAILS_ASSET_ID"] = "" - ActionController::Base.perform_caching = false + config.perform_caching = false assert_raise(Errno::ENOENT) { javascript_include_tag('bank', 'robber', 'missing_security_guard', :cache => true) @@ -693,7 +693,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_stylesheet_link_tag_when_caching_on ENV["RAILS_ASSET_ID"] = "" @controller.config.asset_host = 'http://a0.example.com' - ActionController::Base.perform_caching = true + config.perform_caching = true assert_dom_equal( %(), @@ -760,7 +760,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_stylesheet_link_tag_when_caching_on_and_missing_css_file ENV["RAILS_ASSET_ID"] = "" - ActionController::Base.perform_caching = true + config.perform_caching = true assert_raise(Errno::ENOENT) { stylesheet_link_tag('bank', 'robber', 'missing_security_guard', :cache => true) @@ -781,7 +781,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_stylesheet_link_tag_when_caching_off_and_missing_css_file ENV["RAILS_ASSET_ID"] = "" - ActionController::Base.perform_caching = false + config.perform_caching = false assert_raise(Errno::ENOENT) { stylesheet_link_tag('bank', 'robber', 'missing_security_guard', :cache => true) @@ -803,7 +803,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_stylesheet_link_tag_when_caching_on_with_proc_asset_host ENV["RAILS_ASSET_ID"] = "" @controller.config.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" } - ActionController::Base.perform_caching = true + config.perform_caching = true assert_equal '/stylesheets/styles.css'.length, 23 assert_dom_equal( @@ -820,7 +820,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_stylesheet_link_tag_with_relative_url_root ENV["RAILS_ASSET_ID"] = "" @controller.config.relative_url_root = "/collaboration/hieraki" - ActionController::Base.perform_caching = true + config.perform_caching = true assert_dom_equal( %(), @@ -845,7 +845,7 @@ class AssetTagHelperTest < ActionView::TestCase def test_caching_stylesheet_include_tag_when_caching_off ENV["RAILS_ASSET_ID"] = "" - ActionController::Base.perform_caching = false + config.perform_caching = false assert_dom_equal( %(\n\n), -- cgit v1.2.3 From 4ba334c0f4dac35b0c02cf3c4cca47d328283009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 13 Mar 2010 21:28:34 +0100 Subject: Ensure controller filters are executed before stuff starts to happen. --- actionpack/test/controller/render_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index e3c4869391..20fcb87da6 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -617,6 +617,15 @@ class TestController < ActionController::Base raise end + before_filter :only => :render_with_filters do + request.format = :xml + end + + # Ensure that the before filter is executed *before* self.formats is set. + def render_with_filters + render :action => :formatted_xml_erb + end + private def determine_layout @@ -1034,6 +1043,11 @@ class RenderTest < ActionController::TestCase assert_equal "Hello world!", @response.body end + def test_render_with_filters + get :render_with_filters + assert_equal "passed formatted xml erb", @response.body + end + # :ported: def test_double_render assert_raise(ActionController::DoubleRenderError) { get :double_render } -- cgit v1.2.3 From d1eed89ac3b72457c0327bf1ff2a2a9cc8842910 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sun, 14 Mar 2010 13:11:25 -0300 Subject: There's a Ruby issue with File.basename different versions returns different things, so we shouldn't test that --- actionpack/test/template/asset_tag_helper_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index d36a763876..c471df861d 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -141,7 +141,6 @@ class AssetTagHelperTest < ActionView::TestCase ImageLinkToTag = { %(image_tag("xml.png")) => %(Xml), - %(image_tag("..jpg")) => %(..jpg), %(image_tag("rss.gif", :alt => "rss syndication")) => %(rss syndication), %(image_tag("gold.png", :size => "45x70")) => %(Gold), %(image_tag("gold.png", "size" => "45x70")) => %(Gold), -- cgit v1.2.3 From 16572fd46e189d80c6be7d499e687fe129053a2c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 14 Mar 2010 18:55:13 -0700 Subject: read_ and write_fragment cache preserve html safety yet cache strings only --- actionpack/test/controller/caching_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index a3c8fdbb6e..a792752ef4 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -634,6 +634,19 @@ class FragmentCachingTest < ActionController::TestCase assert_equal 'generated till now -> fragment content', buffer end + def test_html_safety + assert_nil @store.read('views/name') + content = 'value'.html_safe + assert_equal content, @controller.write_fragment('name', content) + + cached = @store.read('views/name') + assert_equal content, cached + assert_equal String, cached.class + + html_safe = @controller.read_fragment('name') + assert_equal content, html_safe + assert html_safe.html_safe? + end end class FunctionalCachingController < CachingController -- cgit v1.2.3 From 96bc6bcfee704701de1a9c4c3c6a7c265610d34d Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 15 Mar 2010 09:45:29 -0500 Subject: Don't force singularization of singleton resource names, e.g. /preferences [#4089 state:resolved] Signed-off-by: Joshua Peek --- actionpack/test/controller/resources_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb index f60045bba6..17c645c04c 100644 --- a/actionpack/test/controller/resources_test.rb +++ b/actionpack/test/controller/resources_test.rb @@ -16,6 +16,7 @@ class AccountsController < ResourcesController; end class AdminController < ResourcesController; end class ProductsController < ResourcesController; end class ImagesController < ResourcesController; end +class PreferencesController < ResourcesController; end module Backoffice class ProductsController < ResourcesController; end @@ -1125,6 +1126,12 @@ class ResourcesTest < ActionController::TestCase end end + def test_singleton_resource_name_is_not_singularized + with_singleton_resources(:preferences) do + assert_singleton_restful_for :preferences + end + end + protected def with_restful_routing(*args) with_routing do |set| -- cgit v1.2.3 From a594a22267bfd3346e00923742c4aa7edad0cef7 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 15 Mar 2010 18:29:21 +0100 Subject: with_output_buffer cannot assume there's an output_buffer [#4182 state:committed] Signed-off-by: Jeremy Kemper --- actionpack/test/template/capture_helper_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/template/capture_helper_test.rb b/actionpack/test/template/capture_helper_test.rb index 2017a18806..f887c9ab5b 100644 --- a/actionpack/test/template/capture_helper_test.rb +++ b/actionpack/test/template/capture_helper_test.rb @@ -12,4 +12,10 @@ class CaptureHelperTest < ActionView::TestCase assert content_for?(:title) assert ! content_for?(:something_else) 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 {} + end end -- cgit v1.2.3 From 9de83050d3a4b260d4aeb5d09ec4eb64f913ba64 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Mon, 15 Mar 2010 11:58:05 -0700 Subject: Add deprecation notices for <% %>. * The approach is to compile <% %> into a method call that checks whether the value returned from a block is a String. If it is, it concats to the buffer and prints a deprecation warning. * <%= %> uses exactly the same logic to compile the template, which first checks to see whether it's compiling a block. * This should have no impact on other uses of block in templates. For instance, in <% [1,2,3].each do |i| %><%= i %><% end %>, the call to each returns an Array, not a String, so the result is not concatenated * In two cases (#capture and #cache), a String can be returned that should *never* be concatenated. We have temporarily created a String subclass called NonConcattingString which behaves (and is serialized) identically to String, but is not concatenated by the code that handles deprecated <% %> block helpers. Once we remove support for <% %> block helpers, we can remove NonConcattingString. --- actionpack/test/controller/caching_test.rb | 15 ++------------ .../test/fixtures/layouts/block_with_layout.erb | 4 ++-- .../fixtures/test/deprecated_nested_layout.erb | 3 +++ actionpack/test/fixtures/test/nested_layout.erb | 2 +- .../test/using_layout_around_block.html.erb | 2 +- actionpack/test/template/erb/tag_helper_test.rb | 24 ++++++++++++++++------ actionpack/test/template/render_test.rb | 8 ++++++++ 7 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 actionpack/test/fixtures/test/deprecated_nested_layout.erb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index a792752ef4..432433f976 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -617,7 +617,7 @@ class FragmentCachingTest < ActionController::TestCase fragment_computed = false buffer = 'generated till now -> '.html_safe - @controller.fragment_for(buffer, 'expensive') { fragment_computed = true } + buffer << @controller.fragment_for('expensive') { fragment_computed = true } assert fragment_computed assert_equal 'generated till now -> ', buffer @@ -628,7 +628,7 @@ class FragmentCachingTest < ActionController::TestCase fragment_computed = false buffer = 'generated till now -> '.html_safe - @controller.fragment_for(buffer, 'expensive') { fragment_computed = true } + buffer << @controller.fragment_for('expensive') { fragment_computed = true } assert !fragment_computed assert_equal 'generated till now -> fragment content', buffer @@ -742,15 +742,4 @@ CACHED assert_equal "

Builder

\n", @store.read('views/test.host/functional_caching/formatted_fragment_cached') end - - def test_js_formatted_fragment_caching - get :formatted_fragment_cached, :format => "js" - assert_response :success - expected_body = %(title = "Hey";\n$("element_1").visualEffect("highlight");\n) + - %($("element_2").visualEffect("highlight");\nfooter = "Bye";) - assert_equal expected_body, @response.body - - assert_equal ['$("element_1").visualEffect("highlight");', '$("element_2").visualEffect("highlight");'], - @store.read('views/test.host/functional_caching/formatted_fragment_cached') - end end diff --git a/actionpack/test/fixtures/layouts/block_with_layout.erb b/actionpack/test/fixtures/layouts/block_with_layout.erb index f25b41271d..73ac833e52 100644 --- a/actionpack/test/fixtures/layouts/block_with_layout.erb +++ b/actionpack/test/fixtures/layouts/block_with_layout.erb @@ -1,3 +1,3 @@ -<% render(:layout => "layout_for_partial", :locals => { :name => "Anthony" }) do %>Inside from first block in layout<% "Return value should be discarded" %><% end %> +<%= render(:layout => "layout_for_partial", :locals => { :name => "Anthony" }) do %>Inside from first block in layout<% "Return value should be discarded" %><% end %> <%= yield %> -<% render(:layout => "layout_for_partial", :locals => { :name => "Ramm" }) do %>Inside from second block in layout<% end %> +<%= render(:layout => "layout_for_partial", :locals => { :name => "Ramm" }) do %>Inside from second block in layout<% end %> diff --git a/actionpack/test/fixtures/test/deprecated_nested_layout.erb b/actionpack/test/fixtures/test/deprecated_nested_layout.erb new file mode 100644 index 0000000000..7b6dcbb6c7 --- /dev/null +++ b/actionpack/test/fixtures/test/deprecated_nested_layout.erb @@ -0,0 +1,3 @@ +<% content_for :title, "title" -%> +<% content_for :column do -%>column<% end -%> +<% render :layout => 'layouts/column' do -%>content<% end -%> \ No newline at end of file diff --git a/actionpack/test/fixtures/test/nested_layout.erb b/actionpack/test/fixtures/test/nested_layout.erb index 7b6dcbb6c7..6078f74b4c 100644 --- a/actionpack/test/fixtures/test/nested_layout.erb +++ b/actionpack/test/fixtures/test/nested_layout.erb @@ -1,3 +1,3 @@ <% content_for :title, "title" -%> <% content_for :column do -%>column<% end -%> -<% render :layout => 'layouts/column' do -%>content<% end -%> \ No newline at end of file +<%= render :layout => 'layouts/column' do -%>content<% end -%> \ No newline at end of file diff --git a/actionpack/test/fixtures/test/using_layout_around_block.html.erb b/actionpack/test/fixtures/test/using_layout_around_block.html.erb index a93fa02c9c..3d6661df9a 100644 --- a/actionpack/test/fixtures/test/using_layout_around_block.html.erb +++ b/actionpack/test/fixtures/test/using_layout_around_block.html.erb @@ -1 +1 @@ -<% render(:layout => "layout_for_partial", :locals => { :name => "David" }) do %>Inside from block<% end %> \ No newline at end of file +<%= render(:layout => "layout_for_partial", :locals => { :name => "David" }) do %>Inside from block<% 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..b5b7ae87de 100644 --- a/actionpack/test/template/erb/tag_helper_test.rb +++ b/actionpack/test/template/erb/tag_helper_test.rb @@ -20,7 +20,7 @@ module ERBTest end class DeprecatedViewContext < ViewContext - include ActionView::Helpers::DeprecatedBlockHelpers + # include ActionView::Helpers::DeprecatedBlockHelpers end module SharedTagHelpers @@ -31,28 +31,36 @@ module ERBTest ActionView::Template::Handlers::Erubis.new(template).evaluate(context.new) end + 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 "
Hello world
", render_content("content_tag :div", "Hello world") + maybe_deprecated { assert_equal "
Hello world
", render_content("content_tag :div", "Hello world") } end test "percent equals works for javascript_tag" do expected_output = "" - 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 = "" - 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 = "
hello
" - 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 = "
foohello
" - 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 @@ -77,6 +85,10 @@ module ERBTest "<% __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/render_test.rb b/actionpack/test/template/render_test.rb index cea8ab1bce..e66202e8fe 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -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\n\n\n
column
\n
content
\n), + @view.render(:file => "test/deprecated_nested_layout.erb", :layout => "layouts/yield") + end + end + def test_render_with_nested_layout assert_equal %(title\n\n\n
column
\n
content
\n), @view.render(:file => "test/nested_layout.erb", :layout => "layouts/yield") -- cgit v1.2.3 From c5a877f1421744096ce6ffbfce3fc1034bda880e Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 16 Mar 2010 00:22:22 +0100 Subject: adds test coverage for with_output_buffer Signed-off-by: Jeremy Kemper --- actionpack/test/template/capture_helper_test.rb | 48 +++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/capture_helper_test.rb b/actionpack/test/template/capture_helper_test.rb index f887c9ab5b..e65f8b15ed 100644 --- a/actionpack/test/template/capture_helper_test.rb +++ b/actionpack/test/template/capture_helper_test.rb @@ -3,6 +3,7 @@ require 'abstract_unit' class CaptureHelperTest < ActionView::TestCase def setup super + @av = ActionView::Base.new @_content_for = Hash.new {|h,k| h[k] = "" } end @@ -13,9 +14,50 @@ 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 = @av.output_buffer.encoding == Encoding::US_ASCII ? Encoding::UTF_8 : Encoding::US_ASCII + @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 end -- cgit v1.2.3 From 8dd731bc502a07f4fb76eb2706a1f3bca479ef63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 16 Mar 2010 02:08:34 +0100 Subject: Move more normalization up to the lookup context, so it does not have to repeat in every resolver. --- actionpack/test/lib/fixture_template.rb | 2 +- actionpack/test/template/lookup_context_test.rb | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb index 02248d84ad..2e876d9249 100644 --- a/actionpack/test/lib/fixture_template.rb +++ b/actionpack/test/lib/fixture_template.rb @@ -12,7 +12,7 @@ module ActionView #:nodoc: def query(partial, path, exts) query = Regexp.escape(path) exts.each do |ext| - query << '(' << ext.map {|e| e && Regexp.escape(".#{e}") }.join('|') << ')' + query << '(' << ext.map {|e| e && Regexp.escape(".#{e}") }.join('|') << '|)' end templates = [] diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index 697ebc694a..37526957bc 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -22,14 +22,14 @@ class LookupContextTest < ActiveSupport::TestCase end test "normalizes details on initialization" do - formats = Mime::SET + [nil] - locale = [I18n.locale, nil] + formats = Mime::SET + locale = [I18n.locale] 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 Hash[:formats => [:html], :locale => [:pt]], @lookup_context.details end test "does not allow details to be modified in place" do @@ -39,17 +39,17 @@ class LookupContextTest < ActiveSupport::TestCase 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] + formats = Mime::SET + locale = [I18n.locale] assert_equal Hash[:formats => formats, :locale => locale], @lookup_context.details 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 Hash[:formats => formats, :locale => [:pt]], @lookup_context.details end - assert_equal Hash[:formats => formats, :locale => [:en, nil]], @lookup_context.details + assert_equal Hash[:formats => formats, :locale => [:en]], @lookup_context.details end test "provides getters and setters for formats" do -- cgit v1.2.3 From 2a50eabf4576580dff9b43c3d830cd78e8fbb353 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 15 Mar 2010 19:26:58 -0700 Subject: Integration test url options should account for :protocol not just https? --- actionpack/test/dispatch/url_generation_test.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/url_generation_test.rb b/actionpack/test/dispatch/url_generation_test.rb index 18b5b7ee00..b863e37a33 100644 --- a/actionpack/test/dispatch/url_generation_test.rb +++ b/actionpack/test/dispatch/url_generation_test.rb @@ -34,5 +34,10 @@ module TestUrlGeneration get "/foo", {}, 'SCRIPT_NAME' => "/new" assert_equal "/new/foo", response.body end + + test "handling http protocol with https set" do + https! + assert_equal "http://www.example.com/bar/foo", foo_url(:protocol => "http") + end end -end \ No newline at end of file +end -- cgit v1.2.3 From b9c48f519fbeee8fda3c989204619648f794fb02 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 15 Mar 2010 22:44:49 -0700 Subject: Swear I ran this test --- actionpack/test/dispatch/url_generation_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/url_generation_test.rb b/actionpack/test/dispatch/url_generation_test.rb index b863e37a33..326b336a1a 100644 --- a/actionpack/test/dispatch/url_generation_test.rb +++ b/actionpack/test/dispatch/url_generation_test.rb @@ -37,7 +37,7 @@ module TestUrlGeneration test "handling http protocol with https set" do https! - assert_equal "http://www.example.com/bar/foo", foo_url(:protocol => "http") + assert_equal "http://www.example.com/foo", foo_url(:protocol => "http") end end end -- cgit v1.2.3 From b3b6ff48dff49ebbdab0a53f576bc0572116767f Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 15 Mar 2010 23:26:48 -0700 Subject: Fix link_to with block --- actionpack/test/template/url_helper_test.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'actionpack/test') 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 'Example site', output_buffer end -- cgit v1.2.3 From b65b989725320dc9e424bab7536f062d0dc94b0f Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 15 Mar 2010 23:48:46 -0700 Subject: Break a window :/ --- actionpack/test/controller/caching_test.rb | 2 +- actionpack/test/fixtures/functional_caching/_partial.erb | 4 ++-- .../fixtures/functional_caching/formatted_fragment_cached.html.erb | 4 ++-- actionpack/test/fixtures/functional_caching/fragment_cached.html.erb | 2 +- .../test/fixtures/functional_caching/inline_fragment_cached.html.erb | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 432433f976..2f9cc44308 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -726,7 +726,7 @@ CACHED def test_html_formatted_fragment_caching get :formatted_fragment_cached, :format => "html" assert_response :success - expected_body = "\n

ERB

\n" + expected_body = "\n

ERB

\n\n" assert_equal expected_body, @response.body diff --git a/actionpack/test/fixtures/functional_caching/_partial.erb b/actionpack/test/fixtures/functional_caching/_partial.erb index d0e4f72b95..ec0da7cf50 100644 --- a/actionpack/test/fixtures/functional_caching/_partial.erb +++ b/actionpack/test/fixtures/functional_caching/_partial.erb @@ -1,3 +1,3 @@ <% cache do %> -Fragment caching in a partial -<% end %> \ No newline at end of file +Old fragment caching in a partial +<% end %> diff --git a/actionpack/test/fixtures/functional_caching/formatted_fragment_cached.html.erb b/actionpack/test/fixtures/functional_caching/formatted_fragment_cached.html.erb index d7f43ad95e..9b88fa1f5a 100644 --- a/actionpack/test/fixtures/functional_caching/formatted_fragment_cached.html.erb +++ b/actionpack/test/fixtures/functional_caching/formatted_fragment_cached.html.erb @@ -1,3 +1,3 @@ -<% cache do %>

ERB

<% end %> - \ No newline at end of file +<%= cache do %>

ERB

<% end %> + diff --git a/actionpack/test/fixtures/functional_caching/fragment_cached.html.erb b/actionpack/test/fixtures/functional_caching/fragment_cached.html.erb index 268a298a42..c479adb897 100644 --- a/actionpack/test/fixtures/functional_caching/fragment_cached.html.erb +++ b/actionpack/test/fixtures/functional_caching/fragment_cached.html.erb @@ -1,2 +1,2 @@ Hello -<% cache do %>This bit's fragment cached<% end %> +<%= cache do %>This bit's fragment cached<% end %> diff --git a/actionpack/test/fixtures/functional_caching/inline_fragment_cached.html.erb b/actionpack/test/fixtures/functional_caching/inline_fragment_cached.html.erb index 87309b8ccb..41647f1404 100644 --- a/actionpack/test/fixtures/functional_caching/inline_fragment_cached.html.erb +++ b/actionpack/test/fixtures/functional_caching/inline_fragment_cached.html.erb @@ -1,2 +1,2 @@ <%= render :inline => 'Some inline content' %> -<% cache do %>Some cached content<% end %> +<%= cache do %>Some cached content<% end %> -- cgit v1.2.3 From c61ed70b00c93bdf42c7538a334f07e58c60bc4e Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Tue, 16 Mar 2010 11:43:04 -0700 Subject: Some more tweaks on <% %>. * The cache helper is now semantically "mark this region for caching" * As a result, <% x = cache do %> no longer works --- actionpack/test/controller/caching_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 2f9cc44308..f6264c0954 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -704,8 +704,8 @@ CACHED def test_fragment_caching_in_partials get :html_fragment_cached_with_partial assert_response :success - assert_match /Fragment caching in a partial/, @response.body - assert_match "Fragment caching in a partial", @store.read('views/test.host/functional_caching/html_fragment_cached_with_partial') + assert_match /Old fragment caching in a partial/, @response.body + assert_match "Old fragment caching in a partial", @store.read('views/test.host/functional_caching/html_fragment_cached_with_partial') end def test_render_inline_before_fragment_caching @@ -719,8 +719,8 @@ CACHED def test_fragment_caching_in_rjs_partials xhr :get, :js_fragment_cached_with_partial assert_response :success - assert_match /Fragment caching in a partial/, @response.body - assert_match "Fragment caching in a partial", @store.read('views/test.host/functional_caching/js_fragment_cached_with_partial') + assert_match /Old fragment caching in a partial/, @response.body + assert_match "Old fragment caching in a partial", @store.read('views/test.host/functional_caching/js_fragment_cached_with_partial') end def test_html_formatted_fragment_caching -- cgit v1.2.3 From 986cac73e3c56b3dfa22fd1464f6913e38d32cc3 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 16 Mar 2010 20:33:38 +0100 Subject: adds tests for #capture Signed-off-by: Jeremy Kemper --- actionpack/test/template/capture_helper_test.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/template/capture_helper_test.rb b/actionpack/test/template/capture_helper_test.rb index e65f8b15ed..c1e83fc04d 100644 --- a/actionpack/test/template/capture_helper_test.rb +++ b/actionpack/test/template/capture_helper_test.rb @@ -7,6 +7,29 @@ class CaptureHelperTest < ActionView::TestCase @_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_in_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' -- cgit v1.2.3 From 9659d18c9b76f6383854af0cd3a75abb6b1784ea Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 16 Mar 2010 21:05:01 +0100 Subject: adds tests for #flush_output_buffer [#4196 state:committed] Signed-off-by: Jeremy Kemper --- actionpack/test/template/capture_helper_test.rb | 39 +++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/capture_helper_test.rb b/actionpack/test/template/capture_helper_test.rb index c1e83fc04d..2216e6b578 100644 --- a/actionpack/test/template/capture_helper_test.rb +++ b/actionpack/test/template/capture_helper_test.rb @@ -18,7 +18,7 @@ class CaptureHelperTest < ActionView::TestCase assert_kind_of ActionView::NonConcattingString, string end - def test_capture_captures_the_value_returned_by_the_block_in_the_temporary_buffer_is_blank + 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 @@ -70,7 +70,7 @@ class CaptureHelperTest < ActionView::TestCase @av.output_buffer = ActionView::OutputBuffer.new # Ensure we set the output buffer to an encoding different than the default one. - alt_encoding = @av.output_buffer.encoding == Encoding::US_ASCII ? Encoding::UTF_8 : Encoding::US_ASCII + alt_encoding = alt_encoding(@av.output_buffer) @av.output_buffer.force_encoding(alt_encoding) @av.with_output_buffer do @@ -83,4 +83,39 @@ class CaptureHelperTest < ActionView::TestCase 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(ActionView::Base.for_controller(TestController.new)) do |view| + view.output_buffer = ActionView::OutputBuffer.new + end + end end -- cgit v1.2.3 From 12bf636461e3aab661119ceb3a104cfb70a11666 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 16 Mar 2010 17:36:09 -0300 Subject: translation method of TranslationHelper module returns always SafeBuffer [#4194 status:resolved] Signed-off-by: Jeremy Kemper --- actionpack/test/fixtures/test/array_translation.erb | 1 + actionpack/test/template/translation_helper_test.rb | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 actionpack/test/fixtures/test/array_translation.erb (limited to 'actionpack/test') diff --git a/actionpack/test/fixtures/test/array_translation.erb b/actionpack/test/fixtures/test/array_translation.erb new file mode 100644 index 0000000000..12c0763313 --- /dev/null +++ b/actionpack/test/fixtures/test/array_translation.erb @@ -0,0 +1 @@ +<%= t(['foo', 'bar']) %> \ No newline at end of file diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index 699fb2f5bc..5a32d71409 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 = 'foobar' + I18n.expects(:translate).with(["foo", "bar"], :raise => true).returns(['foo', 'bar']) + @view = ActionView::Base.new(ActionController::Base.view_paths, {}) + assert_equal expected, @view.render(:file => "test/array_translation") end def test_delegates_localize_to_i18n -- cgit v1.2.3 From 56fb60ebfe9a20ced1366f3e35b2f9bd0bac8e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 16 Mar 2010 23:35:45 +0100 Subject: Fix rendering of HTML partials inside JS templates [#4197 status:resolved] --- actionpack/test/controller/new_base/render_rjs_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/new_base/render_rjs_test.rb b/actionpack/test/controller/new_base/render_rjs_test.rb index f4516ade63..7c8f1cb869 100644 --- a/actionpack/test/controller/new_base/render_rjs_test.rb +++ b/actionpack/test/controller/new_base/render_rjs_test.rb @@ -5,8 +5,10 @@ module RenderRjs self.view_paths = [ActionView::FixtureResolver.new( "render_rjs/basic/index.js.rjs" => "page[:customer].replace_html render(:partial => 'customer')", "render_rjs/basic/index_html.js.rjs" => "page[:customer].replace_html :partial => 'customer'", + "render_rjs/basic/index_no_js.js.rjs" => "page[:developer].replace_html render(:partial => 'developer')", "render_rjs/basic/_customer.js.erb" => "JS Partial", "render_rjs/basic/_customer.html.erb" => "HTML Partial", + "render_rjs/basic/_developer.html.erb" => "HTML Partial", "render_rjs/basic/index_locale.js.rjs" => "page[:customer].replace_html :partial => 'customer'", "render_rjs/basic/_customer.da.html.erb" => "Danish HTML Partial", "render_rjs/basic/_customer.da.js.erb" => "Danish JS Partial" @@ -37,6 +39,11 @@ module RenderRjs assert_response("$(\"customer\").update(\"JS Partial\");") end + test "rendering a partial in an RJS template should pick the HTML one if no JS is available" do + get :index_no_js, "format" => "js" + assert_response("$(\"developer\").update(\"HTML Partial\");") + end + test "replacing an element with a partial in an RJS template should pick the HTML template over the JS one" do get :index_html, "format" => "js" assert_response("$(\"customer\").update(\"HTML Partial\");") -- cgit v1.2.3 From 0c1ac36ccb7d72f3d17d950d030442a7e83d0708 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 16 Mar 2010 20:12:04 -0300 Subject: scope_key_by_partial fix for Ruby 1.9 when there's virtual_path [#4202 state:resolved] Signed-off-by: Jeremy Kemper --- actionpack/test/fixtures/test/scoped_array_translation.erb | 1 + actionpack/test/template/translation_helper_test.rb | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 actionpack/test/fixtures/test/scoped_array_translation.erb (limited to 'actionpack/test') diff --git a/actionpack/test/fixtures/test/scoped_array_translation.erb b/actionpack/test/fixtures/test/scoped_array_translation.erb new file mode 100644 index 0000000000..0a0c79f717 --- /dev/null +++ b/actionpack/test/fixtures/test/scoped_array_translation.erb @@ -0,0 +1 @@ +<%= t(['.foo', '.bar']) %> \ No newline at end of file diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index 5a32d71409..6782bf06d4 100644 --- a/actionpack/test/template/translation_helper_test.rb +++ b/actionpack/test/template/translation_helper_test.rb @@ -41,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 -- cgit v1.2.3 From d69e5616e821afc40efa5936c5ab6e087eb4e0c6 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 16 Mar 2010 22:06:16 -0500 Subject: link_to_function is here to stay --- actionpack/test/template/javascript_helper_test.rb | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'actionpack/test') 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 %(Greeting), + link_to_function("Greeting", "alert('Hello world!')") + end + + def test_link_to_function_with_existing_onclick + assert_dom_equal %(Greeting), + 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', "

Greetings

" + end + assert_dom_equal %(Greet me!), 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', "

Greetings

" + end + assert_dom_equal %(Greet me!), html + end + + def test_link_to_function_with_href + assert_dom_equal %(Greeting), + link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/') + end + def test_javascript_tag self.output_buffer = 'foo' -- cgit v1.2.3 From 13a783672aad338b9c7ade5319ec7967768905d7 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 17 Mar 2010 16:04:41 -0500 Subject: Install url helpers on module instance so they can be accessed globally --- actionpack/test/dispatch/routing_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index f5fcf9b0df..e4d83fa0a4 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -193,6 +193,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest assert_equal '/login', url_for(:controller => 'sessions', :action => 'new', :only_path => true) assert_equal 'http://rubyonrails.org/login', Routes.url_for(:controller => 'sessions', :action => 'create') + assert_equal 'http://rubyonrails.org/login', Routes.url_helpers.login_url end end -- cgit v1.2.3 From 947f86c699b33bd44703b3554db58e4cfca37c86 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Wed, 17 Mar 2010 14:19:42 -0700 Subject: Modify assert_template to use instrumentation --- actionpack/test/abstract_unit.rb | 41 ---------------------------------------- 1 file changed, 41 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 67aa412d3d..a3486aa039 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -239,47 +239,6 @@ module ActionController setup do @router = SharedTestRoutes end - - def assert_template(options = {}, message = nil) - validate_request! - - hax = @controller.view_context.instance_variable_get(:@_rendered) - - case options - when NilClass, String - rendered = (hax[:template] || []).map { |t| t.identifier } - msg = build_message(message, - "expecting but rendering with ", - options, rendered.join(', ')) - assert_block(msg) do - if options.nil? - hax[:template].blank? - else - rendered.any? { |t| t.match(options) } - end - end - when Hash - if expected_partial = options[:partial] - partials = hax[:partials] - if expected_count = options[:count] - found = partials.detect { |p, _| p.identifier.match(expected_partial) } - actual_count = found.nil? ? 0 : found[1] - msg = build_message(message, - "expecting ? to be rendered ? time(s) but rendered ? time(s)", - expected_partial, expected_count, actual_count) - assert(actual_count == expected_count.to_i, msg) - else - msg = build_message(message, - "expecting partial but action rendered ", - options[:partial], partials.keys) - assert(partials.keys.any? { |p| p.identifier.match(expected_partial) }, msg) - end - else - assert hax[:partials].empty?, - "Expected no partials to be rendered" - end - end - end end end -- cgit v1.2.3 From 6416a35f4b3290a93145d40045147fc01d36e756 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Wed, 17 Mar 2010 14:23:00 -0700 Subject: Remove unneeded AV::Base and AV::Template monkey-patches --- actionpack/test/controller/action_pack_assertions_test.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index 26e0d6d844..b6810b0c27 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -365,11 +365,10 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase # check if we were rendered by a file-based template? def test_rendered_action process :nothing - assert_nil @controller.template.rendered[:template] + assert_template nil process :hello_world - assert @controller.template.rendered[:template] - assert 'hello_world', @controller.template.rendered[:template].to_s + assert_template 'hello_world' end # check the redirection location -- cgit v1.2.3 From d9375f3f302a5d1856ad57946c7263d4e6a45a2a Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Wed, 17 Mar 2010 16:28:05 -0700 Subject: Modify assert_template to use notifications. Also, remove ActionController::Base#template since it is no longer needed. --- .../test/controller/action_pack_assertions_test.rb | 8 +++-- actionpack/test/controller/filters_test.rb | 4 +-- actionpack/test/controller/layout_test.rb | 16 ++++----- actionpack/test/controller/render_test.rb | 6 ++-- actionpack/test/template/body_parts_test.rb | 3 +- actionpack/test/template/erb/form_for_test.rb | 11 ++++++ actionpack/test/template/erb/helper.rb | 30 ++++++++++++++++ actionpack/test/template/erb/tag_helper_test.rb | 40 ++-------------------- actionpack/test/template/output_buffer_test.rb | 18 +++++----- 9 files changed, 72 insertions(+), 64 deletions(-) create mode 100644 actionpack/test/template/erb/form_for_test.rb create mode 100644 actionpack/test/template/erb/helper.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index b6810b0c27..1741b58f72 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -181,6 +181,8 @@ module Admin end end +# require "action_dispatch/test_process" + # a test case to exercise the new capabilities TestRequest & TestResponse class ActionPackAssertionsControllerTest < ActionController::TestCase # -- assertion-based testing ------------------------------------------------ @@ -303,14 +305,14 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase # make sure that the template objects exist def test_template_objects_alive process :assign_this - assert !@controller.template.instance_variable_get(:"@hi") - assert @controller.template.instance_variable_get(:"@howdy") + assert !@controller.instance_variable_get(:"@hi") + assert @controller.instance_variable_get(:"@howdy") end # make sure we don't have template objects when we shouldn't def test_template_object_missing process :nothing - assert_nil @controller.template.assigns['howdy'] + assert_nil @controller.instance_variable_get(:@howdy) end # check the empty flashing diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index 004e1369d3..ea740f7233 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -651,9 +651,9 @@ class FilterTest < ActionController::TestCase assert_equal %w( ensure_login find_user ), assigns["ran_filter"] test_process(ConditionalSkippingController, "login") - assert_nil @controller.template.controller.instance_variable_get("@ran_after_filter") + assert_nil @controller.instance_variable_get("@ran_after_filter") test_process(ConditionalSkippingController, "change_password") - assert_equal %w( clean_up ), @controller.template.controller.instance_variable_get("@ran_after_filter") + assert_equal %w( clean_up ), @controller.instance_variable_get("@ran_after_filter") end def test_conditional_skipping_of_filters_when_parent_filter_is_also_conditional diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index f635253156..4d687c1ec6 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -120,19 +120,19 @@ class LayoutSetInResponseTest < ActionController::TestCase def test_layout_set_when_using_default_layout @controller = DefaultLayoutController.new get :hello - assert @controller.template.layout.include?('layouts/layout_test') + assert_template :layout => "layouts/layout_test" end def test_layout_set_when_set_in_controller @controller = HasOwnLayoutController.new get :hello - assert @controller.template.layout.include?('layouts/item') + assert_template :layout => "layouts/item" end def test_layout_only_exception_when_included @controller = OnlyLayoutController.new get :hello - assert @controller.template.layout.include?('layouts/item') + assert_template :layout => "layouts/item" end def test_layout_only_exception_when_excepted @@ -144,7 +144,7 @@ class LayoutSetInResponseTest < ActionController::TestCase def test_layout_except_exception_when_included @controller = ExceptLayoutController.new get :hello - assert @controller.template.layout.include?('layouts/item') + assert_template :layout => "layouts/item" end def test_layout_except_exception_when_excepted @@ -156,19 +156,19 @@ class LayoutSetInResponseTest < ActionController::TestCase def test_layout_set_when_using_render @controller = SetsLayoutInRenderController.new get :hello - assert @controller.template.layout.include?('layouts/third_party_template_library') + assert_template :layout => "layouts/third_party_template_library" end def test_layout_is_not_set_when_none_rendered @controller = RendersNoLayoutController.new get :hello - assert_nil @controller.template.layout + assert_template :layout => nil end def test_layout_is_picked_from_the_controller_instances_view_path @controller = PrependsViewPathController.new get :hello - assert @controller.template.layout =~ /layouts\/alt\.\w+/ + assert_template :layout => /layouts\/alt\.\w+/ end def test_absolute_pathed_layout @@ -219,7 +219,7 @@ unless RUBY_PLATFORM =~ /(:?mswin|mingw|bccwin)/ @controller = LayoutSymlinkedTest.new get :hello assert_response 200 - assert @controller.template.layout.include?("layouts/symlinked/symlinked_layout") + assert_template :layout => "layouts/symlinked/symlinked_layout" end end end diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 20fcb87da6..1d7692f0a8 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -418,7 +418,7 @@ class TestController < ActionController::Base def rendering_with_conflicting_local_vars @name = "David" - def @template.name() nil end + def view_context.name() nil end render :action => "potential_conflicts" end @@ -526,11 +526,11 @@ class TestController < ActionController::Base end def partial_with_form_builder - render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, {}, Proc.new {}) + render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {}, Proc.new {}) end def partial_with_form_builder_subclass - render :partial => LabellingFormBuilder.new(:post, nil, @template, {}, Proc.new {}) + render :partial => LabellingFormBuilder.new(:post, nil, view_context, {}, Proc.new {}) end def partial_collection 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/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 "
", 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 b5b7ae87de..64a88bde1d 100644 --- a/actionpack/test/template/erb/tag_helper_test.rb +++ b/actionpack/test/template/erb/tag_helper_test.rb @@ -1,36 +1,10 @@ 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) - end - def maybe_deprecated if @deprecated assert_deprecated { yield } @@ -64,11 +38,7 @@ module ERBTest end end - class TagHelperTest < ActiveSupport::TestCase - def context - ViewContext - end - + class TagHelperTest < BlockTestCase def block_helper(str, rest) "<%= #{str} do %>#{rest}<% end %>" end @@ -76,11 +46,7 @@ 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 diff --git a/actionpack/test/template/output_buffer_test.rb b/actionpack/test/template/output_buffer_test.rb index 36bbaf9099..9016b74489 100644 --- a/actionpack/test/template/output_buffer_test.rb +++ b/actionpack/test/template/output_buffer_test.rb @@ -19,21 +19,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 + @controller.view_context.output_buffer = '' + @controller.view_context.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 + @controller.view_context.output_buffer = 'bar' + @controller.view_context.flush_output_buffer assert_equal '', output_buffer assert_equal ['foo', 'bar'], body_parts end @@ -41,8 +41,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 + @controller.view_context.output_buffer = original_buffer + @controller.view_context.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 +51,10 @@ class OutputBufferTest < ActionController::TestCase protected def output_buffer - @controller.template.output_buffer + @controller.view_context.output_buffer end def body_parts - @controller.template.response.body_parts + @controller.response.body_parts end end -- cgit v1.2.3 From c8dd6f224c9345b7e95cc0203b636e49412b71dc Mon Sep 17 00:00:00 2001 From: Mathias Biilmann Christensen Date: Thu, 18 Mar 2010 01:15:52 +0100 Subject: Deleting and setting a cookie in the same request was broken Made sure to remove a cookie from @deleted_cookies when set [#4211 state:committed] Signed-off-by: Jeremy Kemper --- actionpack/test/controller/cookie_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/cookie_test.rb b/actionpack/test/controller/cookie_test.rb index 908967a110..36498d13a9 100644 --- a/actionpack/test/controller/cookie_test.rb +++ b/actionpack/test/controller/cookie_test.rb @@ -64,6 +64,12 @@ class CookieTest < ActionController::TestCase cookies.permanent.signed[:remember_me] = 100 head :ok end + + def delete_and_set_cookie + cookies.delete :user_name + cookies[:user_name] = { :value => "david", :expires => Time.utc(2005, 10, 10,5) } + head :ok + end end tests TestController @@ -152,6 +158,11 @@ class CookieTest < ActionController::TestCase assert_equal 100, @controller.send(:cookies).signed[:remember_me] end + def test_delete_and_set_cookie + get :delete_and_set_cookie + assert_cookie_header "user_name=david; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT" + assert_equal({"user_name" => "david"}, @response.cookies) + end private def assert_cookie_header(expected) -- cgit v1.2.3 From 191a2f78b1abeb4d3b5b27de83ffe14b833c4c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 18 Mar 2010 12:10:40 +0100 Subject: Sending the partial as info is no longer required. --- actionpack/test/lib/fixture_template.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb index 2e876d9249..74c0918a14 100644 --- a/actionpack/test/lib/fixture_template.rb +++ b/actionpack/test/lib/fixture_template.rb @@ -9,7 +9,7 @@ module ActionView #:nodoc: private - def query(partial, path, exts) + def query(path, exts) query = Regexp.escape(path) exts.each do |ext| query << '(' << ext.map {|e| e && Regexp.escape(".#{e}") }.join('|') << '|)' @@ -19,7 +19,7 @@ module ActionView #:nodoc: @hash.select { |k,v| k =~ /^#{query}$/ }.each do |path, source| handler, format = extract_handler_and_format(path) templates << Template.new(source, path, handler, - :partial => partial, :virtual_path => path, :format => format) + :virtual_path => path, :format => format) end templates.sort_by {|t| -t.identifier.match(/^#{query}$/).captures.reject(&:blank?).size } -- cgit v1.2.3 From 71c9337f45f9c5461cbc6ddf6cab764ad0f82c3b Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Thu, 18 Mar 2010 15:52:43 -0700 Subject: All tests pass without memoizing view_context --- actionpack/test/controller/caching_test.rb | 8 ++++++-- actionpack/test/controller/render_test.rb | 8 +++++++- actionpack/test/template/capture_helper_test.rb | 2 +- actionpack/test/template/output_buffer_test.rb | 15 ++++++++------- actionpack/test/template/render_test.rb | 2 +- 5 files changed, 23 insertions(+), 12 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index f6264c0954..5157454be0 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -616,8 +616,10 @@ class FragmentCachingTest < ActionController::TestCase @store.write('views/expensive', 'fragment content') fragment_computed = false + view_context = @controller.view_context + buffer = 'generated till now -> '.html_safe - buffer << @controller.fragment_for('expensive') { fragment_computed = true } + buffer << view_context.send(:fragment_for, 'expensive') { fragment_computed = true } assert fragment_computed assert_equal 'generated till now -> ', buffer @@ -627,8 +629,10 @@ class FragmentCachingTest < ActionController::TestCase @store.write('views/expensive', 'fragment content') fragment_computed = false + view_context = @controller.view_context + buffer = 'generated till now -> '.html_safe - buffer << @controller.fragment_for('expensive') { fragment_computed = true } + buffer << view_context.send(:fragment_for, 'expensive') { fragment_computed = true } assert !fragment_computed assert_equal 'generated till now -> fragment content', buffer diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 1d7692f0a8..24817a93d2 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -18,6 +18,13 @@ class TestController < ActionController::Base layout :determine_layout + def name + nil + end + + private :name + helper_method :name + def hello_world end @@ -418,7 +425,6 @@ class TestController < ActionController::Base def rendering_with_conflicting_local_vars @name = "David" - def view_context.name() nil end render :action => "potential_conflicts" end diff --git a/actionpack/test/template/capture_helper_test.rb b/actionpack/test/template/capture_helper_test.rb index 2216e6b578..bf541c17d3 100644 --- a/actionpack/test/template/capture_helper_test.rb +++ b/actionpack/test/template/capture_helper_test.rb @@ -114,7 +114,7 @@ class CaptureHelperTest < ActionView::TestCase end def view_with_controller - returning(ActionView::Base.for_controller(TestController.new)) do |view| + returning(TestController.new.view_context) do |view| view.output_buffer = ActionView::OutputBuffer.new end end diff --git a/actionpack/test/template/output_buffer_test.rb b/actionpack/test/template/output_buffer_test.rb index 9016b74489..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 @@ -25,15 +26,15 @@ class OutputBufferTest < ActionController::TestCase end test 'flushing ignores empty output buffer' do - @controller.view_context.output_buffer = '' - @controller.view_context.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.view_context.output_buffer = 'bar' - @controller.view_context.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.view_context.output_buffer = original_buffer - @controller.view_context.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,7 +52,7 @@ class OutputBufferTest < ActionController::TestCase protected def output_buffer - @controller.view_context.output_buffer + @vc.output_buffer end def body_parts diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index e66202e8fe..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! -- cgit v1.2.3 From edb5991a14e0dfb65d2b8802d61b44fc47004921 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Thu, 18 Mar 2010 16:55:32 -0700 Subject: Make render :partial, :layout consistent between AC and AV --- actionpack/test/controller/render_test.rb | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 24817a93d2..2f3997518f 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -513,10 +513,6 @@ class TestController < ActionController::Base end end - def partial_only_with_layout - render :partial => "partial_only", :layout => true - end - def render_to_string_with_partial @partial_only = render_to_string :partial => "partial_only" @partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") } @@ -640,8 +636,7 @@ class TestController < ActionController::Base "rendering_nothing_on_layout", "render_text_hello_world", "render_text_hello_world_with_layout", "hello_world_with_layout_false", - "partial_only", "partial_only_with_layout", - "accessing_params_in_template", + "partial_only", "accessing_params_in_template", "accessing_params_in_template_with_layout", "render_with_explicit_template", "render_with_explicit_string_template", @@ -1204,11 +1199,6 @@ class RenderTest < ActionController::TestCase assert_equal 'partial html', @response.body end - def test_partial_only_with_layout - get :partial_only_with_layout - assert_equal "only partial", @response.body - end - def test_render_to_string_partial get :render_to_string_with_partial assert_equal "only partial", assigns(:partial_only) -- cgit v1.2.3 From 1dacc19702f88a18a57615d1a5eeab3d0f5f9007 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Thu, 18 Mar 2010 17:32:53 -0700 Subject: Return a valid Rack response from bare ActionController::Metal --- .../test/controller/new_base/bare_metal_test.rb | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 actionpack/test/controller/new_base/bare_metal_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/new_base/bare_metal_test.rb b/actionpack/test/controller/new_base/bare_metal_test.rb new file mode 100644 index 0000000000..df4b39a103 --- /dev/null +++ b/actionpack/test/controller/new_base/bare_metal_test.rb @@ -0,0 +1,27 @@ +require "abstract_unit" + +module BareMetalTest + class BareController < ActionController::Metal + def index + self.response_body = "Hello world" + end + end + + class BareTest < ActiveSupport::TestCase + test "response body is a Rack-compatible response" do + status, headers, body = BareController.action(:index).call(Rack::MockRequest.env_for("/")) + assert_equal 200, status + string = "" + + body.each do |part| + assert part.is_a?(String), "Each part of the body must be a String" + string << part + end + + assert headers.is_a?(Hash), "Headers must be a Hash" + assert headers["Content-Type"], "Content-Type must exist" + + assert_equal "Hello world", string + end + end +end \ No newline at end of file -- cgit v1.2.3 From e629e21135d9d0e87c02e1106e489f943f19d2d9 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 17 Mar 2010 14:46:21 -0300 Subject: remove duplicated self.view_paths assingment on controller tests [#4206 state:commited] Signed-off-by: wycats --- actionpack/test/controller/new_base/render_action_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/new_base/render_action_test.rb b/actionpack/test/controller/new_base/render_action_test.rb index 0804d7b09d..d92e9c4bf2 100644 --- a/actionpack/test/controller/new_base/render_action_test.rb +++ b/actionpack/test/controller/new_base/render_action_test.rb @@ -117,7 +117,7 @@ module RenderActionWithApplicationLayout # # ==== Render actions with layouts ==== class BasicController < ::ApplicationController # Set the view path to an application view structure with layouts - self.view_paths = self.view_paths = [ActionView::FixtureResolver.new( + self.view_paths = [ActionView::FixtureResolver.new( "render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!", "render_action_with_application_layout/basic/hello.html.builder" => "xml.p 'Hello'", "layouts/application.html.erb" => "Hi <%= yield %> OK, Bye", @@ -202,7 +202,7 @@ end module RenderActionWithControllerLayout class BasicController < ActionController::Base - self.view_paths = self.view_paths = [ActionView::FixtureResolver.new( + self.view_paths = [ActionView::FixtureResolver.new( "render_action_with_controller_layout/basic/hello_world.html.erb" => "Hello World!", "layouts/render_action_with_controller_layout/basic.html.erb" => "With Controller Layout! <%= yield %> Bye" )] -- cgit v1.2.3 From f28d856cece877d1b2f306f54aeb12cce1db1023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 19 Mar 2010 17:20:15 +0100 Subject: Improve performance of the rendering stack by freezing formats as a sign that they shouldn't be further modified. --- .../test/controller/new_base/render_rjs_test.rb | 15 +++++++- actionpack/test/lib/fixture_template.rb | 4 +- actionpack/test/template/lookup_context_test.rb | 43 ++++++++++++++-------- 3 files changed, 43 insertions(+), 19 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/new_base/render_rjs_test.rb b/actionpack/test/controller/new_base/render_rjs_test.rb index 7c8f1cb869..b602b9f8e9 100644 --- a/actionpack/test/controller/new_base/render_rjs_test.rb +++ b/actionpack/test/controller/new_base/render_rjs_test.rb @@ -5,7 +5,7 @@ module RenderRjs self.view_paths = [ActionView::FixtureResolver.new( "render_rjs/basic/index.js.rjs" => "page[:customer].replace_html render(:partial => 'customer')", "render_rjs/basic/index_html.js.rjs" => "page[:customer].replace_html :partial => 'customer'", - "render_rjs/basic/index_no_js.js.rjs" => "page[:developer].replace_html render(:partial => 'developer')", + "render_rjs/basic/index_no_js.js.erb" => "<%= render(:partial => 'developer') %>", "render_rjs/basic/_customer.js.erb" => "JS Partial", "render_rjs/basic/_customer.html.erb" => "HTML Partial", "render_rjs/basic/_developer.html.erb" => "HTML Partial", @@ -18,6 +18,12 @@ module RenderRjs render end + def index_respond_to + respond_to do |format| + format.js { render :action => "index_no_js" } + end + end + def index_locale self.locale = :da end @@ -41,7 +47,12 @@ module RenderRjs test "rendering a partial in an RJS template should pick the HTML one if no JS is available" do get :index_no_js, "format" => "js" - assert_response("$(\"developer\").update(\"HTML Partial\");") + assert_response("HTML Partial") + end + + test "rendering a partial in an RJS template should pick the HTML one if no JS is available on respond_to" do + get :index_respond_to, "format" => "js" + assert_response("HTML Partial") end test "replacing an element with a partial in an RJS template should pick the HTML template over the JS one" do diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb index 74c0918a14..b49ccd39ca 100644 --- a/actionpack/test/lib/fixture_template.rb +++ b/actionpack/test/lib/fixture_template.rb @@ -9,7 +9,7 @@ module ActionView #:nodoc: private - def query(path, exts) + def query(path, exts, formats) query = Regexp.escape(path) exts.each do |ext| query << '(' << ext.map {|e| e && Regexp.escape(".#{e}") }.join('|') << '|)' @@ -17,7 +17,7 @@ module ActionView #:nodoc: templates = [] @hash.select { |k,v| k =~ /^#{query}$/ }.each do |path, source| - handler, format = extract_handler_and_format(path) + handler, format = extract_handler_and_format(path, formats) templates << Template.new(source, path, handler, :virtual_path => path, :format => format) end diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index 37526957bc..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 - locale = [I18n.locale] - 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], :locale => [:pt]], @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 - locale = [I18n.locale] - 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 @lookup_context.update_details(:locale => :pt) do - assert_equal Hash[:formats => formats, :locale => [:pt]], @lookup_context.details + assert_equal formats, @lookup_context.formats + assert_equal :pt, @lookup_context.locale end - assert_equal Hash[:formats => formats, :locale => [:en]], @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 -- cgit v1.2.3 From e1c030edd8362bceab287523fba7ec6a76aa9beb Mon Sep 17 00:00:00 2001 From: wycats Date: Fri, 19 Mar 2010 18:04:00 -0700 Subject: Fixed a bunch of tests that failed in 1.9 because they assumed that a Rack response was a String. --- actionpack/test/abstract_unit.rb | 13 +++++++++++++ actionpack/test/controller/caching_test.rb | 9 ++++++--- actionpack/test/controller/mime_responds_test.rb | 2 +- actionpack/test/controller/new_base/metal_test.rb | 6 ++++-- actionpack/test/controller/new_base/middleware_test.rb | 2 +- 5 files changed, 25 insertions(+), 7 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index a3486aa039..5b2ff3e871 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -44,6 +44,19 @@ ORIGINAL_LOCALES = I18n.available_locales.map {|locale| locale.to_s }.sort FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures') FIXTURES = Pathname.new(FIXTURE_LOAD_PATH) +module RackTestUtils + def body_to_string(body) + if body.respond_to?(:each) + str = "" + body.each {|s| str << s } + str + else + body + end + end + extend self +end + module SetupOnce extend ActiveSupport::Concern diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 5157454be0..f0ad652d50 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -285,6 +285,8 @@ class ActionCacheTest < ActionController::TestCase assert_not_equal cached_time, @response.body end + include RackTestUtils + def test_action_cache_with_layout get :with_layout cached_time = content_to_cache @@ -294,8 +296,8 @@ class ActionCacheTest < ActionController::TestCase get :with_layout assert_not_equal cached_time, @response.body - - assert_equal @response.body, read_fragment('hostname.com/action_caching_test/with_layout') + body = body_to_string(read_fragment('hostname.com/action_caching_test/with_layout')) + assert_equal @response.body, body end def test_action_cache_with_layout_and_layout_cache_false @@ -308,7 +310,8 @@ class ActionCacheTest < ActionController::TestCase get :layout_false assert_not_equal cached_time, @response.body - assert_equal cached_time, read_fragment('hostname.com/action_caching_test/layout_false') + body = body_to_string(read_fragment('hostname.com/action_caching_test/layout_false')) + assert_equal cached_time, body end def test_action_cache_conditional_options diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 5c1eaf453c..53cd3f0801 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -765,7 +765,7 @@ class RespondWithControllerTest < ActionController::TestCase Customer.any_instance.stubs(:errors).returns(errors) post :using_resource_with_action - assert_equal "foo - #{[:html].to_s}", @controller.response_body + assert_equal "foo - #{[:html].to_s}", @controller.response.body end def test_respond_as_responder_entry_point diff --git a/actionpack/test/controller/new_base/metal_test.rb b/actionpack/test/controller/new_base/metal_test.rb index ab61fd98ee..45a6619eb4 100644 --- a/actionpack/test/controller/new_base/metal_test.rb +++ b/actionpack/test/controller/new_base/metal_test.rb @@ -18,6 +18,8 @@ module MetalTest end class TestMiddleware < ActiveSupport::TestCase + include RackTestUtils + def setup @app = Rack::Builder.new do use MetalTest::MetalMiddleware @@ -29,14 +31,14 @@ module MetalTest env = Rack::MockRequest.env_for("/authed") response = @app.call(env) - assert_equal "Hello World", response[2] + assert_equal "Hello World", body_to_string(response[2]) end test "it can return a response using the normal AC::Metal techniques" do env = Rack::MockRequest.env_for("/") response = @app.call(env) - assert_equal "Not authed!", response[2] + assert_equal "Not authed!", body_to_string(response[2]) assert_equal 401, response[0] end end diff --git a/actionpack/test/controller/new_base/middleware_test.rb b/actionpack/test/controller/new_base/middleware_test.rb index ada0215b1a..65942ebc15 100644 --- a/actionpack/test/controller/new_base/middleware_test.rb +++ b/actionpack/test/controller/new_base/middleware_test.rb @@ -44,7 +44,7 @@ module MiddlewareTest test "middleware that is 'use'd is called as part of the Rack application" do result = @app.call(env_for("/")) - assert_equal "Hello World", result[2] + assert_equal "Hello World", RackTestUtils.body_to_string(result[2]) assert_equal "Success", result[1]["Middleware-Test"] end -- cgit v1.2.3 From 4998e097cc597f26cbe292552bcf5608b87cb1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 22 Mar 2010 21:03:43 +0100 Subject: Make router shortcuts more polite to URLs starting with a leading slash. --- actionpack/test/dispatch/routing_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index e4d83fa0a4..e0500af29d 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -34,6 +34,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest match 'account/login', :to => redirect("/login") match 'account/overview' + match '/account/nested/overview' match 'account/modulo/:name', :to => redirect("/%{name}s") match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" } @@ -121,6 +122,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest match 'articles/:year/:month/:day/:title', :to => "articles#show", :as => :article namespace :account do + match 'shorthand' match 'description', :to => "account#description", :as => "description" resource :subscription, :credit, :credit_card @@ -655,6 +657,22 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest end end + def test_convention_match_inside_namespace + with_test_routes do + assert_equal '/account/shorthand', account_shorthand_path + get '/account/shorthand' + assert_equal 'account#shorthand', @response.body + end + end + + def test_convention_match_nested_and_with_leading_slash + with_test_routes do + assert_equal '/account/nested/overview', account_nested_overview_path + get '/account/nested/overview' + assert_equal 'account/nested#overview', @response.body + end + end + def test_redirect_with_complete_url with_test_routes do get '/account/google' -- cgit v1.2.3 From 75904c566e3ea475045450ba8fb1a74070a94fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20de=20P=C3=A1dua?= Date: Sat, 20 Mar 2010 17:37:38 -0300 Subject: Adds number_to_human and several improvements in NumberHelper. [#4239 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../test/template/number_helper_i18n_test.rb | 128 +++++++++------- actionpack/test/template/number_helper_test.rb | 170 ++++++++++++++++++--- 2 files changed, 228 insertions(+), 70 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/number_helper_i18n_test.rb b/actionpack/test/template/number_helper_i18n_test.rb index bf5b81292f..07a0e2792c 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_unsignificant_zeros => false }, + :currency => { :format => { :unit => '&$', :format => '%u - %n', :precision => 2 } }, + :human => { + :format => { + :precision => 2, + :significant => true, + :strip_unsignificant_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_unsignificant_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_unsignificant_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..c90d89fb61 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)) @@ -43,7 +52,7 @@ 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_equal("$x", number_to_currency("x")) assert_nil number_to_currency(nil) end @@ -55,6 +64,7 @@ class NumberHelperTest < ActionView::TestCase 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_unsignificant_zeros => true)) assert_equal("1.000,000%", number_to_percentage(1000, :delimiter => '.', :separator => ',')) assert_nil number_to_percentage(nil) end @@ -81,6 +91,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)) @@ -93,6 +113,7 @@ class NumberHelperTest < ActionView::TestCase assert_equal("1234567892", number_with_precision(1234567891.50, :precision => 0)) # Return non-numeric params unchanged. + assert_equal("x.", number_with_precision("x.")) assert_equal("x", number_with_precision("x")) assert_nil number_with_precision(nil) end @@ -102,48 +123,159 @@ 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_unsignificant_zeros + assert_equal "9775.43", number_with_precision(9775.43, :precision => 4, :strip_unsignificant_zeros => true ) + assert_equal "9775.2", number_with_precision(9775.2, :precision => 6, :significant => true, :strip_unsignificant_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 + + # Return non-numeric params unchanged. + assert_equal "x", number_to_human_size('x') 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_unsignificant_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_unsignificant_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 + + # Return non-numeric params unchanged. + assert_equal "x", number_to_human('x') + assert_nil number_to_human(nil) + 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 + end -- cgit v1.2.3 From a4090bca6a837a187ea0698c25e5d66c89409667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20de=20P=C3=A1dua?= Date: Mon, 22 Mar 2010 16:19:30 -0300 Subject: NumberHelper methods should now return html_safe strings (when the inputs are valid numbers or are html_safe). Also adds :raise => true (used internaly) to make the number helpers throw InvalidNumberError when the given number is invalid. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/test/template/number_helper_test.rb | 143 +++++++++++++++++++++---- 1 file changed, 121 insertions(+), 22 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index c90d89fb61..6adbe9c098 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -40,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 @@ -52,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.")) - assert_equal("$x", number_to_currency("x")) - assert_nil number_to_currency(nil) end def test_number_to_percentage @@ -63,10 +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_unsignificant_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 @@ -80,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 @@ -111,11 +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_equal("x", number_with_precision("x")) - assert_nil number_with_precision(nil) end def test_number_with_precision_with_custom_delimiter_and_separator @@ -183,10 +169,6 @@ class NumberHelperTest < ActionView::TestCase 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) - - # Return non-numeric params unchanged. - assert_equal "x", number_to_human_size('x') - assert_nil number_to_human_size(nil) end def test_number_to_human_size_with_options_hash @@ -234,10 +216,6 @@ class NumberHelperTest < ActionView::TestCase 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 - - # Return non-numeric params unchanged. - assert_equal "x", number_to_human('x') - assert_nil number_to_human(nil) end def test_number_to_human_with_custom_units @@ -278,4 +256,125 @@ class NumberHelperTest < ActionView::TestCase 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("").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("").html_safe? + assert number_to_human_size("asdf".html_safe).html_safe? + + assert number_with_precision(1, :strip_unsignificant_zeros => false).html_safe? + assert number_with_precision(1, :strip_unsignificant_zeros => true).html_safe? + assert !number_with_precision("").html_safe? + assert number_with_precision("asdf".html_safe).html_safe? + + assert number_to_currency(1).html_safe? + assert !number_to_currency("").html_safe? + assert number_to_currency("asdf".html_safe).html_safe? + + assert number_to_percentage(1).html_safe? + assert !number_to_percentage("").html_safe? + assert number_to_percentage("asdf".html_safe).html_safe? + + assert number_to_phone(1).html_safe? + assert !number_to_phone("").html_safe? + assert number_to_phone("asdf".html_safe).html_safe? + + assert number_with_delimiter(1).html_safe? + assert !number_with_delimiter("").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 -- cgit v1.2.3 From 13bb4a6e68f3aa5b6e8d0a649f8a105819014974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 22 Mar 2010 23:57:06 +0100 Subject: Current url helpers become actions in controller. Added a failing test case for it. --- actionpack/test/controller/base_test.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index f047e7da30..8b935097da 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -124,11 +124,11 @@ class ControllerInstanceTests < Test::Unit::TestCase def test_action_methods @empty_controllers.each do |c| - assert_equal Set.new, c.class.__send__(:action_methods), "#{c.controller_path} should be empty!" + assert_equal Set.new, c.class.action_methods, "#{c.controller_path} should be empty!" end @non_empty_controllers.each do |c| - assert_equal Set.new(%w(public_action)), c.class.__send__(:action_methods), "#{c.controller_path} should not be empty!" + assert_equal Set.new(%w(public_action)), c.class.action_methods, "#{c.controller_path} should not be empty!" end end @@ -191,7 +191,7 @@ class UrlOptionsTest < ActionController::TestCase def test_url_options_override with_routing do |set| - set.draw do |map| + set.draw do match 'from_view', :to => 'url_options#from_view', :as => :from_view match ':controller/:action' end @@ -202,7 +202,18 @@ class UrlOptionsTest < ActionController::TestCase assert_equal 'http://www.override.com/from_view?locale=en', @controller.send(:from_view_url) assert_equal 'http://www.override.com/default_url_options/new?locale=en', @controller.url_for(:controller => 'default_url_options') end - end + end + + def test_url_helpers_does_not_become_actions + with_routing do |set| + set.draw do + match "account/overview" + end + + @controller.class.send(:include, set.url_helpers) + assert !@controller.class.action_methods.include?("account_overview_path") + end + end end class DefaultUrlOptionsTest < ActionController::TestCase @@ -216,7 +227,7 @@ class DefaultUrlOptionsTest < ActionController::TestCase def test_default_url_options_override with_routing do |set| - set.draw do |map| + set.draw do match 'from_view', :to => 'default_url_options#from_view', :as => :from_view match ':controller/:action' end @@ -231,7 +242,7 @@ class DefaultUrlOptionsTest < ActionController::TestCase def test_default_url_options_are_used_in_non_positional_parameters with_routing do |set| - set.draw do |map| + set.draw do scope("/:locale") do resources :descriptions end -- cgit v1.2.3 From c53f77f3be76b37e9692db0e3fd0e8c16d954e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 23 Mar 2010 00:26:12 +0100 Subject: Rename unsignificant to insignificant. --- actionpack/test/template/number_helper_i18n_test.rb | 8 ++++---- actionpack/test/template/number_helper_test.rb | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/number_helper_i18n_test.rb b/actionpack/test/template/number_helper_i18n_test.rb index 07a0e2792c..f730a0d7f5 100644 --- a/actionpack/test/template/number_helper_i18n_test.rb +++ b/actionpack/test/template/number_helper_i18n_test.rb @@ -6,13 +6,13 @@ class NumberHelperTest < ActionView::TestCase def setup I18n.backend.store_translations 'ts', :number => { - :format => { :precision => 3, :delimiter => ',', :separator => '.', :significant => false, :strip_unsignificant_zeros => false }, + :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_unsignificant_zeros => true + :strip_insignificant_zeros => true }, :storage_units => { :format => "%n %u", @@ -35,7 +35,7 @@ class NumberHelperTest < ActionView::TestCase } } }, - :percentage => { :format => {:delimiter => '', :precision => 2, :strip_unsignificant_zeros => true} }, + :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"} @@ -60,7 +60,7 @@ class NumberHelperTest < ActionView::TestCase end def test_number_to_percentage - # to see if strip_unsignificant_zeros is true + # 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')) diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index 6adbe9c098..50c57a5588 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -58,7 +58,7 @@ 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("123.4%", number_to_percentage(123.400, :precision => 3, :strip_unsignificant_zeros => true)) + 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 => ',')) end @@ -124,9 +124,9 @@ class NumberHelperTest < ActionView::TestCase assert_equal "5.392900", number_with_precision(5.3929, :precision => 7, :significant => true ) end - def test_number_with_precision_with_strip_unsignificant_zeros - assert_equal "9775.43", number_with_precision(9775.43, :precision => 4, :strip_unsignificant_zeros => true ) - assert_equal "9775.2", number_with_precision(9775.2, :precision => 6, :significant => true, :strip_unsignificant_zeros => true ) + 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 @@ -181,7 +181,7 @@ class NumberHelperTest < ActionView::TestCase 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_unsignificant_zeros => false) + 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 @@ -212,7 +212,7 @@ class NumberHelperTest < ActionView::TestCase 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_unsignificant_zeros => false) + 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 @@ -289,8 +289,8 @@ class NumberHelperTest < ActionView::TestCase assert !number_to_human_size("").html_safe? assert number_to_human_size("asdf".html_safe).html_safe? - assert number_with_precision(1, :strip_unsignificant_zeros => false).html_safe? - assert number_with_precision(1, :strip_unsignificant_zeros => true).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("").html_safe? assert number_with_precision("asdf".html_safe).html_safe? -- cgit v1.2.3 From 15c31c7639b4329eba341bbe894abc9b79edc5c3 Mon Sep 17 00:00:00 2001 From: wycats Date: Mon, 22 Mar 2010 17:04:56 -0700 Subject: open_session can just return the a dup of the current context. At this point, its entire purpose in the open_session {} case was to delegate back to the IntegrationTest anyway. --- actionpack/test/controller/integration_test.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 2180466ca7..c9782856bd 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -173,14 +173,12 @@ class IntegrationTestTest < Test::Unit::TestCase end def test_opens_new_session - @test.class.expects(:fixture_table_names).times(2).returns(['foo']) - session1 = @test.open_session { |sess| } session2 = @test.open_session # implicit session - assert_kind_of ::ActionController::Integration::Session, session1 - assert_kind_of ::ActionController::Integration::Session, session2 - assert_not_equal session1, session2 + assert session1.respond_to?(:assert_template), "open_session makes assert_template available" + assert session2.respond_to?(:assert_template), "open_session makes assert_template available" + assert !session1.equal?(session2) end # RSpec mixes Matchers (which has a #method_missing) into -- cgit v1.2.3 From b2c2b0ce459a215d389f3ab8bb9e33718460cf51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 26 Mar 2010 15:41:05 +0100 Subject: Rails router automatically calculated for you the controller and named routes in the following scenarios: match "home/about" #=> maps to home#about with named route home_about_path match "about" #=> does not work because it cannot guess the controller match "about" => "home#about" #=> maps to home#about with named route home_about_path match "home/about", :as => "about" #=> maps to home#about with named route about_path --- actionpack/test/dispatch/routing_test.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index e0500af29d..87a46feec7 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -18,10 +18,9 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest default_url_options :host => "rubyonrails.org" controller :sessions do - get 'login' => :new, :as => :login + get 'login' => :new post 'login' => :create - - delete 'logout' => :destroy, :as => :logout + delete 'logout' => :destroy end resource :session do @@ -35,6 +34,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest match 'account/overview' match '/account/nested/overview' + match 'sign_in' => "sessions#new" match 'account/modulo/:name', :to => redirect("/%{name}s") match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" } @@ -673,6 +673,14 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest end end + def test_convention_with_explicit_end + with_test_routes do + get '/sign_in' + assert_equal 'sessions#new', @response.body + assert_equal '/sign_in', sign_in_path + end + end + def test_redirect_with_complete_url with_test_routes do get '/account/google' -- cgit v1.2.3 From 3d746fcdb584767c476408f395320e934fd5383e Mon Sep 17 00:00:00 2001 From: Andrew White Date: Fri, 26 Mar 2010 12:16:25 +0000 Subject: Add parameter defaults support to new routing DSL [#4265 state:resolved] Signed-off-by: wycats --- actionpack/test/dispatch/routing_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 87a46feec7..cbe9b5488c 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -121,6 +121,13 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest # misc match 'articles/:year/:month/:day/:title', :to => "articles#show", :as => :article + # default params + match 'inline_pages/(:id)', :to => 'pages#show', :id => 'home' + match 'default_pages/(:id)', :to => 'pages#show', :defaults => { :id => 'home' } + defaults :id => 'home' do + match 'scoped_pages/(:id)', :to => 'pages#show' + end + namespace :account do match 'shorthand' match 'description', :to => "account#description", :as => "description" @@ -769,6 +776,19 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest end end + def test_default_params + with_test_routes do + get '/inline_pages' + assert_equal 'home', @request.params[:id] + + get '/default_pages' + assert_equal 'home', @request.params[:id] + + get '/scoped_pages' + assert_equal 'home', @request.params[:id] + end + end + private def with_test_routes yield -- cgit v1.2.3 From 167017f65558a4461aaf8dc26a6f329a283366c2 Mon Sep 17 00:00:00 2001 From: Rizwan Reza Date: Fri, 26 Mar 2010 17:30:13 +0430 Subject: Tests method_missing to raise NameError. [#2522 state:resolved] Signed-off-by: wycats --- actionpack/test/controller/base_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index 8b935097da..49f79681f6 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -56,6 +56,16 @@ protected end end +class AnotherMethodMissingController < ActionController::Base + cattr_accessor :_exception + rescue_from Exception, :with => :_exception= + + protected + def method_missing(*attrs, &block) + super + end +end + class DefaultUrlOptionsController < ActionController::Base def from_view render :inline => "<%= #{params[:route]} %>" @@ -173,6 +183,12 @@ class PerformActionTest < ActionController::TestCase assert_equal 'method_missing', @response.body end + def test_method_missing_should_recieve_symbol + use_controller AnotherMethodMissingController + get :some_action + assert_kind_of NameError, @controller._exception + end + def test_get_on_hidden_should_fail use_controller NonEmptyController assert_raise(ActionController::UnknownAction) { get :hidden_action } -- cgit v1.2.3 From 39c35ff04b4478968b8994bc5fad74f5840eb64c Mon Sep 17 00:00:00 2001 From: Andrew White Date: Thu, 25 Mar 2010 13:25:56 +0000 Subject: Fix named routes for member actions of singleton resources [#4266 state:resolved] Signed-off-by: wycats --- actionpack/test/dispatch/routing_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index cbe9b5488c..c4e71a8689 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -25,6 +25,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest resource :session do get :create + post :reset resource :info end @@ -247,6 +248,10 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest get '/session/edit' assert_equal 'sessions#edit', @response.body assert_equal '/session/edit', edit_session_path + + post '/session/reset' + assert_equal 'sessions#reset', @response.body + assert_equal '/session/reset', reset_session_path end end -- cgit v1.2.3 From 334983eca042b5016d3d79d7ed5761b60ba871ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Rodri=CC=81guez=20Troitin=CC=83o?= Date: Mon, 15 Mar 2010 19:11:46 +0100 Subject: Recovers error_messages for ActiveRecordInstanceTag. [#4078 state:resolved] Signed-off-by: wycats --- actionpack/test/template/active_model_helper_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/template/active_model_helper_test.rb b/actionpack/test/template/active_model_helper_test.rb index 371aa53c68..71c7d5b621 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| + %(
#{html_tag} #{[instance.error_message].join(', ')}
).html_safe + end + + assert_dom_equal( + %(
can't be empty
), + 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( %(


\n


), -- cgit v1.2.3 From 0a352056a4055c170e62a521267f5bc51c6659a0 Mon Sep 17 00:00:00 2001 From: wycats Date: Sat, 27 Mar 2010 03:09:33 -0700 Subject: Fixes a bug where error_messages_for was returning an empty div [#4048 state:resolved] (ht: Geoff Garside) --- actionpack/test/template/active_model_helper_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/template/active_model_helper_test.rb b/actionpack/test/template/active_model_helper_test.rb index 71c7d5b621..7a665b00bc 100644 --- a/actionpack/test/template/active_model_helper_test.rb +++ b/actionpack/test/template/active_model_helper_test.rb @@ -266,6 +266,10 @@ class ActiveModelHelperTest < ActionView::TestCase assert_dom_equal "
beforecan't be emptyafter
", 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 %(

2 errors prohibited this post from being saved

There were problems with the following fields:

  • Author name can't be empty
  • User email can't be empty
), error_messages_for("post", "user") -- cgit v1.2.3