test/test_command_dsl.rb in cri-2.8.0 vs test/test_command_dsl.rb in cri-2.9.0
- old
+ new
@@ -24,28 +24,31 @@
end
command = dsl.command
# Run
$did_it_work = :sadly_not
- command.run(%w(-a x -b y -c -d -e))
+ command.run(%w[-a x -b y -c -d -e])
assert_equal :probably, $did_it_work
# Check
assert_equal 'moo', command.name
assert_equal 'dunno whatever', command.usage
assert_equal 'does stuff', command.summary
assert_equal 'This command does a lot of stuff.', command.description
# Check options
- expected_option_definitions = Set.new([
- { short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil },
- { short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil },
- { short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil },
- { short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil },
- { short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil },
- { short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil },
- ])
+ expected_option_definitions =
+ Set.new(
+ [
+ { short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil, default: nil },
+ { short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil, default: nil },
+ { short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil, default: nil },
+ { short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
+ { short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
+ { short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil },
+ ],
+ )
actual_option_definitions = Set.new(command.option_definitions)
assert_equal expected_option_definitions, actual_option_definitions
end
def test_optional_options
@@ -66,18 +69,21 @@
end
command = dsl.command
# Run
$did_it_work = :sadly_not
- command.run(%w(-s --long))
+ command.run(%w[-s --long])
assert_equal :probably, $did_it_work
# Check options
- expected_option_definitions = Set.new([
- { short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil },
- { short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil },
- ])
+ expected_option_definitions =
+ Set.new(
+ [
+ { short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
+ { short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
+ ],
+ )
actual_option_definitions = Set.new(command.option_definitions)
assert_equal expected_option_definitions, actual_option_definitions
end
def test_multiple
@@ -91,15 +97,18 @@
run { |_opts, _args| }
end
command = dsl.command
# Check options
- expected_option_definitions = Set.new([
- { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil },
- { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil },
- { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil },
- ])
+ expected_option_definitions =
+ Set.new(
+ [
+ { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil, default: nil },
+ { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil, default: nil },
+ { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil, default: nil },
+ ],
+ )
actual_option_definitions = Set.new(command.option_definitions)
assert_equal expected_option_definitions, actual_option_definitions
end
def test_hidden
@@ -113,15 +122,18 @@
run { |_opts, _args| }
end
command = dsl.command
# Check options
- expected_option_definitions = Set.new([
- { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil },
- { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil },
- { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil },
- ])
+ expected_option_definitions =
+ Set.new(
+ [
+ { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil },
+ { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil, default: nil },
+ { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil, default: nil },
+ ],
+ )
actual_option_definitions = Set.new(command.option_definitions)
assert_equal expected_option_definitions, actual_option_definitions
end
def test_required_short_and_long
@@ -147,10 +159,32 @@
optional nil, nil, 'meh'
end
end
end
+ def test_default_value_errors_when_requiredness_is_required
+ dsl = Cri::CommandDSL.new
+
+ err = assert_raises ArgumentError do
+ dsl.instance_eval do
+ required 'a', 'animal', 'Specify animal', default: 'giraffe'
+ end
+ end
+ assert_equal('a default value cannot be specified for options with required values', err.message)
+ end
+
+ def test_default_value_errors_when_requiredness_is_forbidden
+ dsl = Cri::CommandDSL.new
+
+ err = assert_raises ArgumentError do
+ dsl.instance_eval do
+ flag 'a', 'animal', 'Allow animal', default: 'giraffe'
+ end
+ end
+ assert_equal('a default value cannot be specified for options with forbidden values', err.message)
+ end
+
def test_subcommand
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
name 'super'
@@ -173,11 +207,11 @@
aliases :moo, :aah
end
command = dsl.command
# Check
- assert_equal %w(aah moo), command.aliases.sort
+ assert_equal %w[aah moo], command.aliases.sort
end
def test_run_arity
dsl = Cri::CommandDSL.new
assert_raises ArgumentError do
@@ -202,10 +236,10 @@
EOS
command = dsl.command
# Check
$did_it_work = false
- command.run(%w(certainly))
+ command.run(%w[certainly])
assert_equal 'certainly', $did_it_work
end
end
end