This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix unit tests for experimental::builtin warnings
[perl5.git] / lib / overloading.t
CommitLineData
e46c382e
YK
1#./perl
2
70cbce25 3use Test::More;
e46c382e 4
e678439a 5no warnings 'experimental::builtin';
ccc91139 6use builtin qw(refaddr);
e46c382e
YK
7
8{
9 package Stringifies;
10
11 use overload (
12 fallback => 1,
13 '""' => sub { "foo" },
14 '0+' => sub { 42 },
abb619ad 15 cos => sub { "far side of overload table" },
e46c382e
YK
16 );
17
18 sub new { bless {}, shift };
19}
20
21my $x = Stringifies->new;
a75c6ed6
FC
22my $y = qr//;
23my $ystr = "$y";
e46c382e
YK
24
25is( "$x", "foo", "stringifies" );
a75c6ed6 26is( "$y", $ystr, "stringifies qr//" );
e46c382e 27is( 0 + $x, 42, "numifies" );
abb619ad 28is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
29
30{
31 no overloading;
32 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 33 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 34 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 35 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
36
37 {
38 no overloading '""';
39 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 40 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 41 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 42 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
43 }
44}
45
46{
47 no overloading '""';
48
49 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 50 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 51 is( 0 + $x, 42, "numifies" );
abb619ad 52 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e 53
37c07a4b
FC
54 my $q = qr/abc/;
55 ok "abc" =~ $q, '=~ qr// with no "" overloading';
56 ok "abcd" =~ /${q}d/, '=~ /foo$qr/ with no "" overloading';
57 {
58 no overloading 'qr';
59 my $q = qr/abc/;
60 ok "abc" =~ $q, '=~ qr// with no "" or qr overloading';
61 ok "abcd" =~ /${q}d/, '=~ /foo$qr/ with no "" or qr overloading';
62 }
63
e46c382e
YK
64 {
65 no overloading;
66 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 67 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 68 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 69 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
70 }
71
72 use overloading '""';
73
74 is( "$x", "foo", "stringifies" );
a75c6ed6 75 is( "$y", $ystr, "stringifies qr//" );
e46c382e 76 is( 0 + $x, 42, "numifies" );
abb619ad 77 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
78
79 no overloading '0+';
80 is( "$x", "foo", "stringifies" );
a75c6ed6 81 is( "$y", $ystr, "stringifies qr//" );
e46c382e 82 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 83 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
84
85 {
86 no overloading '""';
87 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 88 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 89 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 90 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
91
92 {
93 use overloading;
94 is( "$x", "foo", "stringifies" );
a75c6ed6 95 is( "$y", $ystr, "stringifies qr//" );
e46c382e 96 is( 0 + $x, 42, "numifies" );
abb619ad 97 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
98 }
99 }
100
101 is( "$x", "foo", "stringifies" );
a75c6ed6 102 is( "$y", $ystr, "stringifies qr//" );
e46c382e 103 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 104 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e 105
abb619ad
RGS
106 no overloading "cos";
107 is( "$x", "foo", "stringifies" );
a75c6ed6 108 is( "$y", $ystr, "stringifies qr//" );
abb619ad
RGS
109 is( 0 + $x, refaddr($x), "no numification" );
110 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
111
112 BEGIN { ok(exists($^H{overloading}), "overloading hint present") }
113
114 use overloading;
115
116 BEGIN { ok(!exists($^H{overloading}), "overloading hint removed") }
117}
70cbce25
MB
118
119done_testing();