<commit_before>require 'helper'

class CommandsTest < TestCase

  def setup
    @commands = Slop::Commands.new do
      on 'new' do
        on '--force', 'Force creation'
        on '--outdir=', 'Output directory'
      end

      on 'version' do
        add_callback(:empty) { 'version 1' }
      end
    end
  end

  test "it nests instances of Slop" do
    assert_empty Slop::Commands.new.commands
    @commands.commands.each_value { |k| assert_kind_of Slop, k }
  end

  test "accessing Slop instances via get/[]" do
    assert_kind_of Slop, @commands['new']
    assert_kind_of Slop, @commands[:new]
    assert_nil @commands[:unknown]
    assert_equal 'Force creation', @commands[:new].fetch_option(:force).description
  end

  test "to_hash" do
    assert_equal({
      :new => { :force => nil, :outdir => nil },
      :version => {}
    }, @commands.to_hash)
  end

  test "raising on unknown commands with :strict => true" do
    cmds = Slop::Commands.new(:strict => true)
    assert_raises(Slop::InvalidCommandError) { cmds.parse %w( abc ) }
  end

  test "adding global options" do
    cmds = Slop::Commands.new { global { on '--verbose' } }
    cmds.parse %w( --verbose )
    assert cmds[:global].verbose?
  end

  test "adding default options" do
    cmds = Slop::Commands.new { default { on '--verbose' } }
    cmds.parse %w( --verbose )
    assert cmds[:default].verbose?<commit_msg>tests for gathering global/default command options<commit_after>require 'helper'

class CommandsTest < TestCase

  def setup
    @commands = Slop::Commands.new do
      on 'new' do
        on '--force', 'Force creation'
        on '--outdir=', 'Output directory'
      end

      on 'version' do
        add_callback(:empty) { 'version 1' }
      end
    end
  end

  test "it nests instances of Slop" do
    assert_empty Slop::Commands.new.commands
    @commands.commands.each_value { |k| assert_kind_of Slop, k }
  end

  test "accessing Slop instances via get/[]" do
    assert_kind_of Slop, @commands['new']
    assert_kind_of Slop, @commands[:new]
    assert_nil @commands[:unknown]
    assert_equal 'Force creation', @commands[:new].fetch_option(:force).description
  end

  test "to_hash" do
    assert_equal({
      :new => { :force => nil, :outdir => nil },
      :version => {}
    }, @commands.to_hash)
  end

  test "raising on unknown commands with :strict => true" do
    cmds = Slop::Commands.new(:strict => true)
    assert_raises(Slop::InvalidCommandError) { cmds.parse %w( abc ) }
  end

  test "adding global options" do
    cmds = Slop::Commands.new { global { on '--verbose' } }
    cmds.parse %w( --verbose )
    assert cmds[:global].verbose?
  end

  test "global options are always executed" do
    @commands.global { on 'foo=' }
    @commands.parse %w( new --force --foo bar )
    assert_equal 'bar', @commands[:global][:foo]
  end

  test "default options are only executed when there's nothing else" do
    @commands.default { on 'foo=' }
    @commands.parse %w( new --force --foo bar )
    assert_nil @commands[:default][:foo]
  end

  test "adding default options" do
    cmds = Slop::Commands.new { default { on '--verbose' } }
    cmds.parse %w( --verbose )
    assert cmds[:default].verbose?
{'fraction_non_alphanumeric': 0.13140726933830382, 'fraction_numerical': 0.0006213109661385523, 'mean_word_length': 2.985148514851485, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '7210394', 'n_tokens_mistral': 1047, 'n_tokens_neox': 1035, 'n_words': 299}