From 5b6eb1d58b48fada298215b2cccda89f993890c3 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 6 Aug 2016 19:35:13 +0200 Subject: modernizes hash syntax in actionpack --- actionpack/test/controller/url_for_test.rb | 138 ++++++++++++++--------------- 1 file changed, 69 insertions(+), 69 deletions(-) (limited to 'actionpack/test/controller/url_for_test.rb') diff --git a/actionpack/test/controller/url_for_test.rb b/actionpack/test/controller/url_for_test.rb index 7c8e5675b6..6b0dd0ac88 100644 --- a/actionpack/test/controller/url_for_test.rb +++ b/actionpack/test/controller/url_for_test.rb @@ -21,17 +21,17 @@ module AbstractController klass = Class.new { include ActionDispatch::Routing::RouteSet.new.tap { |r| r.draw { - get "/foo/(:bar/(:baz))/:zot", :as => "fun", - :controller => :articles, - :action => :index + get "/foo/(:bar/(:baz))/:zot", as: "fun", + controller: :articles, + action: :index } }.url_helpers self.default_url_options[:host] = "example.com" } - path = klass.new.fun_path({:controller => :articles, - :baz => "baz", - :zot => "zot"}) + path = klass.new.fun_path({controller: :articles, + baz: "baz", + zot: "zot"}) # :bar key isn't provided assert_equal "/foo/zot", path end @@ -50,13 +50,13 @@ module AbstractController def test_exception_is_thrown_without_host assert_raise ArgumentError do - W.new.url_for :controller => "c", :action => "a", :id => "i" + W.new.url_for controller: "c", action: "a", id: "i" end end def test_anchor assert_equal("/c/a#anchor", - W.new.url_for(:only_path => true, :controller => "c", :action => "a", :anchor => "anchor") + W.new.url_for(only_path: true, controller: "c", action: "a", anchor: "anchor") ) end @@ -76,40 +76,40 @@ module AbstractController def test_anchor_should_call_to_param assert_equal("/c/a#anchor", - W.new.url_for(:only_path => true, :controller => "c", :action => "a", :anchor => Struct.new(:to_param).new("anchor")) + W.new.url_for(only_path: true, controller: "c", action: "a", anchor: Struct.new(:to_param).new("anchor")) ) end def test_anchor_should_escape_unsafe_pchar assert_equal("/c/a#%23anchor", - W.new.url_for(:only_path => true, :controller => "c", :action => "a", :anchor => Struct.new(:to_param).new("#anchor")) + W.new.url_for(only_path: true, controller: "c", action: "a", anchor: Struct.new(:to_param).new("#anchor")) ) end def test_anchor_should_not_escape_safe_pchar assert_equal("/c/a#name=user&email=user@domain.com", - W.new.url_for(:only_path => true, :controller => "c", :action => "a", :anchor => Struct.new(:to_param).new("name=user&email=user@domain.com")) + W.new.url_for(only_path: true, controller: "c", action: "a", anchor: Struct.new(:to_param).new("name=user&email=user@domain.com")) ) end def test_default_host add_host! assert_equal("http://www.basecamphq.com/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i") + W.new.url_for(controller: "c", action: "a", id: "i") ) end def test_host_may_be_overridden add_host! assert_equal("http://37signals.basecamphq.com/c/a/i", - W.new.url_for(:host => "37signals.basecamphq.com", :controller => "c", :action => "a", :id => "i") + W.new.url_for(host: "37signals.basecamphq.com", controller: "c", action: "a", id: "i") ) end def test_subdomain_may_be_changed add_host! assert_equal("http://api.basecamphq.com/c/a/i", - W.new.url_for(:subdomain => "api", :controller => "c", :action => "a", :id => "i") + W.new.url_for(subdomain: "api", controller: "c", action: "a", id: "i") ) end @@ -117,56 +117,56 @@ module AbstractController model = Class.new { def self.to_param; "api"; end } add_host! assert_equal("http://api.basecamphq.com/c/a/i", - W.new.url_for(:subdomain => model, :controller => "c", :action => "a", :id => "i") + W.new.url_for(subdomain: model, controller: "c", action: "a", id: "i") ) end def test_subdomain_may_be_removed add_host! assert_equal("http://basecamphq.com/c/a/i", - W.new.url_for(:subdomain => false, :controller => "c", :action => "a", :id => "i") + W.new.url_for(subdomain: false, controller: "c", action: "a", id: "i") ) end def test_subdomain_may_be_removed_with_blank_string W.default_url_options[:host] = "api.basecamphq.com" assert_equal("http://basecamphq.com/c/a/i", - W.new.url_for(:subdomain => "", :controller => "c", :action => "a", :id => "i") + W.new.url_for(subdomain: "", controller: "c", action: "a", id: "i") ) end def test_multiple_subdomains_may_be_removed W.default_url_options[:host] = "mobile.www.api.basecamphq.com" assert_equal("http://basecamphq.com/c/a/i", - W.new.url_for(:subdomain => false, :controller => "c", :action => "a", :id => "i") + W.new.url_for(subdomain: false, controller: "c", action: "a", id: "i") ) end def test_subdomain_may_be_accepted_with_numeric_host add_numeric_host! assert_equal("http://127.0.0.1/c/a/i", - W.new.url_for(:subdomain => "api", :controller => "c", :action => "a", :id => "i") + W.new.url_for(subdomain: "api", controller: "c", action: "a", id: "i") ) end def test_domain_may_be_changed add_host! assert_equal("http://www.37signals.com/c/a/i", - W.new.url_for(:domain => "37signals.com", :controller => "c", :action => "a", :id => "i") + W.new.url_for(domain: "37signals.com", controller: "c", action: "a", id: "i") ) end def test_tld_length_may_be_changed add_host! assert_equal("http://mobile.www.basecamphq.com/c/a/i", - W.new.url_for(:subdomain => "mobile", :tld_length => 2, :controller => "c", :action => "a", :id => "i") + W.new.url_for(subdomain: "mobile", tld_length: 2, controller: "c", action: "a", id: "i") ) end def test_port add_host! assert_equal("http://www.basecamphq.com:3000/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :port => 3000) + W.new.url_for(controller: "c", action: "a", id: "i", port: 3000) ) end @@ -174,37 +174,37 @@ module AbstractController add_host! add_port! assert_equal("http://www.basecamphq.com:3000/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i") + W.new.url_for(controller: "c", action: "a", id: "i") ) end def test_protocol add_host! assert_equal("https://www.basecamphq.com/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => "https") + W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https") ) end def test_protocol_with_and_without_separators add_host! assert_equal("https://www.basecamphq.com/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => "https") + W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https") ) assert_equal("https://www.basecamphq.com/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => "https:") + W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https:") ) assert_equal("https://www.basecamphq.com/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => "https://") + W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https://") ) end def test_without_protocol add_host! assert_equal("//www.basecamphq.com/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => "//") + W.new.url_for(controller: "c", action: "a", id: "i", protocol: "//") ) assert_equal("//www.basecamphq.com/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => false) + W.new.url_for(controller: "c", action: "a", id: "i", protocol: false) ) end @@ -213,42 +213,42 @@ module AbstractController add_port! assert_equal("//www.basecamphq.com:3000/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => "//") + W.new.url_for(controller: "c", action: "a", id: "i", protocol: "//") ) assert_equal("//www.basecamphq.com:3000/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => false) + W.new.url_for(controller: "c", action: "a", id: "i", protocol: false) ) end def test_trailing_slash add_host! - options = {:controller => "foo", :trailing_slash => true, :action => "bar", :id => "33"} + options = {controller: "foo", trailing_slash: true, action: "bar", id: "33"} assert_equal("http://www.basecamphq.com/foo/bar/33/", W.new.url_for(options) ) end def test_trailing_slash_with_protocol add_host! - options = { :trailing_slash => true,:protocol => "https", :controller => "foo", :action => "bar", :id => "33"} + options = { trailing_slash: true,protocol: "https", controller: "foo", action: "bar", id: "33"} assert_equal("https://www.basecamphq.com/foo/bar/33/", W.new.url_for(options) ) - assert_equal "https://www.basecamphq.com/foo/bar/33/?query=string", W.new.url_for(options.merge({:query => "string"})) + assert_equal "https://www.basecamphq.com/foo/bar/33/?query=string", W.new.url_for(options.merge({query: "string"})) end def test_trailing_slash_with_only_path - options = {:controller => "foo", :trailing_slash => true} - assert_equal "/foo/", W.new.url_for(options.merge({:only_path => true})) - options.update({:action => "bar", :id => "33"}) - assert_equal "/foo/bar/33/", W.new.url_for(options.merge({:only_path => true})) - assert_equal "/foo/bar/33/?query=string", W.new.url_for(options.merge({:query => "string",:only_path => true})) + options = {controller: "foo", trailing_slash: true} + assert_equal "/foo/", W.new.url_for(options.merge({only_path: true})) + options.update({action: "bar", id: "33"}) + assert_equal "/foo/bar/33/", W.new.url_for(options.merge({only_path: true})) + assert_equal "/foo/bar/33/?query=string", W.new.url_for(options.merge({query: "string",only_path: true})) end def test_trailing_slash_with_anchor - options = {:trailing_slash => true, :controller => "foo", :action => "bar", :id => "33", :only_path => true, :anchor=> "chapter7"} + options = {trailing_slash: true, controller: "foo", action: "bar", id: "33", only_path: true, anchor: "chapter7"} assert_equal "/foo/bar/33/#chapter7", W.new.url_for(options) - assert_equal "/foo/bar/33/?query=string#chapter7", W.new.url_for(options.merge({:query => "string"})) + assert_equal "/foo/bar/33/?query=string#chapter7", W.new.url_for(options.merge({query: "string"})) end def test_trailing_slash_with_params - url = W.new.url_for(:trailing_slash => true, :only_path => true, :controller => "cont", :action => "act", :p1 => "cafe", :p2 => "link") + url = W.new.url_for(trailing_slash: true, only_path: true, controller: "cont", action: "act", p1: "cafe", p2: "link") params = extract_params(url) assert_equal({p1: "cafe"}.to_query, params[0]) assert_equal({p2: "link"}.to_query, params[1]) @@ -257,7 +257,7 @@ module AbstractController def test_relative_url_root_is_respected add_host! assert_equal("https://www.basecamphq.com/subdir/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => "https", :script_name => "/subdir") + W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https", script_name: "/subdir") ) end @@ -271,15 +271,15 @@ module AbstractController } add_host!(w) assert_equal("https://www.basecamphq.com/subdir/c/a/i", - w.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => "https") + w.new.url_for(controller: "c", action: "a", id: "i", protocol: "https") ) end def test_named_routes with_routing do |set| set.draw do - get "this/is/verbose", :to => "home#index", :as => :no_args - get "home/sweet/home/:user", :to => "home#index", :as => :home + get "this/is/verbose", to: "home#index", as: :no_args + get "home/sweet/home/:user", to: "home#index", as: :home end # We need to create a new class in order to install the new named route. @@ -288,39 +288,39 @@ module AbstractController controller = kls.new assert controller.respond_to?(:home_url) assert_equal "http://www.basecamphq.com/home/sweet/home/again", - controller.send(:home_url, :host => "www.basecamphq.com", :user => "again") + controller.send(:home_url, host: "www.basecamphq.com", user: "again") - assert_equal("/home/sweet/home/alabama", controller.send(:home_path, :user => "alabama", :host => "unused")) - assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller.send(:home_url, :user => "alabama", :host => "www.basecamphq.com")) - assert_equal("http://www.basecamphq.com/this/is/verbose", controller.send(:no_args_url, :host=>"www.basecamphq.com")) + assert_equal("/home/sweet/home/alabama", controller.send(:home_path, user: "alabama", host: "unused")) + assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller.send(:home_url, user: "alabama", host: "www.basecamphq.com")) + assert_equal("http://www.basecamphq.com/this/is/verbose", controller.send(:no_args_url, host: "www.basecamphq.com")) end end def test_relative_url_root_is_respected_for_named_routes with_routing do |set| set.draw do - get "/home/sweet/home/:user", :to => "home#index", :as => :home + get "/home/sweet/home/:user", to: "home#index", as: :home end kls = Class.new { include set.url_helpers } controller = kls.new assert_equal "http://www.basecamphq.com/subdir/home/sweet/home/again", - controller.send(:home_url, :host => "www.basecamphq.com", :user => "again", :script_name => "/subdir") + controller.send(:home_url, host: "www.basecamphq.com", user: "again", script_name: "/subdir") end end def test_using_nil_script_name_properly_concats_with_original_script_name add_host! assert_equal("https://www.basecamphq.com/subdir/c/a/i", - W.new.url_for(:controller => "c", :action => "a", :id => "i", :protocol => "https", :script_name => nil, :original_script_name => "/subdir") + W.new.url_for(controller: "c", action: "a", id: "i", protocol: "https", script_name: nil, original_script_name: "/subdir") ) end def test_only_path with_routing do |set| set.draw do - get "home/sweet/home/:user", :to => "home#index", :as => :home + get "home/sweet/home/:user", to: "home#index", as: :home ActiveSupport::Deprecation.silence do get ":controller/:action/:id" @@ -332,42 +332,42 @@ module AbstractController controller = kls.new assert_respond_to controller, :home_url assert_equal "/brave/new/world", - controller.url_for(:controller => "brave", :action => "new", :id => "world", :only_path => true) + controller.url_for(controller: "brave", action: "new", id: "world", only_path: true) - assert_equal("/home/sweet/home/alabama", controller.home_path(:user => "alabama", :host => "unused")) + assert_equal("/home/sweet/home/alabama", controller.home_path(user: "alabama", host: "unused")) assert_equal("/home/sweet/home/alabama", controller.home_path("alabama")) end end def test_one_parameter assert_equal("/c/a?param=val", - W.new.url_for(:only_path => true, :controller => "c", :action => "a", :param => "val") + W.new.url_for(only_path: true, controller: "c", action: "a", param: "val") ) end def test_two_parameters - url = W.new.url_for(:only_path => true, :controller => "c", :action => "a", :p1 => "X1", :p2 => "Y2") + url = W.new.url_for(only_path: true, controller: "c", action: "a", p1: "X1", p2: "Y2") params = extract_params(url) assert_equal({p1: "X1"}.to_query, params[0]) assert_equal({p2: "Y2"}.to_query, params[1]) end def test_hash_parameter - url = W.new.url_for(:only_path => true, :controller => "c", :action => "a", :query => {:name => "Bob", :category => "prof"}) + url = W.new.url_for(only_path: true, controller: "c", action: "a", query: {name: "Bob", category: "prof"}) params = extract_params(url) assert_equal({"query[category]" => "prof"}.to_query, params[0]) assert_equal({"query[name]" => "Bob"}.to_query, params[1]) end def test_array_parameter - url = W.new.url_for(:only_path => true, :controller => "c", :action => "a", :query => ["Bob", "prof"]) + url = W.new.url_for(only_path: true, controller: "c", action: "a", query: ["Bob", "prof"]) params = extract_params(url) assert_equal({"query[]" => "Bob"}.to_query, params[0]) assert_equal({"query[]" => "prof"}.to_query, params[1]) end def test_hash_recursive_parameters - url = W.new.url_for(:only_path => true, :controller => "c", :action => "a", :query => {:person => {:name => "Bob", :position => "prof"}, :hobby => "piercing"}) + url = W.new.url_for(only_path: true, controller: "c", action: "a", query: {person: {name: "Bob", position: "prof"}, hobby: "piercing"}) params = extract_params(url) assert_equal({"query[hobby]" => "piercing"}.to_query, params[0]) assert_equal({"query[person][name]" => "Bob" }.to_query, params[1]) @@ -375,7 +375,7 @@ module AbstractController end def test_hash_recursive_and_array_parameters - url = W.new.url_for(:only_path => true, :controller => "c", :action => "a", :id => 101, :query => {:person => {:name => "Bob", :position => ["prof", "art director"]}, :hobby => "piercing"}) + url = W.new.url_for(only_path: true, controller: "c", action: "a", id: 101, query: {person: {name: "Bob", position: ["prof", "art director"]}, hobby: "piercing"}) assert_match(%r(^/c/a/101), url) params = extract_params(url) assert_equal({"query[hobby]" => "piercing" }.to_query, params[0]) @@ -387,19 +387,19 @@ module AbstractController def test_url_action_controller_parameters add_host! assert_raise(ArgumentError) do - W.new.url_for(ActionController::Parameters.new(:controller => "c", :action => "a", protocol: "javascript", f: "%0Aeval(name)")) + W.new.url_for(ActionController::Parameters.new(controller: "c", action: "a", protocol: "javascript", f: "%0Aeval(name)")) end end def test_path_generation_for_symbol_parameter_keys - assert_generates("/image", :controller=> :image) + assert_generates("/image", controller: :image) end def test_named_routes_with_nil_keys with_routing do |set| set.draw do - get "posts.:format", :to => "posts#index", :as => :posts - get "/", :to => "posts#index", :as => :main + get "posts.:format", to: "posts#index", as: :posts + get "/", to: "posts#index", as: :main end # We need to create a new class in order to install the new named route. @@ -407,7 +407,7 @@ module AbstractController kls.default_url_options[:host] = "www.basecamphq.com" controller = kls.new - params = {:action => :index, :controller => :posts, :format => :xml} + params = {action: :index, controller: :posts, format: :xml} assert_equal("http://www.basecamphq.com/posts.xml", controller.send(:url_for, params)) params[:format] = nil assert_equal("http://www.basecamphq.com/", controller.send(:url_for, params)) @@ -442,11 +442,11 @@ module AbstractController end def test_url_params_with_nil_to_param_are_not_in_url - assert_equal("/c/a", W.new.url_for(:only_path => true, :controller => "c", :action => "a", :id => Struct.new(:to_param).new(nil))) + assert_equal("/c/a", W.new.url_for(only_path: true, controller: "c", action: "a", id: Struct.new(:to_param).new(nil))) end def test_false_url_params_are_included_in_query - assert_equal("/c/a?show=false", W.new.url_for(:only_path => true, :controller => "c", :action => "a", :show => false)) + assert_equal("/c/a?show=false", W.new.url_for(only_path: true, controller: "c", action: "a", show: false)) end def test_url_generation_with_array_and_hash -- cgit v1.2.3