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

describe "Module#public_instance_method" do
  it "is a public method" do
    Module.should have_public_instance_method(:public_instance_method, false)
  end

  it "requires an argument" do
    Module.new.method(:public_instance_method).arity.should == 1
  end

  describe "when given a public method name" do
    it "returns an UnboundMethod corresponding to the defined Module" do
      ret = ModuleSpecs::Super.public_instance_method(:public_module)
      ret.should be_an_instance_of(UnboundMethod)
      ret.owner.should equal(ModuleSpecs::Basic)

      ret = ModuleSpecs::Super.public_instance_method(:public_super_module)
      ret.should be_an_instance_of(UnboundMethod)
      ret.owner.should equal(ModuleSpecs::Super)
    end

    it "accepts if the name is a Symbol or String" do
      ret = ModuleSpecs::Basic.public_instance_method(:public_module)
      ModuleSpecs::Basic.public_instance_method("public_module").should == ret
    end
  end

  it "raises a TypeError when given a name is not Symbol or String" do
    lambda { Module.new.public_instance_method(nil) }.should raise_error(TypeError)
  end

  it "raises a NameError when given a protected method name" do
    lambda do
      ModuleSpecs::Basic.public_instance_method(:protected_module)
    end.should raise_error(NameError)
  end

  it "raises a NameError if the method is private" do
    lambda do
      ModuleSpecs::Basic.public_instance_method(:private_module)
    end.should raise_error(NameError)
  end

  it "raises a NameError if the method has been undefined" do
    lambda do
      ModuleSpecs::Parent.public_instance_method(:undefed_method)
    end.should raise_error(NameError)
  end

  it "raises a NameError if the method does not exist" do
    lambda do
      Module.new.public_instance_method(:missing)
    end.should raise_error(NameError)
  end

  it "sets the NameError#name attribute to the name of the missing method" do
    begin
      Module.new.public_instance_method(:missing)
    rescue NameError => e
      e.name.should == :missing
    end
  end
end