ead60f0a3d7cb2e118ca7b06d8231f6146a9be69
[gitmo/MooseX-Getopt.git] / t / 005_strict.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 40;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('MooseX::Getopt');
11 }
12
13 {
14
15     package App;
16     use Moose;
17
18     with 'MooseX::Getopt::Strict';
19
20     has 'data' => (
21         metaclass => 'Getopt',
22         is        => 'ro',
23         isa       => 'Str',
24         default   => 'file.dat',
25         cmd_flag  => 'f',
26     );
27
28     has 'cow' => (
29         metaclass   => 'Getopt',
30         is          => 'ro',
31         isa         => 'Str',
32         default     => 'moo',
33         cmd_aliases => [qw/ moocow m c /],
34     );
35
36     has 'horse' => (
37         metaclass   => 'Getopt',
38         is          => 'ro',
39         isa         => 'Str',
40         default     => 'bray',
41         cmd_flag    => 'horsey',
42         cmd_aliases => 'x',
43     );
44
45     has 'length' => (
46         is      => 'ro',
47         isa     => 'Int',
48         default => 24
49     );
50
51     has 'verbose' => (
52         is  => 'ro',
53         isa => 'Bool',
54     );
55
56     has 'libs' => (
57         is      => 'ro',
58         isa     => 'ArrayRef',
59         default => sub { [] },
60     );
61
62     has 'details' => (
63         is      => 'ro',
64         isa     => 'HashRef',
65         default => sub { {} },
66     );
67
68     has 'private_stuff' => (
69         is       => 'ro',
70         isa      => 'Int',
71         default  => 713
72     );
73
74     has '_private_stuff_cmdline' => (
75         is        => 'ro',
76         isa       => 'Int',
77         default   => 832,
78         cmd_flag  => 'p',
79     );
80
81 }
82
83 foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive MooseX::Getopt::Parser::Default)) {
84     SKIP: {
85         if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') {
86             eval { require Getopt::Long::Descriptive };
87             skip "Getopt::Long::Descriptive not installed", 13 if $@;
88         }
89
90         {
91             local @ARGV = ();
92
93             my $parser = $parser_name->new;
94             ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
95
96             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
97             isa_ok($getopt, 'MooseX::Getopt::Session');
98
99             my $app = App->new_with_options( getopt => $getopt );
100             isa_ok( $app, 'App' );
101
102             ok( !$app->verbose, '... verbosity is off as expected' );
103             is( $app->length, 24,         '... length is 24 as expected' );
104             is( $app->data,   'file.dat', '... data is file.dat as expected' );
105             is_deeply( $app->libs, [], '... libs is [] as expected' );
106             is_deeply( $app->details, {}, '... details is {} as expected' );
107             is($app->private_stuff, 713, '... private stuff is 713 as expected');
108         }
109
110         {
111             local @ARGV = (qw/--private_stuff 317/);
112
113             throws_ok {
114                 my $parser = $parser_name->new;
115                 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
116
117                 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
118                 isa_ok($getopt, 'MooseX::Getopt::Session');
119
120                 App->new_with_options( getopt => $getopt );
121             } qr/Unknown option: private_stuff/;
122         }
123
124         {
125             local @ARGV = (qw/--length 100/);
126
127             throws_ok {
128                 my $parser = $parser_name->new;
129                 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
130
131                 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
132                 isa_ok($getopt, 'MooseX::Getopt::Session');
133
134                 App->new_with_options( getopt => $getopt );
135             } qr/Unknown option: length/;
136         }
137
138     }
139 }