2013-09-04 00:46:33 -04:00
|
|
|
require 'cases/helper'
|
|
|
|
require 'models/post'
|
|
|
|
require 'models/comment'
|
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class DelegationTest < ActiveRecord::TestCase
|
|
|
|
fixtures :posts
|
|
|
|
|
2013-12-03 15:30:13 -05:00
|
|
|
def call_method(target, method)
|
|
|
|
method_arity = target.to_a.method(method).arity
|
2013-10-07 04:24:14 -04:00
|
|
|
|
2013-12-03 15:30:13 -05:00
|
|
|
if method_arity.zero?
|
2013-12-12 17:19:04 -05:00
|
|
|
target.public_send(method)
|
2013-12-03 15:30:13 -05:00
|
|
|
elsif method_arity < 0
|
|
|
|
if method == :shuffle!
|
2013-12-12 17:19:04 -05:00
|
|
|
target.public_send(method)
|
2013-09-04 00:46:33 -04:00
|
|
|
else
|
2013-12-12 17:19:04 -05:00
|
|
|
target.public_send(method, 1)
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
2013-12-03 15:30:13 -05:00
|
|
|
elsif method_arity == 1
|
2013-12-12 17:19:04 -05:00
|
|
|
target.public_send(method, 1)
|
2013-12-03 15:30:13 -05:00
|
|
|
else
|
|
|
|
raise NotImplementedError
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-12 23:10:03 -05:00
|
|
|
module DelegationWhitelistBlacklistTests
|
2013-12-13 19:11:39 -05:00
|
|
|
ARRAY_DELEGATES = [
|
|
|
|
:+, :-, :|, :&, :[],
|
|
|
|
:all?, :collect, :detect, :each, :each_cons, :each_with_index,
|
|
|
|
:exclude?, :find_all, :flat_map, :group_by, :include?, :length,
|
|
|
|
:map, :none?, :one?, :partition, :reject, :reverse,
|
|
|
|
:sample, :second, :sort, :sort_by, :third,
|
2014-05-05 03:48:59 -04:00
|
|
|
:to_ary, :to_set, :to_xml, :to_yaml, :join
|
2013-12-13 19:11:39 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
ARRAY_DELEGATES.each do |method|
|
2013-12-12 23:10:03 -05:00
|
|
|
define_method "test_delegates_#{method}_to_Array" do
|
2013-12-03 15:30:13 -05:00
|
|
|
assert_respond_to target, method
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-13 19:11:39 -05:00
|
|
|
ActiveRecord::Delegation::BLACKLISTED_ARRAY_METHODS.each do |method|
|
2013-12-12 23:10:03 -05:00
|
|
|
define_method "test_#{method}_is_not_delegated_to_Array" do
|
2013-12-03 15:30:13 -05:00
|
|
|
assert_raises(NoMethodError) { call_method(target, method) }
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-12 23:10:03 -05:00
|
|
|
class DelegationAssociationTest < DelegationTest
|
|
|
|
include DelegationWhitelistBlacklistTests
|
2013-11-11 05:43:34 -05:00
|
|
|
|
2013-09-04 00:46:33 -04:00
|
|
|
def target
|
2013-12-12 23:10:03 -05:00
|
|
|
Post.first.comments
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
2013-12-12 23:10:03 -05:00
|
|
|
end
|
2013-09-04 00:46:33 -04:00
|
|
|
|
2013-12-12 23:10:03 -05:00
|
|
|
class DelegationRelationTest < DelegationTest
|
|
|
|
include DelegationWhitelistBlacklistTests
|
2013-09-04 00:46:33 -04:00
|
|
|
|
2013-12-12 23:10:03 -05:00
|
|
|
fixtures :comments
|
|
|
|
|
|
|
|
def target
|
|
|
|
Comment.all
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|