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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
# frozen_string_literal: true
RSpec.describe Pry::CodeObject do
let(:pry) do
Pry.new.tap { |p| p.binding_stack = [binding] }
end
describe ".lookup" do
context "when looking up method" do
let(:pry) do
obj = Class.new.new
def obj.foo_method; end
Pry.new.tap { |p| p.binding_stack = [binding] }
end
it "finds methods defined on objects" do
code_object = described_class.lookup('obj.foo_method', pry)
expect(code_object).to be_a(Pry::Method)
expect(code_object.name).to eq('foo_method')
end
end
context "when looking up modules" do
module FindMeModule; end
after { Object.remove_const(:FindMeModule) }
it "finds modules" do
code_object = described_class.lookup('FindMeModule', pry)
expect(code_object).to be_a(Pry::WrappedModule)
end
end
context "when looking up classes" do
class FindMeClass; end
after { Object.remove_const(:FindMeClass) }
it "finds classes" do
code_object = described_class.lookup('FindMeClass', pry)
expect(code_object).to be_a(Pry::WrappedModule)
end
end
context "when looking up procs" do
let(:test_proc) { proc { :hello } }
it "finds classes" do
code_object = described_class.lookup('test_proc', pry)
expect(code_object).to be_a(Pry::Method)
expect(code_object.wrapped.call).to eql(test_proc)
end
end
context "when looking up Pry::BlockCommand" do
let(:pry) do
pry = Pry.new
pry.commands.command('test-block-command') {}
pry.binding_stack = [binding]
pry
end
it "finds Pry:BlockCommand" do
code_object = described_class.lookup('test-block-command', pry)
expect(code_object.command_name).to eq('test-block-command')
end
end
context "when looking up Pry::ClassCommand" do
class TestClassCommand < Pry::ClassCommand
match 'test-class-command'
end
let(:pry) do
pry = Pry.new
pry.commands.add_command(TestClassCommand)
pry.binding_stack = [binding]
pry
end
after { Object.remove_const(:TestClassCommand) }
it "finds Pry:BlockCommand" do
code_object = described_class.lookup('test-class-command', pry)
expect(code_object.command_name).to eq('test-class-command')
end
end
context "when looking up Pry commands by class" do
class TestCommand < Pry::ClassCommand
match 'test-command'
end
let(:pry) do
pry = Pry.new
pry.commands.add_command(TestCommand)
pry.binding_stack = [binding]
pry
end
after { Object.remove_const(:TestCommand) }
it "finds Pry::WrappedModule" do
code_object = described_class.lookup('TestCommand', pry)
expect(code_object).to be_a(Pry::WrappedModule)
end
end
context "when looking up Pry commands by listing" do
let(:pry) do
pry = Pry.new
pry.commands.command('test-command', listing: 'test-listing') {}
pry.binding_stack = [binding]
pry
end
it "finds Pry::WrappedModule" do
code_object = described_class.lookup('test-listing', pry)
expect(code_object.command_name).to eq('test-listing')
end
end
context "when looking up 'nil'" do
it "returns nil" do
pry = Pry.new
pry.binding_stack = [binding]
code_object = described_class.lookup(nil, pry)
expect(code_object).to be_nil
end
end
context "when looking up 'nil' while being inside a module" do
let(:pry) do
Pry.new.tap { |p| p.binding_stack = [Pry.binding_for(Module)] }
end
it "infers the module" do
code_object = described_class.lookup(nil, pry)
expect(code_object).to be_a(Pry::WrappedModule)
end
end
context "when looking up empty string while being inside a module" do
let(:pry) do
Pry.new.tap { |p| p.binding_stack = [Pry.binding_for(Module)] }
end
it "infers the module" do
code_object = described_class.lookup('', pry)
expect(code_object).to be_a(Pry::WrappedModule)
end
end
context "when looking up 'nil' while being inside a class instance" do
let(:pry) do
Pry.new.tap { |p| p.binding_stack = [Pry.binding_for(Module.new)] }
end
it "infers the module" do
code_object = described_class.lookup(nil, pry)
expect(code_object).to be_a(Pry::WrappedModule)
end
end
context "when looking up empty string while being inside a class instance" do
let(:pry) do
Pry.new.tap { |p| p.binding_stack = [Pry.binding_for(Module.new)] }
end
it "infers the module" do
code_object = described_class.lookup('', pry)
expect(code_object).to be_a(Pry::WrappedModule)
end
end
context "when looking up 'nil' while being inside a method" do
let(:pry) do
klass = Class.new do
def test_binding
binding
end
end
Pry.new.tap { |p| p.binding_stack = [klass.new.test_binding] }
end
it "infers the method" do
code_object = described_class.lookup(nil, pry)
expect(code_object).to be_a(Pry::Method)
end
end
context "when looking up empty string while being inside a method" do
let(:pry) do
klass = Class.new do
def test_binding
binding
end
end
Pry.new.tap { |p| p.binding_stack = [klass.new.test_binding] }
end
it "infers the method" do
code_object = described_class.lookup('', pry)
expect(code_object).to be_a(Pry::Method)
end
end
context "when looking up instance methods of a class" do
let(:pry) do
instance = Class.new do
def instance_method; end
end
Pry.new.tap { |p| p.binding_stack = [binding] }
end
it "finds instance methods" do
code_object = described_class.lookup('instance#instance_method', pry)
expect(code_object).to be_a(Pry::Method)
end
end
context "when looking up instance methods" do
let(:pry) do
instance = Class.new do
def instance_method; end
end
Pry.new.tap { |p| p.binding_stack = [binding] }
end
it "finds instance methods via the # notation" do
code_object = described_class.lookup('instance#instance_method', pry)
expect(code_object).to be_a(Pry::Method)
end
it "finds instance methods via the . notation" do
code_object = described_class.lookup('instance.instance_method', pry)
expect(code_object).to be_a(Pry::Method)
end
end
context "when looking up anonymous class methods" do
let(:pry) do
klass = Class.new do
def self.class_method; end
end
Pry.new.tap { |p| p.binding_stack = [binding] }
end
it "finds instance methods via the # notation" do
code_object = described_class.lookup('klass.class_method', pry)
expect(code_object).to be_a(Pry::Method)
end
end
context "when looking up class methods of a named class" do
before do
class TestClass
def self.class_method; end
end
end
after { Object.remove_const(:TestClass) }
it "finds instance methods via the # notation" do
code_object = described_class.lookup('TestClass.class_method', pry)
expect(code_object).to be_a(Pry::Method)
end
end
context "when looking up classes by names of variables" do
let(:pry) do
klass = Class.new
Pry.new.tap { |p| p.binding_stack = [binding] }
end
it "finds instance methods via the # notation" do
code_object = described_class.lookup('klass', pry)
expect(code_object).to be_a(Pry::WrappedModule)
end
end
context "when looking up classes with 'super: 0'" do
let(:pry) do
class ParentClass; end
class ChildClass < ParentClass; end
Pry.new.tap { |p| p.binding_stack = [binding] }
end
after do
Object.remove_const(:ChildClass)
Object.remove_const(:ParentClass)
end
it "finds the child class" do
code_object = described_class.lookup('ChildClass', pry, super: 0)
expect(code_object).to be_a(Pry::WrappedModule)
expect(code_object.wrapped).to eq(ChildClass)
end
end
context "when looking up classes with 'super: 1'" do
let(:pry) do
class ParentClass; end
class ChildClass < ParentClass; end
Pry.new.tap { |p| p.binding_stack = [binding] }
end
after do
Object.remove_const(:ChildClass)
Object.remove_const(:ParentClass)
end
it "finds the parent class" do
code_object = described_class.lookup('ChildClass', pry, super: 1)
expect(code_object).to be_a(Pry::WrappedModule)
expect(code_object.wrapped).to eq(ParentClass)
end
end
context "when looking up commands with the super option" do
let(:pry) do
pry = Pry.new
pry.commands.command('test-command') {}
pry.binding_stack = [binding]
pry
end
it "finds the command ignoring the super option" do
code_object = described_class.lookup('test-command', pry, super: 1)
expect(code_object.command_name).to eq('test-command')
end
end
context "when there is a class and a method who is a namesake" do
let(:pry) do
class TestClass
class InnerTestClass; end
end
def TestClass; end
Pry.new.tap { |p| p.binding_stack = [binding] }
end
after { Object.remove_const(:TestClass) }
it "finds the class before the method" do
code_object = described_class.lookup('TestClass', pry)
expect(code_object).to be_a(Pry::WrappedModule)
end
it "finds the method when the look up ends with ()" do
code_object = described_class.lookup('TestClass()', pry)
expect(code_object).to be_a(Pry::Method)
end
it "finds the class before the method when it's namespaced" do
code_object = described_class.lookup('TestClass::InnerTestClass', pry)
expect(code_object).to be_a(Pry::WrappedModule)
expect(code_object.wrapped).to eq(TestClass::InnerTestClass)
end
end
end
end
|