Upgrade to Test-Simple-0.82.
[p5sagit/p5-mst-13.2.git] / lib / Test / Simple / t / overload.t
1 #!/usr/bin/perl -w
2 # $Id: /mirror/googlecode/test-more/t/overload.t 57943 2008-08-18T02:09:22.275428Z brooklyn.kid51  $
3
4 BEGIN {
5     if( $ENV{PERL_CORE} ) {
6         chdir 't';
7         @INC = ('../lib', 'lib');
8     }
9     else {
10         unshift @INC, 't/lib';
11     }
12 }
13
14 use strict;
15 use Test::More;
16
17 BEGIN {
18     if( !eval "require overload" ) {
19         plan skip_all => "needs overload.pm";
20     }
21     else {
22         plan tests => 13;
23     }
24 }
25
26
27 package Overloaded;
28
29 use overload
30         q{""}    => sub { $_[0]->{string} },
31         q{0+}    => sub { $_[0]->{num} };
32
33 sub new {
34     my $class = shift;
35     bless { string => shift, num => shift }, $class;
36 }
37
38
39 package main;
40
41 local $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
47 my $obj = Overloaded->new('foo', 42);
48 isa_ok $obj, 'Overloaded';
49
50 is $obj, 'foo',            'is() with string overloading';
51 cmp_ok $obj, 'eq', 'foo',  'cmp_ok() ...';
52 cmp_ok $obj, '==', 42,     'cmp_ok() with number overloading';
53
54 is_deeply [$obj], ['foo'],                 'is_deeply with string overloading';
55 ok eq_array([$obj], ['foo']),              'eq_array ...';
56 ok eq_hash({foo => $obj}, {foo => 'foo'}), 'eq_hash ...';
57
58 # rt.cpan.org 13506
59 is_deeply $obj, 'foo',        'is_deeply with string overloading at the top';
60
61 Test::More->builder->is_num($obj, 42);
62 Test::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 }