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
|
#!perl
use strict;
use Test::More tests => 2;
use String::Formatter;
{
package Zombie;
sub new { bless {} }
sub groan { 'nnnnngh' }
sub request { "Send... more... $_[1]!" }
}
{
my $fmt = String::Formatter->new({
input_processor => 'require_single_input',
string_replacer => 'method_replace',
codes => {
g => 'groan',
r => 'request',
i => sub { 0 + $_[0] },
},
});
{
my $zombie = Zombie->new;
my $have = $fmt->format(q(%g... zombie number %i say: %{cops}r), $zombie);
my $zid = 0 + $zombie;
my $want = "nnnnngh... zombie number $zid say: Send... more... cops!";
is($have, $want, "method_replace GOOD. fire BAD");
}
}
{
my $coderef = String::Formatter->new({
input_processor => 'require_single_input',
string_replacer => 'method_replace',
codes => {
f => sub { $_->{foo} },
r => sub { $_->{bar} },
},
});
is(
$coderef->format('%f => %r', { foo => 'FOO', bar => 'BAR' }),
'FOO => BAR',
'topicalized method replace',
)
}
|