File: choices.t

package info (click to toggle)
libgetopt-argparse-perl 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 348 kB
  • sloc: perl: 1,133; makefile: 2
file content (84 lines) | stat: -rw-r--r-- 1,797 bytes parent folder | download | duplicates (3)
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
use lib "lib";
use Test::More;
use Test::Exception;

use Getopt::ArgParse::Parser;

$p = Getopt::ArgParse::Parser->new();
ok($p, "new argparser");

$p->add_argument(
    '--choice',
    choices => [ 'a', 'b', 'c' ],
);

throws_ok(
    sub { $n = $p->parse_args(split(' ', '--choice hello')); },
    qr/not in/,
    'choice error: not in choices - arrayref'
);

$p->add_argument(
    '--choice1',
    choices => sub {
        die "not in ['a', 'b', 'c']" unless $_[0] =~ /^(a|b|c)$/i;
    }
);

throws_ok(
    sub { $n = $p->parse_args(split(' ', '--choice1 hello')); },
    qr/not in/,
    'choice error: not in choices - coderef'
);

$n = $p->parse_args(split(' ', '--choice1 A --choice a'));

ok($n->choice eq 'a', 'choice ok - fixed value a');

ok($n->choice1 eq 'A', 'choice ok - case insensative A');

$p = Getopt::ArgParse::Parser->new();

throws_ok ( sub {
                $p->add_argument(
                    '--choice',
                    choices => [ 'a', 'b', 'c' ],
                    choices_i => [ 'A', 'B', 'C' ],
                );
            },
            qr/Not allow to specify/,
            'not allow to specify choices and choices_i',
);

throws_ok(
    sub {
        $p->add_argument(
            '--choice',
            choices_i => sub { die 'choices' },
        );
    },
    qr/arrayref/,
    'only allow arrayref',
);

lives_ok(
    sub {
        $p->add_argument(
            '--choice',
            choices_i => [ 'hello', 'world' ],
        );
});

throws_ok(
    sub { $n = $p->parse_args('--choice', 'abc'); },
    qr/not in choices/,
    'not in allowed choices_i',
);

$n = $p->parse_args('--choice', 'WORLD');

ok($n->choice eq 'WORLD', "WORLD is OK");
$n = $p->parse_args('--choice', 'HEllo');
ok($n->choice eq 'HEllo', "HEllo is OK too");

done_testing;