File: methods_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (173 lines) | stat: -rw-r--r-- 6,262 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../../../fixtures/reflection', __FILE__)

# TODO: rewrite
# On Ruby < 1.9 #methods returns an Array of Strings
ruby_version_is ""..."1.9" do
  describe "Kernel#methods" do
    it "returns singleton methods defined by obj.meth" do
      KernelSpecs::Methods.methods(false).should include("ichi")
    end

    it "returns singleton methods defined in 'class << self'" do
      KernelSpecs::Methods.methods(false).should include("san")
    end

    it "returns private singleton methods defined by obj.meth" do
      KernelSpecs::Methods.methods(false).should include("shi")
    end

    it "returns singleton methods defined in 'class << self' when it follows 'private'" do
      KernelSpecs::Methods.methods(false).should include("roku")
    end

    it "does not return private singleton methods defined in 'class << self'" do
      KernelSpecs::Methods.methods(false).should_not include("shichi")
    end

    it "returns the publicly accessible methods of the object" do
      meths =  KernelSpecs::Methods.methods(false)
      meths.should include("hachi", "ichi", "juu", "juu_ichi",
                           "juu_ni", "roku", "san", "shi")

      KernelSpecs::Methods.new.methods(false).should == []
    end

    it "returns the publicly accessible methods in the object, its ancestors and mixed-in modules" do
      meths = KernelSpecs::Methods.methods(false) & KernelSpecs::Methods.methods
      meths.should include("hachi", "ichi", "juu", "juu_ichi",
                           "juu_ni", "roku", "san", "shi")

      KernelSpecs::Methods.new.methods.should include("ku", "ni", "juu_san")
    end

    it "returns methods added to the metaclass through extend" do
      meth = KernelSpecs::Methods.new
      meth.methods.should_not include("peekaboo")
      meth.extend(KernelSpecs::Methods::MetaclassMethods)
      meth.methods.should include("peekaboo")
    end

    it "does not return undefined singleton methods defined by obj.meth" do
      o = KernelSpecs::Child.new
      def o.single; end
      o.methods.should include("single")

      class << o; self; end.send :undef_method, :single
      o.methods.should_not include("single")
    end

    it "does not return superclass methods undefined in the object's class" do
      KernelSpecs::Child.new.methods.should_not include("parent_method")
    end

    it "does not return superclass methods undefined in a superclass" do
      KernelSpecs::Grandchild.new.methods.should_not include("parent_method")
    end

    it "does not return included module methods undefined in the object's class" do
      KernelSpecs::Grandchild.new.methods.should_not include("parent_mixin_method")
    end
  end
end

# On MRI >= 1.9 #methods returns an Array of Symbols.
ruby_version_is "1.9" do
  describe "Kernel#methods" do
    it "returns singleton methods defined by obj.meth" do
      KernelSpecs::Methods.methods(false).should include(:ichi)
    end

    it "returns singleton methods defined in 'class << self'" do
      KernelSpecs::Methods.methods(false).should include(:san)
    end

    it "returns private singleton methods defined by obj.meth" do
      KernelSpecs::Methods.methods(false).should include(:shi)
    end

    it "returns singleton methods defined in 'class << self' when it follows 'private'" do
      KernelSpecs::Methods.methods(false).should include(:roku)
    end

    it "does not return private singleton methods defined in 'class << self'" do
      KernelSpecs::Methods.methods(false).should_not include(:shichi)
    end

    it "returns the publicly accessible methods of the object" do
      meths =  KernelSpecs::Methods.methods(false)
      meths.should include(:hachi, :ichi, :juu, :juu_ichi,
                           :juu_ni, :roku, :san, :shi)

      KernelSpecs::Methods.new.methods(false).should == []
    end

    it "returns the publicly accessible methods in the object, its ancestors and mixed-in modules" do
      meths = KernelSpecs::Methods.methods(false) & KernelSpecs::Methods.methods
      meths.should include(:hachi, :ichi, :juu, :juu_ichi,
                           :juu_ni, :roku, :san, :shi)

      KernelSpecs::Methods.new.methods.should include(:ku, :ni, :juu_san)
    end

    it "returns methods added to the metaclass through extend" do
      meth = KernelSpecs::Methods.new
      meth.methods.should_not include(:peekaboo)
      meth.extend(KernelSpecs::Methods::MetaclassMethods)
      meth.methods.should include(:peekaboo)
    end

    it "does not return undefined singleton methods defined by obj.meth" do
      o = KernelSpecs::Child.new
      def o.single; end
      o.methods.should include(:single)

      class << o; self; end.send :undef_method, :single
      o.methods.should_not include(:single)
    end

    it "does not return superclass methods undefined in the object's class" do
      KernelSpecs::Child.new.methods.should_not include(:parent_method)
    end

    it "does not return superclass methods undefined in a superclass" do
      KernelSpecs::Grandchild.new.methods.should_not include(:parent_method)
    end

    it "does not return included module methods undefined in the object's class" do
      KernelSpecs::Grandchild.new.methods.should_not include(:parent_mixin_method)
    end
  end
end

describe :kernel_methods_supers, :shared => true do
  before :all do
    @ms = [stasy(:pro), stasy(:pub)]
  end

  it "returns a unique list for an object extended by a module" do
    m = ReflectSpecs.oed.methods(*@object)
    m.select { |x| @ms.include? x }.sort.should == @ms
  end

  it "returns a unique list for a class including a module" do
    m = ReflectSpecs::D.new.methods(*@object)
    m.select { |x| @ms.include? x }.sort.should == @ms
  end

  it "returns a unique list for a subclass of a class that includes a module" do
    m = ReflectSpecs::E.new.methods(*@object)
    m.select { |x| @ms.include? x }.sort.should == @ms
  end
end

describe "Kernel#methods" do
  describe "when not passed an argument" do
    it_behaves_like :kernel_methods_supers, nil, []
  end

  describe "when passed true" do
    it_behaves_like :kernel_methods_supers, nil, true
  end
end