102_basic_basic.t 5.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 69;

BEGIN {
    use_ok('MouseX::Getopt::Basic');
}

{
    package App;
    use Mouse;

    with 'MouseX::Getopt::Basic';

    has 'data' => (
        metaclass => 'MouseX::Getopt::Meta::Attribute',
        is        => 'ro',
        isa       => 'Str',
        default   => 'file.dat',
        cmd_flag  => 'f',
    );

    has 'cow' => (
        metaclass   => 'Getopt',
        is          => 'ro',
        isa         => 'Str',
        default     => 'moo',
        cmd_aliases => [qw/ moocow m c /],
    );

    has 'horse' => (
        metaclass   => 'MouseX::Getopt::Meta::Attribute',
        is          => 'ro',
        isa         => 'Str',
        default     => 'bray',
        cmd_flag    => 'horsey',
        cmd_aliases => 'x',
    );

    has 'length' => (
        is      => 'ro',
        isa     => 'Int',
        default => 24
    );

    has 'verbose' => (
        is     => 'ro',
        isa    => 'Bool',
    );

    has 'libs' => (
        is      => 'ro',
        isa     => 'ArrayRef',
        default => sub { [] },
    );

    has 'details' => (
        is      => 'ro',
        isa     => 'HashRef',
        default => sub { {} },
    );

    has '_private_stuff' => (
        is      => 'ro',
        isa     => 'Int',
        default => 713
    );

    has '_private_stuff_cmdline' => (
        metaclass => 'MouseX::Getopt::Meta::Attribute',
        is        => 'ro',
        isa       => 'Int',
        default   => 832,
        cmd_flag  => 'p',
    );

}

foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) {
    my $attr = App->meta->get_attribute($attr_name);
    isa_ok($attr, 'Mouse::Meta::Attribute');
    isa_ok($attr, 'MouseX::Getopt::Meta::Attribute');
    can_ok($attr, 'cmd_flag');
    can_ok($attr, 'cmd_aliases');
}

{
    local @ARGV = ();

    my $app = App->new_with_options;
    isa_ok($app, 'App');

    ok(!$app->verbose, '... verbosity is off as expected');
    is($app->length, 24, '... length is 24 as expected');
    is($app->data, 'file.dat', '... data is file.dat as expected');
    is_deeply($app->libs, [], '... libs is [] as expected');
    is_deeply($app->details, {}, '... details is {} as expected');
}

{
    local @ARGV = ('--verbose', '--length', 50);

    my $app = App->new_with_options;
    isa_ok($app, 'App');

    ok($app->verbose, '... verbosity is turned on as expected');
    is($app->length, 50, '... length is 50 as expected');
    is($app->data, 'file.dat', '... data is file.dat as expected');
    is_deeply($app->libs, [], '... libs is [] as expected');
    is_deeply($app->details, {}, '... details is {} as expected');
}

{
    local @ARGV = ('--verbose', '-f', 'foo.txt');

    my $app = App->new_with_options;
    isa_ok($app, 'App');

    ok($app->verbose, '... verbosity is turned on as expected');
    is($app->length, 24, '... length is 24 as expected');
    is($app->data, 'foo.txt', '... data is foo.txt as expected');
    is_deeply($app->libs, [], '... libs is [] as expected');
    is_deeply($app->details, {}, '... details is {} as expected');
}

{
    local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');

    my $app = App->new_with_options;
    isa_ok($app, 'App');

    ok($app->verbose, '... verbosity is turned on as expected');
    is($app->length, 24, '... length is 24 as expected');
    is($app->data, 'file.dat', '... data is foo.txt as expected');
    is_deeply($app->libs,
    ['libs/', 'includes/lib'],
    '... libs is [libs/, includes/lib] as expected');
    is_deeply($app->details, {}, '... details is {} as expected');
}

{
    local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');

    my $app = App->new_with_options;
    isa_ok($app, 'App');

    ok(!$app->verbose, '... verbosity is turned on as expected');
    is($app->length, 24, '... length is 24 as expected');
    is($app->data, 'file.dat', '... data is foo.txt as expected');
    is_deeply($app->libs, [], '... libs is [] as expected');
    is_deeply($app->details,
    { os => 'mac', name => 'foo' },
    '... details is { os => mac, name => foo } as expected');
}

{
    # Test negation on booleans too ...
    local @ARGV = ('--noverbose');

    my $app = App->new_with_options;
    isa_ok($app, 'App');

    ok(!$app->verbose, '... verbosity is turned off as expected');
    is($app->length, 24, '... length is 24 as expected');
    is($app->data, 'file.dat', '... file is file.dat as expected');
    is_deeply($app->libs, [], '... libs is [] as expected');
    is_deeply($app->details, {}, '... details is {} as expected');
}

# Test cmd_alias without cmd_flag
{
    local @ARGV = ('--cow', '42');
    my $app = App->new_with_options;
    isa_ok($app, 'App');
    is($app->cow, 42, 'cmd_alias, but not using it');
}
{
    local @ARGV = ('--moocow', '88');
    my $app = App->new_with_options;
    isa_ok($app, 'App');
    is($app->cow, 88, 'cmd_alias, using long one');
}
{
    local @ARGV = ('-c', '99');
    my $app = App->new_with_options;
    isa_ok($app, 'App');
    is($app->cow, 99, 'cmd_alias, using short one');
}

# Test cmd_alias + cmd_flag
{
    local @ARGV = ('--horsey', '123');
    my $app = App->new_with_options;
    isa_ok($app, 'App');
    is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
}
{
    local @ARGV = ('-x', '321');
    my $app = App->new_with_options;
    isa_ok($app, 'App');
    is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
}

# Test _foo + cmd_flag
{
    local @ARGV = ('-p', '666');
    my $app = App->new_with_options;
    isa_ok($app, 'App');
    is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
}

# Test ARGV support
{
    my @args = ('-p', 12345, '-c', 99, '-');
    local @ARGV = @args;
    my $app = App->new_with_options;
    isa_ok($app, 'App');
    is_deeply($app->ARGV, \@args, 'ARGV accessor');
    is_deeply(\@ARGV, \@args, '@ARGV unmangled');
    is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');
}