summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module/method_added_spec.rb
blob: 643291be173e58095a83716d61eff1467c25f6a7 (plain)
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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Module#method_added" do
  it "is a private instance method" do
    Module.should have_private_instance_method(:method_added)
  end

  it "returns nil in the default implementation" do
    Module.new do
      method_added(:test).should == nil
    end
  end

  it "is called when a new instance method is defined in self" do
    ScratchPad.record []

    Module.new do
      def self.method_added(name)
        ScratchPad << name
      end

      def test() end
      def test2() end
      def test() end
      alias_method :aliased_test, :test
      alias aliased_test2 test
    end

    ScratchPad.recorded.should == [:test, :test2, :test, :aliased_test, :aliased_test2]
  end

  it "is not called when a singleton method is added" do
    # obj.singleton_method_added is called instead
    ScratchPad.record []

    klass = Class.new
    def klass.method_added(name)
      ScratchPad << name
    end

    obj = klass.new
    def obj.new_singleton_method
    end

    ScratchPad.recorded.should == []
  end

  it "is not called when a method is undefined in self" do
    m = Module.new do
      def method_to_undef
      end

      def self.method_added(name)
        fail("method_added called by undef_method")
      end

      undef_method :method_to_undef
    end
    m.should_not have_method(:method_to_undef)
  end
end