diff options
author | dakkar <dakkar@thenautilus.net> | 2018-07-13 18:52:08 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2018-07-13 18:52:08 +0100 |
commit | 9c7b213f58fe533953953e90b5bc77087ca4d45d (patch) | |
tree | c78cd04844a4ba1e92f1bf1dc29c14b3813e4bc7 /t/tests/parsing.t | |
parent | I'm losing the plot, here (diff) | |
download | Getopt-Dakkar-9c7b213f58fe533953953e90b5bc77087ca4d45d.tar.gz Getopt-Dakkar-9c7b213f58fe533953953e90b5bc77087ca4d45d.tar.bz2 Getopt-Dakkar-9c7b213f58fe533953953e90b5bc77087ca4d45d.zip |
Diffstat (limited to 't/tests/parsing.t')
-rw-r--r-- | t/tests/parsing.t | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/t/tests/parsing.t b/t/tests/parsing.t new file mode 100644 index 0000000..2ac394e --- /dev/null +++ b/t/tests/parsing.t @@ -0,0 +1,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; |