File: 50cmd-print-table.t

package info (click to toggle)
libdevel-mat-perl 0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: perl: 6,224; makefile: 3
file content (77 lines) | stat: -rw-r--r-- 1,520 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
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Devel::MAT;

sub lines_from(&)
{
   my ( $code ) = @_;

   my @lines;

   no warnings 'once';
   local *Devel::MAT::Cmd::printf = sub {
      shift;
      my ( $format, @args ) = @_;
      push @lines, sprintf $format, @args;
   };

   $code->();

   return join "", @lines;
}

# Basic table
{
   is( lines_from { Devel::MAT::Cmd->print_table( [ [ "A", "B", "C" ] ] ) },
       "A B C\n",
       'Single row' );

   is( lines_from { Devel::MAT::Cmd->print_table( [ [ "A", "B", "C" ], [ "De", "Fgh", "Ijkl" ] ] ) },
       "A  B   C\n" .
       "De Fgh Ijkl\n",
       'Rows are aligned' );
}

# Separator
{
   is( lines_from { Devel::MAT::Cmd->print_table( [ [ "A", "B", "C" ] ], sep => "," ) },
       "A,B,C\n",
       'Single row with separator' );
}

# Right alignment
{
   is( lines_from {
         Devel::MAT::Cmd->print_table( [ [ "A", 12, "C" ], [ "De", 3456, "Ijkl" ] ], align => [ undef, "right" ] );
      },
       "A    12 C\n" .
       "De 3456 Ijkl\n",
       'Rows are aligned' );
}

# Headings
{
   no warnings 'once';
   local *Devel::MAT::Cmd::format_heading = sub { return "*$_[1]*" };

   is( lines_from {
         Devel::MAT::Cmd->print_table( [ [ "1", "2" ] ], headings => [ "A", "B" ] ) },
      "*A* *B*\n" .
      "1   2\n",
      'Row with headings' );
}

# Indent
{
   is( lines_from {
         Devel::MAT::Cmd->print_table( [ [ "x", "y", "z" ] ], indent => 3 ) },
      "   x y z\n",
      'Row with indent' );
}

done_testing;