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