From 6a23bf0f4c33151e0cec0648e271dc6f5ab3f686 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 19 Aug 2014 19:32:51 -0700 Subject: Preparing for 4.2.0.beta1 release --- activesupport/lib/active_support/gem_version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/gem_version.rb b/activesupport/lib/active_support/gem_version.rb index 83a3bf7a5d..e026c4f899 100644 --- a/activesupport/lib/active_support/gem_version.rb +++ b/activesupport/lib/active_support/gem_version.rb @@ -8,7 +8,7 @@ module ActiveSupport MAJOR = 4 MINOR = 2 TINY = 0 - PRE = "alpha" + PRE = "beta1" STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") end -- cgit v1.2.3 From 141d864e0ec1098176141e6cb6aef3eaf07e830f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 14:58:36 -0700 Subject: Added yield to Object#presence --- activesupport/CHANGELOG.md | 6 ++++++ activesupport/lib/active_support/core_ext/object/blank.rb | 13 ++++++++++++- activesupport/test/core_ext/object/blank_test.rb | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index e0d154271f..6830360eba 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,9 @@ +* Added yield to Object#presence, so you can do this: + + person.presence { |p| p.name.first } || 'Nobody' + + *DHH* + * Fix the `ActiveSupport::Duration#instance_of?` method to return the right value with the class itself since it was previously delegated to the internal value. diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 38e43478df..164a3c47d0 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -39,9 +39,20 @@ class Object # # region = params[:state].presence || params[:country].presence || 'US' # + # You can also use this with a block that will be yielded if the object is present + # and the result of that block will then be returned + # + # person.presence { |p| p.name.first } || 'Nobody' + # # @return [Object] def presence - self if present? + if present? + if block_given? + yield self + else + self + end + end end end diff --git a/activesupport/test/core_ext/object/blank_test.rb b/activesupport/test/core_ext/object/blank_test.rb index 246bc7fa61..7b3f10b4da 100644 --- a/activesupport/test/core_ext/object/blank_test.rb +++ b/activesupport/test/core_ext/object/blank_test.rb @@ -33,4 +33,9 @@ class BlankTest < ActiveSupport::TestCase BLANK.each { |v| assert_equal nil, v.presence, "#{v.inspect}.presence should return nil" } NOT.each { |v| assert_equal v, v.presence, "#{v.inspect}.presence should return self" } end + + def test_presence_with_a_block + assert_equal "SALLY", "sally".presence(&:upcase) || "Nobody" + assert_equal "Nobody", nil.presence(&:upcase) || "Nobody" + end end -- cgit v1.2.3 From cc0d8fbec96aeb1664fe98d45929a236b18aec81 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 15:04:37 -0700 Subject: Update examples to show real worth --- activesupport/CHANGELOG.md | 11 ++++++++++- activesupport/lib/active_support/core_ext/object/blank.rb | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 6830360eba..b32c97bead 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,6 +1,15 @@ * Added yield to Object#presence, so you can do this: - person.presence { |p| p.name.first } || 'Nobody' + project.account.owner.presence { |p| p.name.first } || 'Nobody' + + instead of calling twice (which may incur double SQL calls): + + project.account.owner ? project.account.owner.name.first || 'Nobody' + + or assigning to local variable: + + owner = project.account.owner + owner ? owner.name.first || 'Nobody' *DHH* diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 164a3c47d0..893858fcd6 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -42,7 +42,7 @@ class Object # You can also use this with a block that will be yielded if the object is present # and the result of that block will then be returned # - # person.presence { |p| p.name.first } || 'Nobody' + # project.account.owner.presence { |p| p.name.first } || 'Nobody' # # @return [Object] def presence -- cgit v1.2.3 From 961945046848492ddc541700419cf553e7817c94 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 15:19:32 -0700 Subject: Use instance_eval on @tenderlove's suggestion :trollface: --- activesupport/CHANGELOG.md | 2 +- activesupport/lib/active_support/core_ext/object/blank.rb | 9 +++++---- activesupport/test/core_ext/object/blank_test.rb | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index b32c97bead..5894dab230 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,6 +1,6 @@ * Added yield to Object#presence, so you can do this: - project.account.owner.presence { |p| p.name.first } || 'Nobody' + project.account.owner.presence { name.first } || 'Nobody' instead of calling twice (which may incur double SQL calls): diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 893858fcd6..99fd22ff56 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -40,15 +40,16 @@ class Object # region = params[:state].presence || params[:country].presence || 'US' # # You can also use this with a block that will be yielded if the object is present - # and the result of that block will then be returned + # and the result of that block will then be returned. The block itself is run against + # the instance you're running #presence on (using instance_eval) # - # project.account.owner.presence { |p| p.name.first } || 'Nobody' + # project.account.owner.presence { name.first } || 'Nobody' # # @return [Object] - def presence + def presence(&block) if present? if block_given? - yield self + instance_eval(&block) else self end diff --git a/activesupport/test/core_ext/object/blank_test.rb b/activesupport/test/core_ext/object/blank_test.rb index 7b3f10b4da..34d10c6981 100644 --- a/activesupport/test/core_ext/object/blank_test.rb +++ b/activesupport/test/core_ext/object/blank_test.rb @@ -35,7 +35,7 @@ class BlankTest < ActiveSupport::TestCase end def test_presence_with_a_block - assert_equal "SALLY", "sally".presence(&:upcase) || "Nobody" - assert_equal "Nobody", nil.presence(&:upcase) || "Nobody" + assert_equal "SALLY", "sally".presence { upcase } || "Nobody" + assert_equal "Nobody", nil.presence { upcase } || "Nobody" end end -- cgit v1.2.3 From 39691ba2f5664aa83720fa3c2a1ca14937d29009 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 15:24:36 -0700 Subject: Clarify the origin of this great addition to Rails :trollface: :trollface :trollface: --- activesupport/test/core_ext/object/blank_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/test/core_ext/object/blank_test.rb b/activesupport/test/core_ext/object/blank_test.rb index 34d10c6981..4deac4b0aa 100644 --- a/activesupport/test/core_ext/object/blank_test.rb +++ b/activesupport/test/core_ext/object/blank_test.rb @@ -35,7 +35,7 @@ class BlankTest < ActiveSupport::TestCase end def test_presence_with_a_block - assert_equal "SALLY", "sally".presence { upcase } || "Nobody" + assert_equal "THIS WAS TENDERLOVE'S IDEA", "this was tenderlove's idea".presence { upcase } || "Nobody" assert_equal "Nobody", nil.presence { upcase } || "Nobody" end end -- cgit v1.2.3 From 5e51bdda59c9ba8e5faf86294e3e431bd45f1830 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 15:32:24 -0700 Subject: We tenderized the wrong method! Object#try already had the yield option, just needed some tenderloving instance_eval to fit the bill --- activesupport/CHANGELOG.md | 13 ++++--------- .../lib/active_support/core_ext/object/blank.rb | 16 ++-------------- activesupport/lib/active_support/core_ext/object/try.rb | 11 ++++++++++- activesupport/test/core_ext/object/blank_test.rb | 10 ---------- activesupport/test/core_ext/object/try_test.rb | 4 ++++ 5 files changed, 20 insertions(+), 34 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 5894dab230..3cd2235aec 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,15 +1,10 @@ -* Added yield to Object#presence, so you can do this: +* Added instance_eval version to Object#try, so you can do this: - project.account.owner.presence { name.first } || 'Nobody' + person.try { name.first } - instead of calling twice (which may incur double SQL calls): + instead of: - project.account.owner ? project.account.owner.name.first || 'Nobody' - - or assigning to local variable: - - owner = project.account.owner - owner ? owner.name.first || 'Nobody' + person.try { |person| person.name.first } *DHH* diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 99fd22ff56..38e43478df 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -39,21 +39,9 @@ class Object # # region = params[:state].presence || params[:country].presence || 'US' # - # You can also use this with a block that will be yielded if the object is present - # and the result of that block will then be returned. The block itself is run against - # the instance you're running #presence on (using instance_eval) - # - # project.account.owner.presence { name.first } || 'Nobody' - # # @return [Object] - def presence(&block) - if present? - if block_given? - instance_eval(&block) - else - self - end - end + def presence + self if present? end end diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb index 48190e1e66..31919474ed 100644 --- a/activesupport/lib/active_support/core_ext/object/try.rb +++ b/activesupport/lib/active_support/core_ext/object/try.rb @@ -33,6 +33,11 @@ class Object # ... # end # + # You can also call try with a block without accepting an argument, and the block + # will be instance_eval'ed instead: + # + # @person.try { upcase.truncate(50) } + # # Please also note that +try+ is defined on +Object+, therefore it won't work # with instances of classes that do not have +Object+ among their ancestors, # like direct subclasses of +BasicObject+. For example, using +try+ with @@ -40,7 +45,11 @@ class Object # delegator itself. def try(*a, &b) if a.empty? && block_given? - yield self + if b.arity.zero? + instance_eval(&b) + else + yield self + end else public_send(*a, &b) if respond_to?(a.first) end diff --git a/activesupport/test/core_ext/object/blank_test.rb b/activesupport/test/core_ext/object/blank_test.rb index 4deac4b0aa..d6c69bd582 100644 --- a/activesupport/test/core_ext/object/blank_test.rb +++ b/activesupport/test/core_ext/object/blank_test.rb @@ -28,14 +28,4 @@ class BlankTest < ActiveSupport::TestCase BLANK.each { |v| assert_equal false, v.present?, "#{v.inspect} should not be present" } NOT.each { |v| assert_equal true, v.present?, "#{v.inspect} should be present" } end - - def test_presence - BLANK.each { |v| assert_equal nil, v.presence, "#{v.inspect}.presence should return nil" } - NOT.each { |v| assert_equal v, v.presence, "#{v.inspect}.presence should return self" } - end - - def test_presence_with_a_block - assert_equal "THIS WAS TENDERLOVE'S IDEA", "this was tenderlove's idea".presence { upcase } || "Nobody" - assert_equal "Nobody", nil.presence { upcase } || "Nobody" - end end diff --git a/activesupport/test/core_ext/object/try_test.rb b/activesupport/test/core_ext/object/try_test.rb index 8b754ced53..225c20fa36 100644 --- a/activesupport/test/core_ext/object/try_test.rb +++ b/activesupport/test/core_ext/object/try_test.rb @@ -65,6 +65,10 @@ class ObjectTryTest < ActiveSupport::TestCase assert_equal false, ran end + def test_try_with_instance_eval_block + assert_equal @string.reverse, @string.try { reverse } + end + def test_try_with_private_method_bang klass = Class.new do private -- cgit v1.2.3 From 365aa654d8bbe2889b75df2f5147bc25c54fa751 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 29 Aug 2014 18:19:30 -0700 Subject: reduce object allocations in utc_offset `try` allocates an array on every call, we should avoid calling it in hotspots. This reduced AttributeMethodsTest#test_setting_time_zone_aware_attribute_with_string from 18k allocations to 14k --- activesupport/lib/active_support/values/time_zone.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index ee62523824..49dfee96ec 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -276,8 +276,8 @@ module ActiveSupport if @utc_offset @utc_offset else - @current_period ||= tzinfo.try(:current_period) - @current_period.try(:utc_offset) + @current_period ||= tzinfo.current_period if tzinfo + @current_period.utc_offset if @current_period end end -- cgit v1.2.3 From c67c5e6ba0fd098d25094810ac7243b73a140be1 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Fri, 29 Aug 2014 21:51:32 -0700 Subject: Bring back the test cases for `presence` This was removed by mistake in 5e51bdd --- activesupport/test/core_ext/object/blank_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activesupport') diff --git a/activesupport/test/core_ext/object/blank_test.rb b/activesupport/test/core_ext/object/blank_test.rb index d6c69bd582..246bc7fa61 100644 --- a/activesupport/test/core_ext/object/blank_test.rb +++ b/activesupport/test/core_ext/object/blank_test.rb @@ -28,4 +28,9 @@ class BlankTest < ActiveSupport::TestCase BLANK.each { |v| assert_equal false, v.present?, "#{v.inspect} should not be present" } NOT.each { |v| assert_equal true, v.present?, "#{v.inspect} should be present" } end + + def test_presence + BLANK.each { |v| assert_equal nil, v.presence, "#{v.inspect}.presence should return nil" } + NOT.each { |v| assert_equal v, v.presence, "#{v.inspect}.presence should return self" } + end end -- cgit v1.2.3 From 84c0f73c8daf50fa98d1c4a0c1bab8708e49d0e4 Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Sat, 30 Aug 2014 11:58:23 +0200 Subject: Refer to the library name instead of the constant When we are loading a component and we want to know its version, we are actually not speaking about the constant but the library itself. [ci skip] [Godfrey Chan & Xavier Noria] --- activesupport/lib/active_support/gem_version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/gem_version.rb b/activesupport/lib/active_support/gem_version.rb index e026c4f899..0a2b4c1618 100644 --- a/activesupport/lib/active_support/gem_version.rb +++ b/activesupport/lib/active_support/gem_version.rb @@ -1,5 +1,5 @@ module ActiveSupport - # Returns the version of the currently loaded ActiveSupport as a Gem::Version + # Returns the version of the currently loaded Active Support as a Gem::Version def self.gem_version Gem::Version.new VERSION::STRING end -- cgit v1.2.3 From 6e0f273dfd4dc30ec4fa1c6db1079f5fd3f717ff Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Sun, 10 Aug 2014 14:56:03 +0800 Subject: Use `safe_constantize`. Fixes https://github.com/rails/rails/issues/9933. --- .../lib/active_support/testing/constant_lookup.rb | 6 +---- activesupport/test/constantize_test_cases.rb | 30 ++++++++++++++++++++++ activesupport/test/dependencies_test.rb | 2 ++ activesupport/test/testing/constant_lookup_test.rb | 8 ++++++ 4 files changed, 41 insertions(+), 5 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/testing/constant_lookup.rb b/activesupport/lib/active_support/testing/constant_lookup.rb index 1b2a75c35d..07d477c0db 100644 --- a/activesupport/lib/active_support/testing/constant_lookup.rb +++ b/activesupport/lib/active_support/testing/constant_lookup.rb @@ -36,12 +36,8 @@ module ActiveSupport while names.size > 0 do names.last.sub!(/Test$/, "") begin - constant = names.join("::").constantize + constant = names.join("::").safe_constantize break(constant) if yield(constant) - rescue NoMethodError # subclass of NameError - raise - rescue NameError - # Constant wasn't found, move on ensure names.pop end diff --git a/activesupport/test/constantize_test_cases.rb b/activesupport/test/constantize_test_cases.rb index 8a9fd4996b..366e4e5ef0 100644 --- a/activesupport/test/constantize_test_cases.rb +++ b/activesupport/test/constantize_test_cases.rb @@ -1,3 +1,5 @@ +require 'dependencies_test_helpers' + module Ace module Base class Case @@ -23,6 +25,8 @@ class Object end module ConstantizeTestCases + include DependenciesTestHelpers + def run_constantize_tests_on assert_equal Ace::Base::Case, yield("Ace::Base::Case") assert_equal Ace::Base::Case, yield("::Ace::Base::Case") @@ -56,6 +60,19 @@ module ConstantizeTestCases assert_raises(NameError) { yield("Ace::Gas::ConstantizeTestCases") } assert_raises(NameError) { yield("") } assert_raises(NameError) { yield("::") } + assert_raises(NameError) { yield("Ace::gas") } + + assert_raises(NameError) do + with_autoloading_fixtures do + yield("RaisesNameError") + end + end + + assert_raises(NoMethodError) do + with_autoloading_fixtures do + yield("RaisesNoMethodError") + end + end end def run_safe_constantize_tests_on @@ -82,5 +99,18 @@ module ConstantizeTestCases assert_nil yield("Ace::Gas::Base") assert_nil yield("Ace::Gas::ConstantizeTestCases") assert_nil yield("#::Nested_1") + assert_nil yield("Ace::gas") + + assert_raises(NameError) do + with_autoloading_fixtures do + yield("RaisesNameError") + end + end + + assert_raises(NoMethodError) do + with_autoloading_fixtures do + yield("RaisesNoMethodError") + end + end end end diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 5fc3de651a..899bb75eae 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -888,6 +888,8 @@ class DependenciesTest < ActiveSupport::TestCase assert_raise(NameError) { assert_equal 123, ::RaisesNameError::FooBarBaz } end end + ensure + remove_constants(:RaisesNameError) end def test_autoload_doesnt_shadow_name_error diff --git a/activesupport/test/testing/constant_lookup_test.rb b/activesupport/test/testing/constant_lookup_test.rb index 71a9561189..0f16419c8b 100644 --- a/activesupport/test/testing/constant_lookup_test.rb +++ b/activesupport/test/testing/constant_lookup_test.rb @@ -65,4 +65,12 @@ class ConstantLookupTest < ActiveSupport::TestCase } } end + + def test_does_not_swallow_exception_on_no_name_error_within_constant + assert_raises(NameError) do + with_autoloading_fixtures do + self.class.determine_constant_from_test_name('RaisesNameError') + end + end + end end -- cgit v1.2.3 From 3579120367f32c87826bbfb9cce4c5e0a48fd0c8 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 2 Sep 2014 08:42:44 +0200 Subject: remove trailing whitespace. [ci skip] --- activesupport/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 3cd2235aec..80fdb56087 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,9 +1,9 @@ * Added instance_eval version to Object#try, so you can do this: person.try { name.first } - + instead of: - + person.try { |person| person.name.first } *DHH* -- cgit v1.2.3 From 2f52f969885b2834198de0045748436a4651a94e Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Tue, 2 Sep 2014 23:48:23 +0930 Subject: Leave all our tests as order_dependent! for now We're seeing too many failures to believe otherwise. This reverts commits bc116a55ca3dd9f63a1f1ca7ade3623885adcc57, cbde413df3839e06dd14e3c220e9800af91e83ab, bf0a67931dd8e58f6f878b9510ae818ae1f29a3a, and 2440933fe2c27b27bcafcd9019717800db2641aa. --- activesupport/test/abstract_unit.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activesupport') diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 7ffcae6007..52fbaf8a85 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -38,3 +38,8 @@ def jruby_skip(message = '') end require 'mocha/setup' # FIXME: stop using mocha + +# FIXME: we have tests that depend on run order, we should fix that and +# remove this method call. +require 'active_support/test_case' +ActiveSupport::TestCase.my_tests_are_order_dependent! -- cgit v1.2.3 From fdc5e768ca29e3ad4288dcd8a8c7147cea8a1f97 Mon Sep 17 00:00:00 2001 From: Peter Jaros Date: Wed, 3 Sep 2014 16:00:06 -0400 Subject: Methods are not duplicable. --- activesupport/CHANGELOG.md | 5 +++++ activesupport/lib/active_support/core_ext/object/duplicable.rb | 10 ++++++++++ activesupport/test/core_ext/object/duplicable_test.rb | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 617baaa1a8..62e16ed9bd 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* `Method` objects now report themselves as not `duplicable?`. This allows + hashes and arrays containing `Method` objects to be `deep_dup`ed. + + *Peter Jaros* + * `determine_constant_from_test_name` does no longer shadow `NameError`s which happen during constant autoloading. diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb index c5d59128e5..665cb0f96d 100644 --- a/activesupport/lib/active_support/core_ext/object/duplicable.rb +++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb @@ -91,3 +91,13 @@ class BigDecimal # can't dup, so use superclass implementation end end + +class Method + # Methods are not duplicable: + # + # method(:puts).duplicable? # => false + # method(:puts).dup # => TypeError: allocator undefined for Method + def duplicable? + false + end +end diff --git a/activesupport/test/core_ext/object/duplicable_test.rb b/activesupport/test/core_ext/object/duplicable_test.rb index 84512380cf..34679cb362 100644 --- a/activesupport/test/core_ext/object/duplicable_test.rb +++ b/activesupport/test/core_ext/object/duplicable_test.rb @@ -4,7 +4,7 @@ require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/numeric/time' class DuplicableTest < ActiveSupport::TestCase - RAISE_DUP = [nil, false, true, :symbol, 1, 2.3, 5.seconds] + RAISE_DUP = [nil, false, true, :symbol, 1, 2.3, 5.seconds, method(:puts)] ALLOW_DUP = ['1', Object.new, /foo/, [], {}, Time.now, Class.new, Module.new] # Needed to support Ruby 1.9.x, as it doesn't allow dup on BigDecimal, instead -- cgit v1.2.3 From 57b2c371f03982813f6dc2e7f07467b4fca3a6ce Mon Sep 17 00:00:00 2001 From: Agis- Date: Mon, 1 Sep 2014 15:54:29 +0300 Subject: Time#change throws exception with an out-of-range :usec https://github.com/rails/rails/commit/98b46bf5e201307cae56ee14bf41363a539779c5 did not properly handled out-of-range `:usec`s. Passing a `:usec` that's out of range now throws an `ArgumentError` as it should. Fixes #16759. --- activesupport/CHANGELOG.md | 5 +++++ activesupport/lib/active_support/core_ext/time/calculations.rb | 1 + activesupport/test/core_ext/time_ext_test.rb | 1 + 3 files changed, 7 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 62e16ed9bd..cae5ac6e17 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* Make Time#change throw an exception if the :usec option is out of range and + the time has an offset other than UTC or local. + + *Agis Anastasopoulos* + * `Method` objects now report themselves as not `duplicable?`. This allows hashes and arrays containing `Method` objects to be `deep_dup`ed. diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 89cd7516cd..6a7bf7445a 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -87,6 +87,7 @@ class Time elsif zone ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec) else + raise ArgumentError, 'argument out of range' if new_usec > 999999 ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset) end end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index c8283cddc5..9a5bd19be2 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -405,6 +405,7 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.new(2005,2,22,16,0,0,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:hour => 16) assert_equal Time.new(2005,2,22,16,45,0,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:hour => 16, :min => 45) assert_equal Time.new(2005,2,22,15,45,0,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:min => 45) + assert_raise(ArgumentError) { Time.new(2005, 2, 22, 15, 15, 45, "-08:00").change(:usec => 1000000) } end def test_advance -- cgit v1.2.3 From ccbb48196efe06a0c1c360951caff74ee74a8d14 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Sat, 6 Sep 2014 04:56:25 +0930 Subject: Fix for inflector's incorrect camelCase replacement for acronyms Fixes #8015, #9756. [Fred Wu & Matthew Draper] --- activesupport/CHANGELOG.md | 7 +++++++ activesupport/lib/active_support/inflector/methods.rb | 2 +- activesupport/test/inflector_test.rb | 1 + activesupport/test/inflector_test_cases.rb | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index cae5ac6e17..f995082a15 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,10 @@ +* Fixed a bug in Inflector#underscore where acroynms in nested constant names + are incorrectly parsed as camelCase. + + Fixes #8015. + + *Fred Wu*, *Matthew Draper* + * Make Time#change throw an exception if the :usec option is out of range and the time has an offset other than UTC or local. diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index 53022de549..f35e71ce81 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -91,7 +91,7 @@ module ActiveSupport def underscore(camel_cased_word) return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/ word = camel_cased_word.to_s.gsub('::', '/') - word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } + word.gsub!(/(?:([A-Za-z\d])|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 58fdea0972..b37f31bc5f 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -120,6 +120,7 @@ class InflectorTest < ActiveSupport::TestCase ["SSLError", "ssl_error", "SSL error", "SSL Error"], ["RESTful", "restful", "RESTful", "RESTful"], ["RESTfulController", "restful_controller", "RESTful controller", "RESTful Controller"], + ["Nested::RESTful", "nested/restful", "Nested/RESTful", "Nested/RESTful"], ["IHeartW3C", "i_heart_w3c", "I heart W3C", "I Heart W3C"], ["PhDRequired", "phd_required", "PhD required", "PhD Required"], ["IRoRU", "i_ror_u", "I RoR u", "I RoR U"], diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index b556da0046..3770f00fe3 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -141,6 +141,7 @@ module InflectorTestCases "HTMLTidyGenerator" => "html_tidy_generator", "FreeBSD" => "free_bsd", "HTML" => "html", + "ForceXMLController" => "force_xml_controller", } CamelWithModuleToUnderscoreWithSlash = { -- cgit v1.2.3 From 2b41343c34bcbe809537590152506690b84832df Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Mon, 8 Sep 2014 05:32:16 -0700 Subject: Default to sorting user's test cases for now Goals: 1. Default to :random for newly generated applications 2. Default to :sorted for existing applications with a warning 3. Only show the warning once 4. Only show the warning if the app actually uses AS::TestCase Fixes #16769 --- activesupport/CHANGELOG.md | 12 +++---- activesupport/lib/active_support/test_case.rb | 32 ++++++++++++++++++ activesupport/test/abstract_unit.rb | 2 +- activesupport/test/test_case_test.rb | 47 +++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 7 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index f995082a15..0e5a28e3fc 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,9 @@ +* Introduced new configuration option `active_support.test_order` for + specifying the order test cases are executed. This option currently defaults + to `:sorted` but will be changed to `:random` in Rails 5.0. + + *Akira Matsuda*, *Godfrey Chan* + * Fixed a bug in Inflector#underscore where acroynms in nested constant names are incorrectly parsed as camelCase. @@ -43,12 +49,6 @@ *DHH* -* Fix ActiveSupport::TestCase not to order users' test cases by default. - If this change breaks your tests because your tests are order dependent, you need to explicitly call - ActiveSupport::TestCase.my_tests_are_order_dependent! at the top of your tests. - - *Akira Matsuda* - * Fix DateTime comparison with DateTime::Infinity object. *Rafael Mendonça França* diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 0df599b692..33139320fa 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -12,10 +12,42 @@ require 'active_support/core_ext/kernel/reporting' require 'active_support/deprecation' module ActiveSupport + class << self + delegate :test_order, :test_order=, to: :'ActiveSupport::TestCase' + end + class TestCase < ::Minitest::Test Assertion = Minitest::Assertion + @@test_order = nil + class << self + def test_order=(new_order) + @@test_order = new_order + end + + def test_order + if @@test_order.nil? + ActiveSupport::Deprecation.warn "You did not specify a value for the " \ + "configuration option 'active_support.test_order'. In Rails 5.0, " \ + "the default value of this option will change from `:sorted` to " \ + "`:random`.\n" \ + "To disable this warning and keep the current behavior, you can add " \ + "the following line to your `config/environments/test.rb`:\n" \ + "\n" \ + " Rails.application.configure do\n" \ + " config.active_support.test_order = :sorted\n" \ + " end\n" \ + "\n" \ + "Alternatively, you can opt into the future behavior by setting this " \ + "option to `:random`." + + @@test_order = :sorted + end + + @@test_order + end + alias :my_tests_are_order_dependent! :i_suck_and_my_tests_are_order_dependent! end diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 52fbaf8a85..f65ec962f9 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -42,4 +42,4 @@ require 'mocha/setup' # FIXME: stop using mocha # FIXME: we have tests that depend on run order, we should fix that and # remove this method call. require 'active_support/test_case' -ActiveSupport::TestCase.my_tests_are_order_dependent! +ActiveSupport::TestCase.test_order = :sorted diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb index c93192f207..5e852c8050 100644 --- a/activesupport/test/test_case_test.rb +++ b/activesupport/test/test_case_test.rb @@ -172,3 +172,50 @@ class TestCaseTaggedLoggingTest < ActiveSupport::TestCase assert_match "#{self.class}: #{name}\n", @out.string end end + +class TestOrderTest < ActiveSupport::TestCase + def setup + @original_test_order = ActiveSupport::TestCase.test_order + end + + def teardown + ActiveSupport::TestCase.test_order = @original_test_order + end + + def test_defaults_to_sorted_with_warning + ActiveSupport::TestCase.test_order = nil + + assert_equal :sorted, assert_deprecated { ActiveSupport::TestCase.test_order } + + # It should only produce a deprecation warning the first time this is accessed + assert_equal :sorted, assert_not_deprecated { ActiveSupport::TestCase.test_order } + assert_equal :sorted, assert_not_deprecated { ActiveSupport.test_order } + end + + def test_test_order_is_global + ActiveSupport::TestCase.test_order = :random + + assert_equal :random, ActiveSupport.test_order + assert_equal :random, ActiveSupport::TestCase.test_order + assert_equal :random, self.class.test_order + assert_equal :random, Class.new(ActiveSupport::TestCase).test_order + + ActiveSupport.test_order = :sorted + + assert_equal :sorted, ActiveSupport.test_order + assert_equal :sorted, ActiveSupport::TestCase.test_order + assert_equal :sorted, self.class.test_order + assert_equal :sorted, Class.new(ActiveSupport::TestCase).test_order + end + + def test_i_suck_and_my_tests_are_order_dependent! + ActiveSupport::TestCase.test_order = :random + + klass = Class.new(ActiveSupport::TestCase) do + i_suck_and_my_tests_are_order_dependent! + end + + assert_equal :alpha, klass.test_order + assert_equal :random, ActiveSupport::TestCase.test_order + end +end -- cgit v1.2.3 From 53e877f7d9291b2bf0b8c425f9e32ef35829f35b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 11 Sep 2014 00:54:43 -0300 Subject: Define the configuration at Active Support --- activesupport/lib/active_support.rb | 10 ++++++++++ activesupport/lib/active_support/test_case.rb | 17 +++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index ab0054b339..94468240a4 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -70,6 +70,16 @@ module ActiveSupport NumberHelper.eager_load! end + + @@test_order = nil + + def self.test_order=(new_order) + @@test_order = new_order + end + + def self.test_order + @@test_order + end end autoload :I18n, "active_support/i18n" diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 33139320fa..4c3e77b7fd 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -12,22 +12,18 @@ require 'active_support/core_ext/kernel/reporting' require 'active_support/deprecation' module ActiveSupport - class << self - delegate :test_order, :test_order=, to: :'ActiveSupport::TestCase' - end - class TestCase < ::Minitest::Test Assertion = Minitest::Assertion - @@test_order = nil - class << self def test_order=(new_order) - @@test_order = new_order + ActiveSupport.test_order = new_order end def test_order - if @@test_order.nil? + test_order = ActiveSupport.test_order + + if test_order.nil? ActiveSupport::Deprecation.warn "You did not specify a value for the " \ "configuration option 'active_support.test_order'. In Rails 5.0, " \ "the default value of this option will change from `:sorted` to " \ @@ -42,10 +38,11 @@ module ActiveSupport "Alternatively, you can opt into the future behavior by setting this " \ "option to `:random`." - @@test_order = :sorted + test_order = :sorted + self.test_order = test_order end - @@test_order + test_order end alias :my_tests_are_order_dependent! :i_suck_and_my_tests_are_order_dependent! -- cgit v1.2.3 From 4bf9d1938bb144ef2a6c3d03a2d01add62e90114 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Fri, 12 Sep 2014 14:58:04 +0200 Subject: MessageVerifier raises an appropriate exception if the secret is nil Otherwise this will lead to another error later on when generating a signature: TypeError (no implicit conversion of nil into String). --- activesupport/CHANGELOG.md | 5 +++++ activesupport/lib/active_support/message_verifier.rb | 1 + activesupport/test/message_verifier_test.rb | 7 +++++++ 3 files changed, 13 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 0e5a28e3fc..8b76fa97d1 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* `MessageVerifier.new` raises an appropriate exception if the secret is `nil`. + This prevents `MessageVerifier#generate` from raising a cryptic error later on. + + *Kostiantyn Kahanskyi* + * Introduced new configuration option `active_support.test_order` for specifying the order test cases are executed. This option currently defaults to `:sorted` but will be changed to `:random` in Rails 5.0. diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb index 8e6e1dcfeb..41e2e5a88f 100644 --- a/activesupport/lib/active_support/message_verifier.rb +++ b/activesupport/lib/active_support/message_verifier.rb @@ -27,6 +27,7 @@ module ActiveSupport class InvalidSignature < StandardError; end def initialize(secret, options = {}) + raise ArgumentError, 'Secret should not be nil.' if secret.nil? @secret = secret @digest = options[:digest] || 'SHA1' @serializer = options[:serializer] || Marshal diff --git a/activesupport/test/message_verifier_test.rb b/activesupport/test/message_verifier_test.rb index a5748d28ba..28035bc428 100644 --- a/activesupport/test/message_verifier_test.rb +++ b/activesupport/test/message_verifier_test.rb @@ -69,6 +69,13 @@ class MessageVerifierTest < ActiveSupport::TestCase "undefined class/module MessageVerifierTest::AutoloadClass"], exception.message end + def test_raise_error_when_secret_is_nil + exception = assert_raise(ArgumentError) do + ActiveSupport::MessageVerifier.new(nil) + end + assert_equal exception.message, 'Secret should not be nil.' + end + def assert_not_verified(message) assert_raise(ActiveSupport::MessageVerifier::InvalidSignature) do @verifier.verify(message) -- cgit v1.2.3 From 1ac20c59f305b9375add46f81b233f93a94ae70d Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Fri, 12 Sep 2014 18:47:33 +0200 Subject: Changes "if secret.nil?" to unless secret in MessageVerfier --- activesupport/lib/active_support/message_verifier.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb index 41e2e5a88f..6cb2884fb7 100644 --- a/activesupport/lib/active_support/message_verifier.rb +++ b/activesupport/lib/active_support/message_verifier.rb @@ -27,7 +27,7 @@ module ActiveSupport class InvalidSignature < StandardError; end def initialize(secret, options = {}) - raise ArgumentError, 'Secret should not be nil.' if secret.nil? + raise ArgumentError, 'Secret should not be nil.' unless secret @secret = secret @digest = options[:digest] || 'SHA1' @serializer = options[:serializer] || Marshal -- cgit v1.2.3