This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move a test from t/lib/warnings/sv to .../9uninit
[perl5.git] / lib / overloading.t
CommitLineData
e46c382e
YK
1#./perl
2
bd3fc147 3use Test::More tests => 35;
e46c382e
YK
4
5use Scalar::Util qw(refaddr);
6
7{
8 package Stringifies;
9
10 use overload (
11 fallback => 1,
12 '""' => sub { "foo" },
13 '0+' => sub { 42 },
abb619ad 14 cos => sub { "far side of overload table" },
e46c382e
YK
15 );
16
17 sub new { bless {}, shift };
18}
19
20my $x = Stringifies->new;
21
22is( "$x", "foo", "stringifies" );
23is( 0 + $x, 42, "numifies" );
abb619ad 24is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
25
26{
27 no overloading;
28 is( "$x", overload::StrVal($x), "no stringification" );
29 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 30 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
31
32 {
33 no overloading '""';
34 is( "$x", overload::StrVal($x), "no stringification" );
35 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 36 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
37 }
38}
39
40{
41 no overloading '""';
42
43 is( "$x", overload::StrVal($x), "no stringification" );
44 is( 0 + $x, 42, "numifies" );
abb619ad 45 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
46
47 {
48 no overloading;
49 is( "$x", overload::StrVal($x), "no stringification" );
50 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 51 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
52 }
53
54 use overloading '""';
55
56 is( "$x", "foo", "stringifies" );
57 is( 0 + $x, 42, "numifies" );
abb619ad 58 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
59
60 no overloading '0+';
61 is( "$x", "foo", "stringifies" );
62 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 63 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
64
65 {
66 no overloading '""';
67 is( "$x", overload::StrVal($x), "no stringification" );
68 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 69 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
70
71 {
72 use overloading;
73 is( "$x", "foo", "stringifies" );
74 is( 0 + $x, 42, "numifies" );
abb619ad 75 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
76 }
77 }
78
79 is( "$x", "foo", "stringifies" );
80 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 81 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e 82
abb619ad
RGS
83 no overloading "cos";
84 is( "$x", "foo", "stringifies" );
85 is( 0 + $x, refaddr($x), "no numification" );
86 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
87
88 BEGIN { ok(exists($^H{overloading}), "overloading hint present") }
89
90 use overloading;
91
92 BEGIN { ok(!exists($^H{overloading}), "overloading hint removed") }
93}