aboutsummaryrefslogtreecommitdiffstats
path: root/spec/rubyspec/core/kernel/respond_to_missing_spec.rb
blob: f116f19dbd83fa45dc968b74af709a6fcf95fd4e (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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Kernel#respond_to_missing?" do
  before :each do
    @a = KernelSpecs::A.new
  end

  it "is a private method" do
    Kernel.should have_private_instance_method(:respond_to_missing?, false)
  end

  it "is only an instance method" do
    Kernel.method(:respond_to_missing?).owner.should == Kernel
  end

  it "is not called when #respond_to? would return true" do
    obj = mock('object')
    obj.stub!(:glark)
    obj.should_not_receive(:respond_to_missing?)
    obj.respond_to?(:glark).should be_true
  end

  it "is called with a 2nd argument of false when #respond_to? is" do
    obj = mock('object')
    obj.should_receive(:respond_to_missing?).with(:undefined_method, false)
    obj.respond_to?(:undefined_method, false)
  end

  it "is called a 2nd argument of false when #respond_to? is called with only 1 argument" do
    obj = mock('object')
    obj.should_receive(:respond_to_missing?).with(:undefined_method, false)
    obj.respond_to?(:undefined_method)
  end

  it "is called with true as the second argument when #respond_to? is" do
    obj = mock('object')
    obj.should_receive(:respond_to_missing?).with(:undefined_method, true)
    obj.respond_to?(:undefined_method, true)
  end

  it "is called when #respond_to? would return false" do
    obj = mock('object')
    obj.should_receive(:respond_to_missing?).with(:undefined_method, false)
    obj.respond_to?(:undefined_method)
  end

  it "causes #respond_to? to return true if called and not returning false" do
    obj = mock('object')
    obj.should_receive(:respond_to_missing?).with(:undefined_method, false).and_return(:glark)
    obj.respond_to?(:undefined_method).should be_true
  end

  it "causes #respond_to? to return false if called and returning false" do
    obj = mock('object')
    obj.should_receive(:respond_to_missing?).with(:undefined_method, false).and_return(false)
    obj.respond_to?(:undefined_method).should be_false
  end

  it "causes #respond_to? to return false if called and returning nil" do
    obj = mock('object')
    obj.should_receive(:respond_to_missing?).with(:undefined_method, false).and_return(nil)
    obj.respond_to?(:undefined_method).should be_false
  end

  it "isn't called when obj responds to the given public method" do
    @a.should_not_receive(:respond_to_missing?)
    @a.respond_to?(:pub_method).should be_true
  end

  it "isn't called when obj responds to the given public method, include_private = true" do
    @a.should_not_receive(:respond_to_missing?)
    @a.respond_to?(:pub_method, true).should be_true
  end

  it "is called when obj responds to the given protected method, include_private = false" do
    @a.should_receive(:respond_to_missing?)
    @a.respond_to?(:protected_method, false).should be_false
  end

  it "isn't called when obj responds to the given protected method, include_private = true" do
    @a.should_not_receive(:respond_to_missing?)
    @a.respond_to?(:protected_method, true).should be_true
  end

  it "is called when obj responds to the given private method, include_private = false" do
    @a.should_receive(:respond_to_missing?).with(:private_method, false)
    @a.respond_to?(:private_method)
  end

  it "isn't called when obj responds to the given private method, include_private = true" do
    @a.should_not_receive(:respond_to_missing?)
    @a.respond_to?(:private_method, true).should be_true
  end

  it "is called for missing class methods" do
    @a.class.should_receive(:respond_to_missing?).with(:oOoOoO, false)
    @a.class.respond_to?(:oOoOoO)
  end
end