Merge pull request #3 from Madlohe/master
[Nagios-Plugin.git] / t / Nagios-Monitoring-Plugin-Getopt-04.t
blob15771c7bb97be8a1f04e819f362e7bd4603fe838
1 # Nagios::Monitoring::Plugin::Getopt spec-to-help generation tests
3 use strict;
5 use Test::More tests => 11;
6 BEGIN { use_ok('Nagios::Monitoring::Plugin::Getopt') };
8 # Needed to get evals to work in testing
9 Nagios::Monitoring::Plugin::Functions::_use_die(1);
11 my %PARAM = (
12     version => '0.01',
13     usage => "Don't use this plugin!",
16 sub setup 
18   # Instantiate object
19   my $ng = Nagios::Monitoring::Plugin::Getopt->new(%PARAM);
20   ok($ng, 'constructor ok');
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   );
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   );
41   # Named args, multiple mixed, no label
42   $ng->arg(
43     spec => 'long|longer|longest|l',
44     help => qq(Long format),
45   );
47   # Named args, long + short, explicit label
48   $ng->arg(
49     spec => 'hostname|H=s',
50     label => 'ADDRESS',
51     help => qq(Hostname),
52   );
54   # Positional args, long only, explicit label
55   $ng->arg('avatar=s', 'Avatar', undef, undef, 'AVATAR');
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   );
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   );
81   return $ng;
84 my $ng;
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 $@;