File: basic.t

package info (click to toggle)
libstring-formatter-perl 0.102080-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 164 kB
  • ctags: 16
  • sloc: perl: 471; makefile: 12
file content (91 lines) | stat: -rw-r--r-- 1,993 bytes parent folder | download | duplicates (2)
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
#!perl
use strict;

use Test::More tests => 11;

use String::Formatter;

my $fmt = String::Formatter->new({
  codes => {
    a => "apples",
    b => "bananas",
    g => "grapefruits",
    m => "melons",
    w => "watermelons",
    '*' => 'brussel sprouts',
  },
});

{
  my $have = $fmt->format(qq(please have some %w\n));
  my $want = "please have some watermelons\n";

  is($have, $want, "formatting with no text after last code");
}

{
  my $have = $fmt->format(qq(w: %w\nb: %b\n));
  my $want = "w: watermelons\nb: bananas\n";

  is($have, $want, "format with multiple newlines");
}

{
  my $have = $fmt->format(q(10%% discount on %w));
  my $want = '10% discount on watermelons';

  is($have, $want, "%% -> %");
}

{
  my $have = $fmt->format(q(I like %a, %b, and %g, but not %m or %w.));
  my $want = 'I like apples, bananas, and grapefruits, '
           . 'but not melons or watermelons.';

  is($have, $want, "formatting with text after last code");
}

{
  my $have = $fmt->format(q(This has no stuff.));
  my $want = 'This has no stuff.';

  is($have, $want, "formatting with no %codes");
}

{
  my $ok    = eval { $fmt->format(q(What is %z for?)); 1 };
  my $error = $@;
  like($error, qr/Unknown conversion/i, 'unknown conversions are fatal');
}

{
  my $have = $fmt->format("We have %.5w.");
  my $want = "We have water.";
  is($have, $want, "truncate at max_chars");
}

{
  my $have = $fmt->format("We have %10a.");
  my $want = "We have     apples.";
  is($have, $want, "left-pad to reach min_chars");
}

{
  my $have = $fmt->format("We have %10.a.");
  my $want = "We have     apples.";
  is($have, $want, "left-pad to reach min_chars (with dot)");
}

{
  my $have = $fmt->format("We have %-10a.");
  my $want = "We have apples    .";
  is($have, $want, "right-pad to reach min_chars (-10)");
}

{
  my $have = $fmt->format('Please do not mention the %*.');
  my $want = 'Please do not mention the brussel sprouts.';
  is($have, $want, "non-identifier format characters");
}