File: 13_callback_rules_with_whitespace.t

package info (click to toggle)
libtext-table-perl 1.135-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 264 kB
  • sloc: perl: 906; makefile: 5
file content (84 lines) | stat: -rw-r--r-- 2,446 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 1;
use List::Util qw(sum);
use Text::Table;

# Standard Perl Cookbook function.
sub commify {
    my $text = reverse $_[0];
    $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
    return scalar reverse $text;
}

my $colsep = "  ";      # Use no-pixel column separators.

# Make up some data.
my @names = ("some stuff that's kind of long", "short", "weird negative thing", "the last thing");
my @dur = (123456, 1234.567890, -123456, 123.456);
my @calls = (4082, 477, 91, 45);
my $sumdur = sum(@dur);
my $N = scalar(@names);

# Define table columns, and load the table header.
my %sep = ( is_sep => 1, title => $colsep, body => $colsep );
my $tb = Text::Table->new(
    { title => "FIRST",     align_title => "left",  align => "left" },  \%sep,
    { title => "SECOND",    align_title => "right", align => "num"  },  \%sep,
    { title => "%",         align_title => "right", align => "num"  },  \%sep,
    { title => "FOUR",      align_title => "right", align => "num", sample => "123,456,789" },
);

# Load the table body.
for (0 .. $N - 1) {
    $tb->load([
        $names[$_],
        commify((sprintf "%.6f", $dur[$_])),
        sprintf("%.1f%%", $dur[$_]/$sumdur * 100),
        commify($calls[$_]),
    ]);
}

# Load the table footer.
$tb->load([
    "TOTAL ($N)",
    commify(sprintf "%.6f", $sumdur),
    sprintf("%.1f%%", 100),
    commify(sum(@calls)),
]);

# Print the table.
my $rule = $tb->rule(
    sub {
        my ($i, $l) = @_;
        # printf "\n1: i=%d l=%d\n", $i, $l;
        return "-" x $l;
    },
    sub {
        my ($i, $l) = @_;
        # printf "\n2: i=%d l=%d\n", $i, $l;
        return " " x $l;
    }
);

my $output =
$tb->title .
$rule .
join('', map { $tb->body($_) } (0 .. $N-1)) .
$rule .
$tb->body($N);

# TEST
is($output, <<'EOF', 'Spaces are handled correctly in rules.');
FIRST                                    SECOND         %         FOUR
------------------------------  ---------------  --------  -----------
some stuff that's kind of long   123,456.000000   9090.9%        4,082
short                              1,234.567890     90.9%          477
weird negative thing            -123,456.000000  -9090.9%           91
the last thing                       123.456000      9.1%           45
------------------------------  ---------------  --------  -----------
TOTAL (4)                          1,358.023890    100.0%        4,695
EOF