summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Gass2020-08-25 15:07:00 +0200
committerNathan Gass2020-08-25 15:07:00 +0200
commit3a86b2303e0b6050ab3363aaaf1dd27157bf7896 (patch)
treec4b5347085f7aa5ef721a1355dfd6b371d979985
parenteee098c8d18abddfbc1f593db1bad49fb765c075 (diff)
downloaditools-3a86b2303e0b6050ab3363aaaf1dd27157bf7896.tar.gz
itools-3a86b2303e0b6050ab3363aaaf1dd27157bf7896.tar.bz2
itools-3a86b2303e0b6050ab3363aaaf1dd27157bf7896.zip
more tests for arguments without value, also test failure modes
-rwxr-xr-xtest/getopt.t17
1 files changed, 13 insertions, 4 deletions
diff --git a/test/getopt.t b/test/getopt.t
index 87f7550..e4c98ed 100755
--- a/test/getopt.t
+++ b/test/getopt.t
@@ -13,7 +13,7 @@ $GLOBALS['usage'] = "Usage: doesnotexist.php [OPTIONS] POSITIONAL
function getopt_ok($argv, $exp, $name)
{
$_SERVER['argv'] = array_merge(['doesnotexist.php'], $argv);
- $got = it::getopt($GLOBALS['usage']);
+ $got = it::getopt($GLOBALS['usage'], ['noexit' => true]);
return is($got, $exp, $name);
}
@@ -24,6 +24,15 @@ foreach (["" => "blah gnaber", " (umlaute)" => "pre üäpost"] as $variant => $t
getopt_ok([$testarg, "--argument=$testarg"], $exp, "Long version with equal" . $variant);
}
-$_SERVER['argv'] = ['doesnotexist.php', 'posarg', '-0'];
-$zero_opts = it::getopt($GLOBALS['usage']);
-ok($zero_opts['zero'], '-0');
+$exp = ['args' => [], 'positional' => 'posarg', 'zero' => true];
+getopt_ok(['posarg', '-0'], $exp, 'short argument -0 without value');
+getopt_ok(['posarg', '--zero'], $exp, 'long argument --zero without value');
+getopt_ok(['posarg', '-0', 'vararg'], ['args' => ['vararg']] + $exp, "additional value after short argument -0");
+getopt_ok(['posarg', '--zero', 'vararg'], ['args' => ['vararg']] + $exp, "additional value after long argument --zero");
+
+fclose(STDERR);
+getopt_ok(['posarg', '--unknown'], false, "Unknown long named argument fails");
+getopt_ok(['posarg', '-u'], false, "Unknown short named argument fails");
+getopt_ok([], false, "Missing positional argument fails");
+getopt_ok(['posarg', '--argument'], false, "Missing long named argument fails");
+getopt_ok(['posarg', '-a'], false, "Missing short named argument fails");