xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/t/Legacy/More.t (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
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 => 57;
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');
27{
28    use warnings;
29    my $warning;
30    local $SIG{__WARN__}= sub { $warning = $_[0] };
31    isn::t("foo", "bar",     'foo isn\'t bar');
32    is($warning, "Use of apostrophe as package separator was deprecated in Perl 5.37.9,\n"
33               . "and will be removed in Perl 5.42.0.  You should change code that uses\n"
34               . "Test::More::isn't() to use Test::More::isnt() as a replacement"
35               . " at t/Legacy/More.t line 31\n",
36            "Got expected warning from isn::t() under use warnings");
37}
38{
39    no warnings "deprecated";
40    my $warning;
41    local $SIG{__WARN__}= sub { $warning = $_[0] };
42    isn::t("foo", "bar",     'foo isn\'t bar');
43    is($warning, undef, "No warnings from isn::t() under no warnings deprecated");
44}
45
46#'#
47like("fooble", '/^foo/',    'foo is like fooble');
48like("FooBle", '/foo/i',   'foo is like FooBle');
49like("/usr/local/pr0n/", '/^\/usr\/local/',   'regexes with slashes in like' );
50
51unlike("fbar", '/^bar/',    'unlike bar');
52unlike("FooBle", '/foo/',   'foo is unlike FooBle');
53unlike("/var/local/pr0n/", '/^\/usr\/local/','regexes with slashes in unlike' );
54
55my @foo = qw(foo bar baz);
56unlike(@foo, '/foo/');
57
58can_ok('Test::More', qw(require_ok use_ok ok is isnt like skip can_ok
59                        pass fail eq_array eq_hash eq_set));
60can_ok(bless({}, "Test::More"), qw(require_ok use_ok ok is isnt like skip
61                                   can_ok pass fail eq_array eq_hash eq_set));
62
63
64isa_ok(bless([], "Foo"), "Foo");
65isa_ok([], 'ARRAY');
66isa_ok(\42, 'SCALAR');
67{
68    local %Bar::;
69    local @Foo::ISA = 'Bar';
70    isa_ok( "Foo", "Bar" );
71}
72
73
74# can_ok() & isa_ok should call can() & isa() on the given object, not
75# just class, in case of custom can()
76{
77       local *Foo::can;
78       local *Foo::isa;
79       *Foo::can = sub { $_[0]->[0] };
80       *Foo::isa = sub { $_[0]->[0] };
81       my $foo = bless([0], 'Foo');
82       ok( ! $foo->can('bar') );
83       ok( ! $foo->isa('bar') );
84       $foo->[0] = 1;
85       can_ok( $foo, 'blah');
86       isa_ok( $foo, 'blah');
87}
88
89
90pass('pass() passed');
91
92ok( eq_array([qw(this that whatever)], [qw(this that whatever)]),
93    'eq_array with simple arrays' );
94is @Test::More::Data_Stack, 0, '@Data_Stack not holding onto things';
95
96ok( eq_hash({ foo => 42, bar => 23 }, {bar => 23, foo => 42}),
97    'eq_hash with simple hashes' );
98is @Test::More::Data_Stack, 0;
99
100ok( eq_set([qw(this that whatever)], [qw(that whatever this)]),
101    'eq_set with simple sets' );
102is @Test::More::Data_Stack, 0;
103
104my @complex_array1 = (
105                      [qw(this that whatever)],
106                      {foo => 23, bar => 42},
107                      "moo",
108                      "yarrow",
109                      [qw(498 10 29)],
110                     );
111my @complex_array2 = (
112                      [qw(this that whatever)],
113                      {foo => 23, bar => 42},
114                      "moo",
115                      "yarrow",
116                      [qw(498 10 29)],
117                     );
118
119is_deeply( \@complex_array1, \@complex_array2,    'is_deeply with arrays' );
120ok( eq_array(\@complex_array1, \@complex_array2),
121    'eq_array with complicated arrays' );
122ok( eq_set(\@complex_array1, \@complex_array2),
123    'eq_set with complicated arrays' );
124
125my @array1 = (qw(this that whatever),
126              {foo => 23, bar => 42} );
127my @array2 = (qw(this that whatever),
128              {foo => 24, bar => 42} );
129
130ok( !eq_array(\@array1, \@array2),
131    'eq_array with slightly different complicated arrays' );
132is @Test::More::Data_Stack, 0;
133
134ok( !eq_set(\@array1, \@array2),
135    'eq_set with slightly different complicated arrays' );
136is @Test::More::Data_Stack, 0;
137
138my %hash1 = ( foo => 23,
139              bar => [qw(this that whatever)],
140              har => { foo => 24, bar => 42 },
141            );
142my %hash2 = ( foo => 23,
143              bar => [qw(this that whatever)],
144              har => { foo => 24, bar => 42 },
145            );
146
147is_deeply( \%hash1, \%hash2,    'is_deeply with complicated hashes' );
148ok( eq_hash(\%hash1, \%hash2),  'eq_hash with complicated hashes');
149
150%hash1 = ( foo => 23,
151           bar => [qw(this that whatever)],
152           har => { foo => 24, bar => 42 },
153         );
154%hash2 = ( foo => 23,
155           bar => [qw(this tha whatever)],
156           har => { foo => 24, bar => 42 },
157         );
158
159ok( !eq_hash(\%hash1, \%hash2),
160    'eq_hash with slightly different complicated hashes' );
161is @Test::More::Data_Stack, 0;
162
163is( Test::Builder->new, Test::More->builder,    'builder()' );
164
165
166cmp_ok(42, '==', 42,        'cmp_ok ==');
167cmp_ok('foo', 'eq', 'foo',  '       eq');
168cmp_ok(42.5, '<', 42.6,     '       <');
169cmp_ok(0, '||', 1,          '       ||');
170
171
172# Piers pointed out sometimes people override isa().
173{
174    package Wibble;
175    sub isa {
176        my($self, $class) = @_;
177        return 1 if $class eq 'Wibblemeister';
178    }
179    sub new { bless {} }
180}
181isa_ok( Wibble->new, 'Wibblemeister' );
182
183my $sub = sub {};
184is_deeply( $sub, $sub, 'the same function ref' );
185
186use Symbol;
187my $glob = gensym;
188is_deeply( $glob, $glob, 'the same glob' );
189
190is_deeply( { foo => $sub, bar => [1, $glob] },
191           { foo => $sub, bar => [1, $glob] }
192         );
193
194
195# rt.cpan.org 53469  is_deeply with regexes
196is_deeply( qr/a/, qr/a/, "same regex" );
197
198
199# These two tests must remain at the end.
200is( $@, $Err,               '$@ untouched' );
201cmp_ok( $!, '==', $Errno,   '$! untouched' );
202