delegation_test.rb 1.5 KB
Newer Older
1 2 3
require "cases/helper"
require "models/post"
require "models/comment"
4 5 6 7 8

module ActiveRecord
  class DelegationTest < ActiveRecord::TestCase
    fixtures :posts

9 10
    def call_method(target, method)
      method_arity = target.to_a.method(method).arity
11

12
      if method_arity.zero?
13
        target.public_send(method)
14 15
      elsif method_arity < 0
        if method == :shuffle!
16
          target.public_send(method)
17
        else
18
          target.public_send(method, 1)
19
        end
20
       elsif method_arity == 1
21
         target.public_send(method, 1)
22 23
      else
        raise NotImplementedError
24 25 26 27
      end
    end
  end

28
  module DelegationWhitelistBlacklistTests
29
    ARRAY_DELEGATES = [
30
      :+, :-, :|, :&, :[], :shuffle,
R
Rafael Mendonça França 已提交
31
      :all?, :collect, :compact, :detect, :each, :each_cons, :each_with_index,
32 33 34
      :exclude?, :find_all, :flat_map, :group_by, :include?, :length,
      :map, :none?, :one?, :partition, :reject, :reverse,
      :sample, :second, :sort, :sort_by, :third,
35
      :to_ary, :to_set, :to_xml, :to_yaml, :join
36 37 38
    ]

    ARRAY_DELEGATES.each do |method|
39
      define_method "test_delegates_#{method}_to_Array" do
40
        assert_respond_to target, method
41 42 43 44
      end
    end
  end

45 46
  class DelegationAssociationTest < DelegationTest
    include DelegationWhitelistBlacklistTests
47

48
    def target
49
      Post.first.comments
50
    end
51
  end
52

53 54
  class DelegationRelationTest < DelegationTest
    include DelegationWhitelistBlacklistTests
55

56 57 58 59
    fixtures :comments

    def target
      Comment.all
60 61 62
    end
  end
end