-
Notifications
You must be signed in to change notification settings - Fork 135
/
fmt.t
93 lines (78 loc) · 2.56 KB
/
fmt.t
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
use v6;
use Test;
plan 21;
# L<S02/"Names and Variables"/"formatted representation"
# of "any scalar value" ".fmt('%03d')">
{
is "Hi".fmt("[%s]"), "[Hi]", 'fmt() works with %s';
is '3.141'.fmt("[%d]"), "[3]", "fmt() works with %d";
is (5.6).fmt('%f'), '5.600000', 'fmt() works with %f';
}
# L<S02/"Names and Variables"/"format an array value"
# "supply a second argument">
{
is (1.3,2.4,3).fmt("%d", "_"), "1_2_3", "fmt() works with plain lists";
my @list = 'a'..'c';
is @list.fmt('<%s>', ':'), '<a>:<b>:<c>', 'fmt() works with @ array';
my $list = ['a', 'b', 'c'];
is $list.fmt('[%s]', ','), '[a],[b],[c]', 'fmt() works with Array object';
# single elem Array:
$list = ['a'];
is $list.fmt('<<%s>>', '!!!'), '<<a>>', 'fmt() works for single elem array';
}
# L<S02/"Names and Variables"/"hash value" "formats for both key and value">
#?DOES 1
{
my $hash = {
a => 1.3,
b => 2.4,
};
my $str = $hash.fmt("%s:%d", "_");
if $str eq "a:1_b:2" || $str eq "b:2_a:1" {
pass "fmt() works with hashes";
} else {
flunk "fmt() fails to work with hashes";
}
}
is (1..3).fmt('%02d', '/'), '01/02/03', 'Range.fmt';
is (1..3).fmt, '1 2 3', 'Range.fmt with defaults';
# L<S02/"Names and Variables"/"list of pairs" "formats for both key and value">
#?DOES 4
{
# a single pair:
my $pair = (100 => 'lovely');
is $pair.fmt("%d ==> %s"), "100 ==> lovely", '.fmt works with a single pair';
# list of a single pair:
my @pairs = (100 => 'lovely');
is(@pairs.fmt("%d ==> %s", "\n"), "100 ==> lovely", '.fmt works with lists of a single pair');
# list of pair:
@pairs = (a => 1.3, b => 2.4);
is @pairs.fmt("%s:%d", "_"), "a:1_b:2", "fmt() works with lists of pairs";
is @pairs.fmt("(%s => %f)", ""), "(a => 1.300000)(b => 2.400000)",
"fmt() works with lists of pairs";
}
# Test defaults on $comma
#?DOES 2
{
is([1..3].fmt("%d"), "1 2 3", 'default $comma for array');
my $hash = {
a => 1.3,
b => 2.4,
};
my $str = $hash.fmt("%s:%d");
if $str eq "a:1\nb:2" || $str eq "b:2\na:1" {
pass 'default $comma works with hashes';
} else {
flunk 'default $comma fails to work with hashes';
}
}
# .fmt without arguments
#?DOES 5
{
is (1).fmt(), '1', 'scalar .fmt without $fmt';
is (1=>"a").fmt(), "1\ta", 'pair .fmt without $fmt';
is (1,2).fmt(), '1 2', 'list .fmt without $fmt';
is [1,2].fmt(), '1 2', 'array .fmt without $fmt';
is {1=>"a"}.fmt(), "1\ta", 'hash .fmt without $fmt';
}
# vim: ft=perl6