| 1 |
|
| 2 |
require 'irb' |
| 3 |
|
| 4 |
require_relative "../helper" |
| 5 |
|
| 6 |
module TestIRB |
| 7 |
class ShowSourceTest < IntegrationTestCase |
| 8 |
def setup |
| 9 |
super |
| 10 |
|
| 11 |
write_rc <<~'RUBY' |
| 12 |
IRB.conf[:USE_PAGER] = false |
| 13 |
RUBY |
| 14 |
end |
| 15 |
|
| 16 |
def test_show_source |
| 17 |
write_ruby <<~'RUBY' |
| 18 |
binding.irb |
| 19 |
RUBY |
| 20 |
|
| 21 |
out = run_ruby_file do |
| 22 |
type "show_source IRB.conf" |
| 23 |
type "exit" |
| 24 |
end |
| 25 |
|
| 26 |
assert_match(%r[/irb\/init\.rb], out) |
| 27 |
end |
| 28 |
|
| 29 |
def test_show_source_alias |
| 30 |
write_ruby <<~'RUBY' |
| 31 |
binding.irb |
| 32 |
RUBY |
| 33 |
|
| 34 |
out = run_ruby_file do |
| 35 |
type "$ IRB.conf" |
| 36 |
type "exit" |
| 37 |
end |
| 38 |
|
| 39 |
assert_match(%r[/irb\/init\.rb], out) |
| 40 |
end |
| 41 |
|
| 42 |
def test_show_source_with_missing_signature |
| 43 |
write_ruby <<~'RUBY' |
| 44 |
binding.irb |
| 45 |
RUBY |
| 46 |
|
| 47 |
out = run_ruby_file do |
| 48 |
type "show_source foo" |
| 49 |
type "exit" |
| 50 |
end |
| 51 |
|
| 52 |
assert_match(%r[Couldn't locate a definition for foo], out) |
| 53 |
end |
| 54 |
|
| 55 |
def test_show_source_with_missing_constant |
| 56 |
write_ruby <<~'RUBY' |
| 57 |
binding.irb |
| 58 |
RUBY |
| 59 |
|
| 60 |
out = run_ruby_file do |
| 61 |
type "show_source Foo" |
| 62 |
type "exit" |
| 63 |
end |
| 64 |
|
| 65 |
assert_match(%r[Couldn't locate a definition for Foo], out) |
| 66 |
end |
| 67 |
|
| 68 |
def test_show_source_string |
| 69 |
write_ruby <<~'RUBY' |
| 70 |
binding.irb |
| 71 |
RUBY |
| 72 |
|
| 73 |
out = run_ruby_file do |
| 74 |
type "show_source 'IRB.conf'" |
| 75 |
type "exit" |
| 76 |
end |
| 77 |
|
| 78 |
assert_match(%r[/irb\/init\.rb], out) |
| 79 |
end |
| 80 |
|
| 81 |
def test_show_source_method_s |
| 82 |
write_ruby <<~RUBY |
| 83 |
class Baz |
| 84 |
def foo |
| 85 |
end |
| 86 |
end |
| 87 |
|
| 88 |
class Bar < Baz |
| 89 |
def foo |
| 90 |
super |
| 91 |
end |
| 92 |
end |
| 93 |
|
| 94 |
binding.irb |
| 95 |
RUBY |
| 96 |
|
| 97 |
out = run_ruby_file do |
| 98 |
type "show_source Bar#foo -s" |
| 99 |
type "exit" |
| 100 |
end |
| 101 |
|
| 102 |
assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end\r\n], out) |
| 103 |
end |
| 104 |
|
| 105 |
def test_show_source_method_s_with_incorrect_signature |
| 106 |
write_ruby <<~RUBY |
| 107 |
class Baz |
| 108 |
def foo |
| 109 |
end |
| 110 |
end |
| 111 |
|
| 112 |
class Bar < Baz |
| 113 |
def foo |
| 114 |
super |
| 115 |
end |
| 116 |
end |
| 117 |
|
| 118 |
binding.irb |
| 119 |
RUBY |
| 120 |
|
| 121 |
out = run_ruby_file do |
| 122 |
type "show_source Bar#fooo -s" |
| 123 |
type "exit" |
| 124 |
end |
| 125 |
|
| 126 |
assert_match(%r[Error: Couldn't locate a super definition for Bar#fooo], out) |
| 127 |
end |
| 128 |
|
| 129 |
def test_show_source_private_method |
| 130 |
write_ruby <<~RUBY |
| 131 |
class Bar |
| 132 |
private def foo |
| 133 |
end |
| 134 |
end |
| 135 |
binding.irb |
| 136 |
RUBY |
| 137 |
|
| 138 |
out = run_ruby_file do |
| 139 |
type "show_source Bar#foo" |
| 140 |
type "exit" |
| 141 |
end |
| 142 |
|
| 143 |
assert_match(%r[#{@ruby_file.to_path}:2\s+private def foo\r\n end\r\n], out) |
| 144 |
end |
| 145 |
|
| 146 |
def test_show_source_private_singleton_method |
| 147 |
write_ruby <<~RUBY |
| 148 |
class Bar |
| 149 |
private def foo |
| 150 |
end |
| 151 |
end |
| 152 |
binding.irb |
| 153 |
RUBY |
| 154 |
|
| 155 |
out = run_ruby_file do |
| 156 |
type "bar = Bar.new" |
| 157 |
type "show_source bar.foo" |
| 158 |
type "exit" |
| 159 |
end |
| 160 |
|
| 161 |
assert_match(%r[#{@ruby_file.to_path}:2\s+private def foo\r\n end\r\n], out) |
| 162 |
end |
| 163 |
|
| 164 |
def test_show_source_method_multiple_s |
| 165 |
write_ruby <<~RUBY |
| 166 |
class Baz |
| 167 |
def foo |
| 168 |
end |
| 169 |
end |
| 170 |
|
| 171 |
class Bar < Baz |
| 172 |
def foo |
| 173 |
super |
| 174 |
end |
| 175 |
end |
| 176 |
|
| 177 |
class Bob < Bar |
| 178 |
def foo |
| 179 |
super |
| 180 |
end |
| 181 |
end |
| 182 |
|
| 183 |
binding.irb |
| 184 |
RUBY |
| 185 |
|
| 186 |
out = run_ruby_file do |
| 187 |
type "show_source Bob#foo -ss" |
| 188 |
type "exit" |
| 189 |
end |
| 190 |
|
| 191 |
assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end\r\n], out) |
| 192 |
end |
| 193 |
|
| 194 |
def test_show_source_method_no_instance_method |
| 195 |
write_ruby <<~RUBY |
| 196 |
class Baz |
| 197 |
end |
| 198 |
|
| 199 |
class Bar < Baz |
| 200 |
def foo |
| 201 |
super |
| 202 |
end |
| 203 |
end |
| 204 |
|
| 205 |
binding.irb |
| 206 |
RUBY |
| 207 |
|
| 208 |
out = run_ruby_file do |
| 209 |
type "show_source Bar#foo -s" |
| 210 |
type "exit" |
| 211 |
end |
| 212 |
|
| 213 |
assert_match(%r[Error: Couldn't locate a super definition for Bar#foo], out) |
| 214 |
end |
| 215 |
|
| 216 |
def test_show_source_method_exceeds_super_chain |
| 217 |
write_ruby <<~RUBY |
| 218 |
class Baz |
| 219 |
def foo |
| 220 |
end |
| 221 |
end |
| 222 |
|
| 223 |
class Bar < Baz |
| 224 |
def foo |
| 225 |
super |
| 226 |
end |
| 227 |
end |
| 228 |
|
| 229 |
binding.irb |
| 230 |
RUBY |
| 231 |
|
| 232 |
out = run_ruby_file do |
| 233 |
type "show_source Bar#foo -ss" |
| 234 |
type "exit" |
| 235 |
end |
| 236 |
|
| 237 |
assert_match(%r[Error: Couldn't locate a super definition for Bar#foo], out) |
| 238 |
end |
| 239 |
|
| 240 |
def test_show_source_method_accidental_characters |
| 241 |
write_ruby <<~'RUBY' |
| 242 |
class Baz |
| 243 |
def foo |
| 244 |
end |
| 245 |
end |
| 246 |
|
| 247 |
class Bar < Baz |
| 248 |
def foo |
| 249 |
super |
| 250 |
end |
| 251 |
end |
| 252 |
|
| 253 |
binding.irb |
| 254 |
RUBY |
| 255 |
|
| 256 |
out = run_ruby_file do |
| 257 |
type "show_source Bar#foo -sddddd" |
| 258 |
type "exit" |
| 259 |
end |
| 260 |
|
| 261 |
assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end], out) |
| 262 |
end |
| 263 |
|
| 264 |
def test_show_source_receiver_super |
| 265 |
write_ruby <<~RUBY |
| 266 |
class Baz |
| 267 |
def foo |
| 268 |
end |
| 269 |
end |
| 270 |
|
| 271 |
class Bar < Baz |
| 272 |
def foo |
| 273 |
super |
| 274 |
end |
| 275 |
end |
| 276 |
|
| 277 |
binding.irb |
| 278 |
RUBY |
| 279 |
|
| 280 |
out = run_ruby_file do |
| 281 |
type "bar = Bar.new" |
| 282 |
type "show_source bar.foo -s" |
| 283 |
type "exit" |
| 284 |
end |
| 285 |
|
| 286 |
assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end], out) |
| 287 |
end |
| 288 |
|
| 289 |
def test_show_source_with_double_colons |
| 290 |
write_ruby <<~RUBY |
| 291 |
class Foo |
| 292 |
end |
| 293 |
|
| 294 |
class Foo |
| 295 |
class Bar |
| 296 |
end |
| 297 |
end |
| 298 |
|
| 299 |
binding.irb |
| 300 |
RUBY |
| 301 |
|
| 302 |
out = run_ruby_file do |
| 303 |
type "show_source ::Foo" |
| 304 |
type "exit" |
| 305 |
end |
| 306 |
|
| 307 |
assert_match(%r[#{@ruby_file.to_path}:1\s+class Foo\r\nend], out) |
| 308 |
|
| 309 |
out = run_ruby_file do |
| 310 |
type "show_source ::Foo::Bar" |
| 311 |
type "exit" |
| 312 |
end |
| 313 |
|
| 314 |
assert_match(%r[#{@ruby_file.to_path}:5\s+class Bar\r\n end], out) |
| 315 |
end |
| 316 |
|
| 317 |
def test_show_source_keep_script_lines |
| 318 |
pend unless defined?(RubyVM.keep_script_lines) |
| 319 |
|
| 320 |
write_ruby <<~RUBY |
| 321 |
binding.irb |
| 322 |
RUBY |
| 323 |
|
| 324 |
out = run_ruby_file do |
| 325 |
type "def foo; end" |
| 326 |
type "show_source foo" |
| 327 |
type "exit" |
| 328 |
end |
| 329 |
|
| 330 |
assert_match(%r[#{@ruby_file.to_path}\(irb\):1\s+def foo; end], out) |
| 331 |
end |
| 332 |
|
| 333 |
def test_show_source_unavailable_source |
| 334 |
write_ruby <<~RUBY |
| 335 |
binding.irb |
| 336 |
RUBY |
| 337 |
|
| 338 |
out = run_ruby_file do |
| 339 |
type "RubyVM.keep_script_lines = false if defined?(RubyVM.keep_script_lines)" |
| 340 |
type "def foo; end" |
| 341 |
type "show_source foo" |
| 342 |
type "exit" |
| 343 |
end |
| 344 |
assert_match(%r[#{@ruby_file.to_path}\(irb\):2\s+Source not available], out) |
| 345 |
end |
| 346 |
|
| 347 |
def test_show_source_shows_binary_source |
| 348 |
write_ruby <<~RUBY |
| 349 |
# io-console is an indirect dependency of irb |
| 350 |
require "io/console" |
| 351 |
|
| 352 |
binding.irb |
| 353 |
RUBY |
| 354 |
|
| 355 |
out = run_ruby_file do |
| 356 |
|
| 357 |
type "show_source IO::ConsoleMode" |
| 358 |
type "exit" |
| 359 |
end |
| 360 |
|
| 361 |
|
| 362 |
refute_match(/NameError/, out) |
| 363 |
assert_match(%r[Defined in binary file:.+io/console], out) |
| 364 |
end |
| 365 |
|
| 366 |
def test_show_source_with_constant_lookup |
| 367 |
write_ruby <<~RUBY |
| 368 |
X = 1 |
| 369 |
module M |
| 370 |
Y = 1 |
| 371 |
Z = 2 |
| 372 |
end |
| 373 |
class A |
| 374 |
Z = 1 |
| 375 |
Array = 1 |
| 376 |
class B |
| 377 |
include M |
| 378 |
Object.new.instance_eval { binding.irb } |
| 379 |
end |
| 380 |
end |
| 381 |
RUBY |
| 382 |
|
| 383 |
out = run_ruby_file do |
| 384 |
type "show_source X" |
| 385 |
type "show_source Y" |
| 386 |
type "show_source Z" |
| 387 |
type "show_source Array" |
| 388 |
type "exit" |
| 389 |
end |
| 390 |
|
| 391 |
assert_match(%r[#{@ruby_file.to_path}:1\s+X = 1], out) |
| 392 |
assert_match(%r[#{@ruby_file.to_path}:3\s+Y = 1], out) |
| 393 |
assert_match(%r[#{@ruby_file.to_path}:7\s+Z = 1], out) |
| 394 |
assert_match(%r[#{@ruby_file.to_path}:8\s+Array = 1], out) |
| 395 |
end |
| 396 |
end |
| 397 |
end |