summaryrefslogtreecommitdiff
path: root/spec/ruby/shared/basicobject/method_missing.rb
blob: e331d7262f026533f75b1ccf915d96be99cf6dd9 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require_relative '../../spec_helper'
require_relative '../../fixtures/basicobject/method_missing'

describe :method_missing_defined_module, shared: true do
  describe "for a Module with #method_missing defined" do
    it "is not called when a defined method is called" do
      @object.method_public.should == :module_public_method
    end

    it "is called when a not defined method is called" do
      @object.not_defined_method.should == :module_method_missing
    end

    it "is called when a protected method is called" do
      @object.method_protected.should == :module_method_missing
    end

    it "is called when a private method is called" do
      @object.method_private.should == :module_method_missing
    end
  end
end

describe :method_missing_module, shared: true do
  describe "for a Module" do
    it "raises a NoMethodError when an undefined method is called" do
      lambda { @object.no_such_method }.should raise_error(NoMethodError)
    end

    it "raises a NoMethodError when a protected method is called" do
      lambda { @object.method_protected }.should raise_error(NoMethodError)
    end

    it "raises a NoMethodError when a private method is called" do
      lambda { @object.method_private }.should raise_error(NoMethodError)
    end
  end
end

describe :method_missing_defined_class, shared: true do
  describe "for a Class with #method_missing defined" do
    it "is not called when a defined method is called" do
      @object.method_public.should == :class_public_method
    end

    it "is called when an undefined method is called" do
      @object.no_such_method.should == :class_method_missing
    end

    it "is called when an protected method is called" do
      @object.method_protected.should == :class_method_missing
    end

    it "is called when an private method is called" do
      @object.method_private.should == :class_method_missing
    end
  end
end

describe :method_missing_class, shared: true do
  describe "for a Class" do
    it "raises a NoMethodError when an undefined method is called" do
      lambda { @object.no_such_method }.should raise_error(NoMethodError)
    end

    it "raises a NoMethodError when a protected method is called" do
      lambda { @object.method_protected }.should raise_error(NoMethodError)
    end

    it "raises a NoMethodError when a private method is called" do
      lambda { @object.method_private }.should raise_error(NoMethodError)
    end
  end
end

describe :method_missing_defined_instance, shared: true do
  describe "for an instance with #method_missing defined" do
    before :each do
      @instance = @object.new
    end

    it "is not called when a defined method is called" do
      @instance.method_public.should == :instance_public_method
    end

    it "is called when an undefined method is called" do
      @instance.no_such_method.should == :instance_method_missing
    end

    it "is called when an protected method is called" do
      @instance.method_protected.should == :instance_method_missing
    end

    it "is called when an private method is called" do
      @instance.method_private.should == :instance_method_missing
    end
  end
end

describe :method_missing_instance, shared: true do
  describe "for an instance" do
    it "raises a NoMethodError when an undefined method is called" do
      lambda { @object.new.no_such_method }.should raise_error(NoMethodError)
    end

    it "raises a NoMethodError when a protected method is called" do
      lambda { @object.new.method_protected }.should raise_error(NoMethodError)
    end

    it "raises a NoMethodError when a private method is called" do
      lambda { @object.new.method_private }.should raise_error(NoMethodError)
    end

    ruby_version_is "2.3" do
      it 'sets the receiver of the raised NoMethodError' do
        obj = @object.new

        begin
          obj.method_private
        rescue NoMethodError => error
          (error.receiver == obj).should == true
        end
      end
    end
  end
end