1#!perl -w
2
3BEGIN {
4    if( $ENV{PERL_CORE} ) {
5        chdir 't';
6        @INC = qw(../lib ../lib/Test/Simple/t/lib);
7    }
8}
9
10use lib 't/lib';
11use Test::More tests => 54;
12
13# Make sure we don't mess with $@ or $!.  Test at bottom.
14my $Err   = "this should not be touched";
15my $Errno = 42;
16$@ = $Err;
17$! = $Errno;
18
19use_ok('Dummy');
20is( $Dummy::VERSION, '0.01', 'use_ok() loads a module' );
21require_ok('Test::More');
22
23
24ok( 2 eq 2,             'two is two is two is two' );
25is(   "foo", "foo",       'foo is foo' );
26isnt( "foo", "bar",     'foo isnt bar');
27isn::t("foo", "bar",     'foo isn\'t bar');
28
29#'#
30like("fooble", '/^foo/',    'foo is like fooble');
31like("FooBle", '/foo/i',   'foo is like FooBle');
32like("/usr/local/pr0n/", '/^\/usr\/local/',   'regexes with slashes in like' );
33
34unlike("fbar", '/^bar/',    'unlike bar');
35unlike("FooBle", '/foo/',   'foo is unlike FooBle');
36unlike("/var/local/pr0n/", '/^\/usr\/local/','regexes with slashes in unlike' );
37
38my @foo = qw(foo bar baz);
39unlike(@foo, '/foo/');
40
41can_ok('Test::More', qw(require_ok use_ok ok is isnt like skip can_ok
42                        pass fail eq_array eq_hash eq_set));
43can_ok(bless({}, "Test::More"), qw(require_ok use_ok ok is isnt like skip
44                                   can_ok pass fail eq_array eq_hash eq_set));
45
46
47isa_ok(bless([], "Foo"), "Foo");
48isa_ok([], 'ARRAY');
49isa_ok(\42, 'SCALAR');
50{
51    local %Bar::;
52    local @Foo::ISA = 'Bar';
53    isa_ok( "Foo", "Bar" );
54}
55
56
57# can_ok() & isa_ok should call can() & isa() on the given object, not
58# just class, in case of custom can()
59{
60       local *Foo::can;
61       local *Foo::isa;
62       *Foo::can = sub { $_[0]->[0] };
63       *Foo::isa = sub { $_[0]->[0] };
64       my $foo = bless([0], 'Foo');
65       ok( ! $foo->can('bar') );
66       ok( ! $foo->isa('bar') );
67       $foo->[0] = 1;
68       can_ok( $foo, 'blah');
69       isa_ok( $foo, 'blah');
70}
71
72
73pass('pass() passed');
74
75ok( eq_array([qw(this that whatever)], [qw(this that whatever)]),
76    'eq_array with simple arrays' );
77is @Test::More::Data_Stack, 0, '@Data_Stack not holding onto things';
78
79ok( eq_hash({ foo => 42, bar => 23 }, {bar => 23, foo => 42}),
80    'eq_hash with simple hashes' );
81is @Test::More::Data_Stack, 0;
82
83ok( eq_set([qw(this that whatever)], [qw(that whatever this)]),
84    'eq_set with simple sets' );
85is @Test::More::Data_Stack, 0;
86
87my @complex_array1 = (
88                      [qw(this that whatever)],
89                      {foo => 23, bar => 42},
90                      "moo",
91                      "yarrow",
92                      [qw(498 10 29)],
93                     );
94my @complex_array2 = (
95                      [qw(this that whatever)],
96                      {foo => 23, bar => 42},
97                      "moo",
98                      "yarrow",
99                      [qw(498 10 29)],
100                     );
101
102is_deeply( \@complex_array1, \@complex_array2,    'is_deeply with arrays' );
103ok( eq_array(\@complex_array1, \@complex_array2),
104    'eq_array with complicated arrays' );
105ok( eq_set(\@complex_array1, \@complex_array2),
106    'eq_set with complicated arrays' );
107
108my @array1 = (qw(this that whatever),
109              {foo => 23, bar => 42} );
110my @array2 = (qw(this that whatever),
111              {foo => 24, bar => 42} );
112
113ok( !eq_array(\@array1, \@array2),
114    'eq_array with slightly different complicated arrays' );
115is @Test::More::Data_Stack, 0;
116
117ok( !eq_set(\@array1, \@array2),
118    'eq_set with slightly different complicated arrays' );
119is @Test::More::Data_Stack, 0;
120
121my %hash1 = ( foo => 23,
122              bar => [qw(this that whatever)],
123              har => { foo => 24, bar => 42 },
124            );
125my %hash2 = ( foo => 23,
126              bar => [qw(this that whatever)],
127              har => { foo => 24, bar => 42 },
128            );
129
130is_deeply( \%hash1, \%hash2,    'is_deeply with complicated hashes' );
131ok( eq_hash(\%hash1, \%hash2),  'eq_hash with complicated hashes');
132
133%hash1 = ( foo => 23,
134           bar => [qw(this that whatever)],
135           har => { foo => 24, bar => 42 },
136         );
137%hash2 = ( foo => 23,
138           bar => [qw(this tha whatever)],
139           har => { foo => 24, bar => 42 },
140         );
141
142ok( !eq_hash(\%hash1, \%hash2),
143    'eq_hash with slightly different complicated hashes' );
144is @Test::More::Data_Stack, 0;
145
146is( Test::Builder->new, Test::More->builder,    'builder()' );
147
148
149cmp_ok(42, '==', 42,        'cmp_ok ==');
150cmp_ok('foo', 'eq', 'foo',  '       eq');
151cmp_ok(42.5, '<', 42.6,     '       <');
152cmp_ok(0, '||', 1,          '       ||');
153
154
155# Piers pointed out sometimes people override isa().
156{
157    package Wibble;
158    sub isa {
159        my($self, $class) = @_;
160        return 1 if $class eq 'Wibblemeister';
161    }
162    sub new { bless {} }
163}
164isa_ok( Wibble->new, 'Wibblemeister' );
165
166my $sub = sub {};
167is_deeply( $sub, $sub, 'the same function ref' );
168
169use Symbol;
170my $glob = gensym;
171is_deeply( $glob, $glob, 'the same glob' );
172
173is_deeply( { foo => $sub, bar => [1, $glob] },
174           { foo => $sub, bar => [1, $glob] }
175         );
176
177
178# rt.cpan.org 53469  is_deeply with regexes
179is_deeply( qr/a/, qr/a/, "same regex" );
180
181
182# These two tests must remain at the end.
183is( $@, $Err,               '$@ untouched' );
184cmp_ok( $!, '==', $Errno,   '$! untouched' );
185