1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
# frozen_string_literal: true
require 'helper'
module Cri
class BasicHelpTestCase < Cri::TestCase
def test_run_without_supercommand
cmd = Cri::Command.new_basic_help
assert_raises Cri::NoHelpAvailableError do
cmd.run([])
end
end
def test_run_with_supercommand
cmd = Cri::Command.define do
name 'meh'
end
help_cmd = Cri::Command.new_basic_help
cmd.add_command(help_cmd)
help_cmd.run([])
end
def test_run_with_chain_of_commands
cmd = Cri::Command.define do
name 'root'
summary 'I am root!'
subcommand do
name 'foo'
summary 'I am foo!'
subcommand do
name 'subsubby'
summary 'I am subsubby!'
end
end
end
help_cmd = Cri::Command.new_basic_help
cmd.add_command(help_cmd)
# Simple call
stdout, stderr = capture_io_while do
help_cmd.run(['foo'])
end
assert_match(/I am foo!/m, stdout)
assert_equal('', stderr)
# Subcommand
stdout, stderr = capture_io_while do
help_cmd.run(%w[foo subsubby])
end
assert_match(/I am subsubby!/m, stdout)
assert_equal('', stderr)
# Non-existing subcommand
stdout, stderr = capture_io_while do
assert_raises SystemExit do
help_cmd.run(%w[foo mysterycmd])
end
end
assert_equal '', stdout
assert_match(/foo: unknown command 'mysterycmd'/, stderr)
end
end
end
|