summaryrefslogtreecommitdiff
path: root/t/tests/parsing.t
diff options
context:
space:
mode:
Diffstat (limited to 't/tests/parsing.t')
-rw-r--r--t/tests/parsing.t110
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;