From e8776e8a2cb43a0ad29fd4e9a908195a1c20bc71 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 6 May 2011 22:44:59 +1200 Subject: [PATCH] added some tests for cd command (and new behaviour) in test_default_commands.rb --- test/helper.rb | 20 ++++ test/test_commandset.rb | 184 ---------------------------------- test/test_default_commands.rb | 31 ++++++ 3 files changed, 51 insertions(+), 184 deletions(-) delete mode 100644 test/test_commandset.rb create mode 100644 test/test_default_commands.rb diff --git a/test/helper.rb b/test/helper.rb index 17be1c8a..09ef5edc 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -10,6 +10,26 @@ Pry::RC_FILES.clear Pry.color = false Pry.should_load_rc = false +def redirect_global_pry_input(new_io) + old_io = Pry.input + Pry.input = new_io + begin + yield + ensure + Pry.input = old_io + end +end + +def redirect_global_pry_output(new_io) + old_io = Pry.output + Pry.output = new_io + begin + yield + ensure + Pry.output = old_io + end +end + class Module public :remove_const end diff --git a/test/test_commandset.rb b/test/test_commandset.rb deleted file mode 100644 index e2edea70..00000000 --- a/test/test_commandset.rb +++ /dev/null @@ -1,184 +0,0 @@ -require 'helper' - -describe Pry::CommandSet do - before do - @set = Pry::CommandSet.new(:some_name) - end - - it 'should use the name specified at creation' do - @set.name.should == :some_name - end - - it 'should call the block used for the command when it is called' do - run = false - @set.command 'foo' do - run = true - end - - @set.run_command nil, 'foo' - run.should == true - end - - it 'should pass arguments of the command to the block' do - @set.command 'foo' do |*args| - args.should == [1, 2, 3] - end - - @set.run_command nil, 'foo', 1, 2, 3 - end - - it 'should use the first argument as self' do - @set.command 'foo' do - self.should == true - end - - @set.run_command true, 'foo' - end - - it 'should raise an error when calling an undefined comand' do - @set.command('foo') {} - lambda { - @set.run_command nil, 'bar' - }.should.raise(Pry::NoCommandError) - end - - it 'should be able to remove its own commands' do - @set.command('foo') {} - @set.delete 'foo' - - lambda { - @set.run_command nil, 'foo' - }.should.raise(Pry::NoCommandError) - end - - it 'should be able to import some commands from other sets' do - run = false - - other_set = Pry::CommandSet.new :foo do - command('foo') { run = true } - command('bar') {} - end - - @set.import_from(other_set, 'foo') - - @set.run_command nil, 'foo' - run.should == true - - lambda { - @set.run_command nil, 'bar' - }.should.raise(Pry::NoCommandError) - end - - it 'should be able to import a whole set' do - run = [] - - other_set = Pry::CommandSet.new :foo do - command('foo') { run << true } - command('bar') { run << true } - end - - @set.import other_set - - @set.run_command nil, 'foo' - @set.run_command nil, '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(:other, @set).run_command nil, 'foo' - run.should == true - end - - it 'should set the descriptions of commands' do - @set.command('foo', 'some stuff') {} - @set.commands['foo'].description.should == 'some stuff' - end - - it 'should be able to alias method' do - run = false - @set.command('foo', 'stuff') { run = true } - - @set.alias_command 'bar', 'foo' - @set.commands['bar'].name.should == 'bar' - @set.commands['bar'].description.should == 'stuff' - - @set.run_command nil, 'bar' - run.should == true - end - - it 'should be able to change the descritpions of methods' do - @set.command('foo', 'bar') {} - @set.desc 'foo', 'baz' - - @set.commands['foo'].description.should == 'baz' - end - - it 'should return nil for commands by default' do - @set.command('foo') { 3 } - @set.run_command(nil, 'foo').should == nil - end - - it 'should be able to keep return values' do - @set.command('foo', '', :keep_retval => true) { 3 } - @set.run_command(nil, 'foo').should == 3 - end - - it 'should be able to have its own helpers' do - @set.command('foo') do - should.respond_to :my_helper - end - - @set.helpers do - def my_helper; end - end - - @set.run_command(Pry::CommandContext.new, 'foo') - Pry::CommandContext.new.should.not.respond_to :my_helper - end - - it 'should not recreate a new heler module when helpers is called' do - @set.command('foo') do - should.respond_to :my_helper - should.respond_to :my_other_helper - end - - @set.helpers do - def my_helper; end - end - - @set.helpers do - def my_other_helper; end - end - - @set.run_command(Pry::CommandContext.new, 'foo') - end - - it 'should import helpers from imported sets' do - imported_set = Pry::CommandSet.new :test do - helpers do - def imported_helper_method; end - end - end - - @set.import imported_set - @set.command('foo') { should.respond_to :imported_helper_method } - @set.run_command(Pry::CommandContext.new, 'foo') - end - - it 'should import helpers even if only some commands are imported' do - imported_set = Pry::CommandSet.new :test do - helpers do - def imported_helper_method; end - end - - command('bar') {} - end - - @set.import_from imported_set, 'bar' - @set.command('foo') { should.respond_to :imported_helper_method } - @set.run_command(Pry::CommandContext.new, 'foo') - end -end diff --git a/test/test_default_commands.rb b/test/test_default_commands.rb new file mode 100644 index 00000000..c63e992a --- /dev/null +++ b/test/test_default_commands.rb @@ -0,0 +1,31 @@ +require 'helper' + +describe Pry::Commands do + describe "cd" do + it 'should cd into simple input' do + str_output = StringIO.new + b = Pry.binding_for(Object.new) + b.eval("x = :mon_ouie") + + redirect_global_pry_input(InputTester.new("cd x", "exit-all")) do + Pry.new(:output => str_output).rep(b) + end + + str_output.string.should =~ /:mon_ouie/ + end + + it 'should cd into complex input (with spaces)' do + str_output = StringIO.new + o = Object.new + def o.hello(x, y, z) + :mon_ouie + end + + redirect_global_pry_input(InputTester.new("cd hello 1, 2, 3", "exit-all")) do + Pry.new(:output => str_output).rep(o) + end + + str_output.string.should =~ /:mon_ouie/ + end + end +end