* MooseX::Getopt: ARGV and extra_argv are deletaged from MooseX::Getopt::Session.
[gitmo/MooseX-Getopt.git] / t / 004_nogetop.t
CommitLineData
a01f08fb 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
ac2073c8 6use Test::More tests => 34;
bff3807b 7use Test::Exception;
a01f08fb 8
9BEGIN {
10 use_ok('MooseX::Getopt');
11}
12
13{
a01f08fb 14 package App;
15 use Moose;
16
17 with 'MooseX::Getopt';
18
19 has 'data' => (
0f8232b6 20 metaclass => 'Getopt',
a01f08fb 21 is => 'ro',
22 isa => 'Str',
23 default => 'file.dat',
24 cmd_flag => 'f',
25 );
26
27 has 'cow' => (
28 metaclass => 'Getopt',
29 is => 'ro',
30 isa => 'Str',
31 default => 'moo',
32 cmd_aliases => [qw/ moocow m c /],
33 );
34
35 has 'horse' => (
0f8232b6 36 metaclass => 'Getopt',
a01f08fb 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' => (
0f8232b6 68 metaclass => 'NoGetopt',
a01f08fb 69 is => 'ro',
70 isa => 'Int',
71 default => 713
72 );
73
74 has '_private_stuff_cmdline' => (
0f8232b6 75 metaclass => 'Getopt',
a01f08fb 76 is => 'ro',
77 isa => 'Int',
78 default => 832,
79 cmd_flag => 'p',
80 );
81
82}
83
ac2073c8 84foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive MooseX::Getopt::Parser::Default)) {
19b87ede 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 }
a01f08fb 90
19b87ede 91 {
92 local @ARGV = ();
a01f08fb 93
19b87ede 94 my $parser = $parser_name->new;
ac2073c8 95 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
bff3807b 96
19b87ede 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 {
19b87ede 115 my $parser = $parser_name->new;
ac2073c8 116 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
19b87ede 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 )
19b87ede 122 } qr/Unknown option: private_stuff/;
123 }
bff3807b 124
19b87ede 125 }
bff3807b 126}