2019-05-02 18:33:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
RSpec.describe Pry::Command do
|
|
|
|
subject do
|
|
|
|
Class.new(described_class) do
|
|
|
|
def process; end
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
let(:default_options) do
|
|
|
|
{
|
|
|
|
argument_required: false,
|
|
|
|
interpolate: true,
|
|
|
|
keep_retval: false,
|
|
|
|
shellwords: true,
|
|
|
|
takes_block: false,
|
|
|
|
use_prefix: true,
|
|
|
|
listing: 'nil'
|
|
|
|
}
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".match" do
|
|
|
|
context "when no argument is given" do
|
|
|
|
context "and when match was defined previously" do
|
|
|
|
before { subject.match('old-match') }
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "doesn't overwrite match" do
|
|
|
|
expect(subject.match).to eq('old-match')
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "and when match was not defined previously" do
|
|
|
|
it "sets match to nil" do
|
|
|
|
subject.match
|
|
|
|
expect(subject.match).to be_nil
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when given an argument" do
|
|
|
|
context "and when match is a string" do
|
|
|
|
it "sets command options with listing as match" do
|
|
|
|
subject.match('match') # rubocop:disable Performance/RedundantMatch
|
|
|
|
expect(subject.command_options).to include(listing: 'match')
|
2015-03-11 10:17:10 -04:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "and when match is an object" do
|
|
|
|
let(:object) do
|
|
|
|
obj = Object.new
|
|
|
|
def obj.inspect
|
|
|
|
'inspect'
|
|
|
|
end
|
|
|
|
obj
|
2015-03-11 10:17:10 -04:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets command options with listing as object's inspect" do
|
|
|
|
subject.match(object)
|
|
|
|
expect(subject.command_options).to include(listing: 'inspect')
|
2015-03-11 10:17:10 -04:00
|
|
|
end
|
2018-11-17 11:36:09 -05:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".description" do
|
|
|
|
context "and when description was defined previously" do
|
|
|
|
before { subject.description('old description') }
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "doesn't overwrite match" do
|
|
|
|
subject.description
|
|
|
|
expect(subject.description).to eq('old description')
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "and when description was not defined previously" do
|
|
|
|
it "sets description to nil" do
|
|
|
|
expect(subject.description).to be_nil
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when given an argument" do
|
|
|
|
it "sets description" do
|
|
|
|
subject.description('description')
|
|
|
|
expect(subject.description).to eq('description')
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".command_options" do
|
|
|
|
context "when no argument is given" do
|
|
|
|
context "and when command options were defined previously" do
|
|
|
|
before { subject.command_options(foo: :bar) }
|
|
|
|
|
|
|
|
it "returns memoized command options" do
|
|
|
|
expect(subject.command_options).to eq(default_options.merge(foo: :bar))
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "and when command options were not defined previously" do
|
|
|
|
it "sets command options to default options" do
|
|
|
|
subject.command_options
|
|
|
|
expect(subject.command_options).to eq(default_options)
|
|
|
|
end
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2011-12-31 10:57:50 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when given an argument" do
|
|
|
|
let(:new_option) { { new_option: 'value' } }
|
2011-12-31 10:57:50 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "merges the argument with command options" do
|
|
|
|
expect(subject.command_options(new_option))
|
|
|
|
.to eq(default_options.merge(new_option))
|
|
|
|
end
|
2011-12-31 10:57:50 -05:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".banner" do
|
|
|
|
context "when no argument is given" do
|
|
|
|
context "and when banner was defined previously" do
|
|
|
|
before { subject.banner('banner') }
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns the memoized banner" do
|
|
|
|
expect(subject.banner).to eq('banner')
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
2014-09-12 12:40:37 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "and when banner was not defined previously" do
|
|
|
|
it "return nil" do
|
|
|
|
subject.banner
|
|
|
|
expect(subject.banner).to be_nil
|
2014-09-12 12:40:37 -04:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when given an argument" do
|
|
|
|
it "merges the argument with command options" do
|
|
|
|
expect(subject.banner('banner')).to eq('banner')
|
2014-09-12 12:40:37 -04:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".block" do
|
|
|
|
context "when block exists" do
|
|
|
|
let(:block) { proc {} }
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns the block" do
|
|
|
|
subject.block = block
|
|
|
|
expect(subject.block).to eql(block)
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when block doesn't exist" do
|
|
|
|
it "uses #process method" do
|
|
|
|
expect(subject.block.name).to eq(:process)
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".source" do
|
|
|
|
it "returns source code of the method" do
|
|
|
|
expect(subject.source).to eq("def process; end\n")
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".doc" do
|
|
|
|
subject do
|
|
|
|
Class.new(described_class) do
|
|
|
|
def help
|
|
|
|
'help'
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns help output" do
|
|
|
|
expect(subject.doc).to eq('help')
|
|
|
|
end
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".source_file" do
|
|
|
|
it "returns source file" do
|
|
|
|
expect(subject.source_file).to match(__FILE__)
|
|
|
|
end
|
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".source_line" do
|
|
|
|
it "returns source line" do
|
|
|
|
expect(subject.source_line).to be_kind_of(Integer)
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2012-01-14 23:54:30 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".default_options" do
|
|
|
|
context "when given a String argument" do
|
|
|
|
it "returns default options with string listing" do
|
|
|
|
expect(subject.default_options('listing'))
|
|
|
|
.to eq(default_options.merge(listing: 'listing'))
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when given an Object argument" do
|
|
|
|
let(:object) do
|
|
|
|
obj = Object.new
|
|
|
|
def obj.inspect
|
|
|
|
'inspect'
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
obj
|
|
|
|
end
|
2012-11-26 11:07:32 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns default options with object's inspect as listing" do
|
|
|
|
expect(subject.default_options(object))
|
|
|
|
.to eq(default_options.merge(listing: 'inspect'))
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
|
|
|
end
|
2012-11-26 11:07:32 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".name" do
|
|
|
|
it "returns the name of the command" do
|
|
|
|
expect(subject.name).to eq('#<class(Pry::Command nil)>')
|
2012-11-26 11:07:32 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when super command name exists" do
|
|
|
|
subject do
|
|
|
|
parent = Class.new(described_class) do
|
|
|
|
def name
|
|
|
|
'parent name'
|
2012-08-08 00:08:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
Class.new(parent)
|
2012-08-08 00:08:30 -04:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns the name of the parent command" do
|
|
|
|
expect(subject.name).to eq('#<class(Pry::Command nil)>')
|
2012-01-14 23:54:30 -05:00
|
|
|
end
|
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2013-01-01 15:22:00 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".inspect" do
|
|
|
|
subject do
|
|
|
|
Class.new(described_class) do
|
|
|
|
def self.name
|
|
|
|
'name'
|
2013-01-01 15:22:00 -05:00
|
|
|
end
|
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2013-01-01 15:22:00 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns command name" do
|
|
|
|
expect(subject.inspect).to eq('name')
|
2013-01-01 15:22:00 -05:00
|
|
|
end
|
2011-12-31 10:21:58 -05:00
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".command_name" do
|
|
|
|
before { subject.match('foo') }
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns listing" do
|
|
|
|
expect(subject.command_name).to eq('foo')
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".subclass" do
|
|
|
|
it "returns a new class" do
|
|
|
|
klass = subject.subclass('match', 'desc', {}, Module.new)
|
|
|
|
expect(klass).to be_a(Class)
|
|
|
|
expect(klass).not_to eql(subject)
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "includes helpers to the new class" do
|
|
|
|
mod = Module.new { def foo; end }
|
|
|
|
klass = subject.subclass('match', 'desc', {}, mod)
|
|
|
|
expect(klass.new).to respond_to(:foo)
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets match on the new class" do
|
|
|
|
klass = subject.subclass('match', 'desc', {}, Module.new)
|
|
|
|
expect(klass.match).to eq('match')
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets description on the new class" do
|
|
|
|
klass = subject.subclass('match', 'desc', {}, Module.new)
|
|
|
|
expect(klass.description).to eq('desc')
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets command options on the new class" do
|
|
|
|
klass = subject.subclass('match', 'desc', { foo: :bar }, Module.new)
|
|
|
|
expect(klass.command_options).to include(foo: :bar)
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets block on the new class" do
|
|
|
|
block = proc {}
|
|
|
|
klass = subject.subclass('match', 'desc', { foo: :bar }, Module.new, &block)
|
|
|
|
expect(klass.block).to eql(block)
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".matches?" do
|
|
|
|
context "when given value matches command regex" do
|
|
|
|
before { subject.match('test-command') }
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns true" do
|
|
|
|
expect(subject.matches?('test-command')).to be_truthy
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when given value doesn't match command regex" do
|
|
|
|
it "returns false" do
|
|
|
|
expect(subject.matches?('test-command')).to be_falsey
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".match_score" do
|
|
|
|
context "when command regex matches given value" do
|
|
|
|
context "and when the size of last match is more than 1" do
|
|
|
|
before { subject.match(/\.(.*)/) }
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns the length of the first match" do
|
|
|
|
expect(subject.match_score('.||')).to eq(1)
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "and when the size of last match is 1 or 0" do
|
|
|
|
before { subject.match('hi') }
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns the length of the last match" do
|
|
|
|
expect(subject.match_score('hi there')).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
2012-01-28 22:39:31 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when command regex doesn't match given value" do
|
|
|
|
it "returns -1" do
|
|
|
|
expect(subject.match_score('test')).to eq(-1)
|
2012-01-28 22:39:31 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
|
|
|
end
|
2012-01-28 22:39:31 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".command_regex" do
|
|
|
|
before { subject.match('test-command') }
|
2012-01-28 22:39:31 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when use_prefix is true" do
|
|
|
|
before { subject.command_options(use_prefix: true) }
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns a Regexp without a prefix" do
|
2019-07-11 09:54:10 -04:00
|
|
|
expect(subject.command_regex).to eq(/\Atest\-command(?!\S)/)
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when use_prefix is false" do
|
|
|
|
before { subject.command_options(use_prefix: false) }
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns a Regexp with a prefix" do
|
2019-07-11 09:54:10 -04:00
|
|
|
expect(subject.command_regex).to eq(/\A(?:)?test\-command(?!\S)/)
|
2018-11-17 11:36:09 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".convert_to_regex" do
|
|
|
|
context "when given object is a String" do
|
|
|
|
it "escapes the string as a Regexp" do
|
|
|
|
expect(subject.convert_to_regex('foo.+')).to eq('foo\\.\\+')
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when given object is an Object" do
|
|
|
|
let(:obj) { Object.new }
|
|
|
|
|
|
|
|
it "returns the given object" do
|
|
|
|
expect(subject.convert_to_regex(obj)).to eql(obj)
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
2019-02-24 16:36:01 -05:00
|
|
|
end
|
2012-02-23 09:30:48 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe ".group" do
|
|
|
|
context "when name is given" do
|
|
|
|
it "sets group to that name" do
|
|
|
|
expect(subject.group('Test Group')).to eq('Test Group')
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-02-23 09:30:48 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when source file matches a pry command" do
|
|
|
|
before do
|
|
|
|
expect_any_instance_of(Pry::Method).to receive(:source_file)
|
|
|
|
.and_return('/pry/test_commands/test_command.rb')
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets group name to command name" do
|
|
|
|
expect(subject.group).to eq('Test command')
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-02-23 09:30:48 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when source file matches a pry plugin" do
|
|
|
|
before do
|
|
|
|
expect_any_instance_of(Pry::Method).to receive(:source_file)
|
|
|
|
.and_return('pry-test-1.2.3')
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets group name to plugin name" do
|
|
|
|
expect(subject.group).to eq('pry-test (v1.2.3)')
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-02-26 07:05:19 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when source file matches 'pryrc'" do
|
|
|
|
before do
|
|
|
|
expect_any_instance_of(Pry::Method).to receive(:source_file)
|
|
|
|
.and_return('pryrc')
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-02-26 07:05:19 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets group name to pryrc" do
|
|
|
|
expect(subject.group).to eq('pryrc')
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2012-02-23 09:30:48 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when source file doesn't match anything" do
|
|
|
|
it "returns '(other)'" do
|
|
|
|
expect(subject.group).to eq('(other)')
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2012-04-01 19:18:11 -04:00
|
|
|
|
2019-04-29 12:21:54 -04:00
|
|
|
describe ".state" do
|
|
|
|
it "returns a command state" do
|
|
|
|
expect(described_class.state).to be_an(OpenStruct)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#run" do
|
|
|
|
let(:command_set) do
|
|
|
|
set = Pry::CommandSet.new
|
|
|
|
set.command('test') {}
|
|
|
|
set
|
|
|
|
end
|
2012-04-01 19:18:11 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
subject do
|
|
|
|
command = Class.new(described_class)
|
|
|
|
command.new(command_set: command_set, pry_instance: Pry.new)
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "runs a command from another command" do
|
|
|
|
result = subject.run('test')
|
|
|
|
expect(result).to be_command
|
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#commands" do
|
|
|
|
let(:command_set) do
|
|
|
|
set = Pry::CommandSet.new
|
|
|
|
set.command('test') do
|
|
|
|
def process; end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
set
|
|
|
|
end
|
2012-04-01 19:18:11 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
subject do
|
|
|
|
command = Class.new(described_class)
|
|
|
|
command.new(command_set: command_set, pry_instance: Pry.new)
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns command set as a hash" do
|
|
|
|
expect(subject.commands).to eq('test' => command_set['test'])
|
|
|
|
end
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#void" do
|
|
|
|
it "returns void value" do
|
|
|
|
expect(subject.new.void).to eq(Pry::Command::VOID_VALUE)
|
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#target_self" do
|
|
|
|
let(:target) { binding }
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
subject { Class.new(described_class).new(target: target) }
|
2012-04-01 19:18:11 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns the value of self inside the target binding" do
|
|
|
|
expect(subject.target_self).to eq(target.eval('self'))
|
|
|
|
end
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#state" do
|
|
|
|
let(:target) { binding }
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
subject { Class.new(described_class).new(pry_instance: Pry.new) }
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-03-24 20:40:05 -04:00
|
|
|
it "returns a state object" do
|
2019-04-29 12:21:54 -04:00
|
|
|
expect(subject.state).to be_an(OpenStruct)
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-04-01 19:20:35 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "remembers the state" do
|
2019-04-29 16:52:05 -04:00
|
|
|
subject.state.foo = :bar
|
|
|
|
expect(subject.state.foo).to eq(:bar)
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#interpolate_string" do
|
|
|
|
context "when given string contains \#{" do
|
|
|
|
let(:target) do
|
|
|
|
foo = 'bar'
|
|
|
|
binding
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-06-15 08:36:03 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
subject { Class.new(described_class).new(target: target) }
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns the result of eval within target" do
|
|
|
|
# rubocop:disable Lint/InterpolationCheck
|
|
|
|
expect(subject.interpolate_string('#{foo}')).to eq('bar')
|
|
|
|
# rubocop:enable Lint/InterpolationCheck
|
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when given string doesn't contain \#{" do
|
|
|
|
it "returns the given string" do
|
|
|
|
expect(subject.new.interpolate_string('foo')).to eq('foo')
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2012-06-15 08:36:03 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#check_for_command_collision" do
|
|
|
|
let(:command_set) do
|
|
|
|
set = Pry::CommandSet.new
|
|
|
|
set.command('test') do
|
|
|
|
def process; end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
set
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-06-15 08:36:03 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
let(:output) { StringIO.new }
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
subject do
|
|
|
|
command = Class.new(described_class)
|
|
|
|
command.new(command_set: command_set, target: target, output: output)
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when a command collides with a local variable" do
|
|
|
|
let(:target) do
|
|
|
|
test = 'foo'
|
|
|
|
binding
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-06-15 08:36:03 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "displays a warning" do
|
|
|
|
subject.check_for_command_collision('test', '')
|
|
|
|
expect(output.string)
|
|
|
|
.to match("'test', which conflicts with a local-variable")
|
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when a command collides with a method" do
|
|
|
|
let(:target) do
|
|
|
|
def test; end
|
|
|
|
binding
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "displays a warning" do
|
|
|
|
subject.check_for_command_collision('test', '')
|
|
|
|
expect(output.string).to match("'test', which conflicts with a method")
|
|
|
|
end
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when a command doesn't collide" do
|
|
|
|
let(:target) do
|
|
|
|
def test; end
|
|
|
|
binding
|
|
|
|
end
|
2012-09-09 02:12:25 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "doesn't display a warning" do
|
|
|
|
subject.check_for_command_collision('nothing', '')
|
|
|
|
expect(output.string).to be_empty
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#tokenize" do
|
|
|
|
let(:target) { binding }
|
|
|
|
let(:klass) { Class.new(described_class) }
|
|
|
|
let(:target) { binding }
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
subject { klass.new(target: target) }
|
2015-01-22 16:52:20 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
before { klass.match('test') }
|
|
|
|
|
|
|
|
context "when given string uses interpolation" do
|
|
|
|
let(:target) do
|
|
|
|
foo = 4
|
|
|
|
binding
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
before { klass.command_options(interpolate: true) }
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "interpolates the string in the target's context" do
|
|
|
|
# rubocop:disable Lint/InterpolationCheck
|
|
|
|
expect(subject.tokenize('test #{1 + 2} #{3 + foo}'))
|
|
|
|
.to eq(['test', '3 7', [], %w[3 7]])
|
|
|
|
# rubocop:enable Lint/InterpolationCheck
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "and when interpolation is disabled" do
|
|
|
|
before { klass.command_options(interpolate: false) }
|
2012-12-25 15:18:14 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "doesn't interpolate the string" do
|
|
|
|
# rubocop:disable Lint/InterpolationCheck
|
|
|
|
expect(subject.tokenize('test #{3 + foo}'))
|
|
|
|
.to eq(['test', '#{3 + foo}', [], %w[#{3 + foo}]])
|
|
|
|
# rubocop:enable Lint/InterpolationCheck
|
|
|
|
end
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when given string doesn't match a command" do
|
|
|
|
it "raises CommandError" do
|
|
|
|
expect { subject.tokenize('boom') }
|
|
|
|
.to raise_error(Pry::CommandError, /command which didn't match/)
|
2013-01-03 20:44:57 -05:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when target is not set" do
|
|
|
|
subject { klass.new }
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "still returns tokens" do
|
|
|
|
expect(subject.tokenize('test --help'))
|
|
|
|
.to eq(['test', '--help', [], ['--help']])
|
|
|
|
end
|
|
|
|
end
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when shellwords is enabled" do
|
|
|
|
before { klass.command_options(shellwords: true) }
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "strips quotes from the arguments" do
|
|
|
|
expect(subject.tokenize(%(test "foo" 'bar' 1)))
|
|
|
|
.to eq(['test', %("foo" 'bar' 1), [], %w[foo bar 1]])
|
|
|
|
end
|
|
|
|
end
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when shellwords is disabled" do
|
|
|
|
before { klass.command_options(shellwords: false) }
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "doesn't split quotes from the arguments" do
|
|
|
|
# rubocop:disable Lint/PercentStringArray
|
|
|
|
expect(subject.tokenize(%(test "foo" 'bar' 1)))
|
|
|
|
.to eq(['test', %("foo" 'bar' 1), [], %w["foo" 'bar' 1]])
|
|
|
|
# rubocop:enable Lint/PercentStringArray
|
|
|
|
end
|
|
|
|
end
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when command regex has captures" do
|
|
|
|
before { klass.match(/perfectly (normal)( beast)/i) }
|
2013-01-03 20:44:57 -05:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "returns the captures" do
|
|
|
|
expect(subject.tokenize('Perfectly Normal Beast (honest!)')).to eq(
|
|
|
|
[
|
|
|
|
'Perfectly Normal Beast',
|
|
|
|
'(honest!)',
|
|
|
|
['Normal', ' Beast'],
|
|
|
|
['(honest!)']
|
|
|
|
]
|
|
|
|
)
|
2013-01-03 20:44:57 -05:00
|
|
|
end
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#process_line" do
|
|
|
|
let(:klass) do
|
|
|
|
Class.new(described_class) do
|
|
|
|
def call(*args); end
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
let(:target) do
|
|
|
|
test = 4
|
|
|
|
binding
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
let(:output) { StringIO.new }
|
|
|
|
|
|
|
|
subject { klass.new(target: target, output: output) }
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
before { klass.match(/test(y)?/) }
|
2012-07-05 15:40:54 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets arg_string" do
|
|
|
|
subject.process_line('test -v')
|
|
|
|
expect(subject.arg_string).to eq('-v')
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "sets captures" do
|
|
|
|
subject.process_line('testy')
|
|
|
|
expect(subject.captures).to eq(['y'])
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|
2012-07-05 16:31:03 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "collision warnings" do
|
|
|
|
context "when collision warnings are configured" do
|
|
|
|
before do
|
|
|
|
expect(Pry.config).to receive(:collision_warning).and_return(true)
|
2012-08-21 01:12:03 -04:00
|
|
|
end
|
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "prints a warning when there's a collision" do
|
|
|
|
subject.process_line('test')
|
|
|
|
expect(output.string).to match(/conflicts with a local-variable/)
|
|
|
|
end
|
2012-09-06 01:39:29 -04:00
|
|
|
end
|
2012-08-21 01:12:03 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
context "when collision warnings are not set" do
|
|
|
|
before do
|
|
|
|
expect(Pry.config).to receive(:collision_warning).and_return(false)
|
2019-02-24 17:31:59 -05:00
|
|
|
end
|
2012-07-05 16:31:03 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
it "prints a warning when there's a collision" do
|
|
|
|
subject.process_line('test')
|
|
|
|
expect(output.string).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2012-07-05 16:31:03 -04:00
|
|
|
end
|
2019-04-08 19:58:34 -04:00
|
|
|
end
|
2012-07-05 16:31:03 -04:00
|
|
|
|
2019-04-08 19:58:34 -04:00
|
|
|
describe "#complete" do
|
|
|
|
it "returns empty array" do
|
|
|
|
expect(subject.new.complete('')).to eq([])
|
2012-07-05 16:31:03 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-05 15:40:54 -04:00
|
|
|
end
|