File: 50builtin-help.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 (121 lines) | stat: -rw-r--r-- 2,567 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
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
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Commandable::Finder::Packages;
use Commandable::Invocation;

package MyTest::Command::one {
   use constant COMMAND_NAME => "one";
   use constant COMMAND_DESC => "the one command";
   use constant COMMAND_ARGS => (
      { name => "arg", description => "the argument" }
   );
   use constant COMMAND_OPTS => (
      { name => "verbose|v", description => "verbose option" },
      { name => "target|t=", description => "target option" },
      { name => "silent",    description => "silent option", negatable => 1 },
   );
   sub run {}
}

package MyTest::Command::two {
   use constant COMMAND_NAME => "two";
   use constant COMMAND_DESC => "the two command";
   sub run {}
}

my $finder = Commandable::Finder::Packages->new(
   base => "MyTest::Command",
);

$finder->add_global_options(
   { name => "one", into => \my $ONE,
      description => "the 'one' option" },
   { name => "two=", into => \my $TWO, default => 444,
      description => "the 'two' option" },
);

sub output_from_command
{
   my ( $cmd ) = @_;

   my $output;

   no warnings 'redefine';
   local *Commandable::Output::printf = sub {
      shift;
      my ( $fmt, @args ) = @_;
      $output .= sprintf $fmt, @args;
   };

   $finder->find_and_invoke( Commandable::Invocation->new( $cmd ) );

   return $output;
}

# Output redirection
{
   my $output = output_from_command( "help" );

   is( $output, <<'EOF', 'Output from builtin help command' );
COMMANDS:
  help: Display a list of available commands
  one : the one command
  two : the two command

GLOBAL OPTIONS:
    --one
      the 'one' option

    --two <value>
      the 'two' option (default: 444)
EOF
}

# Output heading formatting
{
   no warnings 'redefine';
   local *Commandable::Output::format_heading = sub {
      shift;
      my ( $text, $level ) = @_;
      $level //= 1;

      return sprintf "%s %s %s", "*" x $level, $text, "*" x $level;
   };

   local *Commandable::Output::format_note = sub {
      shift;
      my ( $text, $level ) = @_;
      $level //= 0;

      return sprintf "%s%s%s", "<"x($level+1), $text, ">"x($level+1);
   };

   my $output = output_from_command( "help one" );

   is( $output, <<'EOF', 'Output from builtin "help one" command' );
<one> - the one command

* SYNOPSIS: *
  one [OPTIONS...] $ARG

* OPTIONS: *
    <<--[no-]silent>>
      silent option

    <<--target <value>>>, <<-t <value>>>
      target option

    <<--verbose>>, <<-v>>
      verbose option

* ARGUMENTS: *
  <<$ARG>>    the argument
EOF
}

done_testing;