2012-01-13 02:48:09 -05:00
|
|
|
describe "commands" do
|
2012-06-11 09:36:16 -04:00
|
|
|
before do
|
|
|
|
@str_output = StringIO.new
|
2012-07-11 14:34:44 -04:00
|
|
|
@o = Object.new
|
|
|
|
|
2012-07-11 17:32:35 -04:00
|
|
|
# Shortcuts. They save a lot of typing.
|
|
|
|
@bs1 = "Pad.bs1 = _pry_.binding_stack.dup"
|
|
|
|
@bs2 = "Pad.bs2 = _pry_.binding_stack.dup"
|
|
|
|
@bs3 = "Pad.bs3 = _pry_.binding_stack.dup"
|
2012-07-11 14:34:44 -04:00
|
|
|
|
2018-11-04 04:34:24 -05:00
|
|
|
@self = "Pad.self = self"
|
2012-07-11 14:34:44 -04:00
|
|
|
|
2012-12-06 19:52:37 -05:00
|
|
|
@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
|
|
|
|
|
2012-07-11 14:34:44 -04:00
|
|
|
Pad.bong = "bong"
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Pad.clear
|
2012-12-20 04:28:04 -05:00
|
|
|
Pry.reset_defaults
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
2012-03-25 07:26:48 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
pry_tester(commands: set).tap do |t|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.eval('test-command')).to eq t.eval('test-alias')
|
2012-03-25 07:26:48 -04:00
|
|
|
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
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
t = pry_tester(commands: set)
|
2012-03-25 07:26:48 -04:00
|
|
|
|
2012-12-06 19:52:37 -05:00
|
|
|
t.process_command "test-alias hello baby duck"
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.last_output).to match(/testing hello baby duck/)
|
2012-03-25 07:26:48 -04:00
|
|
|
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 }
|
2018-10-12 15:09:29 -04:00
|
|
|
t = pry_tester(obj, commands: set)
|
2012-03-25 07:26:48 -04:00
|
|
|
|
2012-12-06 19:52:37 -05:00
|
|
|
t.process_command "test-alias -i"
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.last_output).to match(/@x/)
|
2012-03-25 07:26:48 -04:00
|
|
|
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) {} } }
|
2018-10-12 15:09:29 -04:00
|
|
|
t = pry_tester(obj, commands: set)
|
2012-12-06 19:52:37 -05:00
|
|
|
t.process_command "test-alias @x"
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.last_output).to match(/plymouth/)
|
2012-03-25 07:26:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to alias a regex command' do
|
|
|
|
set = Pry::CommandSet.new do
|
2015-01-22 16:52:20 -05:00
|
|
|
command(/du.k/) do
|
2012-03-25 07:26:48 -04:00
|
|
|
output.puts "ducky"
|
|
|
|
end
|
|
|
|
alias_command "test-alias", "duck"
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
t = pry_tester(commands: set)
|
2012-12-06 19:52:37 -05:00
|
|
|
t.process_command "test-alias"
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.last_output).to match(/ducky/)
|
2012-03-25 07:26:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to make the alias a regex' do
|
|
|
|
set = Pry::CommandSet.new do
|
2015-01-22 16:52:20 -05:00
|
|
|
command(/du.k/) do
|
2012-03-25 07:26:48 -04:00
|
|
|
output.puts "ducky"
|
|
|
|
end
|
2015-01-22 16:52:20 -05:00
|
|
|
alias_command(/test-ali.s/, "duck")
|
2012-03-25 07:26:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
redirect_pry_io(InputTester.new("test-alias"), out1 = StringIO.new) do
|
2018-10-12 15:09:29 -04:00
|
|
|
Pry.start self, commands: set
|
2012-03-25 07:26:48 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(out1.string).to match(/ducky/)
|
2012-03-25 07:26:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-11 09:36:16 -04:00
|
|
|
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
|
2012-12-06 19:52:37 -05:00
|
|
|
|
2012-07-11 17:32:35 -04:00
|
|
|
redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6", @bs1, "test-run",
|
|
|
|
@self, @bs2, "exit-all")) do
|
2018-10-12 15:09:29 -04:00
|
|
|
Pry.start(@o, commands: set)
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pad.bs1.size).to eq 7
|
|
|
|
expect(Pad.self).to eq @o
|
|
|
|
expect(Pad.bs2.size).to eq 1
|
2012-06-11 09:36:16 -04:00
|
|
|
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
|
2012-07-11 17:32:35 -04:00
|
|
|
redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6", @bs1, "test-run",
|
|
|
|
@self, @bs2, "exit-all")) do
|
2018-10-12 15:09:29 -04:00
|
|
|
Pry.start(@o, commands: set)
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pad.bs1.size).to eq 7
|
|
|
|
expect(Pad.self).to eq @o
|
|
|
|
expect(Pad.bs2.size).to eq 1
|
2012-06-11 09:36:16 -04:00
|
|
|
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
|
2012-07-11 17:32:35 -04:00
|
|
|
redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6", @bs1, "test-run",
|
|
|
|
@self, @bs2, "exit-all")) do
|
2018-10-12 15:09:29 -04:00
|
|
|
Pry.start(@o, commands: set)
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pad.bs1.size).to eq 7
|
|
|
|
expect(Pad.self).to eq @o
|
|
|
|
expect(Pad.bs2.size).to eq 1
|
2012-06-11 09:36:16 -04:00
|
|
|
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
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: klass).eval('run_v')).to match(/v command/)
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should run a regex command from within a command' do
|
|
|
|
klass = Pry::CommandSet.new do
|
2015-01-22 16:52:20 -05:00
|
|
|
command(/v(.*)?/) do |arg|
|
2012-06-11 09:36:16 -04:00
|
|
|
output.puts "v #{arg}"
|
|
|
|
end
|
|
|
|
|
|
|
|
command "run_v" do
|
|
|
|
run "vbaby"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: klass).eval('run_v')).to match(/v baby/)
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should run a command from within a command with arguments' do
|
|
|
|
klass = Pry::CommandSet.new do
|
2015-01-22 16:52:20 -05:00
|
|
|
command(/v(\w+)/) do |arg1, arg2|
|
2012-06-11 09:36:16 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
["run_v_explicit_parameter", "run_v_embedded_parameter"].each do |cmd|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: klass).eval(cmd)).to match(/v baby param/)
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Pry#run_command" do
|
|
|
|
it 'should run a command that modifies the passed in eval_string' do
|
2018-10-12 15:09:29 -04:00
|
|
|
p = Pry.new(output: @str_output)
|
2012-12-28 01:06:50 -05:00
|
|
|
p.eval "def hello\npeter pan\n"
|
2012-12-18 03:53:26 -05:00
|
|
|
p.run_command "amend-line !"
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(p.eval_string).to match(/def hello/)
|
|
|
|
expect(p.eval_string).not_to match(/peter pan/)
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should run a command in the context of a session' do
|
2012-12-23 21:22:58 -05:00
|
|
|
pry_tester(Object.new).tap do |t|
|
2012-09-09 01:29:00 -04:00
|
|
|
t.eval "@session_ivar = 10", "_pry_.run_command('ls')"
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.last_output).to match(/@session_ivar/)
|
2012-09-09 01:29:00 -04:00
|
|
|
end
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-13 02:48:09 -05:00
|
|
|
it 'should interpolate ruby code into commands' do
|
2012-07-11 14:34:44 -04:00
|
|
|
set = Pry::CommandSet.new do
|
2018-10-12 15:09:29 -04:00
|
|
|
command "hello", "", keep_retval: true do |arg|
|
2012-01-13 02:48:09 -05:00
|
|
|
arg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: set).eval('hello #{Pad.bong}')).to match(/bong/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# bug fix for https://github.com/pry/pry/issues/170
|
|
|
|
it 'should not choke on complex string interpolation when checking if ruby code is a command' do
|
2012-06-11 09:36:16 -04:00
|
|
|
redirect_pry_io(InputTester.new('/#{Regexp.escape(File.expand_path("."))}/'), @str_output) do
|
2012-01-13 02:48:09 -05:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@str_output.string).not_to match(/SyntaxError/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should NOT interpolate ruby code into commands if :interpolate => false' do
|
2012-07-11 14:34:44 -04:00
|
|
|
set = Pry::CommandSet.new do
|
2018-10-12 15:09:29 -04:00
|
|
|
command "hello", "", keep_retval: true, interpolate: false do |arg|
|
2012-01-13 02:48:09 -05:00
|
|
|
arg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: set).eval('hello #{Pad.bong}')).
|
2015-03-10 16:49:29 -04:00
|
|
|
to match(/Pad\.bong/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should NOT try to interpolate pure ruby code (no commands) ' do
|
2012-12-15 19:34:56 -05:00
|
|
|
# These should raise RuntimeError instead of NameError
|
Switch test suite to RSpec
Removes Bacon and Mocha
Reasoning explained in this comment: https://github.com/pry/pry/issues/277#issuecomment-51708712
Mostly this went smoothly. There were a few errors that I fixed along
the way, e.g. tests that were failing but for various reasons still
passed. Should have documented them, but didn't think about it until
very near the end. But generaly, I remember 2 reasons this would happen:
`lambda { raise "omg" }.should.raise(RuntimeError, /not-omg/)` will pass
because the second argument is ignored by Bacon. And `1.should == 2`
will return false instead of raising an error when it is not in an it
block (e.g. if stuck in a describe block, that would just return false)
The only one that I felt unsure about was spec/helpers/table_spec.rb
`Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'`
This is wrong, but was not failing because it was in a describe block
instead of an it block. In reality, it returns `"head: ing\n"`,
I updated the test to reflect this, though I don't know for sure
this is the right thing to do
This will fail on master until https://github.com/pry/pry/pull/1281 is merged.
This makes https://github.com/pry/pry/pull/1278 unnecessary.
2014-08-10 18:26:47 -04:00
|
|
|
expect { pry_eval 'raise \'#{aggy}\'' }.to raise_error RuntimeError
|
|
|
|
expect { pry_eval 'raise #{aggy}' }.to raise_error RuntimeError
|
2012-01-13 02:48:09 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_eval('format \'#{my_var}\'')).to eq "\#{my_var}"
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
2012-12-15 19:34:56 -05:00
|
|
|
it 'should create a command with a space in its name zzz' do
|
2012-01-13 02:48:09 -05:00
|
|
|
set = Pry::CommandSet.new do
|
|
|
|
command "hello baby", "" do
|
|
|
|
output.puts "hello baby command"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: set).eval('hello baby')).
|
2015-03-10 16:49:29 -04:00
|
|
|
to match(/hello baby command/)
|
2012-01-13 02:48:09 -05:00
|
|
|
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
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: set).eval('hello baby john')).
|
2015-03-10 16:49:29 -04:00
|
|
|
to match(/hello baby command john/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create a regex command and be able to invoke it' do
|
|
|
|
set = Pry::CommandSet.new do
|
2015-01-22 16:52:20 -05:00
|
|
|
command(/hello(.)/, "") do
|
2012-01-13 02:48:09 -05:00
|
|
|
c = captures.first
|
|
|
|
output.puts "hello#{c}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: set).eval('hello1')).to match(/hello1/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create a regex command and pass captures into the args list before regular arguments' do
|
|
|
|
set = Pry::CommandSet.new do
|
2015-01-22 16:52:20 -05:00
|
|
|
command(/hello(.)/, "") do |c1, a1|
|
2012-01-13 02:48:09 -05:00
|
|
|
output.puts "hello #{c1} #{a1}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: set).eval('hello1 baby')).to match(/hello 1 baby/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create a regex command and interpolate the captures' do
|
|
|
|
set = Pry::CommandSet.new do
|
2015-01-22 16:52:20 -05:00
|
|
|
command(/hello (.*)/, "") do |c1|
|
2012-01-13 02:48:09 -05:00
|
|
|
output.puts "hello #{c1}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-15 19:34:56 -05:00
|
|
|
bong = "bong"
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(binding, commands: set).eval('hello #{bong}')).
|
2015-03-10 16:49:29 -04:00
|
|
|
to match(/hello #{bong}/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create a regex command and arg_string should be interpolated' do
|
|
|
|
set = Pry::CommandSet.new do
|
2015-01-22 16:52:20 -05:00
|
|
|
command(/hello(\w+)/, "") do |c1, a1, a2, a3|
|
2012-01-13 02:48:09 -05:00
|
|
|
output.puts "hello #{c1} #{a1} #{a2} #{a3}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-15 19:34:56 -05:00
|
|
|
bing = 'bing'
|
|
|
|
bong = 'bong'
|
|
|
|
bang = 'bang'
|
2012-01-13 02:48:09 -05:00
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(binding, commands: set).
|
2015-03-10 16:49:29 -04:00
|
|
|
eval('hellojohn #{bing} #{bong} #{bang}')).
|
|
|
|
to match(/hello john #{bing} #{bong} #{bang}/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'if a regex capture is missing it should be nil' do
|
|
|
|
set = Pry::CommandSet.new do
|
2015-01-22 16:52:20 -05:00
|
|
|
command(/hello(.)?/, "") do |c1, a1|
|
2012-01-13 02:48:09 -05:00
|
|
|
output.puts "hello #{c1.inspect} #{a1}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: set).eval('hello baby')).to match(/hello nil baby/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
2012-07-11 14:34:44 -04:00
|
|
|
it 'should create a command in a nested context and that command should be accessible from the parent' do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_tester(Object.new).eval(*(<<-RUBY.split("\n")))).to match(/instance variables:\s+@x/m)
|
2012-12-15 19:34:56 -05:00
|
|
|
@x = nil
|
|
|
|
cd 7
|
|
|
|
_pry_.commands.instance_eval { command('bing') { |arg| run arg } }
|
|
|
|
cd ..
|
|
|
|
bing ls
|
|
|
|
RUBY
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define a command that keeps its return value' do
|
|
|
|
klass = Pry::CommandSet.new do
|
2018-10-12 15:09:29 -04:00
|
|
|
command "hello", "", keep_retval: true do
|
2012-01-13 02:48:09 -05:00
|
|
|
:kept_hello
|
|
|
|
end
|
|
|
|
end
|
2012-06-11 09:36:16 -04:00
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
t = pry_tester(commands: klass)
|
2012-12-15 19:34:56 -05:00
|
|
|
t.eval("hello\n")
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.last_command_result).to eq :kept_hello
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define a command that does NOT keep its return value' do
|
|
|
|
klass = Pry::CommandSet.new do
|
2018-10-12 15:09:29 -04:00
|
|
|
command "hello", "", keep_retval: false do
|
2012-01-13 02:48:09 -05:00
|
|
|
:kept_hello
|
|
|
|
end
|
|
|
|
end
|
2012-06-11 09:36:16 -04:00
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
t = pry_tester(commands: klass)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.eval("hello\n")).to eq ''
|
|
|
|
expect(t.last_command_result).to eq Pry::Command::VOID_VALUE
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define a command that keeps its return value even when nil' do
|
|
|
|
klass = Pry::CommandSet.new do
|
2018-10-12 15:09:29 -04:00
|
|
|
command "hello", "", keep_retval: true do
|
2012-01-13 02:48:09 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2012-06-11 09:36:16 -04:00
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
t = pry_tester(commands: klass)
|
2012-12-15 19:34:56 -05:00
|
|
|
t.eval("hello\n")
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.last_command_result).to eq nil
|
2012-01-13 02:48:09 -05:00
|
|
|
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
|
2018-10-12 15:09:29 -04:00
|
|
|
command "hello", "", keep_retval: true do
|
2012-01-13 02:48:09 -05:00
|
|
|
void
|
|
|
|
end
|
|
|
|
end
|
2012-06-11 09:36:16 -04:00
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: klass).eval("hello\n").empty?).to eq true
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'a command (with :keep_retval => false) that replaces eval_string with a valid expression should not have the expression value suppressed' do
|
|
|
|
klass = Pry::CommandSet.new do
|
|
|
|
command "hello", "" do
|
|
|
|
eval_string.replace("6")
|
|
|
|
end
|
|
|
|
end
|
2012-06-11 09:36:16 -04:00
|
|
|
|
2015-01-23 08:24:35 -05:00
|
|
|
redirect_pry_io(InputTester.new('def yo', 'hello'), @str_output) do
|
2018-10-12 15:09:29 -04:00
|
|
|
Pry.start self, commands: klass
|
2012-12-15 19:34:56 -05:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@str_output.string).to match(/6/)
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'a command (with :keep_retval => true) that replaces eval_string with a valid expression should overwrite the eval_string with the return value' do
|
|
|
|
klass = Pry::CommandSet.new do
|
2018-10-12 15:09:29 -04:00
|
|
|
command "hello", "", keep_retval: true do
|
2012-01-13 02:48:09 -05:00
|
|
|
eval_string.replace("6")
|
|
|
|
7
|
|
|
|
end
|
|
|
|
end
|
2012-06-11 09:36:16 -04:00
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: klass).eval("def yo\nhello\n")).to eq 7
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'a command that return a value in a multi-line expression should clear the expression and return the value' do
|
|
|
|
klass = Pry::CommandSet.new do
|
2018-10-12 15:09:29 -04:00
|
|
|
command "hello", "", keep_retval: true do
|
2012-01-13 02:48:09 -05:00
|
|
|
5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: klass).eval("def yo\nhello\n")).to eq 5
|
2012-06-11 09:36:16 -04:00
|
|
|
end
|
2012-01-13 02:48:09 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands = klass
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_tester.eval("hello")).to eq "hello world\n"
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: other_klass).eval("goodbye")).to eq "goodbye world\n"
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should inherit commands from Pry::Commands' do
|
|
|
|
klass = Pry::CommandSet.new Pry::Commands do
|
|
|
|
command "v" do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
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
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should change description of a command using desc' do
|
2012-08-11 20:22:29 -04:00
|
|
|
klass = Pry::CommandSet.new do
|
|
|
|
import Pry::Commands
|
|
|
|
end
|
2014-02-03 23:12:28 -05:00
|
|
|
orig = klass["help"].description
|
2012-01-13 02:48:09 -05:00
|
|
|
klass.instance_eval do
|
|
|
|
desc "help", "blah"
|
|
|
|
end
|
2014-02-03 23:12:28 -05:00
|
|
|
commands = klass.to_hash
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(commands["help"].description).not_to eq orig
|
|
|
|
expect(commands["help"].description).to eq "blah"
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
|
|
|
|
klass = Pry::CommandSet.new do
|
|
|
|
command "v" do
|
|
|
|
output.puts "#{target.eval('self')}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
child_klass = Pry::CommandSet.new klass do
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
mock_pry(Pry.binding_for('john'), "v", print: proc {}, commands: child_klass,
|
|
|
|
output: @str_output)
|
2012-12-18 03:15:15 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@str_output.string).to eq "john\n"
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should import commands from another command object' do
|
|
|
|
klass = Pry::CommandSet.new do
|
|
|
|
import_from Pry::Commands, "ls", "jump-to"
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(klass.to_hash.include?("ls")).to eq true
|
|
|
|
expect(klass.to_hash.include?("jump-to")).to eq true
|
2012-01-13 02:48:09 -05:00
|
|
|
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
|
|
|
|
|
2014-02-03 23:12:28 -05:00
|
|
|
commands = klass.to_hash
|
2015-03-10 16:49:29 -04:00
|
|
|
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
|
2012-01-13 02:48:09 -05:00
|
|
|
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
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
t = pry_tester(commands: klass)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.eval('jump-to')).to eq "jump-to the music\n"
|
|
|
|
expect(t.eval('help')).to eq "help to the music\n"
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should run a command with no parameter' do
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: @command_tester).eval('command1')).
|
2015-03-10 16:49:29 -04:00
|
|
|
to eq "command1\n"
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should run a command with one parameter' do
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(pry_tester(commands: @command_tester).eval('command2 horsey')).
|
2015-03-10 16:49:29 -04:00
|
|
|
to eq "horsey\n"
|
2012-01-13 02:48:09 -05:00
|
|
|
end
|
|
|
|
end
|