xref: /openbsd/gnu/usr.bin/perl/lib/overloading.t (revision 256a93a4)
143003dfeSmillert#./perl
243003dfeSmillert
3*256a93a4Safresh1use Test::More;
443003dfeSmillert
5*256a93a4Safresh1no warnings 'experimental::builtin';
6*256a93a4Safresh1use builtin qw(refaddr);
743003dfeSmillert
843003dfeSmillert{
943003dfeSmillert    package Stringifies;
1043003dfeSmillert
1143003dfeSmillert    use overload (
1243003dfeSmillert	fallback => 1,
1343003dfeSmillert	'""' => sub { "foo" },
1443003dfeSmillert	'0+' => sub { 42 },
1543003dfeSmillert	cos => sub { "far side of overload table" },
1643003dfeSmillert    );
1743003dfeSmillert
1843003dfeSmillert    sub new { bless {}, shift };
1943003dfeSmillert}
2043003dfeSmillert
2143003dfeSmillertmy $x = Stringifies->new;
22898184e3Ssthenmy $y = qr//;
23898184e3Ssthenmy $ystr = "$y";
2443003dfeSmillert
2543003dfeSmillertis( "$x", "foo", "stringifies" );
26898184e3Ssthenis( "$y", $ystr, "stringifies qr//" );
2743003dfeSmillertis( 0 + $x, 42, "numifies" );
2843003dfeSmillertis( cos($x), "far side of overload table", "cosinusfies" );
2943003dfeSmillert
3043003dfeSmillert{
3143003dfeSmillert    no overloading;
3243003dfeSmillert    is( "$x", overload::StrVal($x), "no stringification" );
33898184e3Ssthen    is( "$y", overload::StrVal($y), "no stringification of qr//" );
3443003dfeSmillert    is( 0 + $x, refaddr($x), "no numification" );
3543003dfeSmillert    is( cos($x), cos(refaddr($x)), "no cosinusfication" );
3643003dfeSmillert
3743003dfeSmillert    {
3843003dfeSmillert	no overloading '""';
3943003dfeSmillert	is( "$x", overload::StrVal($x), "no stringification" );
40898184e3Ssthen	is( "$y", overload::StrVal($y), "no stringification of qr//" );
4143003dfeSmillert	is( 0 + $x, refaddr($x), "no numification" );
4243003dfeSmillert	is( cos($x), cos(refaddr($x)), "no cosinusfication" );
4343003dfeSmillert    }
4443003dfeSmillert}
4543003dfeSmillert
4643003dfeSmillert{
4743003dfeSmillert    no overloading '""';
4843003dfeSmillert
4943003dfeSmillert    is( "$x", overload::StrVal($x), "no stringification" );
50898184e3Ssthen    is( "$y", overload::StrVal($y), "no stringification of qr//" );
5143003dfeSmillert    is( 0 + $x, 42, "numifies" );
5243003dfeSmillert    is( cos($x), "far side of overload table", "cosinusfies" );
5343003dfeSmillert
54898184e3Ssthen    my $q = qr/abc/;
55898184e3Ssthen    ok "abc" =~ $q, '=~ qr// with no "" overloading';
56898184e3Ssthen    ok "abcd" =~ /${q}d/, '=~ /foo$qr/ with no "" overloading';
57898184e3Ssthen    {
58898184e3Ssthen	no overloading 'qr';
59898184e3Ssthen	my $q = qr/abc/;
60898184e3Ssthen	ok "abc" =~ $q, '=~ qr// with no "" or qr overloading';
61898184e3Ssthen	ok "abcd" =~ /${q}d/, '=~ /foo$qr/ with no "" or qr overloading';
62898184e3Ssthen    }
63898184e3Ssthen
6443003dfeSmillert    {
6543003dfeSmillert	no overloading;
6643003dfeSmillert	is( "$x", overload::StrVal($x), "no stringification" );
67898184e3Ssthen	is( "$y", overload::StrVal($y), "no stringification of qr//" );
6843003dfeSmillert	is( 0 + $x, refaddr($x), "no numification" );
6943003dfeSmillert	is( cos($x), cos(refaddr($x)), "no cosinusfication" );
7043003dfeSmillert    }
7143003dfeSmillert
7243003dfeSmillert    use overloading '""';
7343003dfeSmillert
7443003dfeSmillert    is( "$x", "foo", "stringifies" );
75898184e3Ssthen    is( "$y", $ystr, "stringifies qr//" );
7643003dfeSmillert    is( 0 + $x, 42, "numifies" );
7743003dfeSmillert    is( cos($x), "far side of overload table", "cosinusfies" );
7843003dfeSmillert
7943003dfeSmillert    no overloading '0+';
8043003dfeSmillert    is( "$x", "foo", "stringifies" );
81898184e3Ssthen    is( "$y", $ystr, "stringifies qr//" );
8243003dfeSmillert    is( 0 + $x, refaddr($x), "no numification" );
8343003dfeSmillert    is( cos($x), "far side of overload table", "cosinusfies" );
8443003dfeSmillert
8543003dfeSmillert    {
8643003dfeSmillert	no overloading '""';
8743003dfeSmillert	is( "$x", overload::StrVal($x), "no stringification" );
88898184e3Ssthen	is( "$y", overload::StrVal($y), "no stringification of qr//" );
8943003dfeSmillert	is( 0 + $x, refaddr($x), "no numification" );
9043003dfeSmillert	is( cos($x), "far side of overload table", "cosinusfies" );
9143003dfeSmillert
9243003dfeSmillert	{
9343003dfeSmillert	    use overloading;
9443003dfeSmillert	    is( "$x", "foo", "stringifies" );
95898184e3Ssthen	    is( "$y", $ystr, "stringifies qr//" );
9643003dfeSmillert	    is( 0 + $x, 42, "numifies" );
9743003dfeSmillert	    is( cos($x), "far side of overload table", "cosinusfies" );
9843003dfeSmillert	}
9943003dfeSmillert    }
10043003dfeSmillert
10143003dfeSmillert    is( "$x", "foo", "stringifies" );
102898184e3Ssthen    is( "$y", $ystr, "stringifies qr//" );
10343003dfeSmillert    is( 0 + $x, refaddr($x), "no numification" );
10443003dfeSmillert    is( cos($x), "far side of overload table", "cosinusfies" );
10543003dfeSmillert
10643003dfeSmillert    no overloading "cos";
10743003dfeSmillert    is( "$x", "foo", "stringifies" );
108898184e3Ssthen    is( "$y", $ystr, "stringifies qr//" );
10943003dfeSmillert    is( 0 + $x, refaddr($x), "no numification" );
11043003dfeSmillert    is( cos($x), cos(refaddr($x)), "no cosinusfication" );
11143003dfeSmillert
11243003dfeSmillert    BEGIN { ok(exists($^H{overloading}), "overloading hint present") }
11343003dfeSmillert
11443003dfeSmillert    use overloading;
11543003dfeSmillert
11643003dfeSmillert    BEGIN { ok(!exists($^H{overloading}), "overloading hint removed") }
11743003dfeSmillert}
118*256a93a4Safresh1
119*256a93a4Safresh1done_testing();
120