summaryrefslogtreecommitdiff
path: root/spec/ruby/core/kernel/singleton_methods_spec.rb
blob: 74fd4a960819edc1bce48befe5f0a0e803364852 (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
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
174
175
176
177
178
179
180
require_relative '../../spec_helper'
require_relative '../../fixtures/reflection'
require_relative 'fixtures/classes'

describe :kernel_singleton_methods, shared: true do
  it "returns an empty Array for an object with no singleton methods" do
    ReflectSpecs.o.singleton_methods(*@object).should == []
  end

  it "returns the names of module methods for a module" do
    ReflectSpecs::M.singleton_methods(*@object).should include(:ms_pro, :ms_pub)
  end

  it "does not return private module methods for a module" do
    ReflectSpecs::M.singleton_methods(*@object).should_not include(:ms_pri)
  end

  it "returns the names of class methods for a class" do
    ReflectSpecs::A.singleton_methods(*@object).should include(:as_pro, :as_pub)
  end

  it "does not return private class methods for a class" do
    ReflectSpecs::A.singleton_methods(*@object).should_not include(:as_pri)
  end

  it "returns the names of singleton methods for an object" do
    ReflectSpecs.os.singleton_methods(*@object).should include(:os_pro, :os_pub)
  end
end

describe :kernel_singleton_methods_modules, shared: true do
  it "does not return any included methods for a module including a module" do
    ReflectSpecs::N.singleton_methods(*@object).should include(:ns_pro, :ns_pub)
  end

  it "does not return any included methods for a class including a module" do
    ReflectSpecs::D.singleton_methods(*@object).should include(:ds_pro, :ds_pub)
  end
end

describe :kernel_singleton_methods_supers, shared: true do
  it "returns the names of singleton methods for an object extented with a module" do
    ReflectSpecs.oe.singleton_methods(*@object).should include(:m_pro, :m_pub)
  end

  it "returns a unique list for an object extended with a module" do
    m = ReflectSpecs.oed.singleton_methods(*@object)
    r = m.select { |x| x == :pub or x == :pro }.sort
    r.should == [:pro, :pub]
  end

  it "returns the names of singleton methods for an object extented with two modules" do
    ReflectSpecs.oee.singleton_methods(*@object).should include(:m_pro, :m_pub, :n_pro, :n_pub)
  end

  it "returns the names of singleton methods for an object extented with a module including a module" do
    ReflectSpecs.oei.singleton_methods(*@object).should include(:n_pro, :n_pub, :m_pro, :m_pub)
  end

  it "returns the names of inherited singleton methods for a subclass" do
    ReflectSpecs::B.singleton_methods(*@object).should include(:as_pro, :as_pub, :bs_pro, :bs_pub)
  end

  it "returns a unique list for a subclass" do
    m = ReflectSpecs::B.singleton_methods(*@object)
    r = m.select { |x| x == :pub or x == :pro }.sort
    r.should == [:pro, :pub]
  end

  it "returns the names of inherited singleton methods for a subclass including a module" do
    ReflectSpecs::C.singleton_methods(*@object).should include(:as_pro, :as_pub, :cs_pro, :cs_pub)
  end

  it "returns a unique list for a subclass including a module" do
    m = ReflectSpecs::C.singleton_methods(*@object)
    r = m.select { |x| x == :pub or x == :pro }.sort
    r.should == [:pro, :pub]
  end

  it "returns the names of inherited singleton methods for a subclass of a class including a module" do
    ReflectSpecs::E.singleton_methods(*@object).should include(:ds_pro, :ds_pub, :es_pro, :es_pub)
  end

  it "returns the names of inherited singleton methods for a subclass of a class that includes a module, where the subclass also includes a module" do
    ReflectSpecs::F.singleton_methods(*@object).should include(:ds_pro, :ds_pub, :fs_pro, :fs_pub)
  end

  it "returns the names of inherited singleton methods for a class extended with a module" do
    ReflectSpecs::P.singleton_methods(*@object).should include(:m_pro, :m_pub)
  end
end

describe :kernel_singleton_methods_private_supers, shared: true do
  it "does not return private singleton methods for an object extended with a module" do
    ReflectSpecs.oe.singleton_methods(*@object).should_not include(:m_pri)
  end

  it "does not return private singleton methods for an object extended with two modules" do
    ReflectSpecs.oee.singleton_methods(*@object).should_not include(:m_pri)
  end

  it "does not return private singleton methods for an object extented with a module including a module" do
    ReflectSpecs.oei.singleton_methods(*@object).should_not include(:n_pri, :m_pri)
  end

  it "does not return private singleton methods for a class extended with a module" do
    ReflectSpecs::P.singleton_methods(*@object).should_not include(:m_pri)
  end

  it "does not return private inherited singleton methods for a module including a module" do
    ReflectSpecs::N.singleton_methods(*@object).should_not include(:ns_pri)
  end

  it "does not return private inherited singleton methods for a class including a module" do
    ReflectSpecs::D.singleton_methods(*@object).should_not include(:ds_pri)
  end

  it "does not return private inherited singleton methods for a subclass" do
    ReflectSpecs::B.singleton_methods(*@object).should_not include(:as_pri, :bs_pri)
  end

  it "does not return private inherited singleton methods for a subclass including a module" do
    ReflectSpecs::C.singleton_methods(*@object).should_not include(:as_pri, :cs_pri)
  end

  it "does not return private inherited singleton methods for a subclass of a class including a module" do
    ReflectSpecs::E.singleton_methods(*@object).should_not include(:ds_pri, :es_pri)
  end

  it "does not return private inherited singleton methods for a subclass of a class that includes a module, where the subclass also includes a module" do
    ReflectSpecs::F.singleton_methods(*@object).should_not include(:ds_pri, :fs_pri)
  end
end

describe "Kernel#singleton_methods" do
  describe "when not passed an argument" do
    it_behaves_like :kernel_singleton_methods, nil, []
    it_behaves_like :kernel_singleton_methods_supers, nil, []
    it_behaves_like :kernel_singleton_methods_modules, nil, []
    it_behaves_like :kernel_singleton_methods_private_supers, nil, []
  end

  describe "when passed true" do
    it_behaves_like :kernel_singleton_methods, nil, true
    it_behaves_like :kernel_singleton_methods_supers, nil, true
    it_behaves_like :kernel_singleton_methods_modules, nil, true
    it_behaves_like :kernel_singleton_methods_private_supers, nil, true

  end

  describe "when passed false" do
    it_behaves_like :kernel_singleton_methods, nil, false
    it_behaves_like :kernel_singleton_methods_modules, nil, false
    it_behaves_like :kernel_singleton_methods_private_supers, nil, false

    it "returns an empty Array for an object extented with a module" do
      ReflectSpecs.oe.singleton_methods(false).should == []
    end

    it "returns an empty Array for an object extented with two modules" do
      ReflectSpecs.oee.singleton_methods(false).should == []
    end

    it "returns an empty Array for an object extended with a module including a module" do
      ReflectSpecs.oei.singleton_methods(false).should == []
    end

    it "returns the names of singleton methods of the subclass" do
      ReflectSpecs::B.singleton_methods(false).should include(:bs_pro, :bs_pub)
    end

    it "does not return names of inherited singleton methods for a subclass" do
      ReflectSpecs::B.singleton_methods(false).should_not include(:as_pro, :as_pub)
    end

    it "does not return the names of inherited singleton methods for a class extended with a module" do
      ReflectSpecs::P.singleton_methods(false).should_not include(:m_pro, :m_pub)
    end
  end
end