From 9c7b213f58fe533953953e90b5bc77087ca4d45d Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 13 Jul 2018 18:52:08 +0100 Subject: we can now parse boolean options --- t/tests/parsing.t | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 t/tests/parsing.t (limited to 't/tests/parsing.t') 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; -- cgit v1.2.3