10 | 10 |
use Scalar::Util qw/reftype/;
|
11 | 11 |
|
12 | 12 |
### examples from Log::Report::Message and more
|
|
13 |
# Check overloading
|
|
14 |
|
|
15 |
sub ol_is($$;$)
|
|
16 |
{ # since Test::More 0.95_01, is() does not stringify its arguments.
|
|
17 |
# This means that overloading does not quick in. How to test
|
|
18 |
# overloading now?
|
|
19 |
my ($f, $s, $comment) = @_;
|
|
20 |
overload::Overloaded($f) || overload::Overloaded($s)
|
|
21 |
or die "both not overloaded, in '$f' and '$s'";
|
|
22 |
is("$f", "$s", $comment);
|
|
23 |
}
|
13 | 24 |
|
14 | 25 |
my $a = __"Hello";
|
15 | 26 |
ok(defined $a);
|
16 | 27 |
is(ref $a, 'Log::Report::Message');
|
17 | 28 |
is(reftype $a, 'HASH');
|
18 | |
is(__"Hello World", 'Hello World');
|
19 | |
is(__"Hello World {a}", 'Hello World {a}');
|
20 | |
is(__('Hello World {a}'), 'Hello World {a}');
|
|
29 |
ol_is(__"Hello World", 'Hello World');
|
|
30 |
ol_is(__"Hello World {a}", 'Hello World {a}');
|
|
31 |
ol_is(__('Hello World {a}'), 'Hello World {a}');
|
21 | 32 |
|
22 | 33 |
my $c = __x"Hello";
|
23 | 34 |
ok(defined $c);
|
24 | 35 |
is(ref $c, 'Log::Report::Message');
|
25 | 36 |
is(reftype $c, 'HASH');
|
26 | |
is(__x("Hello World", a => 42), 'Hello World');
|
27 | |
is(__x("Hello World {a}", a => 42), 'Hello World 42');
|
28 | |
is((__x"Hello World {a}", a => 42), 'Hello World 42');
|
29 | |
is((__x "Hello World {a}", a => 42), 'Hello World 42');
|
30 | |
is((__x "{a}{a}{a}", a => 42), '424242');
|
|
37 |
ol_is(__x("Hello World", a => 42), 'Hello World');
|
|
38 |
ol_is(__x("Hello World {a}", a => 42), 'Hello World 42');
|
|
39 |
ol_is((__x"Hello World {a}", a => 42), 'Hello World 42');
|
|
40 |
ol_is((__x "Hello World {a}", a => 42), 'Hello World 42');
|
|
41 |
ol_is((__x "{a}{a}{a}", a => 42), '424242');
|
31 | 42 |
|
32 | 43 |
my $d = __n"Hello","World",3;
|
33 | 44 |
ok(defined $d);
|
34 | 45 |
is(ref $d, 'Log::Report::Message');
|
35 | 46 |
is(reftype $d, 'HASH');
|
36 | |
is(__n("Hello", "World", 1), 'Hello');
|
37 | |
is(__n("Hello", "World", 0), 'World');
|
38 | |
is(__n("Hello", "World", 2), 'World');
|
|
47 |
ol_is(__n("Hello", "World", 1), 'Hello');
|
|
48 |
ol_is(__n("Hello", "World", 0), 'World');
|
|
49 |
ol_is(__n("Hello", "World", 2), 'World');
|
39 | 50 |
|
40 | 51 |
my $e = __nx"Hello","World",3,a=>42;
|
41 | 52 |
ok(defined $e);
|
42 | 53 |
is(ref $e, 'Log::Report::Message');
|
43 | 54 |
is(reftype $e, 'HASH');
|
44 | |
is(__nx("Hel{a}lo", "Wor{a}ld", 1,a=>42), 'Hel42lo');
|
45 | |
is(__nx("Hel{a}lo", "Wor{a}ld", 0,a=>42), 'Wor42ld');
|
46 | |
is(__nx("Hel{a}lo", "Wor{a}ld", 2,a=>42), 'Wor42ld');
|
47 | |
is(__xn("Hel{a}lo", "Wor{a}ld", 2,a=>42), 'Wor42ld');
|
|
55 |
ol_is(__nx("Hel{a}lo", "Wor{a}ld", 1,a=>42), 'Hel42lo');
|
|
56 |
ol_is(__nx("Hel{a}lo", "Wor{a}ld", 0,a=>42), 'Wor42ld');
|
|
57 |
ol_is(__nx("Hel{a}lo", "Wor{a}ld", 2,a=>42), 'Wor42ld');
|
|
58 |
ol_is(__xn("Hel{a}lo", "Wor{a}ld", 2,a=>42), 'Wor42ld');
|
48 | 59 |
|
49 | 60 |
my $e1 = 1;
|
50 | |
is((__nx "one", "more", $e1++), "one");
|
51 | |
is((__nx "one", "more", $e1), "more");
|
|
61 |
ol_is((__nx "one", "more", $e1++), "one");
|
|
62 |
ol_is((__nx "one", "more", $e1), "more");
|
52 | 63 |
my @files = 'monkey';
|
53 | 64 |
my $nr_files = @files;
|
54 | |
is((__nx "one file", "{_count} files", $nr_files), 'one file');
|
55 | |
is((__nx "one file", "{_count} files", @files), 'one file');
|
|
65 |
ol_is((__nx "one file", "{_count} files", $nr_files), 'one file');
|
|
66 |
ol_is((__nx "one file", "{_count} files", @files), 'one file');
|
56 | 67 |
push @files, 'donkey';
|
57 | 68 |
$nr_files = @files;
|
58 | |
is((__nx "one file", "{_count} files", $nr_files), '2 files');
|
59 | |
is((__nx "one file", "{_count} files", @files), '2 files');
|
|
69 |
ol_is((__nx "one file", "{_count} files", $nr_files), '2 files');
|
|
70 |
ol_is((__nx "one file", "{_count} files", @files), '2 files');
|
60 | 71 |
|
61 | 72 |
my $f = N__"Hi";
|
62 | 73 |
ok(defined $f);
|
|
74 | 85 |
# Use _count directly
|
75 | 86 |
#
|
76 | 87 |
|
77 | |
is(__nx("single {_count}", "multi {_count}", 0), 'multi 0');
|
78 | |
is(__nx("single {_count}", "multi {_count}", 1), 'single 1');
|
79 | |
is(__nx("single {_count}", "multi {_count}", 2), 'multi 2');
|
|
88 |
ol_is(__nx("single {_count}", "multi {_count}", 0), 'multi 0');
|
|
89 |
ol_is(__nx("single {_count}", "multi {_count}", 1), 'single 1');
|
|
90 |
ol_is(__nx("single {_count}", "multi {_count}", 2), 'multi 2');
|
80 | 91 |
|
81 | 92 |
#
|
82 | 93 |
# Expand arrays
|
|
84 | 95 |
{
|
85 | 96 |
local $" = ', ';
|
86 | 97 |
my @one = 'rabbit';
|
87 | |
is((__x "files: {f}", f => \@files), "files: monkey, donkey");
|
88 | |
is((__xn "one file: {f}", "{_count} files: {f}", @files, f => \@files),
|
|
98 |
ol_is((__x "files: {f}", f => \@files), "files: monkey, donkey");
|
|
99 |
ol_is((__xn "one file: {f}", "{_count} files: {f}", @files, f => \@files),
|
89 | 100 |
"2 files: monkey, donkey");
|
90 | |
is((__x "files: {f}", f => \@one), "files: rabbit");
|
91 | |
is((__xn "one file: {f}", "{_count} files: {f}", @one, f => \@one),
|
|
101 |
ol_is((__x "files: {f}", f => \@one), "files: rabbit");
|
|
102 |
ol_is((__xn "one file: {f}", "{_count} files: {f}", @one, f => \@one),
|
92 | 103 |
"one file: rabbit");
|
93 | 104 |
}
|
94 | 105 |
|
|
99 | 110 |
my $s2 = __x "found {nr} files", nr => 5;
|
100 | 111 |
my $t2 = $s2->(nr => 3);
|
101 | 112 |
isa_ok($t2, 'Log::Report::Message');
|
102 | |
is($s2, 'found 5 files');
|
103 | |
is($t2, 'found 3 files');
|
|
113 |
ol_is($s2, 'found 5 files');
|
|
114 |
ol_is($t2, 'found 3 files');
|
104 | 115 |
|
105 | 116 |
# clone by overload
|
106 | 117 |
my $s = __x "A={a};B={b}", a=>11, b=>12;
|
107 | 118 |
isa_ok($s, 'Log::Report::Message');
|
108 | |
ok(reftype $s, 'HASH');
|
|
119 |
is(reftype $s, 'HASH');
|
109 | 120 |
is($s->toString, "A=11;B=12");
|
110 | 121 |
|
111 | 122 |
my $t = $s->(b=>13);
|
112 | 123 |
isa_ok($t, 'Log::Report::Message');
|
113 | |
ok(reftype $t, 'HASH');
|
|
124 |
is(reftype $t, 'HASH');
|
114 | 125 |
isnt("$s", "$t");
|
115 | 126 |
is($t->toString, "A=11;B=13");
|
116 | 127 |
is($s->toString, "A=11;B=12"); # unchanged
|
|
122 | 133 |
use constant PI => 4 * atan2(1, 1);
|
123 | 134 |
my $approx = 'approx pi: 3.141593';
|
124 | 135 |
is((sprintf "approx pi: %.6f", PI), $approx);
|
125 | |
is((__x "approx pi: {approx}", approx => sprintf("%.6f", PI)), $approx);
|
126 | |
is((__x "approx pi: {pi%.6f}", pi => PI), $approx);
|
|
136 |
ol_is((__x "approx pi: {approx}", approx => sprintf("%.6f", PI)), $approx);
|
|
137 |
ol_is((__x "approx pi: {pi%.6f}", pi => PI), $approx);
|
127 | 138 |
|
128 | |
is((__x "{perms} {links%2d} {user%-8s} {size%8d} {fn}"
|
|
139 |
ol_is((__x "{perms} {links%2d} {user%-8s} {size%8d} {fn}"
|
129 | 140 |
, perms => '-rw-r--r--', links => 1, user => 'superman'
|
130 | 141 |
, size => '1234567', fn => '/etc/profile')
|
131 | 142 |
, '-rw-r--r-- 1 superman 1234567 /etc/profile');
|