aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/hash
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-19 12:01:03 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-29 13:31:43 -0600
commitb2cf8b251aac39c1e3ce71bc1de34a2ce5ef52b1 (patch)
treed4b765dc9a155a22fed30278c07c63d0fecf453f /activesupport/test/core_ext/hash
parent9ca0f8da2abe735c57eacd9b687ee7c3fae3685d (diff)
downloadrails-b2cf8b251aac39c1e3ce71bc1de34a2ce5ef52b1.tar.gz
rails-b2cf8b251aac39c1e3ce71bc1de34a2ce5ef52b1.tar.bz2
rails-b2cf8b251aac39c1e3ce71bc1de34a2ce5ef52b1.zip
Add `Hash#map_values` to ActiveSupport
Didn't get a chance to convert existing code, I'll skim through the code base to make use of this later this afternoon.
Diffstat (limited to 'activesupport/test/core_ext/hash')
-rw-r--r--activesupport/test/core_ext/hash/transform_values_test.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/hash/transform_values_test.rb b/activesupport/test/core_ext/hash/transform_values_test.rb
new file mode 100644
index 0000000000..4449c94701
--- /dev/null
+++ b/activesupport/test/core_ext/hash/transform_values_test.rb
@@ -0,0 +1,49 @@
+require 'abstract_unit'
+require 'active_support/core_ext/hash/indifferent_access'
+require 'active_support/core_ext/hash/transform_values'
+
+class TransformValuesTest < ActiveSupport::TestCase
+ test "transform_values returns a new hash with the values computed from the block" do
+ original = { a: 'a', b: 'b' }
+ mapped = original.transform_values { |v| v + '!' }
+
+ assert_equal({ a: 'a', b: 'b' }, original)
+ assert_equal({ a: 'a!', b: 'b!' }, mapped)
+ end
+
+ test "transform_values! modifies the values of the original" do
+ original = { a: 'a', b: 'b' }
+ mapped = original.transform_values! { |v| v + '!' }
+
+ assert_equal({ a: 'a!', b: 'b!' }, original)
+ assert_same original, mapped
+ end
+
+ test "indifferent access is still indifferent after mapping values" do
+ original = { a: 'a', b: 'b' }.with_indifferent_access
+ mapped = original.transform_values { |v| v + '!' }
+
+ assert_equal 'a!', mapped[:a]
+ assert_equal 'a!', mapped['a']
+ end
+
+ # This is to be consistent with the behavior of Ruby's built in methods
+ # (e.g. #select, #reject) as of 2.2
+ test "default values do not persist during mapping" do
+ original = Hash.new('foo')
+ original[:a] = 'a'
+ mapped = original.transform_values { |v| v + '!' }
+
+ assert_equal 'a!', mapped[:a]
+ assert_nil mapped[:b]
+ end
+
+ test "default procs do not persist after mapping" do
+ original = Hash.new { 'foo' }
+ original[:a] = 'a'
+ mapped = original.transform_values { |v| v + '!' }
+
+ assert_equal 'a!', mapped[:a]
+ assert_nil mapped[:b]
+ end
+end