File: basic.t

package info (click to toggle)
libstring-formatter-perl 1.235-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 224 kB
  • sloc: perl: 658; makefile: 7
file content (111 lines) | stat: -rw-r--r-- 2,486 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
#!perl
use strict;

use Test::More tests => 12;

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");
}

{
  my $fmt = String::Formatter->new({
    input_processor => 'require_single_input',
    string_replacer => 'keyed_replace',

    codes => {
      g => 'groan',
      r => 'request',
    },
  });

  {
    my $zombie = {
        groan => 'nnnnngh',
        request => "Send... more...brainz...",
    };
    my $have = $fmt->format(q(%g... zombie says: %r), $zombie);
    my $want = "nnnnngh... zombie says: Send... more...brainz...";
    is($have, $want, "keyed_replace GOOD. fire BAD");
  }
}