##// END OF EJS Templates
add patch for regression of Hash#reject in Ruby 2.1.1 (#16194)...
Toshi MARUYAMA -
r12840:82fa2cf79a29
parent child
Show More
@@ -203,6 +203,30 module ActionController
203 end
203 end
204 end
204 end
205
205
206 if Rails::VERSION::MAJOR < 4 && RUBY_VERSION >= "2.1"
207 module ActiveSupport
208 class HashWithIndifferentAccess
209 def select(*args, &block)
210 dup.tap { |hash| hash.select!(*args, &block) }
211 end
212
213 def reject(*args, &block)
214 dup.tap { |hash| hash.reject!(*args, &block) }
215 end
216 end
217
218 class OrderedHash
219 def select(*args, &block)
220 dup.tap { |hash| hash.select!(*args, &block) }
221 end
222
223 def reject(*args, &block)
224 dup.tap { |hash| hash.reject!(*args, &block) }
225 end
226 end
227 end
228 end
229
206 require 'awesome_nested_set/version'
230 require 'awesome_nested_set/version'
207
231
208 module CollectiveIdea
232 module CollectiveIdea
@@ -22,6 +22,16 class PatchesTest < ActiveSupport::TestCase
22
22
23 def setup
23 def setup
24 Setting.default_language = 'en'
24 Setting.default_language = 'en'
25 @symbols = { :a => 1, :b => 2 }
26 @keys = %w( blue green red pink orange )
27 @values = %w( 000099 009900 aa0000 cc0066 cc6633 )
28 @hash = Hash.new
29 @ordered_hash = ActiveSupport::OrderedHash.new
30
31 @keys.each_with_index do |key, index|
32 @hash[key] = @values[index]
33 @ordered_hash[key] = @values[index]
34 end
25 end
35 end
26
36
27 test "ActiveRecord::Base.human_attribute_name should transform name to field_name" do
37 test "ActiveRecord::Base.human_attribute_name should transform name to field_name" do
@@ -35,4 +45,55 class PatchesTest < ActiveSupport::TestCase
35 test "ActiveRecord::Base.human_attribute_name should default to humanized value if no translation has been found (useful for custom fields)" do
45 test "ActiveRecord::Base.human_attribute_name should default to humanized value if no translation has been found (useful for custom fields)" do
36 assert_equal 'Patch name', ActiveRecord::Base.human_attribute_name('Patch name')
46 assert_equal 'Patch name', ActiveRecord::Base.human_attribute_name('Patch name')
37 end
47 end
48
49 # https://github.com/rails/rails/pull/14198/files
50 if RUBY_VERSION >= "1.9"
51 def test_indifferent_select
52 hash = ActiveSupport::HashWithIndifferentAccess.new(@symbols).select { |_ ,v| v == 1 }
53 assert_equal({ 'a' => 1 }, hash)
54 assert_instance_of (RUBY_VERSION < "2.1" ?
55 Hash : ActiveSupport::HashWithIndifferentAccess),
56 hash
57 end
58
59 def test_indifferent_select_bang
60 indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@symbols)
61 indifferent_strings.select! { |_, v| v == 1 }
62 assert_equal({ 'a' => 1 }, indifferent_strings)
63 assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
64 end
65 end
66
67 def test_indifferent_reject
68 hash = ActiveSupport::HashWithIndifferentAccess.new(@symbols).reject { |_, v| v != 1 }
69 assert_equal({ 'a' => 1 }, hash)
70 assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash
71 end
72
73 def test_indifferent_reject_bang
74 indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@symbols)
75 indifferent_strings.reject! { |_, v| v != 1 }
76 assert_equal({ 'a' => 1 }, indifferent_strings)
77 assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
78 end
79
80 if RUBY_VERSION >= "1.9"
81 def test_select
82 assert_equal @keys, @ordered_hash.select { true }.map(&:first)
83 new_ordered_hash = @ordered_hash.select { true }
84 assert_equal @keys, new_ordered_hash.map(&:first)
85 assert_instance_of (RUBY_VERSION < "2.1" ?
86 Hash : ActiveSupport::OrderedHash),
87 new_ordered_hash
88 end
89 end
90
91 def test_reject
92 copy = @ordered_hash.dup
93 new_ordered_hash = @ordered_hash.reject { |k, _| k == 'pink' }
94 assert_equal copy, @ordered_hash
95 assert !new_ordered_hash.keys.include?('pink')
96 assert @ordered_hash.keys.include?('pink')
97 assert_instance_of ActiveSupport::OrderedHash, new_ordered_hash
98 end
38 end
99 end
General Comments 0
You need to be logged in to leave comments. Login now