Move Test::Simple from lib to ext.
[p5sagit/p5-mst-13.2.git] / ext / Test-Simple / t / overload.t
CommitLineData
7483b81c 1#!/usr/bin/perl -w
30e302f8 2
3BEGIN {
4 if( $ENV{PERL_CORE} ) {
5 chdir 't';
6 @INC = ('../lib', 'lib');
7 }
8 else {
9 unshift @INC, 't/lib';
10 }
11}
30e302f8 12
7483b81c 13use strict;
3e887aae 14use Test::More tests => 19;
30e302f8 15
16
17package Overloaded;
18
19use overload
3e887aae 20 q{eq} => sub { $_[0]->{string} eq $_[1] },
21 q{==} => sub { $_[0]->{num} == $_[1] },
22 q{""} => sub { $_[0]->{stringify}++; $_[0]->{string} },
82d700dc 23 q{0+} => sub { $_[0]->{numify}++; $_[0]->{num} }
24;
30e302f8 25
26sub new {
27 my $class = shift;
82d700dc 28 bless {
29 string => shift,
30 num => shift,
31 stringify => 0,
32 numify => 0,
33 }, $class;
30e302f8 34}
35
36
37package main;
38
c00d8759 39local $SIG{__DIE__} = sub {
40 my($call_file, $call_line) = (caller)[1,2];
41 fail("SIGDIE accidentally called");
42 diag("From $call_file at $call_line");
43};
44
7483b81c 45my $obj = Overloaded->new('foo', 42);
46isa_ok $obj, 'Overloaded';
30e302f8 47
3e887aae 48cmp_ok $obj, 'eq', 'foo', 'cmp_ok() eq';
49is $obj->{stringify}, 0, ' does not stringify';
50is $obj, 'foo', 'is() with string overloading';
51cmp_ok $obj, '==', 42, 'cmp_ok() with number overloading';
52is $obj->{numify}, 0, ' does not numify';
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}
3e887aae 76
77
78# Make sure 0 isn't a special case. [rt.cpan.org 41109]
79{
80 my $obj = Overloaded->new('0', 42);
81 isa_ok $obj, 'Overloaded';
82
83 cmp_ok $obj, 'eq', '0', 'cmp_ok() eq';
84 is $obj->{stringify}, 0, ' does not stringify';
85 is $obj, '0', 'is() with string overloading';
86}