Upgrade to Test-Simple-0.82.
[p5sagit/p5-mst-13.2.git] / lib / Test / Simple / t / overload.t
CommitLineData
7483b81c 1#!/usr/bin/perl -w
ccbd73a4 2# $Id: /mirror/googlecode/test-more/t/overload.t 57943 2008-08-18T02:09:22.275428Z brooklyn.kid51 $
30e302f8 3
4BEGIN {
5 if( $ENV{PERL_CORE} ) {
6 chdir 't';
7 @INC = ('../lib', 'lib');
8 }
9 else {
10 unshift @INC, 't/lib';
11 }
12}
30e302f8 13
7483b81c 14use strict;
30e302f8 15use Test::More;
16
17BEGIN {
18 if( !eval "require overload" ) {
19 plan skip_all => "needs overload.pm";
20 }
21 else {
b1ddf169 22 plan tests => 13;
30e302f8 23 }
24}
25
26
27package Overloaded;
28
29use overload
7483b81c 30 q{""} => sub { $_[0]->{string} },
b1ddf169 31 q{0+} => sub { $_[0]->{num} };
30e302f8 32
33sub new {
34 my $class = shift;
7483b81c 35 bless { string => shift, num => shift }, $class;
30e302f8 36}
37
38
39package main;
40
c00d8759 41local $SIG{__DIE__} = sub {
42 my($call_file, $call_line) = (caller)[1,2];
43 fail("SIGDIE accidentally called");
44 diag("From $call_file at $call_line");
45};
46
7483b81c 47my $obj = Overloaded->new('foo', 42);
48isa_ok $obj, 'Overloaded';
30e302f8 49
7483b81c 50is $obj, 'foo', 'is() with string overloading';
51cmp_ok $obj, 'eq', 'foo', 'cmp_ok() ...';
b1ddf169 52cmp_ok $obj, '==', 42, 'cmp_ok() with number overloading';
30e302f8 53
7483b81c 54is_deeply [$obj], ['foo'], 'is_deeply with string overloading';
55ok eq_array([$obj], ['foo']), 'eq_array ...';
56ok eq_hash({foo => $obj}, {foo => 'foo'}), 'eq_hash ...';
b1ddf169 57
58# rt.cpan.org 13506
59is_deeply $obj, 'foo', 'is_deeply with string overloading at the top';
60
61Test::More->builder->is_num($obj, 42);
62Test::More->builder->is_eq ($obj, "foo");
63
64
65{
66 # rt.cpan.org 14675
67 package TestPackage;
68 use overload q{""} => sub { ::fail("This should not be called") };
69
70 package Foo;
71 ::is_deeply(['TestPackage'], ['TestPackage']);
72 ::is_deeply({'TestPackage' => 'TestPackage'},
73 {'TestPackage' => 'TestPackage'});
74 ::is_deeply('TestPackage', 'TestPackage');
75}