Skip to content

Commit

Permalink
add command_name to context
Browse files Browse the repository at this point in the history
  • Loading branch information
rf- committed Sep 25, 2011
1 parent d81d2eb commit 8920438
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
1 change: 1 addition & 0 deletions lib/pry/command_context.rb
Expand Up @@ -9,6 +9,7 @@ class CommandContext
# give it a nice inspect
def VOID_VALUE.inspect() "void" end

attr_accessor :command_name
attr_accessor :output
attr_accessor :target
attr_accessor :target_self
Expand Down
2 changes: 2 additions & 0 deletions lib/pry/command_set.rb
Expand Up @@ -11,6 +11,8 @@ class CommandSet
class Command < Struct.new(:name, :description, :options, :block)

def call(context, *args)
context.command_name = options[:listing]

if stub_block = options[:stub_info]
context.instance_eval(&stub_block)
else
Expand Down
57 changes: 29 additions & 28 deletions test/test_command_set.rb
Expand Up @@ -3,6 +3,7 @@
describe Pry::CommandSet do
before do
@set = Pry::CommandSet.new
@ctx = Pry::CommandContext.new
end

it 'should call the block used for the command when it is called' do
Expand All @@ -11,7 +12,7 @@
run = true
end

@set.run_command nil, 'foo'
@set.run_command @ctx, 'foo'
run.should == true
end

Expand All @@ -20,21 +21,23 @@
args.should == [1, 2, 3]
end

@set.run_command nil, 'foo', 1, 2, 3
@set.run_command @ctx, 'foo', 1, 2, 3
end

it 'should use the first argument as self' do
ctx = @ctx

@set.command 'foo' do
self.should == true
self.should == ctx
end

@set.run_command true, 'foo'
@set.run_command @ctx, 'foo'
end

it 'should raise an error when calling an undefined command' do
@set.command('foo') {}
lambda {
@set.run_command nil, 'bar'
@set.run_command @ctx, 'bar'
}.should.raise(Pry::NoCommandError)
end

Expand All @@ -43,7 +46,7 @@
@set.delete 'foo'

lambda {
@set.run_command nil, 'foo'
@set.run_command @ctx, 'foo'
}.should.raise(Pry::NoCommandError)
end

Expand All @@ -57,11 +60,11 @@

@set.import_from(other_set, 'foo')

@set.run_command nil, 'foo'
@set.run_command @ctx, 'foo'
run.should == true

lambda {
@set.run_command nil, 'bar'
@set.run_command @ctx, 'bar'
}.should.raise(Pry::NoCommandError)
end

Expand All @@ -75,16 +78,16 @@

@set.import other_set

@set.run_command nil, 'foo'
@set.run_command nil, 'bar'
@set.run_command @ctx, 'foo'
@set.run_command @ctx, 'bar'
run.should == [true, true]
end

it 'should be able to import sets at creation' do
run = false
@set.command('foo') { run = true }

Pry::CommandSet.new(@set).run_command nil, 'foo'
Pry::CommandSet.new(@set).run_command @ctx, 'foo'
run.should == true
end

Expand All @@ -101,7 +104,7 @@
@set.commands['bar'].name.should == 'bar'
@set.commands['bar'].description.should == ''

@set.run_command nil, 'bar'
@set.run_command @ctx, 'bar'
run.should == true
end

Expand All @@ -114,17 +117,17 @@

it 'should return Pry::CommandContext::VOID_VALUE for commands by default' do
@set.command('foo') { 3 }
@set.run_command(nil, 'foo').should == Pry::CommandContext::VOID_VALUE
@set.run_command(@ctx, 'foo').should == Pry::CommandContext::VOID_VALUE
end

it 'should be able to keep return values' do
@set.command('foo', '', :keep_retval => true) { 3 }
@set.run_command(nil, 'foo').should == 3
@set.run_command(@ctx, 'foo').should == 3
end

it 'should be able to keep return values, even if return value is nil' do
@set.command('foo', '', :keep_retval => true) { nil }
@set.run_command(nil, 'foo').should == nil
@set.run_command(@ctx, 'foo').should == nil
end

it 'should be able to have its own helpers' do
Expand All @@ -136,7 +139,7 @@
def my_helper; end
end

@set.run_command(Pry::CommandContext.new, 'foo')
@set.run_command(@ctx, 'foo')
Pry::CommandContext.new.should.not.respond_to :my_helper
end

Expand All @@ -154,7 +157,7 @@ def my_helper; end
def my_other_helper; end
end

@set.run_command(Pry::CommandContext.new, 'foo')
@set.run_command(@ctx, 'foo')
end

it 'should import helpers from imported sets' do
Expand All @@ -166,7 +169,7 @@ def imported_helper_method; end

@set.import imported_set
@set.command('foo') { should.respond_to :imported_helper_method }
@set.run_command(Pry::CommandContext.new, 'foo')
@set.run_command(@ctx, 'foo')
end

it 'should import helpers even if only some commands are imported' do
Expand All @@ -180,7 +183,7 @@ def imported_helper_method; end

@set.import_from imported_set, 'bar'
@set.command('foo') { should.respond_to :imported_helper_method }
@set.run_command(Pry::CommandContext.new, 'foo')
@set.run_command(@ctx, 'foo')
end

it 'should provide a :listing for a command that defaults to its name' do
Expand All @@ -194,12 +197,11 @@ def imported_helper_method; end
end

it "should provide a 'help' command" do
context = Pry::CommandContext.new
context.command_set = @set
context.output = StringIO.new
@ctx.command_set = @set
@ctx.output = StringIO.new

lambda {
@set.run_command(context, 'help')
@set.run_command(@ctx, 'help')
}.should.not.raise
end

Expand All @@ -209,13 +211,12 @@ def imported_helper_method; end
@set.command 'moo', "Mooerizes" do; end
@set.command 'boo', "Booerizes" do; end

context = Pry::CommandContext.new
context.command_set = @set
context.output = StringIO.new
@ctx.command_set = @set
@ctx.output = StringIO.new

@set.run_command(context, 'help')
@set.run_command(@ctx, 'help')

doc = context.output.string
doc = @ctx.output.string

order = [doc.index("boo"),
doc.index("foo"),
Expand Down

0 comments on commit 8920438

Please sign in to comment.