2014-03-14 00:31:24 -04:00
|
|
|
require_relative 'helper'
|
2011-12-31 10:21:58 -05:00
|
|
|
|
|
|
|
describe "Pry::Command" do
|
|
|
|
before do
|
|
|
|
@set = Pry::CommandSet.new
|
2012-08-11 20:22:29 -04:00
|
|
|
@set.import Pry::Commands
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'call_safely' do
|
|
|
|
it 'should display a message if gems are missing' do
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd = @set.create_command "ford-prefect", "From a planet near Beetlegeuse", requires_gem: %w(ghijkl) do
|
2011-12-31 10:21:58 -05:00
|
|
|
#
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_command(cmd, %w(hello world)).output).to match(/install-command ford-prefect/)
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should abort early if arguments are required' do
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd = @set.create_command 'arthur-dent', "Doesn't understand Thursdays", argument_required: true do
|
2011-12-31 10:21:58 -05:00
|
|
|
#
|
|
|
|
end
|
|
|
|
|
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 { mock_command(cmd, %w()) }.to raise_error Pry::CommandError
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return VOID without keep_retval' do
|
2012-01-21 18:17:43 -05:00
|
|
|
cmd = @set.create_command 'zaphod-beeblebrox', "Likes pan-Galactic Gargle Blasters" do
|
2011-12-31 10:21:58 -05:00
|
|
|
def process
|
|
|
|
3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_command(cmd).return).to eq Pry::Command::VOID_VALUE
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the return value with keep_retval' do
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd = @set.create_command 'tricia-mcmillian', "a.k.a Trillian", keep_retval: true do
|
2011-12-31 10:21:58 -05:00
|
|
|
def process
|
|
|
|
5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_command(cmd).return).to eq 5
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
2015-03-11 10:17:10 -04:00
|
|
|
context "hooks API" do
|
|
|
|
before do
|
|
|
|
@set.create_command 'jamaica', 'Out of Many, One People' do
|
|
|
|
def process
|
|
|
|
output.puts 1 + args[0].to_i
|
|
|
|
end
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
2015-03-11 10:17:10 -04:00
|
|
|
let(:hooks) {
|
|
|
|
h = Pry::Hooks.new
|
|
|
|
h.add_hook('before_jamaica', 'name1') do |i|
|
|
|
|
output.puts 3 - i.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
h.add_hook('before_jamaica', 'name2') do |i|
|
|
|
|
output.puts 4 - i.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
h.add_hook('after_jamaica', 'name3') do |i|
|
|
|
|
output.puts 2 + i.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
h.add_hook('after_jamaica', 'name4') do |i|
|
|
|
|
output.puts 3 + i.to_i
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
it "should call hooks in the right order" do
|
|
|
|
out = pry_tester(hooks: hooks, commands: @set).process_command('jamaica 2')
|
|
|
|
expect(out).to eq("1\n2\n3\n4\n5\n")
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'help' do
|
|
|
|
it 'should default to the description for blocky commands' do
|
|
|
|
@set.command 'oolon-colluphid', "Raving Atheist" do
|
|
|
|
#
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(mock_command(@set['help'], %w(oolon-colluphid), command_set: @set).output).to match(/Raving Atheist/)
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use slop to generate the help for classy commands' do
|
2012-01-21 18:17:43 -05:00
|
|
|
@set.create_command 'eddie', "The ship-board computer" do
|
2011-12-31 10:21:58 -05:00
|
|
|
def options(opt)
|
|
|
|
opt.banner "Over-cheerful, and makes a ticking noise."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
expect(mock_command(@set['help'], %w(eddie), command_set: @set).output).to match(/Over-cheerful/)
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should provide --help for classy commands' do
|
2012-01-21 18:17:43 -05:00
|
|
|
cmd = @set.create_command 'agrajag', "Killed many times by Arthur" do
|
2011-12-31 10:21:58 -05:00
|
|
|
def options(opt)
|
|
|
|
opt.on :r, :retaliate, "Try to get Arthur back"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_command(cmd, %w(--help)).output).to match(/--retaliate/)
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should provide a -h for classy commands' do
|
2012-01-21 18:17:43 -05:00
|
|
|
cmd = @set.create_command 'zarniwoop', "On an intergalactic cruise, in his office." do
|
2011-12-31 10:21:58 -05:00
|
|
|
def options(opt)
|
|
|
|
opt.on :e, :escape, "Help zaphod escape the Total Perspective Vortex"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_command(cmd, %w(--help)).output).to match(/Total Perspective Vortex/)
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2011-12-31 10:57:50 -05:00
|
|
|
|
|
|
|
it 'should use the banner provided' do
|
2012-01-21 18:17:43 -05:00
|
|
|
cmd = @set.create_command 'deep-thought', "The second-best computer ever" do
|
2011-12-31 10:57:50 -05:00
|
|
|
banner <<-BANNER
|
|
|
|
Who's merest operational parameters, I am not worthy to compute.
|
|
|
|
BANNER
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_command(cmd, %w(--help)).output).to match(/Who\'s merest/)
|
2011-12-31 10:57:50 -05:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'context' do
|
2014-09-12 12:40:37 -04:00
|
|
|
let(:context) {
|
|
|
|
{
|
2018-10-12 15:09:29 -04:00
|
|
|
target: binding,
|
|
|
|
output: StringIO.new,
|
|
|
|
eval_string: "eval-string",
|
|
|
|
command_set: @set,
|
|
|
|
pry_instance: Pry.new
|
2014-09-12 12:40:37 -04:00
|
|
|
}
|
2011-12-31 10:21:58 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 12:40:37 -04:00
|
|
|
describe '#setup' do
|
|
|
|
it 'should capture lots of stuff from the hash passed to new before setup' do
|
|
|
|
inside = inner_scope do |probe|
|
|
|
|
cmd = @set.create_command('fenchurch', "Floats slightly off the ground") do
|
|
|
|
define_method(:setup, &probe)
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2014-09-12 12:40:37 -04:00
|
|
|
cmd.new(context).call
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2014-09-12 12:40:37 -04:00
|
|
|
|
|
|
|
expect(inside.context).to eq(context)
|
|
|
|
expect(inside.target).to eq(context[:target])
|
|
|
|
expect(inside.target_self).to eq(context[:target].eval('self'))
|
|
|
|
expect(inside.output).to eq(context[:output])
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2014-09-12 12:40:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#process' do
|
|
|
|
it 'should capture lots of stuff from the hash passed to new before setup' do
|
|
|
|
inside = inner_scope do |probe|
|
|
|
|
cmd = @set.create_command('fenchurch', "Floats slightly off the ground") do
|
|
|
|
define_method(:process, &probe)
|
|
|
|
end
|
|
|
|
|
|
|
|
cmd.new(context).call
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2014-09-12 12:40:37 -04:00
|
|
|
expect(inside.eval_string).to eq("eval-string")
|
|
|
|
expect(inside.__send__(:command_set)).to eq(@set)
|
|
|
|
expect(inside._pry_).to eq(context[:pry_instance])
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'classy api' do
|
2012-08-10 17:23:40 -04:00
|
|
|
it 'should call setup, then subcommands, then options, then process' do
|
2012-01-21 18:17:43 -05:00
|
|
|
cmd = @set.create_command 'rooster', "Has a tasty towel" do
|
2011-12-31 10:21:58 -05:00
|
|
|
def setup
|
|
|
|
output.puts "setup"
|
|
|
|
end
|
|
|
|
|
2018-10-14 02:56:53 -04:00
|
|
|
def subcommands(_cmd)
|
2012-08-10 17:23:40 -04:00
|
|
|
output.puts "subcommands"
|
2012-08-08 00:08:30 -04:00
|
|
|
end
|
|
|
|
|
2018-10-14 02:56:53 -04:00
|
|
|
def options(_opt)
|
2011-12-31 10:21:58 -05:00
|
|
|
output.puts "options"
|
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
|
|
|
output.puts "process"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_command(cmd).output).to eq "setup\nsubcommands\noptions\nprocess\n"
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise a command error if process is not overridden' do
|
2012-01-21 18:17:43 -05:00
|
|
|
cmd = @set.create_command 'jeltz', "Commander of a Vogon constructor fleet" do
|
2011-12-31 10:21:58 -05:00
|
|
|
def proccces
|
|
|
|
#
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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 { mock_command(cmd) }.to raise_error Pry::CommandError
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should work if neither options, nor setup is overridden' do
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd = @set.create_command 'wowbagger', "Immortal, insulting.", keep_retval: true do
|
2011-12-31 10:21:58 -05:00
|
|
|
def process
|
|
|
|
5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_command(cmd).return).to eq 5
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should provide opts and args as provided by slop' do
|
2012-01-21 18:17:43 -05:00
|
|
|
cmd = @set.create_command 'lintilla', "One of 800,000,000 clones" do
|
2012-08-08 00:08:30 -04:00
|
|
|
def options(opt)
|
2018-10-12 15:09:29 -04:00
|
|
|
opt.on :f, :four, "A numeric four", as: Integer, optional_argument: true
|
2012-08-08 00:08:30 -04:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2012-08-08 00:08:30 -04:00
|
|
|
def process
|
2015-01-23 04:30:41 -05:00
|
|
|
output.puts args.inspect
|
|
|
|
output.puts opts[:f]
|
2012-08-08 00:08:30 -04:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
result = mock_command(cmd, %w(--four 4 four))
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(result.output.split).to eq ['["four"]', '4']
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2012-01-14 23:54:30 -05:00
|
|
|
|
2012-11-26 11:07:32 -05:00
|
|
|
it 'should allow overriding options after definition' do
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd = @set.create_command(/number-(one|two)/, "Lieutenants of the Golgafrinchan Captain", shellwords: false) do
|
|
|
|
command_options listing: 'number-one'
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(cmd.command_options[:shellwords]).to eq false
|
|
|
|
expect(cmd.command_options[:listing]).to eq 'number-one'
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should create subcommands" do
|
|
|
|
cmd = @set.create_command 'mum', 'Your mum' do
|
|
|
|
def subcommands(cmd)
|
2013-01-14 10:10:21 -05:00
|
|
|
cmd.command :yell
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
2015-01-23 04:30:41 -05:00
|
|
|
output.puts opts.fetch_command(:blahblah).inspect
|
|
|
|
output.puts opts.fetch_command(:yell).present?
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
result = mock_command(cmd, ['yell'])
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(result.output.split).to eq ['nil', 'true']
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should create subcommand options" do
|
|
|
|
cmd = @set.create_command 'mum', 'Your mum' do
|
2012-08-10 17:23:40 -04:00
|
|
|
def subcommands(cmd)
|
2013-01-14 10:10:21 -05:00
|
|
|
cmd.command :yell do
|
|
|
|
on :p, :person
|
2012-08-08 00:08:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
2015-01-23 04:30:41 -05:00
|
|
|
output.puts args.inspect
|
|
|
|
output.puts opts.fetch_command(:yell).present?
|
|
|
|
output.puts opts.fetch_command(:yell).person?
|
2012-08-08 00:08:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
result = mock_command(cmd, %w|yell --person papa|)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(result.output.split).to eq ['["papa"]', 'true', 'true']
|
2012-08-08 00:08:30 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 11:07:32 -05:00
|
|
|
it "should accept top-level arguments" do
|
|
|
|
cmd = @set.create_command 'mum', 'Your mum' do
|
|
|
|
def subcommands(cmd)
|
|
|
|
cmd.on :yell
|
|
|
|
end
|
2012-01-14 23:54:30 -05:00
|
|
|
|
2012-11-26 11:07:32 -05:00
|
|
|
def process
|
2013-01-14 10:10:21 -05:00
|
|
|
args.should == ['yell', 'papa', 'sonny', 'daughter']
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
2012-01-14 23:54:30 -05:00
|
|
|
end
|
|
|
|
|
2012-11-26 11:07:32 -05:00
|
|
|
mock_command(cmd, %w|yell papa sonny daughter|)
|
2012-01-14 23:54:30 -05:00
|
|
|
end
|
2013-01-01 15:22:00 -05:00
|
|
|
|
|
|
|
describe "explicit classes" do
|
|
|
|
before do
|
|
|
|
@x = Class.new(Pry::ClassCommand) do
|
2018-10-12 15:09:29 -04:00
|
|
|
options baby: :pig
|
2015-01-22 16:52:20 -05:00
|
|
|
match(/goat/)
|
2013-01-01 15:22:00 -05:00
|
|
|
description "waaaninngggiiigygygygygy"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'subclasses should inherit options, match and description from superclass' do
|
|
|
|
k = Class.new(@x)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(k.options).to eq @x.options
|
|
|
|
expect(k.match).to eq @x.match
|
|
|
|
expect(k.description).to eq @x.description
|
2013-01-01 15:22:00 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
|
|
|
describe 'tokenize' do
|
|
|
|
it 'should interpolate string with #{} in them' do
|
2014-09-12 11:55:59 -04:00
|
|
|
expect { |probe|
|
|
|
|
cmd = @set.command('random-dent', &probe)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
_foo = 5
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd.new(target: binding).process_line 'random-dent #{1 + 2} #{3 + _foo}'
|
2014-09-12 11:55:59 -04:00
|
|
|
}.to yield_with_args('3', '8')
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not fail if interpolation is not needed and target is not set' do
|
2014-09-12 11:55:59 -04:00
|
|
|
expect { |probe|
|
|
|
|
cmd = @set.command('the-book', &probe)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2014-09-12 11:55:59 -04:00
|
|
|
cmd.new.process_line 'the-book --help'
|
|
|
|
}.to yield_with_args('--help')
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not interpolate commands with :interpolate => false' do
|
2014-09-12 11:55:59 -04:00
|
|
|
expect { |probe|
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd = @set.command('thor', 'norse god', interpolate: false, &probe)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2014-09-12 11:55:59 -04:00
|
|
|
cmd.new.process_line 'thor %(#{foo})'
|
|
|
|
}.to yield_with_args('%(#{foo})')
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use shell-words to split strings' do
|
2014-09-12 11:55:59 -04:00
|
|
|
expect { |probe|
|
|
|
|
cmd = @set.command('eccentrica', &probe)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2014-09-12 11:55:59 -04:00
|
|
|
cmd.new.process_line %(eccentrica "gallumbits" 'erot''icon' 6)
|
|
|
|
}.to yield_with_args('gallumbits', 'eroticon', '6')
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should split on spaces if shellwords is not used' do
|
2014-09-12 11:55:59 -04:00
|
|
|
expect { |probe|
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd = @set.command('bugblatter-beast', 'would eat its grandmother', shellwords: false, &probe)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2014-09-12 11:55:59 -04:00
|
|
|
cmd.new.process_line %(bugblatter-beast "of traal")
|
|
|
|
}.to yield_with_args('"of', 'traal"')
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should add captures to arguments for regex commands' do
|
2014-09-12 11:55:59 -04:00
|
|
|
expect { |probe|
|
|
|
|
cmd = @set.command(/perfectly (normal)( beast)?/i, &probe)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2014-09-12 11:55:59 -04:00
|
|
|
cmd.new.process_line %(Perfectly Normal Beast (honest!))
|
|
|
|
}.to yield_with_args('Normal', ' Beast', '(honest!)')
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'process_line' do
|
|
|
|
it 'should check for command name collisions if configured' do
|
|
|
|
old = Pry.config.collision_warning
|
|
|
|
Pry.config.collision_warning = true
|
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
cmd = @set.command '_frankie' do
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
_frankie = 'boyle'
|
2012-01-02 19:04:47 -05:00
|
|
|
output = StringIO.new
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd.new(target: binding, output: output).process_line %(_frankie mouse)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(output.string).to match(/command .* conflicts/)
|
2012-01-28 22:39:31 -05:00
|
|
|
|
|
|
|
Pry.config.collision_warning = old
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should spot collision warnings on assignment if configured' do
|
|
|
|
old = Pry.config.collision_warning
|
|
|
|
Pry.config.collision_warning = true
|
|
|
|
|
|
|
|
cmd = @set.command 'frankie' do
|
|
|
|
end
|
|
|
|
|
|
|
|
output = StringIO.new
|
2018-10-12 15:09:29 -04:00
|
|
|
cmd.new(target: binding, output: output).process_line %(frankie = mouse)
|
2012-01-28 22:39:31 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(output.string).to match(/command .* conflicts/)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
|
|
|
Pry.config.collision_warning = old
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should set the commands' arg_string and captures" do
|
2014-09-12 12:40:37 -04:00
|
|
|
inside = inner_scope { |probe|
|
|
|
|
cmd = @set.command(/benj(ie|ei)/, &probe)
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2014-09-12 12:40:37 -04:00
|
|
|
cmd.new.process_line %(benjie mouse)
|
|
|
|
}
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2014-09-12 12:40:37 -04:00
|
|
|
expect(inside.arg_string).to eq("mouse")
|
|
|
|
expect(inside.captures).to eq(['ie'])
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an error if the line doesn't match the command" do
|
|
|
|
cmd = @set.command 'grunthos', 'the flatulent'
|
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 { cmd.new.process_line %(grumpos) }.to raise_error Pry::CommandError
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
2012-02-23 09:30:48 -05:00
|
|
|
end
|
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "block parameters" do
|
|
|
|
before do
|
|
|
|
@context = Object.new
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.command "walking-spanish", "down the hall", takes_block: true do
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, command_block.call, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
@set.import Pry::Commands
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
@t = pry_tester(@context, commands: @set)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-02-23 09:30:48 -05:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
it 'should accept multiline blocks' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval <<-EOS
|
2012-09-09 01:46:45 -04:00
|
|
|
walking-spanish | do
|
|
|
|
:jesus
|
|
|
|
end
|
|
|
|
EOS
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq :jesus
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-02-23 09:30:48 -05:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
it 'should accept normal parameters along with block' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@set.block_command "walking-spanish",
|
|
|
|
"litella's been screeching for a blind pig.",
|
2018-10-12 15:09:29 -04:00
|
|
|
takes_block: true do |x, y|
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, x, target)
|
|
|
|
insert_variable(:@y, y, target)
|
|
|
|
insert_variable(:@block_var, command_block.call, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval 'walking-spanish john carl| { :jesus }'
|
2012-02-23 09:30:48 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq "john"
|
|
|
|
expect(@context.instance_variable_get(:@y)).to eq "carl"
|
|
|
|
expect(@context.instance_variable_get(:@block_var)).to eq :jesus
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-02-26 07:05:19 -05:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "single line blocks" do
|
|
|
|
it 'should accept blocks with do ; end' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval 'walking-spanish | do ; :jesus; end'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq :jesus
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-02-26 07:05:19 -05:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
it 'should accept blocks with do; end' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval 'walking-spanish | do; :jesus; end'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq :jesus
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-02-23 09:30:48 -05:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
it 'should accept blocks with { }' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval 'walking-spanish | { :jesus }'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq :jesus
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-04-01 19:18:11 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "block-related content removed from arguments" do
|
|
|
|
describe "arg_string" do
|
|
|
|
it 'should remove block-related content from arg_string (with one normal arg)' do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.block_command "walking-spanish", "down the hall", takes_block: true do |x, y|
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@arg_string, arg_string, target)
|
|
|
|
insert_variable(:@x, x, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval 'walking-spanish john| { :jesus }'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@arg_string)).to eq @context.instance_variable_get(:@x)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-04-01 19:18:11 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
it 'should remove block-related content from arg_string (with no normal args)' do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.block_command "walking-spanish", "down the hall", takes_block: true do
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@arg_string, arg_string, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval 'walking-spanish | { :jesus }'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@arg_string)).to eq ""
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-04-01 19:18:11 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
it 'should NOT remove block-related content from arg_string when :takes_block => false' do
|
|
|
|
block_string = "| { :jesus }"
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.block_command "walking-spanish", "homemade special", takes_block: false do
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@arg_string, arg_string, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval "walking-spanish #{block_string}"
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@arg_string)).to eq block_string
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-04-01 19:18:11 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "args" do
|
|
|
|
describe "block_command" do
|
|
|
|
it "should remove block-related content from arguments" do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.block_command "walking-spanish", "glass is full of sand", takes_block: true do |x, y|
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, x, target)
|
|
|
|
insert_variable(:@y, y, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval 'walking-spanish | { :jesus }'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq nil
|
|
|
|
expect(@context.instance_variable_get(:@y)).to eq nil
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should NOT remove block-related content from arguments if :takes_block => false" do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.block_command "walking-spanish", "litella screeching for a blind pig", takes_block: false do |x, y|
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, x, target)
|
|
|
|
insert_variable(:@y, y, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval 'walking-spanish | { :jesus }'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq "|"
|
|
|
|
expect(@context.instance_variable_get(:@y)).to eq "{"
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-04-01 19:18:11 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "create_command" do
|
|
|
|
it "should remove block-related content from arguments" do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.create_command "walking-spanish", "punk sanders carved one out of wood", takes_block: true do
|
2012-07-05 15:40:54 -04:00
|
|
|
def process(x, y)
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, x, target)
|
|
|
|
insert_variable(:@y, y, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval 'walking-spanish | { :jesus }'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq nil
|
|
|
|
expect(@context.instance_variable_get(:@y)).to eq nil
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should NOT remove block-related content from arguments if :takes_block => false" do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.create_command "walking-spanish", "down the hall", takes_block: false do
|
2012-07-05 15:40:54 -04:00
|
|
|
def process(x, y)
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, x, target)
|
|
|
|
insert_variable(:@y, y, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval 'walking-spanish | { :jesus }'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq "|"
|
|
|
|
expect(@context.instance_variable_get(:@y)).to eq "{"
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-04-01 19:20:35 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "blocks can take parameters" do
|
|
|
|
describe "{} style blocks" do
|
|
|
|
it 'should accept multiple parameters' do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.block_command "walking-spanish", "down the hall", takes_block: true do
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, command_block.call(1, 2), target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval 'walking-spanish | { |x, y| [x, y] }'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq [1, 2]
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-15 08:36:03 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "do/end style blocks" do
|
|
|
|
it 'should accept multiple parameters' do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.create_command "walking-spanish", "litella", takes_block: true do
|
2012-07-05 15:40:54 -04:00
|
|
|
def process
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, command_block.call(1, 2), target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval <<-EOS
|
|
|
|
walking-spanish | do |x, y|
|
|
|
|
[x, y]
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq [1, 2]
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-15 08:36:03 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "closure behaviour" do
|
|
|
|
it 'should close over locals in the definition context' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval 'var = :hello', 'walking-spanish | { var }'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq :hello
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-15 08:36:03 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "exposing block parameter" do
|
|
|
|
describe "block_command" do
|
|
|
|
it "should expose block in command_block method" do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.block_command "walking-spanish", "glass full of sand", takes_block: true do
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, command_block.call, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval 'walking-spanish | { :jesus }'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq :jesus
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-15 08:36:03 -04:00
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
describe "create_command" do
|
|
|
|
it "should NOT expose &block in create_command's process method" do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.create_command "walking-spanish", "down the hall", takes_block: true do
|
2012-07-05 15:40:54 -04:00
|
|
|
def process(&block)
|
2012-09-09 02:12:25 -04:00
|
|
|
block.call
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
@out = StringIO.new
|
|
|
|
|
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 { @t.eval 'walking-spanish | { :jesus }' }.to raise_error(NoMethodError)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should expose block in command_block method" do
|
2018-10-12 15:09:29 -04:00
|
|
|
@set.create_command "walking-spanish", "homemade special", takes_block: true do
|
2012-07-05 15:40:54 -04:00
|
|
|
def process
|
2017-11-04 00:32:31 -04:00
|
|
|
insert_variable(:@x, command_block.call, target)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
|
|
|
@t.eval 'walking-spanish | { :jesus }'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@context.instance_variable_get(:@x)).to eq :jesus
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-25 15:18:14 -05:00
|
|
|
describe "a command made with a custom sub-class" do
|
|
|
|
before do
|
2012-07-05 15:40:54 -04:00
|
|
|
class MyTestCommand < Pry::ClassCommand
|
2015-01-22 16:52:20 -05:00
|
|
|
match(/my-*test/)
|
2012-12-25 15:18:14 -05:00
|
|
|
description 'So just how many sound technicians does it take to' \
|
|
|
|
'change a lightbulb? 1? 2? 3? 1-2-3? Testing?'
|
2018-10-12 15:09:29 -04:00
|
|
|
options shellwords: false, listing: 'my-test'
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
undef process if method_defined? :process
|
|
|
|
|
2012-07-05 15:40:54 -04:00
|
|
|
def process
|
|
|
|
output.puts command_name * 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands.add_command MyTestCommand
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands.delete 'my-test'
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
2013-01-03 20:44:57 -05:00
|
|
|
it "allows creation of custom subclasses of Pry::Command" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_eval('my---test')).to match(/my-testmy-test/)
|
2012-12-25 15:18:14 -05:00
|
|
|
end
|
|
|
|
|
2013-10-24 01:58:26 -04:00
|
|
|
it "shows the source of the process method" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_eval('show-source my-test')).to match(/output.puts command_name/)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2013-01-03 20:44:57 -05:00
|
|
|
|
|
|
|
describe "command options hash" do
|
|
|
|
it "is always present" do
|
|
|
|
options_hash = {
|
2018-10-12 15:09:29 -04:00
|
|
|
requires_gem: [],
|
|
|
|
keep_retval: false,
|
|
|
|
argument_required: false,
|
|
|
|
interpolate: true,
|
|
|
|
shellwords: false,
|
|
|
|
listing: 'my-test',
|
|
|
|
use_prefix: true,
|
|
|
|
takes_block: false
|
2013-01-03 20:44:57 -05:00
|
|
|
}
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(MyTestCommand.options).to eq options_hash
|
2013-01-03 20:44:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ":listing option" do
|
|
|
|
it "defaults to :match if not set explicitly" do
|
|
|
|
class HappyNewYear < Pry::ClassCommand
|
|
|
|
match 'happy-new-year'
|
|
|
|
description 'Happy New Year 2013'
|
|
|
|
end
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands.add_command HappyNewYear
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(HappyNewYear.options[:listing]).to eq 'happy-new-year'
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands.delete 'happy-new-year'
|
2013-01-03 20:44:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be set explicitly" do
|
|
|
|
class MerryChristmas < Pry::ClassCommand
|
|
|
|
match 'merry-christmas'
|
|
|
|
description 'Merry Christmas!'
|
2018-10-12 15:09:29 -04:00
|
|
|
command_options listing: 'happy-holidays'
|
2013-01-03 20:44:57 -05:00
|
|
|
end
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands.add_command MerryChristmas
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(MerryChristmas.options[:listing]).to eq 'happy-holidays'
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands.delete 'merry-christmas'
|
2013-01-03 20:44:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "equals to :match option's inspect, if :match is Regexp" do
|
|
|
|
class CoolWinter < Pry::ClassCommand
|
2014-06-24 16:07:30 -04:00
|
|
|
match(/.*winter/)
|
2013-01-03 20:44:57 -05:00
|
|
|
description 'Is winter cool or cool?'
|
|
|
|
end
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands.add_command CoolWinter
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(CoolWinter.options[:listing]).to eq '/.*winter/'
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
Pry.config.commands.delete(/.*winter/)
|
2013-01-03 20:44:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "commands can save state" do
|
|
|
|
before do
|
|
|
|
@set = Pry::CommandSet.new do
|
|
|
|
create_command "litella", "desc" do
|
|
|
|
def process
|
|
|
|
state.my_state ||= 0
|
|
|
|
state.my_state += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
create_command "sanders", "desc" do
|
|
|
|
def process
|
|
|
|
state.my_state = "wood"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
create_command(/[Hh]ello-world/, "desc") do
|
2012-07-05 15:40:54 -04:00
|
|
|
def process
|
|
|
|
state.my_state ||= 0
|
|
|
|
state.my_state += 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.import Pry::Commands
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2018-10-12 15:09:29 -04:00
|
|
|
@t = pry_tester(commands: @set)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should save state for the command on the Pry#command_state hash' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval 'litella'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.pry.command_state["litella"].my_state).to eq 1
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should ensure state is maintained between multiple invocations of command' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval 'litella'
|
|
|
|
@t.eval 'litella'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.pry.command_state["litella"].my_state).to eq 2
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should ensure state with same name stored seperately for each command' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval 'litella', 'sanders'
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.pry.command_state["litella"].my_state).to eq 1
|
|
|
|
expect(@t.pry.command_state["sanders"].my_state).to eq("wood")
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should ensure state is properly saved for regex commands' do
|
2012-09-09 02:12:25 -04:00
|
|
|
@t.eval 'hello-world', 'Hello-world'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.pry.command_state[/[Hh]ello-world/].my_state).to eq 4
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-05 16:31:03 -04:00
|
|
|
|
2012-09-06 01:39:29 -04:00
|
|
|
if defined?(Bond)
|
|
|
|
describe 'complete' do
|
|
|
|
it 'should return the arguments that are defined' do
|
|
|
|
@set.create_command "torrid" do
|
|
|
|
def options(opt)
|
|
|
|
opt.on :test
|
|
|
|
opt.on :lest
|
|
|
|
opt.on :pests
|
|
|
|
end
|
2012-08-21 01:12:03 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@set.complete('torrid ')).to.include('--test ')
|
2012-09-06 01:39:29 -04:00
|
|
|
end
|
2012-08-21 01:12:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-05 16:31:03 -04:00
|
|
|
describe 'group' do
|
|
|
|
before do
|
2012-07-06 14:23:31 -04:00
|
|
|
@set.import(
|
|
|
|
Pry::CommandSet.new do
|
|
|
|
create_command("magic") { group("Not for a public use") }
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be correct for default commands' do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@set["help"].group).to eq "Help"
|
2012-07-05 16:31:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not change once it is initialized' do
|
2014-02-03 23:12:28 -05:00
|
|
|
@set["magic"].group("-==CD COMMAND==-")
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@set["magic"].group).to eq "Not for a public use"
|
2012-07-05 16:31:03 -04:00
|
|
|
end
|
|
|
|
|
2012-07-06 14:23:31 -04:00
|
|
|
it 'should not disappear after the call without parameters' do
|
2014-02-03 23:12:28 -05:00
|
|
|
@set["magic"].group
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@set["magic"].group).to eq "Not for a public use"
|
2012-07-05 16:31:03 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|