summaryrefslogtreecommitdiff
path: root/t/tests/parsing.t
blob: 2ac394ed34ae2916242952da46317a56b6e0b410 (plain)
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
use Getopt::Dakkar::Style qw(test);
use Getopt::Dakkar;
use Getopt::Dakkar::Option;
use Getopt::Dakkar::Parameter;
use Getopt::Dakkar::Subcommand;
 
subtest 'nothing' => sub {
    my $getopt = Getopt::Dakkar->new();
 
    my $stash;
    lives_ok { $stash = $getopt->parse([]) }
        'nothing parsing nothing should live';
    cmp_deeply(
        $stash,
        methods(
            options => {},
            arguments => {},
        ),
        'the stash should be empty',
    or explain $stash;
 
    throws_ok { $stash = $getopt->parse([1]) }
        'Getopt::Dakkar::X::ExtraArgs',
        'too many elements will throw';
};
 
subtest 'simple option' => sub {
    my $getopt = Getopt::Dakkar->new({
        options => [
            Getopt::Dakkar::Option->new({
                name => 'foo',
                aliases => ['f'],
            }),
        ],
    });
 
    # we don't yet have the concept of "required option value / 
    # argument", so we can't test the corresponding behaviour 
 
    my $stash;
 
    for my $form (qw(-f --foo)) {
        lives_ok { $stash = $getopt->parse([$form]) }
            "parsing $form should live";
        cmp_deeply(
            $stash,
            methods(
                options => { foo => methods(name=>'foo',value=>1) },
                arguments => {},
            ),
            'the stash should contain the option value',
        or explain $stash;
    }
 
    for my $form (qw(-x -fx --xx)) {
        throws_ok { $stash = $getopt->parse([$form]) }
            'Getopt::Dakkar::X::BadOption',
            "parsing $form should throw";
    }
};
 
subtest 'option bundling' => sub {
    my $getopt = Getopt::Dakkar->new({
        options => [
            Getopt::Dakkar::Option->new({
                name => 'foo',
                aliases => ['f'],
                can_bundle => 1,
            }),
            Getopt::Dakkar::Option->new({
                name => 'bar',
                aliases => ['b'],
                can_bundle => 1,
            }),
        ],
    });
 
    my $stash;
 
    for my $form (
        [qw(-fb)], [qw(-bf)],
        [qw(-f -b)], [qw(-b -f)],
        [qw(--foo -b)], [qw(--bar -f)],
        [qw(--foo --bar)],
    ) {
        lives_ok { $stash = $getopt->parse($form) }
            "parsing $form->@* should live";
        cmp_deeply(
            $stash,
            methods(
                options => {
                    foo => methods(name=>'foo',value=>1),
                    bar => methods(name=>'bar',value=>1),
                },
                arguments => {},
            ),
            'the stash should contain the option value',
        or explain $stash;
    }
 
    for my $form (
        [qw(--foobar)], [qw(-foo)],
    ) {
        throws_ok { $stash = $getopt->parse($form) }
            'Getopt::Dakkar::X::BadOption',
            "parsing $form->@* should throw";
    }
};
 
done_testing;