File: 04subrefs.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 (42 lines) | stat: -rw-r--r-- 1,309 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl
# vim: set ft=perl ts=4 sw=4:

# ======================================================================
# 04subrefs.t
#
# The design of String::Format is such that you can pass a subroutine
# reference as a hash value, and it will be called in place.  Let's
# test that.
# ======================================================================

use strict;

use Test::More tests => 2;
use String::Format;
use POSIX qw(strftime); # for test 1

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

# ======================================================================
# Test 1
# Using strftime in a subroutine reference.
# ======================================================================
$orig   = q(It is now %{%Y/%m%d}d.);
$target = sprintf q(It is now %s.), strftime("%Y/%m/%d", localtime);
$result = stringf $orig, "d" => sub { strftime("%Y/%m/%d", localtime) };
is $target => $result;

# ======================================================================
# Test 2
# using getpwuid
# ======================================================================
SKIP: {
    skip "Test skipped on this platform", 1 
        if $^O eq 'MSWin32';

    $orig   = "I am %u.";
    $target = "I am " . getpwuid($<) . ".";
    $result = stringf $orig, "u" => sub { getpwuid($<) };
    is $target => $result;
}