Clear up test based on line number differences between the core and the
[p5sagit/p5-mst-13.2.git] / lib / 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;
30e302f8 14use Test::More;
15
16BEGIN {
17 if( !eval "require overload" ) {
18 plan skip_all => "needs overload.pm";
19 }
20 else {
b1ddf169 21 plan tests => 13;
30e302f8 22 }
23}
24
25
26package Overloaded;
27
28use overload
7483b81c 29 q{""} => sub { $_[0]->{string} },
b1ddf169 30 q{0+} => sub { $_[0]->{num} };
30e302f8 31
32sub new {
33 my $class = shift;
7483b81c 34 bless { string => shift, num => shift }, $class;
30e302f8 35}
36
37
38package main;
39
7483b81c 40my $obj = Overloaded->new('foo', 42);
41isa_ok $obj, 'Overloaded';
30e302f8 42
7483b81c 43is $obj, 'foo', 'is() with string overloading';
44cmp_ok $obj, 'eq', 'foo', 'cmp_ok() ...';
b1ddf169 45cmp_ok $obj, '==', 42, 'cmp_ok() with number overloading';
30e302f8 46
7483b81c 47is_deeply [$obj], ['foo'], 'is_deeply with string overloading';
48ok eq_array([$obj], ['foo']), 'eq_array ...';
49ok eq_hash({foo => $obj}, {foo => 'foo'}), 'eq_hash ...';
b1ddf169 50
51# rt.cpan.org 13506
52is_deeply $obj, 'foo', 'is_deeply with string overloading at the top';
53
54Test::More->builder->is_num($obj, 42);
55Test::More->builder->is_eq ($obj, "foo");
56
57
58{
59 # rt.cpan.org 14675
60 package TestPackage;
61 use overload q{""} => sub { ::fail("This should not be called") };
62
63 package Foo;
64 ::is_deeply(['TestPackage'], ['TestPackage']);
65 ::is_deeply({'TestPackage' => 'TestPackage'},
66 {'TestPackage' => 'TestPackage'});
67 ::is_deeply('TestPackage', 'TestPackage');
68}