<commit_before>
require File.dirname(__FILE__) + '/spec_helper'

describe Commander do
  before :each do
    $stderr = StringIO.new
    mock_terminal
    create_test_command
  end
	
  describe "#program" do
    it "should set / get program information" do
      program :name, 'test'
  	  program(:name).should == 'test'
    end
    
    it "should allow arbitrary blocks of global help documentation" do
      program :help, 'Copyright', '<NAME>'
  	  program(:help)['Copyright'].should == '<NAME>'
    end
    
    it "should raise an error when required info has not been set" do
      new_command_runner '--help'
  	  program :version, ''
      lambda { run! }.should raise_error(Commander::Runner::CommandError)
    end
    
    it "should allow aliases of help formatters" do
      program :help_formatter, :compact
      program(:help_formatter).should == Commander::HelpFormatter::TerminalCompact
    end
  end
  
  describe "#command" do
  	it "should return a command instance when only the name is passed" do
  	  command(:test).should be_instance_of(Commander::Command)
  	end
    
    it "should return nil when the command does not exist" do
      command(:im_not_real).should be_nil
    end
  end
  
  describe "#separate_switches_from_description" do
    it "should seperate switches and description returning both" do
      switches, description = *Commander::Runner.separate_switches_from_description('-h', '--help', 'display help')
      switches.should == ['-h', '--help']
      description.should == 'display help'
    end
  end
  
  describe "#switch_to_sym" do
    it "should return a symbol based on the switch name" do
     Commander::Runner.switch_to_sym('--trace').should == :trace
     Commander::Runner.switch_to_sym('--foo-bar').should == :foo_bar
     Commander::Runner.switch_to_sym('--[no-]feature"').should == :feature
     Commander::Runner.switch_to_sym('--[no-]feature ARG').should == :feature
     Commander::Runner.switch_to_sym('--file [ARG]').should == :file
     Commander::Runner.switch_to_sym('--colors colors').should == :colors
    end
  end
  
  describe "#alias_command" do
    it "should alias a command" do
      alias_command :foo, :test
      command(:foo).should == command(:test)
    end
    
    it "should pass arguments passed to the alias when called" do
      gem_name = ''
      new_command_runner 'install', 'gem', 'commander' do
        command :install do |c|
          c.option '--gem-name NAME', 'Install a gem'
          c.when_called { |_, options| gem_name = options.gem_name }
        end 
        alias_command :'install gem', :install, '--gem-name'
      end.run!
      gem_name.should == 'commander'
    end
  end
  
  describe "#global_option" do
    it "should be invoked when used in the args list" do
      file = ''
      new_command_runner 'test', '--config', 'foo' do
        global_option('--config FILE') { |f| file = f }
      end.run!
      file.should == 'foo'
    end
    
    it "should be inherited by commands" do
      quiet = nil
      new_command_runner 'foo', '--quiet' do
        global_option('--quiet', 'Suppress output')
        command :foo do |c|
          c.when_called { |_, options| quiet = options.quiet } 
        end
      end.run!
      quiet.should be_true
    end
    
    it "should be inherited by commands even when a block is present" do
      quiet = nil
      new_command_runner 'foo', '--quiet' do
        global_option('--quiet', 'Suppress output') {}
        command :foo do |c|
          c.when_called { |_, options| quiet = options.quiet } 
        end
      end.run!
      quiet.should be_true      
    end
  end
  
  describe "#remove_global_options" do
    it "should description" do
      options, args = [], []
      options << { :switches => ['-t', '--trace'] }
      options << { :switches => ['--help'] }
      options << { :switches => ['--paths PATHS'] }
      args << '-t'
      args << '--help'
      args << '--command'
      args << '--command-with-arg' << 'rawr'
      args << '--paths' << '"lib/**/*.js","spec/**/*.js"'
      command_runner.remove_global_options options, args
      args.should == ['--command', '--command-with-arg', 'rawr']
    end
  end
  
  describe "--trace" do
    it "should display pretty errors by default" do
      pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform::jruby?
      lambda {
        new_command_runner 'foo' do
          command(:foo) { |c| c.when_called { raise 'cookies!' } }
        end.run!
      }.should raise_error(SystemExit, /error: cookies!. Use --trace/)
    end
    
    it "should display callstack when using this switch" do
      lambda {
        new_command_runner 'foo', '--trace' do
          command(:foo) { |c| c.when_called { raise 'cookies!' } }
        end.run!  
      }.should raise_error(RuntimeError)
    end
  end
  
  describe "--version" do
    it "should output program version" do
      run('--version').should == "test 1.2.3\n"
    end
  end
  
  describe "--help" do
    it "should not output an invalid command message" do
      run('--help').should_not == "invalid command. Use --help for more information\n"
    end
  end
  
  describe "with invalid options" do
    it "should output an invalid option message" do
      pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform::jruby?
      lambda {
        run('test', '--invalid-option')  
      }.should raise_error(SystemExit, /invalid option: --invalid-option/)
    end
  end
  
  describe "with invalid command passed" do
    it "should output an invalid command message" do
      pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform::jruby?
      lambda {
        run('foo')  
      }.should raise_error(SystemExit, /invalid command. Use --help for more information/)
    end
  end
  
  describe "with invalid command passed to help" do
    it "should output an invalid command message" do
      pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform::jruby?
      lambda {
        run('help', 'does_not_exist')
      }.should raise_error(SystemExit, /invalid command. Use --help for more information/)
    end
  end
  
  describe "#valid_command_names_from" do
    it "should return array of valid command names" do
      command('foo bar') {}
   	  command('foo bar foo') {}
   	  command_runner.valid_command_names_from('foo', 'bar', 'foo').should == ['foo bar', 'foo bar foo']
    end
    
    it "should return empty array when no possible command names exist" do
   	  command_runner.valid_command_names_from('fake', 'command', 'name').should == []
    end
  end<commit_msg>Fix failing test. Output was correct, just needed a sort.<commit_after>
require File.dirname(__FILE__) + '/spec_helper'

describe Commander do
  before :each do
    $stderr = StringIO.new
    mock_terminal
    create_test_command
  end
	
  describe "#program" do
    it "should set / get program information" do
      program :name, 'test'
  	  program(:name).should == 'test'
    end
    
    it "should allow arbitrary blocks of global help documentation" do
      program :help, 'Copyright', '<NAME>'
  	  program(:help)['Copyright'].should == '<NAME>'
    end
    
    it "should raise an error when required info has not been set" do
      new_command_runner '--help'
  	  program :version, ''
      lambda { run! }.should raise_error(Commander::Runner::CommandError)
    end
    
    it "should allow aliases of help formatters" do
      program :help_formatter, :compact
      program(:help_formatter).should == Commander::HelpFormatter::TerminalCompact
    end
  end
  
  describe "#command" do
  	it "should return a command instance when only the name is passed" do
  	  command(:test).should be_instance_of(Commander::Command)
  	end
    
    it "should return nil when the command does not exist" do
      command(:im_not_real).should be_nil
    end
  end
  
  describe "#separate_switches_from_description" do
    it "should seperate switches and description returning both" do
      switches, description = *Commander::Runner.separate_switches_from_description('-h', '--help', 'display help')
      switches.should == ['-h', '--help']
      description.should == 'display help'
    end
  end
  
  describe "#switch_to_sym" do
    it "should return a symbol based on the switch name" do
     Commander::Runner.switch_to_sym('--trace').should == :trace
     Commander::Runner.switch_to_sym('--foo-bar').should == :foo_bar
     Commander::Runner.switch_to_sym('--[no-]feature"').should == :feature
     Commander::Runner.switch_to_sym('--[no-]feature ARG').should == :feature
     Commander::Runner.switch_to_sym('--file [ARG]').should == :file
     Commander::Runner.switch_to_sym('--colors colors').should == :colors
    end
  end
  
  describe "#alias_command" do
    it "should alias a command" do
      alias_command :foo, :test
      command(:foo).should == command(:test)
    end
    
    it "should pass arguments passed to the alias when called" do
      gem_name = ''
      new_command_runner 'install', 'gem', 'commander' do
        command :install do |c|
          c.option '--gem-name NAME', 'Install a gem'
          c.when_called { |_, options| gem_name = options.gem_name }
        end 
        alias_command :'install gem', :install, '--gem-name'
      end.run!
      gem_name.should == 'commander'
    end
  end
  
  describe "#global_option" do
    it "should be invoked when used in the args list" do
      file = ''
      new_command_runner 'test', '--config', 'foo' do
        global_option('--config FILE') { |f| file = f }
      end.run!
      file.should == 'foo'
    end
    
    it "should be inherited by commands" do
      quiet = nil
      new_command_runner 'foo', '--quiet' do
        global_option('--quiet', 'Suppress output')
        command :foo do |c|
          c.when_called { |_, options| quiet = options.quiet } 
        end
      end.run!
      quiet.should be_true
    end
    
    it "should be inherited by commands even when a block is present" do
      quiet = nil
      new_command_runner 'foo', '--quiet' do
        global_option('--quiet', 'Suppress output') {}
        command :foo do |c|
          c.when_called { |_, options| quiet = options.quiet } 
        end
      end.run!
      quiet.should be_true      
    end
  end
  
  describe "#remove_global_options" do
    it "should description" do
      options, args = [], []
      options << { :switches => ['-t', '--trace'] }
      options << { :switches => ['--help'] }
      options << { :switches => ['--paths PATHS'] }
      args << '-t'
      args << '--help'
      args << '--command'
      args << '--command-with-arg' << 'rawr'
      args << '--paths' << '"lib/**/*.js","spec/**/*.js"'
      command_runner.remove_global_options options, args
      args.should == ['--command', '--command-with-arg', 'rawr']
    end
  end
  
  describe "--trace" do
    it "should display pretty errors by default" do
      pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform::jruby?
      lambda {
        new_command_runner 'foo' do
          command(:foo) { |c| c.when_called { raise 'cookies!' } }
        end.run!
      }.should raise_error(SystemExit, /error: cookies!. Use --trace/)
    end
    
    it "should display callstack when using this switch" do
      lambda {
        new_command_runner 'foo', '--trace' do
          command(:foo) { |c| c.when_called { raise 'cookies!' } }
        end.run!  
      }.should raise_error(RuntimeError)
    end
  end
  
  describe "--version" do
    it "should output program version" do
      run('--version').should == "test 1.2.3\n"
    end
  end
  
  describe "--help" do
    it "should not output an invalid command message" do
      run('--help').should_not == "invalid command. Use --help for more information\n"
    end
  end
  
  describe "with invalid options" do
    it "should output an invalid option message" do
      pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform::jruby?
      lambda {
        run('test', '--invalid-option')  
      }.should raise_error(SystemExit, /invalid option: --invalid-option/)
    end
  end
  
  describe "with invalid command passed" do
    it "should output an invalid command message" do
      pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform::jruby?
      lambda {
        run('foo')  
      }.should raise_error(SystemExit, /invalid command. Use --help for more information/)
    end
  end
  
  describe "with invalid command passed to help" do
    it "should output an invalid command message" do
      pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform::jruby?
      lambda {
        run('help', 'does_not_exist')
      }.should raise_error(SystemExit, /invalid command. Use --help for more information/)
    end
  end
  
  describe "#valid_command_names_from" do
    it "should return array of valid command names" do
      command('foo bar') {}
   	  command('foo bar foo') {}
   	  command_runner.valid_command_names_from('foo', 'bar', 'foo').sort.should == ['foo bar', 'foo bar foo']
    end
    
    it "should return empty array when no possible command names exist" do
   	  command_runner.valid_command_names_from('fake', 'command', 'name').should == []
    end
  end
{'fraction_non_alphanumeric': 0.1231067671416847, 'fraction_numerical': 0.00044766097142430797, 'mean_word_length': 2.6572987721691677, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2534925', 'n_tokens_mistral': 4022, 'n_tokens_neox': 3953, 'n_words': 1230}