aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/relation/delegation_test.rb
blob: 49d4aeafc9c1707752c03acc2d4aaf5e59a88248 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require "cases/helper"
require "models/post"
require "models/comment"

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

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

      if method_arity.zero?
        target.public_send(method)
      elsif method_arity < 0
        if method == :shuffle!
          target.public_send(method)
        else
          target.public_send(method, 1)
        end
       elsif method_arity == 1
         target.public_send(method, 1)
      else
        raise NotImplementedError
      end
    end
  end

  module DelegationWhitelistBlacklistTests
    ARRAY_DELEGATES = [
      :+, :-, :|, :&, :[], :shuffle,
      :all?, :collect, :compact, :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,
      :to_ary, :to_set, :to_xml, :to_yaml, :join,
      :in_groups, :in_groups_of, :to_sentence, :to_formatted_s
    ]

    ARRAY_DELEGATES.each do |method|
      define_method "test_delegates_#{method}_to_Array" do
        assert_respond_to target, method
      end
    end
  end

  class DelegationAssociationTest < DelegationTest
    include DelegationWhitelistBlacklistTests

    def target
      Post.first.comments
    end
  end

  class DelegationRelationTest < DelegationTest
    include DelegationWhitelistBlacklistTests

    fixtures :comments

    def target
      Comment.all
    end
  end
end