Move Test::Simple from lib to ext.
[p5sagit/p5-mst-13.2.git] / ext / Test-Simple / t / More.t
CommitLineData
33459055 1#!perl -w
2
3BEGIN {
a9153838 4 if( $ENV{PERL_CORE} ) {
5 chdir 't';
da0c1bbe 6 @INC = qw(../lib ../lib/Test/Simple/t/lib);
a9153838 7 }
33459055 8}
9
6b38a9b9 10use lib 't/lib';
3e887aae 11use Test::More tests => 53;
a9153838 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;
3f2ec160 18
6b38a9b9 19use_ok('Dummy');
ea2e58b9 20is( $Dummy::VERSION, '0.01', 'use_ok() loads a module' );
3f2ec160 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');
d020a79a 32like("/usr/local/pr0n/", '/^\/usr\/local/', 'regexes with slashes in like' );
33
a9153838 34unlike("fbar", '/^bar/', 'unlike bar');
35unlike("FooBle", '/foo/', 'foo is unlike FooBle');
36unlike("/var/local/pr0n/", '/^\/usr\/local/','regexes with slashes in unlike' );
37
30e302f8 38my @foo = qw(foo bar baz);
39unlike(@foo, '/foo/');
40
d020a79a 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
89c1e84a 46
d020a79a 47isa_ok(bless([], "Foo"), "Foo");
a9153838 48isa_ok([], 'ARRAY');
49isa_ok(\42, 'SCALAR');
3e887aae 50{
51 local %Bar::;
52 local @Foo::ISA = 'Bar';
53 isa_ok( "Foo", "Bar" );
54}
d020a79a 55
3f2ec160 56
89c1e84a 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
3f2ec160 73pass('pass() passed');
74
75ok( eq_array([qw(this that whatever)], [qw(this that whatever)]),
76 'eq_array with simple arrays' );
7483b81c 77is @Test::More::Data_Stack, 0, '@Data_Stack not holding onto things';
78
3f2ec160 79ok( eq_hash({ foo => 42, bar => 23 }, {bar => 23, foo => 42}),
80 'eq_hash with simple hashes' );
7483b81c 81is @Test::More::Data_Stack, 0;
82
3f2ec160 83ok( eq_set([qw(this that whatever)], [qw(that whatever this)]),
84 'eq_set with simple sets' );
7483b81c 85is @Test::More::Data_Stack, 0;
3f2ec160 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
33459055 102is_deeply( \@complex_array1, \@complex_array2, 'is_deeply with arrays' );
3f2ec160 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' );
7483b81c 115is @Test::More::Data_Stack, 0;
116
3f2ec160 117ok( !eq_set(\@array1, \@array2),
118 'eq_set with slightly different complicated arrays' );
7483b81c 119is @Test::More::Data_Stack, 0;
3f2ec160 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
33459055 130is_deeply( \%hash1, \%hash2, 'is_deeply with complicated hashes' );
131ok( eq_hash(\%hash1, \%hash2), 'eq_hash with complicated hashes');
3f2ec160 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' );
7483b81c 144is @Test::More::Data_Stack, 0;
a9153838 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
845d7e37 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 );
a9153838 176
177# These two tests must remain at the end.
178is( $@, $Err, '$@ untouched' );
179cmp_ok( $!, '==', $Errno, '$! untouched' );