summaryrefslogtreecommitdiff
path: root/spec/code_object_spec.rb
blob: 9d23ffaff23ebf43a2c4e712029df840c60e8cfc (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require_relative 'helper'

describe Pry::CodeObject do
  describe "basic lookups" do
    before do
      @obj = Object.new
      def @obj.ziggy
        "a flight of scarlet pigeons thunders round my thoughts"
      end

      class ClassyWassy
        def piggy
          binding
        end
      end

      @p = Pry.new
      @p.binding_stack = [binding]
    end

    after do
      Object.remove_const(:ClassyWassy)
    end

    it 'should lookup methods' do
      m = Pry::CodeObject.lookup("@obj.ziggy", @p)
      m.is_a?(Pry::Method).should.be_true
      m.name.to_sym.should == :ziggy
    end

    it 'should lookup modules' do
      m = Pry::CodeObject.lookup("ClassyWassy", @p)
      m.is_a?(Pry::WrappedModule).should.be_true
      m.source.should =~ /piggy/
    end

    it 'should lookup procs' do
      my_proc = proc { :hello }
      @p.binding_stack = [binding]
      m = Pry::CodeObject.lookup("my_proc", @p)
      m.is_a?(Pry::Method).should.be_true
      m.source.should =~ /hello/
    end

    describe 'commands lookup' do
      before do
        @p = Pry.new
        @p.binding_stack = [binding]
      end

      it 'should return command class' do
        @p.commands.command "jeremy-jones" do
          "lobster"
        end
        m = Pry::CodeObject.lookup("jeremy-jones", @p)
        (m <= Pry::Command).should.be_true
        m.source.should =~ /lobster/
      end

      describe "class commands" do
        before do
          class LobsterLady < Pry::ClassCommand
            match "lobster-lady"
            description "nada."
            def process
              "lobster"
            end
          end
        end

        after do
          Object.remove_const(:LobsterLady)
        end

        it 'should return Pry::ClassCommand class when looking up class command' do
          Pry.commands.add_command(LobsterLady)
          m = Pry::CodeObject.lookup("lobster-lady", @p)
          (m <= Pry::ClassCommand).should.be_true
          m.source.should =~ /class LobsterLady/
          Pry.commands.delete("lobster-lady")
        end

        it 'should return Pry::WrappedModule when looking up command class directly (as a class, not as a command)' do
          Pry.commands.add_command(LobsterLady)
          m = Pry::CodeObject.lookup("LobsterLady", @p)
          m.is_a?(Pry::WrappedModule).should.be_true
          m.source.should =~ /class LobsterLady/
          Pry.commands.delete("lobster-lady")
        end
      end

      it 'looks up commands by :listing name as well' do
        @p.commands.command /jeremy-.*/, "", :listing => "jeremy-baby" do
          "lobster"
        end
        m = Pry::CodeObject.lookup("jeremy-baby", @p)
        (m <= Pry::Command).should.be_true
        m.source.should =~ /lobster/
      end

      it 'finds nothing when passing nil as the first argument' do
        Pry::CodeObject.lookup(nil, @p).should == nil
      end

    end

    it 'should lookup instance methods defined on classes accessed via local variable' do
      o = Class.new do
        def princess_bubblegum
          "mathematic!"
        end
      end

      @p.binding_stack = [binding]
      m = Pry::CodeObject.lookup("o#princess_bubblegum", @p)
      m.is_a?(Pry::Method).should.be_true
      m.source.should =~ /mathematic!/
    end

    it 'should lookup class methods defined on classes accessed via local variable' do
      o = Class.new do
        def self.finn
          "4 realzies"
        end
      end
      @p.binding_stack = [binding]
      m = Pry::CodeObject.lookup("o.finn", @p)
      m.is_a?(Pry::Method).should.be_true
      m.source.should =~ /4 realzies/
    end

    it 'should lookup the class of an object (when given a variable)' do
      moddy = ClassyWassy.new
      @p.binding_stack = [binding]
      m = Pry::CodeObject.lookup("moddy", @p)
      m.is_a?(Pry::WrappedModule).should.be_true
      m.source.should =~ /piggy/
    end

    describe "inferring object from binding when lookup str is empty/nil" do
      before do
        @b1 = Pry.binding_for(ClassyWassy)
        @b2 = Pry.binding_for(ClassyWassy.new)
      end

      describe "infer module objects" do
        it 'should infer module object when binding self is a module' do
          ["", nil].each do |v|
            @p.binding_stack = [@b1]
            m = Pry::CodeObject.lookup(v, @p)
            m.is_a?(Pry::WrappedModule).should.be_true
            m.name.should =~ /ClassyWassy/
          end
        end

        it 'should infer module object when binding self is an instance' do
          ["", nil].each do |v|
            @p.binding_stack = [@b2]
            m = Pry::CodeObject.lookup(v, @p)
            m.is_a?(Pry::WrappedModule).should.be_true
            m.name.should =~ /ClassyWassy/
          end
        end
      end

      describe "infer method objects" do
        it 'should infer method object from binding when inside method context' do
          b = ClassyWassy.new.piggy

          ["", nil].each do |v|
            @p.binding_stack = [b]
            m = Pry::CodeObject.lookup(v, @p)
            m.is_a?(Pry::Method).should.be_true
            m.name.should =~ /piggy/
          end
        end
      end
    end
  end

  describe "lookups with :super" do
    before do
      class MyClassyWassy; end
      class CuteSubclass < MyClassyWassy; end
      @p = Pry.new
      @p.binding_stack = [binding]
    end

    after do
      Object.remove_const(:MyClassyWassy)
      Object.remove_const(:CuteSubclass)
    end

    it 'should lookup original class with :super => 0' do
      m = Pry::CodeObject.lookup("CuteSubclass", @p, :super => 0)
      m.is_a?(Pry::WrappedModule).should.be_true
      m.wrapped.should == CuteSubclass
    end

    it 'should lookup immediate super class with :super => 1' do
      m = Pry::CodeObject.lookup("CuteSubclass", @p, :super => 1)
      m.is_a?(Pry::WrappedModule).should.be_true
      m.wrapped.should == MyClassyWassy
    end

    it 'should ignore :super parameter for commands' do
      p = Pry.new
      p.commands.command "jeremy-jones" do
        "lobster"
      end
      p.binding_stack = [binding]
      m = Pry::CodeObject.lookup("jeremy-jones", p, :super => 10)
      m.source.should =~ /lobster/
    end
  end

  describe "precedence" do
    before do
      class ClassyWassy
        class Puff
          def tiggy
          end
        end

        def Puff
        end

        def piggy
        end
      end

      Object.class_eval do
        def ClassyWassy
          :ducky
        end
      end

      @p = Pry.new
      @p.binding_stack = [binding]
    end

    after do
      Object.remove_const(:ClassyWassy)
      Object.remove_method(:ClassyWassy)
    end

    it 'should look up classes before methods (at top-level)' do
      m = Pry::CodeObject.lookup("ClassyWassy", @p)
      m.is_a?(Pry::WrappedModule).should.be_true
      m.source.should =~ /piggy/
    end

    it 'should look up methods before classes when ending in () (at top-level)' do
      m = Pry::CodeObject.lookup("ClassyWassy()", @p)
      m.is_a?(Pry::Method).should.be_true
      m.source.should =~ /ducky/
    end

    it 'should look up classes before methods when namespaced' do
      m = Pry::CodeObject.lookup("ClassyWassy::Puff", @p)
      m.is_a?(Pry::WrappedModule).should.be_true
      m.source.should =~ /tiggy/
    end

    it 'should look up locals before methods' do
      b = Pry.binding_for(ClassyWassy)
      b.eval("piggy = Puff.new")
      @p.binding_stack = [b]
      o = Pry::CodeObject.lookup("piggy", @p)
      o.is_a?(Pry::WrappedModule).should.be_true
    end

    # actually locals are never looked up (via co.default_lookup)  when they're classes, it
    # just falls through to co.method_or_class
    it 'should look up classes before locals' do
      c = ClassyWassy
      @p.binding_stack = [binding]
      o = Pry::CodeObject.lookup("c", @p)
      o.is_a?(Pry::WrappedModule).should.be_true
      o.wrapped.should == ClassyWassy
    end
  end
end