diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-12-12 14:42:39 -0800 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-12-12 14:42:39 -0800 |
commit | 81433addc2951457e7e201297ae3cd9e83a2affb (patch) | |
tree | 0383c58cee0132b8a862b61bff8b9168aadac460 /activerecord/test/cases/relation | |
parent | 94cd08be38dc086714be8f6cbe17e77f778e1ecc (diff) | |
parent | 1244aa7c5f871da5e907a39242b63a7aee3308a3 (diff) | |
download | rails-81433addc2951457e7e201297ae3cd9e83a2affb.tar.gz rails-81433addc2951457e7e201297ae3cd9e83a2affb.tar.bz2 rails-81433addc2951457e7e201297ae3cd9e83a2affb.zip |
Merge pull request #12590 from laurocaetano/whitelist-to-delegate-array-methods
Create a whitelist of methods to be delegated to Array.
Diffstat (limited to 'activerecord/test/cases/relation')
-rw-r--r-- | activerecord/test/cases/relation/delegation_test.rb | 91 |
1 files changed, 33 insertions, 58 deletions
diff --git a/activerecord/test/cases/relation/delegation_test.rb b/activerecord/test/cases/relation/delegation_test.rb index 13677797cf..b56dd5e5db 100644 --- a/activerecord/test/cases/relation/delegation_test.rb +++ b/activerecord/test/cases/relation/delegation_test.rb @@ -6,22 +6,21 @@ module ActiveRecord class DelegationTest < ActiveRecord::TestCase fixtures :posts - def assert_responds(target, method) - assert target.respond_to?(method) - assert_nothing_raised do - method_arity = target.to_a.method(method).arity + def call_method(target, method) + method_arity = target.to_a.method(method).arity - if method_arity.zero? - target.send(method) - elsif method_arity < 0 - if method == :shuffle! - target.send(method) - else - target.send(method, 1) - end + if method_arity.zero? + target.public_send(method) + elsif method_arity < 0 + if method == :shuffle! + target.public_send(method) else - raise NotImplementedError + target.public_send(method, 1) end + elsif method_arity == 1 + target.public_send(method, 1) + else + raise NotImplementedError end end end @@ -31,31 +30,20 @@ module ActiveRecord Post.first.comments end - [:map, :collect].each do |method| - test "##{method} is delegated" do - assert_responds(target, method) - assert_equal(target.pluck(:body), target.send(method) {|post| post.body }) - end - - test "##{method}! is not delegated" do - assert_deprecated do - assert_responds(target, "#{method}!") - end + [:&, :+, :[], :all?, :collect, :detect, :each, :each_cons, + :each_with_index, :flat_map, :group_by, :include?, :length, + :map, :none?, :one?, :reverse, :sample, :second, :sort, :sort_by, + :to_ary, :to_set, :to_xml, :to_yaml].each do |method| + test "association delegates #{method} to Array" do + assert_respond_to target, method end end [:compact!, :flatten!, :reject!, :reverse!, :rotate!, - :shuffle!, :slice!, :sort!, :sort_by!].each do |method| - test "##{method} delegation is deprecated" do - assert_deprecated do - assert_responds(target, method) - end - end - end - - [:select!, :uniq!].each do |method| - test "##{method} is implemented" do - assert_responds(target, method) + :shuffle!, :slice!, :sort!, :sort_by!, :delete_if, + :keep_if, :pop, :shift, :delete_at, :compact].each do |method| + test "#{method} is not delegated to Array" do + assert_raises(NoMethodError) { call_method(target, method) } end end end @@ -64,36 +52,23 @@ module ActiveRecord fixtures :comments def target - Comment.where(body: 'Normal type') + Comment.all end - [:map, :collect].each do |method| - test "##{method} is delegated" do - assert_responds(target, method) - assert_equal(target.pluck(:body), target.send(method) {|post| post.body }) - end - - test "##{method}! is not delegated" do - assert_deprecated do - assert_responds(target, "#{method}!") - end + [:&, :+, :[], :all?, :collect, :detect, :each, :each_cons, + :each_with_index, :flat_map, :group_by, :include?, :length, + :map, :none?, :one?, :reverse, :sample, :second, :sort, :sort_by, + :to_ary, :to_set, :to_xml, :to_yaml].each do |method| + test "relation delegates #{method} to Array" do + assert_respond_to target, method end end [:compact!, :flatten!, :reject!, :reverse!, :rotate!, - :shuffle!, :slice!, :sort!, :sort_by!].each do |method| - test "##{method} delegation is deprecated" do - assert_deprecated do - assert_responds(target, method) - end - end - end - - [:select!, :uniq!].each do |method| - test "##{method} triggers an immutable error" do - assert_raises ActiveRecord::ImmutableRelation do - assert_responds(target, method) - end + :shuffle!, :slice!, :sort!, :sort_by!, :delete_if, + :keep_if, :pop, :shift, :delete_at, :compact].each do |method| + test "#{method} is not delegated to Array" do + assert_raises(NoMethodError) { call_method(target, method) } end end end |