<commit_before>require 'helper'

class Nanoc::CLI::OptionParserTest < Test::Unit::TestCase

  def setup    ; global_setup    ; end
  def teardown ; global_teardown ; end

  def test_parse_without_options
    input       = %w( foo bar baz )
    definitions = []

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({},                      result[:options])
    assert_equal([ 'foo', 'bar', 'baz' ], result[:arguments])
  end

  def test_parse_without_options
    input       = %w( foo -x )
    definitions = []

    result = nil

    assert_raise(Nanoc::CLI::OptionParser::IllegalOptionError) do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end
  end

  def test_parse_with_long_valueless_option
    input       = %w( foo --aaa bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :forbidden }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => nil },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_long_valueful_option
    input       = %w( foo --aaa xxx bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :required }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => 'xxx' },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_long_valueful_equalsign_option
    input       = %w( foo --aaa=xxx bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :required }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => 'xxx' },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_long_valueful_option_with_missing_value
    input       = %w( foo --aaa )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :required }
    ]

    result = nil

    assert_raise(Nanoc::CLI::OptionParser::OptionRequiresAnArgumentError) do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end
  end

  def test_parse_with_short_valueless_options
    input       = %w( foo -a bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :forbidden }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => nil },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_short_valueful_option_with_missing_value
    input       = %w( foo -a )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :required }
    ]

    result = nil

    assert_raise(Nanoc::CLI::OptionParser::OptionRequiresAnArgumentError) do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end
  end

  def test_parse_with_short_combined_valueless_options
    input       = %w( foo -abc bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :forbidden },
      { :long => 'bbb', :short => 'b', :argument => :forbidden },
      { :long => 'ccc', :short => 'c', :argument => :forbidden }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => nil, :bbb => nil, :ccc => nil },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_end_marker
    input       = %w( foo bar -- -x --yyy -abc )
    definitions = []

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({},                                      result[:options])
    assert_equal([ 'foo', 'bar', '-x', '--yyy', '-abc' ], result[:arguments])
  end

end
<commit_msg>added combined valueful optionparser test<commit_after>require 'helper'

class Nanoc::CLI::OptionParserTest < Test::Unit::TestCase

  def setup    ; global_setup    ; end
  def teardown ; global_teardown ; end

  def test_parse_without_options
    input       = %w( foo bar baz )
    definitions = []

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({},                      result[:options])
    assert_equal([ 'foo', 'bar', 'baz' ], result[:arguments])
  end

  def test_parse_without_options
    input       = %w( foo -x )
    definitions = []

    result = nil

    assert_raise(Nanoc::CLI::OptionParser::IllegalOptionError) do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end
  end

  def test_parse_with_long_valueless_option
    input       = %w( foo --aaa bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :forbidden }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => nil },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_long_valueful_option
    input       = %w( foo --aaa xxx bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :required }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => 'xxx' },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_long_valueful_equalsign_option
    input       = %w( foo --aaa=xxx bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :required }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => 'xxx' },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_long_valueful_option_with_missing_value
    input       = %w( foo --aaa )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :required }
    ]

    result = nil

    assert_raise(Nanoc::CLI::OptionParser::OptionRequiresAnArgumentError) do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end
  end

  def test_parse_with_short_valueless_options
    input       = %w( foo -a bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :forbidden }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => nil },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_short_valueful_option_with_missing_value
    input       = %w( foo -a )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :required }
    ]

    result = nil

    assert_raise(Nanoc::CLI::OptionParser::OptionRequiresAnArgumentError) do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end
  end

  def test_parse_with_short_combined_valueless_options
    input       = %w( foo -abc bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :forbidden },
      { :long => 'bbb', :short => 'b', :argument => :forbidden },
      { :long => 'ccc', :short => 'c', :argument => :forbidden }
    ]

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({ :aaa => nil, :bbb => nil, :ccc => nil },  result[:options])
    assert_equal([ 'foo', 'bar' ], result[:arguments])
  end

  def test_parse_with_short_combined_valueful_options_with_missing_value
    input       = %w( foo -abc bar )
    definitions = [
      { :long => 'aaa', :short => 'a', :argument => :required  },
      { :long => 'bbb', :short => 'b', :argument => :forbidden },
      { :long => 'ccc', :short => 'c', :argument => :forbidden }
    ]

    result = nil

    assert_raise(Nanoc::CLI::OptionParser::OptionRequiresAnArgumentError) do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end
  end

  def test_parse_with_end_marker
    input       = %w( foo bar -- -x --yyy -abc )
    definitions = []

    result = nil

    assert_nothing_raised do
      result = Nanoc::CLI::OptionParser.parse(input, definitions)
    end

    assert_equal({},                                      result[:options])
    assert_equal([ 'foo', 'bar', '-x', '--yyy', '-abc' ], result[:arguments])
  end

end

{'fraction_non_alphanumeric': 0.14227272727272727, 'fraction_numerical': 0.0, 'mean_word_length': 2.8567046450482034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6487815', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2796, 'n_words': 635}