* t/*.t: Fixed test plans.
[gitmo/MooseX-Getopt.git] / t / 005_strict.t
CommitLineData
bff3807b 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
7c1cb743 6use Test::More tests => 46;
bff3807b 7use Test::Exception;
8
9BEGIN {
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' => (
0f8232b6 21 metaclass => 'Getopt',
bff3807b 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' => (
0f8232b6 37 metaclass => 'Getopt',
bff3807b 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
ac2073c8 83foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive MooseX::Getopt::Parser::Default)) {
053fa19e 84 SKIP: {
85 if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') {
86 eval { require Getopt::Long::Descriptive };
7c1cb743 87 skip "Getopt::Long::Descriptive not installed", 15 if $@;
053fa19e 88 }
bff3807b 89
053fa19e 90 {
91 local @ARGV = ();
bff3807b 92
053fa19e 93 my $parser = $parser_name->new;
ac2073c8 94 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
bff3807b 95
053fa19e 96 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
97 isa_ok($getopt, 'MooseX::Getopt::Session');
bff3807b 98
053fa19e 99 my $app = App->new_with_options( getopt => $getopt );
100 isa_ok( $app, 'App' );
6ac028c4 101
053fa19e 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 }
6ac028c4 109
053fa19e 110 {
111 local @ARGV = (qw/--private_stuff 317/);
112
113 throws_ok {
114 my $parser = $parser_name->new;
ac2073c8 115 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
6ac028c4 116
053fa19e 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;
ac2073c8 129 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
053fa19e 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}