X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Foverload.t;h=a30a53b1eb45ecf59c821b997371ae314ae8ca12;hb=53273a086103cdbbf7ebdd5f1a18b2c0777cbc1b;hp=e21e60b69e9078c79e6b13df20430a83f37749ff;hpb=78cd8b71ec0d7665c422181192afa2819a5d887d;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/overload.t b/lib/overload.t index e21e60b..a30a53b 100644 --- a/lib/overload.t +++ b/lib/overload.t @@ -4,7 +4,7 @@ BEGIN { chdir 't' if -d 't'; @INC = '../lib'; require Config; - if (($Config::Config{'extensions'} !~ /\bList::Util\b/) ){ + if (($Config::Config{'extensions'} !~ m!\bList/Util\b!) ){ print "1..0 # Skip -- Perl configured without List::Util module\n"; exit 0; } @@ -57,16 +57,20 @@ sub test { $comment = " # " . $_ [2] if @_ > 2; if ($_[0] eq $_[1]) { print "ok $test$comment\n"; + return 1; } else { $comment .= ": '$_[0]' ne '$_[1]'"; print "not ok $test$comment\n"; + return 0; } } else { if (shift) { print "ok $test\n"; + return 1; } else { print "not ok $test\n"; - } + return 0; + } } } @@ -1093,8 +1097,6 @@ test ($a->xet('b'), 42); test (!defined eval { $a->{b} }); test ($@ =~ /zap/); -test (overload::StrVal(qr/a/) =~ /^Regexp=SCALAR\(0x[0-9a-f]+\)$/); - { package t229; use overload '=' => sub { 42 }, @@ -1123,5 +1125,82 @@ test (overload::StrVal(qr/a/) =~ /^Regexp=SCALAR\(0x[0-9a-f]+\)$/); test($out2, 17, "#24313"); # 232 } +{ + package Numify; + use overload (qw(0+ numify fallback 1)); + + sub new { + my $val = $_[1]; + bless \$val, $_[0]; + } + + sub numify { ${$_[0]} } +} + +{ + package perl31793; + use overload cmp => sub { 0 }; + package perl31793_fb; + use overload cmp => sub { 0 }, fallback => 1; + package main; + my $o = bless [], 'perl31793'; + my $of = bless [], 'perl31793_fb'; + my $no = bless [], 'no_overload'; + test (overload::StrVal(\"scalar") =~ /^SCALAR\(0x[0-9a-f]+\)$/); + test (overload::StrVal([]) =~ /^ARRAY\(0x[0-9a-f]+\)$/); + test (overload::StrVal({}) =~ /^HASH\(0x[0-9a-f]+\)$/); + test (overload::StrVal(sub{1}) =~ /^CODE\(0x[0-9a-f]+\)$/); + test (overload::StrVal(\*GLOB) =~ /^GLOB\(0x[0-9a-f]+\)$/); + test (overload::StrVal(\$o) =~ /^REF\(0x[0-9a-f]+\)$/); + test (overload::StrVal(qr/a/) =~ /^Regexp=SCALAR\(0x[0-9a-f]+\)$/); + test (overload::StrVal($o) =~ /^perl31793=ARRAY\(0x[0-9a-f]+\)$/); + test (overload::StrVal($of) =~ /^perl31793_fb=ARRAY\(0x[0-9a-f]+\)$/); + test (overload::StrVal($no) =~ /^no_overload=ARRAY\(0x[0-9a-f]+\)$/); +} + +# These are all check that overloaded values rather than reference addressess +# are what is getting tested. +my ($two, $one, $un, $deux) = map {new Numify $_} 2, 1, 1, 2; +my ($ein, $zwei) = (1, 2); + +my %map = (one => 1, un => 1, ein => 1, deux => 2, two => 2, zwei => 2); +foreach my $op (qw(<=> == != < <= > >=)) { + foreach my $l (keys %map) { + foreach my $r (keys %map) { + my $ocode = "\$$l $op \$$r"; + my $rcode = "$map{$l} $op $map{$r}"; + + my $got = eval $ocode; + die if $@; + my $expect = eval $rcode; + die if $@; + test ($got, $expect, $ocode) or print "# $rcode\n"; + } + } +} +{ + # check that overloading works in regexes + { + package Foo493; + use overload + '""' => sub { "^$_[0][0]\$" }, + '.' => sub { + bless [ + $_[2] + ? (ref $_[1] ? $_[1][0] : $_[1]) . ':' .$_[0][0] + : $_[0][0] . ':' . (ref $_[1] ? $_[1][0] : $_[1]) + ], 'Foo493' + }; + } + + my $a = bless [ "a" ], 'Foo493'; + test('a' =~ /$a/); + test('x:a' =~ /x$a/); + test('x:a:=' =~ /x$a=$/); + test('x:a:a:=' =~ /x$a$a=$/); + +} + + # Last test is: -sub last {232} +sub last {497}