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
|
|
|
|
|
|
|
|
class DelegationAssociationTest < DelegationTest
|
|
|
|
def target
|
|
|
|
Post.first.comments
|
|
|
|
end
|
|
|
|
|
2013-12-03 15:30:13 -05:00
|
|
|
[:&, :+, :[], :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
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[:compact!, :flatten!, :reject!, :reverse!, :rotate!,
|
2013-12-03 15:30:13 -05:00
|
|
|
: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) }
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DelegationRelationTest < DelegationTest
|
2013-11-11 05:43:34 -05:00
|
|
|
fixtures :comments
|
|
|
|
|
2013-09-04 00:46:33 -04:00
|
|
|
def target
|
2013-12-03 15:30:13 -05:00
|
|
|
Comment.all
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
|
2013-12-03 15:30:13 -05:00
|
|
|
[:&, :+, :[], :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
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[:compact!, :flatten!, :reject!, :reverse!, :rotate!,
|
2013-12-03 15:30:13 -05:00
|
|
|
: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) }
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|