delegation_test.rb 1.6 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
require "cases/helper"
require "models/post"
require "models/comment"
6 7

module ActiveRecord
8
  module DelegationWhitelistTests
9
    ARRAY_DELEGATES = [
10
      :+, :-, :|, :&, :[], :shuffle,
R
Rafael Mendonça França 已提交
11
      :all?, :collect, :compact, :detect, :each, :each_cons, :each_with_index,
12 13 14
      :exclude?, :find_all, :flat_map, :group_by, :include?, :length,
      :map, :none?, :one?, :partition, :reject, :reverse,
      :sample, :second, :sort, :sort_by, :third,
15
      :to_ary, :to_set, :to_xml, :to_yaml, :join,
16
      :in_groups, :in_groups_of, :to_sentence, :to_formatted_s, :as_json
17 18 19
    ]

    ARRAY_DELEGATES.each do |method|
20
      define_method "test_delegates_#{method}_to_Array" do
21
        assert_respond_to target, method
22 23 24 25
      end
    end
  end

26 27 28
  module DeprecatedArelDelegationTests
    AREL_METHODS = [
      :with, :orders, :froms, :project, :projections, :taken, :constraints, :exists, :locked, :where_sql,
S
Sean Griffin 已提交
29
      :ast, :source, :join_sources, :to_dot, :create_insert, :create_true, :create_false
30 31 32 33 34 35 36 37 38
    ]

    def test_deprecate_arel_delegation
      AREL_METHODS.each do |method|
        assert_deprecated { target.public_send(method) }
      end
    end
  end

39 40
  class DelegationAssociationTest < ActiveRecord::TestCase
    include DelegationWhitelistTests
41
    include DeprecatedArelDelegationTests
42 43

    fixtures :posts
44

45
    def target
46
      Post.first.comments
47
    end
48
  end
49

50 51
  class DelegationRelationTest < ActiveRecord::TestCase
    include DelegationWhitelistTests
52
    include DeprecatedArelDelegationTests
53

54 55 56 57
    fixtures :comments

    def target
      Comment.all
58 59 60
    end
  end
end