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
      :exclude?, :find_all, :flat_map, :group_by, :include?, :length,
13 14
      :map, :none?, :one?, :partition, :reject, :reverse, :rotate,
      :sample, :second, :sort, :sort_by, :slice, :third, :index, :rindex,
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
    ]

    def test_deprecate_arel_delegation
      AREL_METHODS.each do |method|
        assert_deprecated { target.public_send(method) }
35
        assert_deprecated { target.public_send(method) }
36 37 38 39
      end
    end
  end

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

44
    def target
45
      Post.new.comments
46
    end
47
  end
48

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

53 54
    def target
      Comment.all
55 56 57
    end
  end
end