Skip to content

Commit

Permalink
move constraint tests to separate file. fix confict/depend error mess…
Browse files Browse the repository at this point in the history
…age tests
  • Loading branch information
nanobowers committed May 18, 2024
1 parent ac50c68 commit b5ad3b3
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 105 deletions.
128 changes: 128 additions & 0 deletions test/optimist/parser_constraint_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require 'test_helper'

module Optimist

class ParserConstraintTest < ::Minitest::Test
def setup
@p = Parser.new
end

def parser
@p ||= Parser.new
end

def test_conflicts
@p.opt :one
assert_raises(ArgumentError) { @p.conflicts :one, :two }
@p.opt :two
@p.conflicts :one, :two
@p.parse %w(--one)
@p.parse %w(--two)
assert_raises(CommandlineError) { @p.parse %w(--one --two) }

@p.opt :hello
@p.opt :yellow
@p.opt :mellow
@p.opt :jello
@p.conflicts :hello, :yellow, :mellow, :jello
assert_raises(CommandlineError) { @p.parse %w(--hello --yellow --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }

@p.parse %w(--hello)
@p.parse %w(--jello)
@p.parse %w(--yellow)
@p.parse %w(--mellow)

@p.parse %w(--mellow --one)
@p.parse %w(--mellow --two)

assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello) }
assert_raises(CommandlineError) { @p.parse %w(--one --mellow --two --jello) }
end

def test_conflict_error_messages
@p.opt :one
@p.opt "two"
@p.conflicts :one, "two"
err_regex = %r/only one of --one, --two can be given/
assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --two) }
end

def test_either
@p.opt :one
assert_raises(ArgumentError) { @p.either :one, :two }
@p.opt :two
@p.either :one, :two
@p.parse %w(--one)
@p.parse %w(--two)
assert_raises(CommandlineError) { @p.parse %w(--one --two) }
assert_raises(CommandlineError) { @p.parse %w() }

@p.opt :hello
@p.opt :yellow
@p.opt :mellow
@p.opt :jello
@p.either :hello, :yellow, :mellow, :jello
assert_raises(CommandlineError) { @p.parse %w(--hello --yellow --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }

@p.parse %w(--hello --one)
@p.parse %w(--jello --two)
@p.parse %w(--mellow --one)
@p.parse %w(--mellow --two)

assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello) }
assert_raises(CommandlineError) { @p.parse %w(--one --mellow --two --jello) }
end

def test_either_error_messages
@p.opt :one
@p.opt :two
@p.opt :three
@p.either :one, :two, :three
err_regex = %r/one and only one of --one, --two, --three is required/
assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --two) }
assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--three --two --one) }
end

def test_depends
@p.opt :one
assert_raises(ArgumentError) { @p.depends :one, :two }
@p.opt :two
@p.depends :one, :two
@p.parse %w(--one --two)
assert_raises(CommandlineError) { @p.parse %w(--one) }
assert_raises(CommandlineError) { @p.parse %w(--two) }

@p.opt :hello
@p.opt :yellow
@p.opt :mellow
@p.opt :jello
@p.depends :hello, :yellow, :mellow, :jello
@p.parse %w(--hello --yellow --mellow --jello)
assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }

assert_raises(CommandlineError) { @p.parse %w(--hello) }
assert_raises(CommandlineError) { @p.parse %w(--mellow) }

@p.parse %w(--hello --yellow --mellow --jello --one --two)
@p.parse %w(--hello --yellow --mellow --jello --one --two a b c)

assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello --one) }
end

def test_depends_error_messages
@p.opt :one
@p.opt "two"
@p.depends :one, "two"

@p.parse %w(--one --two)
err_regex = %r/--one, --two have a dependency and must be given together/
assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one) }
assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--two) }
end
end
end
105 changes: 0 additions & 105 deletions test/optimist/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ def test_synopsis
assert_equal "synopsis string", parser.synopsis
end

# def test_depends
# def test_conflicts
# def test_stop_on
# def test_stop_on_unknown

Expand Down Expand Up @@ -804,109 +802,6 @@ def test_version_and_help_override_errors
assert_raises(VersionNeeded) { @p.parse %w(--asdf --version) }
end

def test_conflicts
@p.opt :one
assert_raises(ArgumentError) { @p.conflicts :one, :two }
@p.opt :two
@p.conflicts :one, :two
@p.parse %w(--one)
@p.parse %w(--two)
assert_raises(CommandlineError) { @p.parse %w(--one --two) }

@p.opt :hello
@p.opt :yellow
@p.opt :mellow
@p.opt :jello
@p.conflicts :hello, :yellow, :mellow, :jello
assert_raises(CommandlineError) { @p.parse %w(--hello --yellow --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }

@p.parse %w(--hello)
@p.parse %w(--jello)
@p.parse %w(--yellow)
@p.parse %w(--mellow)

@p.parse %w(--mellow --one)
@p.parse %w(--mellow --two)

assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello) }
assert_raises(CommandlineError) { @p.parse %w(--one --mellow --two --jello) }
end

def test_either
@p.opt :one
assert_raises(ArgumentError) { @p.either :one, :two }
@p.opt :two
@p.either :one, :two
@p.parse %w(--one)
@p.parse %w(--two)
assert_raises(CommandlineError) { @p.parse %w(--one --two) }
assert_raises(CommandlineError) { @p.parse %w() }

@p.opt :hello
@p.opt :yellow
@p.opt :mellow
@p.opt :jello
@p.either :hello, :yellow, :mellow, :jello
assert_raises(CommandlineError) { @p.parse %w(--hello --yellow --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }

@p.parse %w(--hello --one)
@p.parse %w(--jello --two)
@p.parse %w(--mellow --one)
@p.parse %w(--mellow --two)

assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello) }
assert_raises(CommandlineError) { @p.parse %w(--one --mellow --two --jello) }
end

def test_conflict_error_messages
@p.opt :one
@p.opt "two"
@p.conflicts :one, "two"

assert_raises(CommandlineError, /--one.*--two/) { @p.parse %w(--one --two) }
end

def test_depends
@p.opt :one
assert_raises(ArgumentError) { @p.depends :one, :two }
@p.opt :two
@p.depends :one, :two
@p.parse %w(--one --two)
assert_raises(CommandlineError) { @p.parse %w(--one) }
assert_raises(CommandlineError) { @p.parse %w(--two) }

@p.opt :hello
@p.opt :yellow
@p.opt :mellow
@p.opt :jello
@p.depends :hello, :yellow, :mellow, :jello
@p.parse %w(--hello --yellow --mellow --jello)
assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }

assert_raises(CommandlineError) { @p.parse %w(--hello) }
assert_raises(CommandlineError) { @p.parse %w(--mellow) }

@p.parse %w(--hello --yellow --mellow --jello --one --two)
@p.parse %w(--hello --yellow --mellow --jello --one --two a b c)

assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello --one) }
end

def test_depend_error_messages
@p.opt :one
@p.opt "two"
@p.depends :one, "two"

@p.parse %w(--one --two)

assert_raises(CommandlineError, /--one.*--two/) { @p.parse %w(--one) }
assert_raises(CommandlineError, /--one.*--two/) { @p.parse %w(--two) }
end

## courtesy neill zero
def test_two_required_one_missing_accuses_correctly
Expand Down

0 comments on commit b5ad3b3

Please sign in to comment.