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