From ad5bb4c3990ffc301946ed429f6b49373cd69ac2 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Fri, 4 Jan 2013 03:44:57 +0200 Subject: [PATCH] Assign default value to `:listing` option When you create a class command, there is a problem with `:listing` option, which doesn't carry the correct default value. Consider the example: class MakeMeHappy < Pry::ClassCommand match 'woot' end `MakeMeHappy` command matches against 'woot' String, but its `:listing` option is set to the "nil" String, which is incorrect. We can fix it by setting `:listing` explicitly: command_options :listing => 'woot' It's a repetitive task, so we can automate it. Holy smoke, this why we all use computers, after all! With help of this commit there is no need to set `:listing` manually. Pry will handle it for you. Signed-off-by: Kyrylo Silin --- lib/pry/command.rb | 6 +++- spec/command_spec.rb | 72 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 54f6fb42..311f3bec 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -25,7 +25,11 @@ class Pry attr_writer :match def match(arg=nil) - @match = arg if arg + if arg + @command_options ||= default_options(arg) + @command_options[:listing] = arg.is_a?(String) ? arg : arg.inspect + @match = arg + end @match end diff --git a/spec/command_spec.rb b/spec/command_spec.rb index be37236a..bd4db38b 100644 --- a/spec/command_spec.rb +++ b/spec/command_spec.rb @@ -733,29 +733,71 @@ describe "Pry::Command" do Pry.commands.delete 'my-test' end - it "allows creation of custom sub-classes of Pry::Command" do + it "allows creation of custom subclasses of Pry::Command" do pry_eval('my---test').should =~ /my-testmy-test/ end - it "never loses its command options" do - options_hash = { - :requires_gem => [], - :keep_retval => false, - :argument_required => false, - :interpolate => true, - :shellwords => false, - :listing => 'my-test', - :use_prefix => true, - :takes_block => false - } - MyTestCommand.options.should == options_hash - end - if !mri18_and_no_real_source_location? it "shows the source of the process method" do pry_eval('show-source my-test').should =~ /output.puts command_name/ end end + + describe "command options hash" do + it "is always present" do + options_hash = { + :requires_gem => [], + :keep_retval => false, + :argument_required => false, + :interpolate => true, + :shellwords => false, + :listing => 'my-test', + :use_prefix => true, + :takes_block => false + } + MyTestCommand.options.should == options_hash + 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 + Pry.commands.add_command HappyNewYear + + HappyNewYear.options[:listing].should == 'happy-new-year' + + Pry.commands.delete 'happy-new-year' + end + + it "can be set explicitly" do + class MerryChristmas < Pry::ClassCommand + match 'merry-christmas' + description 'Merry Christmas!' + command_options :listing => 'happy-holidays' + end + Pry.commands.add_command MerryChristmas + + MerryChristmas.options[:listing].should == 'happy-holidays' + + Pry.commands.delete 'merry-christmas' + end + + it "equals to :match option's inspect, if :match is Regexp" do + class CoolWinter < Pry::ClassCommand + match /.*winter/ + description 'Is winter cool or cool?' + end + Pry.commands.add_command CoolWinter + + CoolWinter.options[:listing].should == '/.*winter/' + + Pry.commands.delete /.*winter/ + end + end + end + end describe "commands can save state" do