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

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 29 30
  module DelegationWhitelistBlacklistTests
    ActiveRecord::Delegation::ARRAY_DELEGATES.each do |method|
      define_method "test_delegates_#{method}_to_Array" do
31
        assert_respond_to target, method
32 33 34 35
      end
    end

    [:compact!, :flatten!, :reject!, :reverse!, :rotate!,
36 37
     :shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
     :keep_if, :pop, :shift, :delete_at, :compact].each do |method|
38
      define_method "test_#{method}_is_not_delegated_to_Array" do
39
        assert_raises(NoMethodError) { call_method(target, method) }
40 41 42 43
      end
    end
  end

44 45
  class DelegationAssociationTest < DelegationTest
    include DelegationWhitelistBlacklistTests
46

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

52 53
  class DelegationRelationTest < DelegationTest
    include DelegationWhitelistBlacklistTests
54

55 56 57 58
    fixtures :comments

    def target
      Comment.all
59 60 61
    end
  end
end