This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #108780] Make ‘no overloading’ work with qr//
[perl5.git] / lib / overloading.t
CommitLineData
e46c382e
YK
1#./perl
2
a75c6ed6 3use Test::More tests => 46;
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;
a75c6ed6
FC
21my $y = qr//;
22my $ystr = "$y";
e46c382e
YK
23
24is( "$x", "foo", "stringifies" );
a75c6ed6 25is( "$y", $ystr, "stringifies qr//" );
e46c382e 26is( 0 + $x, 42, "numifies" );
abb619ad 27is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
28
29{
30 no overloading;
31 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 32 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 33 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 34 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
35
36 {
37 no overloading '""';
38 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 39 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 40 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 41 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
42 }
43}
44
45{
46 no overloading '""';
47
48 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 49 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 50 is( 0 + $x, 42, "numifies" );
abb619ad 51 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
52
53 {
54 no overloading;
55 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 56 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 57 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 58 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
59 }
60
61 use overloading '""';
62
63 is( "$x", "foo", "stringifies" );
a75c6ed6 64 is( "$y", $ystr, "stringifies qr//" );
e46c382e 65 is( 0 + $x, 42, "numifies" );
abb619ad 66 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
67
68 no overloading '0+';
69 is( "$x", "foo", "stringifies" );
a75c6ed6 70 is( "$y", $ystr, "stringifies qr//" );
e46c382e 71 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 72 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
73
74 {
75 no overloading '""';
76 is( "$x", overload::StrVal($x), "no stringification" );
a75c6ed6 77 is( "$y", overload::StrVal($y), "no stringification of qr//" );
e46c382e 78 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 79 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
80
81 {
82 use overloading;
83 is( "$x", "foo", "stringifies" );
a75c6ed6 84 is( "$y", $ystr, "stringifies qr//" );
e46c382e 85 is( 0 + $x, 42, "numifies" );
abb619ad 86 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
87 }
88 }
89
90 is( "$x", "foo", "stringifies" );
a75c6ed6 91 is( "$y", $ystr, "stringifies qr//" );
e46c382e 92 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 93 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e 94
abb619ad
RGS
95 no overloading "cos";
96 is( "$x", "foo", "stringifies" );
a75c6ed6 97 is( "$y", $ystr, "stringifies qr//" );
abb619ad
RGS
98 is( 0 + $x, refaddr($x), "no numification" );
99 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
100
101 BEGIN { ok(exists($^H{overloading}), "overloading hint present") }
102
103 use overloading;
104
105 BEGIN { ok(!exists($^H{overloading}), "overloading hint removed") }
106}