File: 05stringfactory.t

package info (click to toggle)
libstring-format-perl 1.18-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 132 kB
  • sloc: perl: 173; makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,231 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env perl
# vim: set ft=perl ts=4 sw=4:

# ======================================================================
# 05stringfactory.t
#
# Test the subroutine generating facilities, supported by the
# stringfactory class method.
# ======================================================================

use strict;
use Test::More tests => 1;
use String::Format;
use POSIX qw(strftime);

my ($orig, $target, $result);

# ======================================================================
# Test 1
# Using instance methods
# ======================================================================
my $tpkg = TestPkg->new;
my %formats = (
    'i' => sub { $tpkg->id },
    'd' => sub { strftime($_[0], localtime($tpkg->date)) },
    'f' => sub { $tpkg->diff($_[0]) }
);
my $formatter = String::Format->stringfactory(\%formats);

$orig   = 'my lovely TestPkg instance has an id of %i.';
$target = 'my lovely TestPkg instance has an id of ' . $tpkg->id . '.';
$result = $formatter->($orig);

is $target => $result;

BEGIN {
    # (silly) embedded package
    package TestPkg;
    sub new  { bless \(my $o = int rand($$)) => $_[0] }
    sub id   { ${$_[0]} }
    sub date { time }
    sub diff { $_[0]->id - ($_[0] || 0) }
}