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