File: 13finder-methodattributes.t

package info (click to toggle)
libcommandable-perl 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: perl: 1,530; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,590 bytes parent folder | download
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
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Commandable::Finder::MethodAttributes;

BEGIN {
   Commandable::Finder::SubAttributes::HAVE_ATTRIBUTE_STORAGE or
      plan skip_all => "Attribute::Storage is not available";
}

my @called;

package MyTest::Commands {
   use Commandable::Finder::MethodAttributes ':attrs';

   sub command_one
      :Command_description("the one command")
      :Command_arg("arg", "the argument")
   {
      my $self = shift;
      my ( $arg ) = @_;

      push @called, { self => $self, arg => $arg };
   }
}

my $finder = Commandable::Finder::MethodAttributes->new(
   object => my $object = bless {}, "MyTest::Commands",
);

# find_commands
{

   is( [ sort map { $_->name } $finder->find_commands ],
      [qw( help one )],
      '$finder->find_commmands' );
}

# a single command
{
   my $one = $finder->find_command( "one" );
   # can't test 'code' directly
   is( { map { $_, $one->$_ } qw( name description package ) },
      {
         name        => "one",
         description => "the one command",
         package     => "MyTest::Commands",
      },
      '$finder->find_command' );

   is( scalar $one->arguments, 1, '$one has an argument' );

   my ( $arg ) = $one->arguments;
   is( { map { $_ => $arg->$_ } qw( name description ) },
      {
         name        => "arg",
         description => "the argument",
      },
      'metadata of argument to one'
   );

   $one->code->( "the argument" );
   is( \@called, [ { self => $object, arg => "the argument" } ],
      'Invoked code sees invocant object' );
}

done_testing;