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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
# frozen_string_literal: true
describe "commands" do
CommandHelper = Struct.new(:bs1, :bs2, :bs3, :self, :bong) do
def reset
@bs1, @bs2, @bs3, @self, @bong = nil
end
end.new
before do
@str_output = StringIO.new
@o = Object.new
# Shortcuts. They save a lot of typing.
@bs1 = "CommandHelper.bs1 = pry_instance.binding_stack.dup"
@bs2 = "CommandHelper.bs2 = pry_instance.binding_stack.dup"
@bs3 = "CommandHelper.bs3 = pry_instance.binding_stack.dup"
@self = "CommandHelper.self = self"
@command_tester = Pry::CommandSet.new do
command "command1", "command 1 test" do
output.puts "command1"
end
command "command2", "command 2 test" do |arg|
output.puts arg
end
end
CommandHelper.bong = "bong"
end
after do
CommandHelper.reset
Pry.reset_defaults
end
describe "alias_command" do
it 'should make an aliasd command behave like its original' do
set = Pry::CommandSet.new do
command "test-command" do
output.puts "testing 1, 2, 3"
end
alias_command "test-alias", "test-command"
end
pry_tester(commands: set).tap do |t|
expect(t.eval('test-command')).to eq t.eval('test-alias')
end
end
it 'should pass on arguments to original' do
set = Pry::CommandSet.new do
command "test-command" do |*args|
output.puts "testing #{args.join(' ')}"
end
alias_command "test-alias", "test-command"
end
t = pry_tester(commands: set)
t.process_command "test-alias hello baby duck"
expect(t.last_output).to match(/testing hello baby duck/)
end
it 'should pass option arguments to original' do
set = Pry::CommandSet.new do
import Pry::Commands
alias_command "test-alias", "ls"
end
obj = Class.new { @x = 10 }
t = pry_tester(obj, commands: set)
t.process_command "test-alias -i"
expect(t.last_output).to match(/@x/)
end
it 'should pass option arguments to original with additional parameters' do
set = Pry::CommandSet.new do
import Pry::Commands
alias_command "test-alias", "ls -M"
end
obj = Class.new { @x = Class.new { define_method(:plymouth) {} } }
t = pry_tester(obj, commands: set)
t.process_command "test-alias @x"
expect(t.last_output).to match(/plymouth/)
end
it 'should be able to alias a regex command' do
set = Pry::CommandSet.new do
command(/du.k/) do
output.puts "ducky"
end
alias_command "test-alias", "duck"
end
t = pry_tester(commands: set)
t.process_command "test-alias"
expect(t.last_output).to match(/ducky/)
end
it 'should be able to make the alias a regex' do
set = Pry::CommandSet.new do
command(/du.k/) do
output.puts "ducky"
end
alias_command(/test-ali.s/, "duck")
end
redirect_pry_io(InputTester.new("test-alias"), out1 = StringIO.new) do
Pry.start self, commands: set
end
expect(out1.string).to match(/ducky/)
end
end
describe "Pry::Command#run" do
it 'should allow running of commands with following whitespace' do
set = Pry::CommandSet.new do
import Pry::Commands
command "test-run" do
run "cd / "
end
end
redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6", @bs1, "test-run",
@self, @bs2, "exit-all")) do
Pry.start(@o, commands: set)
end
expect(CommandHelper.bs1.size).to eq 7
expect(CommandHelper.self).to eq @o
expect(CommandHelper.bs2.size).to eq 1
end
it 'should allow running of cd command when contained in a single string' do
set = Pry::CommandSet.new do
import Pry::Commands
command "test-run" do
run "cd /"
end
end
redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6", @bs1, "test-run",
@self, @bs2, "exit-all")) do
Pry.start(@o, commands: set)
end
expect(CommandHelper.bs1.size).to eq 7
expect(CommandHelper.self).to eq @o
expect(CommandHelper.bs2.size).to eq 1
end
it 'should allow running of cd command when split into array' do
set = Pry::CommandSet.new do
import Pry::Commands
command "test-run" do
run "cd", "/"
end
end
redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6", @bs1, "test-run",
@self, @bs2, "exit-all")) do
Pry.start(@o, commands: set)
end
expect(CommandHelper.bs1.size).to eq 7
expect(CommandHelper.self).to eq @o
expect(CommandHelper.bs2.size).to eq 1
end
it 'should run a command from within a command' do
klass = Pry::CommandSet.new do
command "v" do
output.puts "v command"
end
command "run_v" do
run "v"
end
end
expect(pry_tester(commands: klass).eval('run_v')).to match(/v command/)
end
it 'should run a regex command from within a command' do
klass = Pry::CommandSet.new do
command(/v(.*)?/) do |arg|
output.puts "v #{arg}"
end
command "run_v" do
run "vbaby"
end
end
expect(pry_tester(commands: klass).eval('run_v')).to match(/v baby/)
end
it 'should run a command from within a command with arguments' do
klass = Pry::CommandSet.new do
command(/v(\w+)/) do |arg1, arg2|
output.puts "v #{arg1} #{arg2}"
end
command "run_v_explicit_parameter" do
run "vbaby", "param"
end
command "run_v_embedded_parameter" do
run "vbaby param"
end
end
%w[run_v_explicit_parameter run_v_embedded_parameter].each do |cmd|
expect(pry_tester(commands: klass).eval(cmd)).to match(/v baby param/)
end
end
end
describe "Pry#run_command" do
it 'should run a command that modifies the passed in eval_string' do
p = Pry.new(output: @str_output)
p.eval "def hello\npeter pan\n"
p.run_command "amend-line !"
expect(p.eval_string).to match(/def hello/)
expect(p.eval_string).not_to match(/peter pan/)
end
it 'should run a command in the context of a session' do
pry_tester(Object.new).tap do |t|
t.eval "@session_ivar = 10", "pry_instance.run_command('ls')"
expect(t.last_output).to match(/@session_ivar/)
end
end
end
it 'should interpolate ruby code into commands' do
set = Pry::CommandSet.new do
command "hello", "", keep_retval: true do |arg|
arg
end
end
# rubocop:disable Lint/InterpolationCheck
expect(pry_tester(commands: set).eval('hello #{CommandHelper.bong}')).to match(/bong/)
# rubocop:enable Lint/InterpolationCheck
end
# bug fix for https://github.com/pry/pry/issues/170
it(
"doesn't choke on complex string interpolation when checking if ruby code " \
"is a command"
) do
redirect_pry_io(
# rubocop:disable Lint/InterpolationCheck
InputTester.new('/#{Regexp.escape(File.expand_path("."))}/'),
# rubocop:enable Lint/InterpolationCheck
@str_output
) do
pry
end
expect(@str_output.string).not_to match(/SyntaxError/)
end
it 'should NOT interpolate ruby code into commands if :interpolate => false' do
set = Pry::CommandSet.new do
command "hello", "", keep_retval: true, interpolate: false do |arg|
arg
end
end
# rubocop:disable Lint/InterpolationCheck
expect(pry_tester(commands: set).eval('hello #{CommandHelper.bong}'))
.to match(/CommandHelper\.bong/)
# rubocop:enable Lint/InterpolationCheck
end
it 'should NOT try to interpolate pure ruby code (no commands) ' do
# rubocop:disable Lint/InterpolationCheck
# These should raise RuntimeError instead of NameError
expect { pry_eval 'raise \'#{aggy}\'' }.to raise_error RuntimeError
expect { pry_eval 'raise #{aggy}' }.to raise_error RuntimeError
expect(pry_eval('format \'#{my_var}\'')).to eq "\#{my_var}"
# rubocop:enable Lint/InterpolationCheck
end
it 'should create a command with a space in its name zzz' do
set = Pry::CommandSet.new do
command "hello baby", "" do
output.puts "hello baby command"
end
end
expect(pry_tester(commands: set).eval('hello baby'))
.to match(/hello baby command/)
end
it 'should create a command with a space in its name and pass an argument' do
set = Pry::CommandSet.new do
command "hello baby", "" do |arg|
output.puts "hello baby command #{arg}"
end
end
expect(pry_tester(commands: set).eval('hello baby john'))
.to match(/hello baby command john/)
end
it 'should create a regex command and be able to invoke it' do
set = Pry::CommandSet.new do
command(/hello(.)/, "") do
c = captures.first
output.puts "hello#{c}"
end
end
expect(pry_tester(commands: set).eval('hello1')).to match(/hello1/)
end
it(
'creates a regex command and passes captures into the args list ' \
'before regular arguments'
) do
set = Pry::CommandSet.new do
command(/hello(.)/, "") do |c1, a1|
output.puts "hello #{c1} #{a1}"
end
end
expect(pry_tester(commands: set).eval('hello1 baby')).to match(/hello 1 baby/)
end
it 'should create a regex command and interpolate the captures' do
set = Pry::CommandSet.new do
command(/hello (.*)/, "") do |c1|
output.puts "hello #{c1}"
end
end
bong = "bong"
expect(pry_tester(binding, commands: set).eval("hello #{bong}"))
.to match(/hello #{bong}/)
end
it 'should create a regex command and arg_string should be interpolated' do
set = Pry::CommandSet.new do
command(/hello(\w+)/, "") do |c1, a1, a2, a3|
output.puts "hello #{c1} #{a1} #{a2} #{a3}"
end
end
bing = 'bing'
bong = 'bong'
bang = 'bang'
expect(pry_tester(binding, commands: set)
.eval("hellojohn #{bing} #{bong} #{bang}"))
.to match(/hello john #{bing} #{bong} #{bang}/)
end
it 'if a regex capture is missing it should be nil' do
set = Pry::CommandSet.new do
command(/hello(.)?/, "") do |c1, a1|
output.puts "hello #{c1.inspect} #{a1}"
end
end
expect(pry_tester(commands: set).eval('hello baby')).to match(/hello nil baby/)
end
it(
'creates a command in a nested context and that command should ' \
'be accessible from the parent'
) do
tester = pry_tester(Object.new)
expect(tester.eval(*<<-RUBY.split("\n"))).to match(/instance variables:\s+@x/m)
@x = nil
cd 7
pry_instance.commands.instance_eval { command('bing') { |arg| run arg } }
cd ..
bing ls
RUBY
end
it 'should define a command that keeps its return value' do
klass = Pry::CommandSet.new do
command "hello", "", keep_retval: true do
:kept_hello
end
end
t = pry_tester(commands: klass)
t.eval("hello\n")
expect(t.last_command_result).to eq :kept_hello
end
it 'should define a command that does NOT keep its return value' do
klass = Pry::CommandSet.new do
command "hello", "", keep_retval: false do
:kept_hello
end
end
t = pry_tester(commands: klass)
expect(t.eval("hello\n")).to eq ''
expect(t.last_command_result).to eq Pry::Command::VOID_VALUE
end
it 'should define a command that keeps its return value even when nil' do
klass = Pry::CommandSet.new do
command "hello", "", keep_retval: true do
nil
end
end
t = pry_tester(commands: klass)
t.eval("hello\n")
expect(t.last_command_result).to eq nil
end
it(
'should define a command that keeps its return value but does not ' \
'return when value is void'
) do
klass = Pry::CommandSet.new do
command "hello", "", keep_retval: true do
void
end
end
expect(pry_tester(commands: klass).eval("hello\n").empty?).to eq true
end
it(
"a command (with :keep_retval => false) that replaces eval_string with a " \
"valid expression doesn't have the expression value suppressed"
) do
klass = Pry::CommandSet.new do
command "hello", "" do
eval_string.replace("6")
end
end
redirect_pry_io(InputTester.new('def yo', 'hello'), @str_output) do
Pry.start self, commands: klass
end
expect(@str_output.string).to match(/6/)
end
it(
'a command (with :keep_retval => true) that replaces eval_string with ' \
'a valid expression overwrites the eval_string with the return value'
) do
klass = Pry::CommandSet.new do
command "hello", "", keep_retval: true do
7
end
end
expect(pry_tester(commands: klass).eval("def yo", "hello")).to eq 7
end
it(
'a command that return a value in a multi-line expression clears ' \
'the expression and return the value'
) do
klass = Pry::CommandSet.new do
command "hello", "", keep_retval: true do
5
end
end
expect(pry_tester(commands: klass).eval("def yo", "hello\n")).to eq 5
end
it 'should set the commands default, and the default should be overridable' do
klass = Pry::CommandSet.new do
command "hello" do
output.puts "hello world"
end
end
other_klass = Pry::CommandSet.new do
command "goodbye", "" do
output.puts "goodbye world"
end
end
Pry.config.commands = klass
expect(pry_tester.eval("hello")).to eq "hello world\n"
expect(pry_tester(commands: other_klass).eval("goodbye")).to eq "goodbye world\n"
end
it 'should inherit commands from Pry::Commands' do
klass = Pry::CommandSet.new Pry::Commands do
command "v" do
end
end
expect(klass.to_hash.include?("nesting")).to eq true
expect(klass.to_hash.include?("jump-to")).to eq true
expect(klass.to_hash.include?("cd")).to eq true
expect(klass.to_hash.include?("v")).to eq true
end
it 'should change description of a command using desc' do
klass = Pry::CommandSet.new do
import Pry::Commands
end
orig = klass["help"].description
klass.instance_eval do
desc "help", "blah"
end
commands = klass.to_hash
expect(commands["help"].description).not_to eq orig
expect(commands["help"].description).to eq "blah"
end
it(
'enables an inherited method to access opts, output and target, ' \
'due to instance_exec'
) do
klass = Pry::CommandSet.new do
command "v" do
output.puts target.eval('self').to_s
end
end
child_klass = Pry::CommandSet.new klass do
end
mock_pry(Pry.binding_for('john'), "v", print: proc {}, commands: child_klass,
output: @str_output)
expect(@str_output.string).to eq "john\n"
end
it 'should import commands from another command object' do
klass = Pry::CommandSet.new do
import_from Pry::Commands, "ls", "jump-to"
end
expect(klass.to_hash.include?("ls")).to eq true
expect(klass.to_hash.include?("jump-to")).to eq true
end
it 'should delete some inherited commands when using delete method' do
klass = Pry::CommandSet.new Pry::Commands do
command "v" do
end
delete "show-doc", "show-method"
delete "ls"
end
commands = klass.to_hash
expect(commands.include?("nesting")).to eq true
expect(commands.include?("jump-to")).to eq true
expect(commands.include?("cd")).to eq true
expect(commands.include?("v")).to eq true
expect(commands.include?("show-doc")).to eq false
expect(commands.include?("show-method")).to eq false
expect(commands.include?("ls")).to eq false
end
it 'should override some inherited commands' do
klass = Pry::CommandSet.new Pry::Commands do
command "jump-to" do
output.puts "jump-to the music"
end
command "help" do
output.puts "help to the music"
end
end
t = pry_tester(commands: klass)
expect(t.eval('jump-to')).to eq "jump-to the music\n"
expect(t.eval('help')).to eq "help to the music\n"
end
it 'should run a command with no parameter' do
expect(pry_tester(commands: @command_tester).eval('command1'))
.to eq "command1\n"
end
it 'should run a command with one parameter' do
expect(pry_tester(commands: @command_tester).eval('command2 horsey'))
.to eq "horsey\n"
end
end
|