Skip to content

Commit

Permalink
add test case for sub-command instantiation error
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiguchi-nagisa committed May 5, 2024
1 parent 192db0a commit fef6e95
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 224 deletions.
2 changes: 1 addition & 1 deletion test/analyzer/analyzer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ASTDumpTest : public ::testing::TestWithParam<std::string>, public TempFil

static bool isIgnoredTestCase(const std::string &path) {
const char *ignoredPattern[] = {"mod", "subcmd", "shctl", "complete5", "complete6",
"complete8", "load", "fullname", "cli6.ds"};
"complete8", "load", "fullname", "cli5.ds"};
return std::any_of(std::begin(ignoredPattern), std::end(ignoredPattern),
[&path](const char *pt) { return StringRef(path).contains(pt); });
}
Expand Down
194 changes: 155 additions & 39 deletions test/exec/cases/base/cli4.ds
Original file line number Diff line number Diff line change
@@ -1,47 +1,163 @@

# too large usage
# for sub-commands

[<CLI(desc: "this is sample", verbose: $true)>]
typedef AAA() {
[<Option(help: 'specify count number', placeholder: 'num')>]
var count = 0;
[<CLI(desc: 'save current state to specified file', verbose: $true)>]
typedef _Save() {
[<Flag(help: 'save verbose information')>]
var verbose = $false

[<Arg(required: $true)>]
var file : String?
}

fff(p : AAA) {
echo "count=${p.count}"
[<CLI(desc: 'load state from specified file', verbose: $true)>]
typedef _Load() {
[<Arg(required: $true)>]
var file : String?
}

var ex = 34 as Any
printf -v var -- "%-*s" 2147483640 --hoge
let large = $reply.remove('var')!
[<CLI(desc: 'manipulate history')>]
typedef _Hist() {
[<Flag(stop: $true, help: 'show version information')>]
var version = $false

try {
$ex = 34
echo before usage
new AAA().usage($large, $false)
} catch e {
$ex = $e
}
assert ($ex as OutOfRangeError).message() == 'reach String size limit'
echo after usage

try {
$ex = 34
echo before verbose usage
new AAA().usage($large, $false)
} catch e {
$ex = $e
}
assert ($ex as OutOfRangeError).message() == 'reach String size limit'
echo after verbose usage

try {
$ex = 34
echo before cmd call
fff $large
assert $false
} catch e {
$ex = $e
[<SubCmd(help: 'save current history')>]
var save : _Save?

[<SubCmd(name: 'load', help: 'load history')>]
[<SubCmd(name: 'read', help: "equivalent to 'load'")>]
var load : _Load?
}
assert ($ex as OutOfRangeError).message() == 'reach String size limit'
echo after cmd call

assert (new _Hist()) is CLI
var hist1 = new _Hist()
$hist1.setName('history')

## usage
var out = $hist1.usage('invalid command')
assert diff <(cat <<< $out) <(cat << 'EOF'
history: invalid command
Usage: history [OPTIONS] [COMMAND]

manipulate history

Options:
--version show version information
-h, --help show this help message

Commands:
save save current history
load load history
read equivalent to 'load'
EOF
)

## parse sub-command
var r = $hist1.parse(['--version', 'save'])
assert $r == 1
assert $hist1.version
assert !$hist1.save
assert !$hist1.load

$hist1 = new _Hist()
$hist1.setName('history')
$r = $hist1.parse(['--', 'load', 'save', 'hoge'])
assert $r == 3
assert !$hist1.version
assert !$hist1.save
assert $hist1.load
assert $hist1.load!.name() == 'history load'
assert $hist1.load!.file! == 'save'

$hist1 = new _Hist()
$hist1.setName('history')
$r = $hist1.parse(['read', 'target'])
assert $r == 2
assert !$hist1.version
assert !$hist1.save
assert $hist1.load
assert $hist1.load!.name() == 'history read'
assert $hist1.load!.file! == 'target'

$hist1 = new _Hist()
$hist1.setName('history2')
$r = $hist1.parse(['save', '--verbose', 'hoge'])
assert $r == 3
assert !$hist1.version
assert $hist1.save
assert $hist1.save!.name() == 'history2 save'
assert $hist1.save!.file! == 'hoge'
assert $hist1.save!.verbose

$hist1 = new _Hist()
$hist1.setName('history')
$r = $hist1.parse(['save', 'hello'])
assert $r == 2
assert !$hist1.version
assert $hist1.save
assert $hist1.save!.file! == 'hello'
assert ! $hist1.save!.verbose

## sub-command usage
$out = $hist1.save!.usage('hello world', $true)
assert diff <(cat <<< $out) <(cat << 'EOF'
history save: hello world
Usage: history save [OPTIONS] FILE

save current state to specified file

Options:
--verbose save verbose information
-h, --help show this help message
EOF
)

## sub-command help
$hist1 = new _Hist()
$hist1.setName('history')
var ex =34 as Any
try { $hist1.parse(['save', '--help', 'hoge']); } catch e { $ex = $e; }
assert $ex is CLIError
assert ($ex as CLIError).status() == 0
assert diff <(cat <<< ${($ex as CLIError).message()}) <(cat << 'EOF'
Usage: history save [OPTIONS] FILE

save current state to specified file

Options:
--verbose save verbose information
-h, --help show this help message
EOF
)

## sub-command error
$hist1 = new _Hist()
$hist1.setName('history')
$ex =34
try { $hist1.parse(['save', '--h', 'hoge']); } catch e { $ex = $e; }
assert $ex is CLIError
assert ($ex as CLIError).status() == 2
assert diff <(cat <<< ${($ex as CLIError).message()}) <(cat << 'EOF'
history save: invalid option: --h
Usage: history save [OPTIONS] FILE

save current state to specified file

Options:
--verbose save verbose information
-h, --help show this help message
EOF
)

## undefined sub-command
$hist1 = new _Hist()
$hist1.setName('history')
$ex =34
try { $hist1.parse(['append', '--h', 'hoge']); } catch e { $ex = $e; }
assert $ex is CLIError
assert ($ex as CLIError).status() == 2
assert diff <(cat <<< ${($ex as CLIError).message()}) <(cat << 'EOF'
history: unknown command: append
See `history --help' for more information.
EOF
)
176 changes: 21 additions & 155 deletions test/exec/cases/base/cli5.ds
Original file line number Diff line number Diff line change
@@ -1,163 +1,29 @@

# for sub-commands
# for foreign module cli

[<CLI(desc: 'save current state to specified file', verbose: $true)>]
typedef _Save() {
[<Flag(help: 'save verbose information')>]
var verbose = $false
source ../../_module4test/cli.ds as mod

[<Arg(required: $true)>]
var file : String?
}

[<CLI(desc: 'load state from specified file', verbose: $true)>]
typedef _Load() {
[<Arg(required: $true)>]
var file : String?
}

[<CLI(desc: 'manipulate history')>]
typedef _Hist() {
[<Flag(stop: $true, help: 'show version information')>]
var version = $false

[<SubCmd(help: 'save current history')>]
var save : _Save?

[<SubCmd(name: 'load', help: 'load history')>]
[<SubCmd(name: 'read', help: "equivalent to 'load'")>]
var load : _Load?
}

assert (new _Hist()) is CLI
var hist1 = new _Hist()
$hist1.setName('history')

## usage
var out = $hist1.usage('invalid command')
assert diff <(cat <<< $out) <(cat << 'EOF'
history: invalid command
Usage: history [OPTIONS] [COMMAND]

manipulate history

Options:
--version show version information
-h, --help show this help message

Commands:
save save current history
load load history
read equivalent to 'load'
EOF
)

## parse sub-command
var r = $hist1.parse(['--version', 'save'])
var v = new mod.AAA()
$v.setName('example')
var r = $v.parse(['dump'])
assert $r == 1
assert $hist1.version
assert !$hist1.save
assert !$hist1.load

$hist1 = new _Hist()
$hist1.setName('history')
$r = $hist1.parse(['--', 'load', 'save', 'hoge'])
assert $r == 3
assert !$hist1.version
assert !$hist1.save
assert $hist1.load
assert $hist1.load!.name() == 'history load'
assert $hist1.load!.file! == 'save'
assert $v.dump!.name() == 'example dump'
assert $v.dump!.count == 123
assert $v.dump!.file == '/dev/stdout'

$hist1 = new _Hist()
$hist1.setName('history')
$r = $hist1.parse(['read', 'target'])
$v = new mod.AAA()
$v.setName('example')
$r = $v.parse(['dump', 'target', '!!'])
assert $r == 2
assert !$hist1.version
assert !$hist1.save
assert $hist1.load
assert $hist1.load!.name() == 'history read'
assert $hist1.load!.file! == 'target'
assert $v.dump!.count == 123
assert $v.dump!.file == 'target'

$hist1 = new _Hist()
$hist1.setName('history2')
$r = $hist1.parse(['save', '--verbose', 'hoge'])
$v = new mod.AAA()
$v.setName('example2')
$r = $v.parse(['interval', '34', '89', '99'])
assert $r == 3
assert !$hist1.version
assert $hist1.save
assert $hist1.save!.name() == 'history2 save'
assert $hist1.save!.file! == 'hoge'
assert $hist1.save!.verbose

$hist1 = new _Hist()
$hist1.setName('history')
$r = $hist1.parse(['save', 'hello'])
assert $r == 2
assert !$hist1.version
assert $hist1.save
assert $hist1.save!.file! == 'hello'
assert ! $hist1.save!.verbose

## sub-command usage
$out = $hist1.save!.usage('hello world', $true)
assert diff <(cat <<< $out) <(cat << 'EOF'
history save: hello world
Usage: history save [OPTIONS] FILE

save current state to specified file

Options:
--verbose save verbose information
-h, --help show this help message
EOF
)

## sub-command help
$hist1 = new _Hist()
$hist1.setName('history')
var ex =34 as Any
try { $hist1.parse(['save', '--help', 'hoge']); } catch e { $ex = $e; }
assert $ex is CLIError
assert ($ex as CLIError).status() == 0
assert diff <(cat <<< ${($ex as CLIError).message()}) <(cat << 'EOF'
Usage: history save [OPTIONS] FILE

save current state to specified file

Options:
--verbose save verbose information
-h, --help show this help message
EOF
)

## sub-command error
$hist1 = new _Hist()
$hist1.setName('history')
$ex =34
try { $hist1.parse(['save', '--h', 'hoge']); } catch e { $ex = $e; }
assert $ex is CLIError
assert ($ex as CLIError).status() == 2
assert diff <(cat <<< ${($ex as CLIError).message()}) <(cat << 'EOF'
history save: invalid option: --h
Usage: history save [OPTIONS] FILE

save current state to specified file

Options:
--verbose save verbose information
-h, --help show this help message
EOF
)

## undefined sub-command
$hist1 = new _Hist()
$hist1.setName('history')
$ex =34
try { $hist1.parse(['append', '--h', 'hoge']); } catch e { $ex = $e; }
assert $ex is CLIError
assert ($ex as CLIError).status() == 2
assert diff <(cat <<< ${($ex as CLIError).message()}) <(cat << 'EOF'
history: unknown command: append
See `history --help' for more information.
EOF
)
assert $v.interval
assert $v.interval!.name() == 'example2 interval'
assert $v.interval!.begin == 34
assert $v.interval!.end == 89
assert $v.interval!.dist() == 55
Loading

0 comments on commit fef6e95

Please sign in to comment.