File: Isa.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (63 lines) | stat: -rw-r--r-- 2,884 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use Test2::Bundle::Extended -target => 'Test2::Compare::Isa';

{
    package Foo;

    package Foo::Bar;
    our @ISA = 'Foo';

    package Baz;
}

my $isa_foo         = $CLASS->new(input => 'Foo');
my $isa_foo_bar     = $CLASS->new(input => 'Foo::Bar');
my $not_isa_foo_bar = $CLASS->new(input => 'Foo::Bar', negate => 1);

isa_ok($_, $CLASS, 'Test2::Compare::Base') for $isa_foo, $isa_foo_bar, $not_isa_foo_bar;

subtest name => sub {
    is($isa_foo->name,         'Foo',      "got expected name");
    is($isa_foo_bar->name,     'Foo::Bar', "got expected name");
    is($not_isa_foo_bar->name, 'Foo::Bar', "got expected name");
};

subtest operator => sub {
    is($isa_foo->operator,         'isa',  "got expected operator");
    is($isa_foo_bar->operator,     'isa',  "got expected operator");
    is($not_isa_foo_bar->operator, '!isa', "got expected operator");
};

subtest verify => sub {
    my $foo     = bless {}, 'Foo';
    my $foo_bar = bless {}, 'Foo::Bar';
    my $baz     = bless {}, 'Baz';

    ok(!$isa_foo->verify(exists => 0, got => undef),   'does not verify against DNE');
    ok(!$isa_foo->verify(exists => 1, got => undef),   'undef is not an instance of Foo');
    ok(!$isa_foo->verify(exists => 1, got => 42),      '42 is not an instance of Foo');
    ok($isa_foo->verify(exists => 1, got => $foo),     '$foo is an instance of Foo');
    ok($isa_foo->verify(exists => 1, got => $foo_bar), '$foo_bar is an instance of Foo');
    ok(!$isa_foo->verify(exists => 1, got => $baz),    '$baz is not an instance of Foo');

    ok(!$isa_foo_bar->verify(exists => 0, got => undef),   'does not verify against DNE');
    ok(!$isa_foo_bar->verify(exists => 1, got => undef),   'undef is not an instance of Foo::Bar');
    ok(!$isa_foo_bar->verify(exists => 1, got => 42),      '42 is not an instance of Foo::Bar');
    ok(!$isa_foo_bar->verify(exists => 1, got => $foo),    '$foo is not an instance of Foo::Bar');
    ok($isa_foo_bar->verify(exists => 1, got => $foo_bar), '$foo_bar is an instance of Foo::Bar');
    ok(!$isa_foo_bar->verify(exists => 1, got => $baz),    '$baz is not an instance of Foo::Bar');

    ok(!$not_isa_foo_bar->verify(exists => 0, got => undef),    'does not verify against DNE');
    ok($not_isa_foo_bar->verify(exists => 1, got => undef),     'undef is not an instance of Foo::Bar');
    ok($not_isa_foo_bar->verify(exists => 1, got => 42),        '42 is not an instance of Foo::Bar');
    ok($not_isa_foo_bar->verify(exists => 1, got => $foo),      '$foo is not an instance of Foo::Bar');
    ok(!$not_isa_foo_bar->verify(exists => 1, got => $foo_bar), '$foo_bar is an instance of Foo::Bar');
    ok($not_isa_foo_bar->verify(exists => 1, got => $baz),      '$baz is not an instance of Foo::Bar');
};

like(
    dies { $CLASS->new() },
    qr/input must be defined for 'Isa' check/,
    "Cannot use undef as a class name"
);

done_testing;