test/test_command.rb in cri-2.7.1 vs test/test_command.rb in cri-2.8.0
- old
+ new
@@ -1,5 +1,7 @@
+require 'helper'
+
module Cri
class CommandTestCase < Cri::TestCase
def simple_cmd
Cri::Command.define do
name 'moo'
@@ -143,30 +145,50 @@
assert_equal [], lines(err)
end
def test_invoke_simple_with_missing_opt_arg
out, err = capture_io_while do
- assert_raises SystemExit do
+ err = assert_raises SystemExit do
simple_cmd.run(%w(-b))
end
+ assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ['moo: option requires an argument -- b'], lines(err)
end
+ def test_invoke_simple_with_missing_opt_arg_no_exit
+ out, err = capture_io_while do
+ simple_cmd.run(%w(-b), {}, hard_exit: false)
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ['moo: option requires an argument -- b'], lines(err)
+ end
+
def test_invoke_simple_with_illegal_opt
out, err = capture_io_while do
- assert_raises SystemExit do
+ err = assert_raises SystemExit do
simple_cmd.run(%w(-z))
end
+ assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ['moo: illegal option -- z'], lines(err)
end
+ def test_invoke_simple_with_illegal_opt_no_exit
+ out, err = capture_io_while do
+ simple_cmd.run(%w(-z), {}, hard_exit: false)
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ['moo: illegal option -- z'], lines(err)
+ end
+
def test_invoke_simple_with_opt_with_block
out, err = capture_io_while do
simple_cmd.run(%w(-a 123))
end
@@ -174,19 +196,29 @@
assert_equal [], lines(err)
end
def test_invoke_nested_without_opts_or_args
out, err = capture_io_while do
- assert_raises SystemExit do
+ err = assert_raises SystemExit do
nested_cmd.run(%w())
end
+ assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ['super: no command given'], lines(err)
end
+ def test_invoke_nested_without_opts_or_args_no_exit
+ out, err = capture_io_while do
+ nested_cmd.run(%w(), {}, hard_exit: false)
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ['super: no command given'], lines(err)
+ end
+
def test_invoke_nested_with_correct_command_name
out, err = capture_io_while do
nested_cmd.run(%w(sub))
end
@@ -194,30 +226,50 @@
assert_equal [], lines(err)
end
def test_invoke_nested_with_incorrect_command_name
out, err = capture_io_while do
- assert_raises SystemExit do
+ err = assert_raises SystemExit do
nested_cmd.run(%w(oogabooga))
end
+ assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ["super: unknown command 'oogabooga'"], lines(err)
end
+ def test_invoke_nested_with_incorrect_command_name_no_exit
+ out, err = capture_io_while do
+ nested_cmd.run(%w(oogabooga), {}, hard_exit: false)
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ["super: unknown command 'oogabooga'"], lines(err)
+ end
+
def test_invoke_nested_with_ambiguous_command_name
out, err = capture_io_while do
- assert_raises SystemExit do
+ err = assert_raises SystemExit do
nested_cmd.run(%w(s))
end
+ assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ["super: 's' is ambiguous:", ' sink sub'], lines(err)
end
+ def test_invoke_nested_with_ambiguous_command_name_no_exit
+ out, err = capture_io_while do
+ nested_cmd.run(%w(s), {}, hard_exit: false)
+ end
+
+ assert_equal [], lines(out)
+ assert_equal ["super: 's' is ambiguous:", ' sink sub'], lines(err)
+ end
+
def test_invoke_nested_with_alias
out, err = capture_io_while do
nested_cmd.run(%w(sup))
end
@@ -556,8 +608,29 @@
foo = Cri::Command.define { name 'foo' }
bar = Cri::Command.define { name 'bar' }
qux = Cri::Command.define { name 'qux' }
assert_equal [bar, foo, qux], [foo, bar, qux].sort
+ end
+
+ def test_default_subcommand
+ subcommand = Cri::Command.define do
+ name 'sub'
+
+ run do |_opts, _args, _c|
+ $stdout.puts 'I am the subcommand!'
+ end
+ end
+
+ cmd = Cri::Command.define do
+ name 'super'
+ default_subcommand 'sub'
+ subcommand subcommand
+ end
+
+ out, _err = capture_io_while do
+ cmd.run([])
+ end
+ assert_equal "I am the subcommand!\n", out
end
end
end