File: 08_must.t

package info (click to toggle)
libgetopt-ex-hashed-perl 1.0602-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 312 kB
  • sloc: perl: 409; makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,649 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;
use Test::More;
use lib './t';
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;

use Getopt::Long;
use Getopt::EX::Hashed 'has';

has answer => spec => '=i',
    must => sub { $_[1] == 42 };

has answer_is => spec => '=i',
    must   => [ sub { $_[1] >= 42 }, sub { $_[1] <= 42 } ],
    action => sub {
	$_->{$_[0]} = "Answer is $_[1]";
    };

has question => spec => 'q=s@', default => [],
    must => sub {
	grep { lc($_[1]) eq $_ } qw(life universe everything);
    };

has nature => spec => '=s%', default => {},
    must => sub {
	grep { lc($_[2]) eq $_ } qw(paranoid);
    };

VALID: {
    local @ARGV = qw(--answer 42
		     --answer-is 42
		     -q life
		     -q universe
		     -q everything
		     --nature Marvin=Paranoid
		   );

    my $app = Getopt::EX::Hashed->new() or die;
    GetOptions($app->optspec); # or die;

    is($app->{answer}, 42, "Number");
    is($app->{answer_is}, "Answer is 42", "Number with action");
    is_deeply($app->{question}, [ "life", "universe", "everything" ], "List");
    is($app->{nature}->{Marvin}, "Paranoid", "Hash");
}

INVALID: {
    local @ARGV = qw(--answer 41
		     --answer-is 41
		     -q life
		     -q space
		     -q everything
		     -nature Marvin=Sociable
		   );

    # has [ qw(+answer +answer_is +question +nature) ] => must => undef;

    my $app = Getopt::EX::Hashed->new() or die;
    GetOptions($app->optspec); # or die;

    isnt($app->{answer}, 41, "Number");
    isnt($app->{answer_is}, "Answer is 41", "Number with action");
    isnt($app->{question}->[1], "space", "List");
    isnt($app->{nature}->{Marvin}, "Sociable", "Hash");
}

done_testing;