diff options
Diffstat (limited to 't/Monitoring-Plugin-Getopt-04.t')
-rw-r--r-- | t/Monitoring-Plugin-Getopt-04.t | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/t/Monitoring-Plugin-Getopt-04.t b/t/Monitoring-Plugin-Getopt-04.t new file mode 100644 index 0000000..b6345d0 --- /dev/null +++ b/t/Monitoring-Plugin-Getopt-04.t | |||
@@ -0,0 +1,97 @@ | |||
1 | # Monitoring::Plugin::Getopt spec-to-help generation tests | ||
2 | |||
3 | use strict; | ||
4 | |||
5 | use Test::More tests => 11; | ||
6 | BEGIN { use_ok('Monitoring::Plugin::Getopt') }; | ||
7 | |||
8 | # Needed to get evals to work in testing | ||
9 | Monitoring::Plugin::Functions::_use_die(1); | ||
10 | |||
11 | my %PARAM = ( | ||
12 | version => '0.01', | ||
13 | usage => "Don't use this plugin!", | ||
14 | ); | ||
15 | |||
16 | sub setup | ||
17 | { | ||
18 | # Instantiate object | ||
19 | my $ng = Monitoring::Plugin::Getopt->new(%PARAM); | ||
20 | ok($ng, 'constructor ok'); | ||
21 | |||
22 | # Positional args, no short arguments, INTEGER | ||
23 | $ng->arg('warning=i' => | ||
24 | qq(Exit with WARNING status if less than INTEGER foobars are free), | ||
25 | 5); | ||
26 | |||
27 | # Named args, long + short arguments, INTEGER | ||
28 | $ng->arg( | ||
29 | spec => 'critical|c=i', | ||
30 | help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), | ||
31 | required => 1, | ||
32 | ); | ||
33 | |||
34 | # Named args, multiple short arguments, STRING, default expansion | ||
35 | $ng->arg( | ||
36 | spec => 'x|y|z=s', | ||
37 | help => qq(Foobar. Default: %s), | ||
38 | default => "XYZ", | ||
39 | ); | ||
40 | |||
41 | # Named args, multiple mixed, no label | ||
42 | $ng->arg( | ||
43 | spec => 'long|longer|longest|l', | ||
44 | help => qq(Long format), | ||
45 | ); | ||
46 | |||
47 | # Named args, long + short, explicit label | ||
48 | $ng->arg( | ||
49 | spec => 'hostname|H=s', | ||
50 | label => 'ADDRESS', | ||
51 | help => qq(Hostname), | ||
52 | ); | ||
53 | |||
54 | # Positional args, long only, explicit label | ||
55 | $ng->arg('avatar=s', 'Avatar', undef, undef, 'AVATAR'); | ||
56 | |||
57 | # Multiline help test, named args | ||
58 | $ng->arg( | ||
59 | spec => 'disk=s', | ||
60 | label => [ qw(BYTES PERCENT%), undef ], | ||
61 | help => [ | ||
62 | qq(Disk limit in BYTES), | ||
63 | qq(Disk limit in PERCENT), | ||
64 | qq(Disk limit in FOOBARS (Default: %s)), | ||
65 | ], | ||
66 | default => 1024, | ||
67 | ); | ||
68 | |||
69 | # Multiline help test, positional args | ||
70 | $ng->arg( | ||
71 | 'limit=s', | ||
72 | [ | ||
73 | qq(Limit in BYTES), | ||
74 | qq(Limit in PERCENT), | ||
75 | ], | ||
76 | undef, | ||
77 | undef, | ||
78 | [ undef, 'PERCENT%' ], | ||
79 | ); | ||
80 | |||
81 | return $ng; | ||
82 | } | ||
83 | |||
84 | my $ng; | ||
85 | |||
86 | @ARGV = ( '--help' ); | ||
87 | $ng = setup; | ||
88 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | ||
89 | like($@, qr/\n --warning=INTEGER/, 'warning ok'); | ||
90 | like($@, qr/\n -c, --critical=INTEGER/, 'critical ok'); | ||
91 | like($@, qr/\n -x, -y, -z=STRING\n Foobar. Default: XYZ\n/, 'x|y|z ok'); | ||
92 | like($@, qr/\n -l, --long, --longer, --longest\n Long format\n/, 'long ok'); | ||
93 | like($@, qr/\n -H, --hostname=ADDRESS\n Hostname\n/, 'hostname ok'); | ||
94 | like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); | ||
95 | like($@, qr/\n --disk=BYTES\n Disk limit in BYTES\n --disk=PERCENT%\n Disk limit in PERCENT\n --disk=STRING\n Disk limit in FOOBARS \(Default: 1024\)\n/, 'disk multiline ok'); | ||
96 | like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); | ||
97 | #print $@; | ||