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