Debian Codegraph @

cpan/Test2-Suite/t/modules/Compare/Isa.t

raw
1 use Test2::Bundle::Extended -target => 'Test2::Compare::Isa';
2
3 {
4 package Foo;
5
6 package Foo::Bar;
7 our @ISA = 'Foo';
8
9 package Baz;
10 }
11
12 my $isa_foo = $CLASS->new(input => 'Foo');
13 my $isa_foo_bar = $CLASS->new(input => 'Foo::Bar');
14 my $not_isa_foo_bar = $CLASS->new(input => 'Foo::Bar', negate => 1);
15
16 isa_ok($_, $CLASS, 'Test2::Compare::Base') for $isa_foo, $isa_foo_bar, $not_isa_foo_bar;
17
18 subtest name => sub {
19 is($isa_foo->name, 'Foo', "got expected name");
20 is($isa_foo_bar->name, 'Foo::Bar', "got expected name");
21 is($not_isa_foo_bar->name, 'Foo::Bar', "got expected name");
22 };
23
24 subtest operator => sub {
25 is($isa_foo->operator, 'isa', "got expected operator");
26 is($isa_foo_bar->operator, 'isa', "got expected operator");
27 is($not_isa_foo_bar->operator, '!isa', "got expected operator");
28 };
29
30 subtest verify => sub {
31 my $foo = bless {}, 'Foo';
32 my $foo_bar = bless {}, 'Foo::Bar';
33 my $baz = bless {}, 'Baz';
34
35 ok(!$isa_foo->verify(exists => 0, got => undef), 'does not verify against DNE');
36 ok(!$isa_foo->verify(exists => 1, got => undef), 'undef is not an instance of Foo');
37 ok(!$isa_foo->verify(exists => 1, got => 42), '42 is not an instance of Foo');
38 ok($isa_foo->verify(exists => 1, got => $foo), '$foo is an instance of Foo');
39 ok($isa_foo->verify(exists => 1, got => $foo_bar), '$foo_bar is an instance of Foo');
40 ok(!$isa_foo->verify(exists => 1, got => $baz), '$baz is not an instance of Foo');
41
42 ok(!$isa_foo_bar->verify(exists => 0, got => undef), 'does not verify against DNE');
43 ok(!$isa_foo_bar->verify(exists => 1, got => undef), 'undef is not an instance of Foo::Bar');
44 ok(!$isa_foo_bar->verify(exists => 1, got => 42), '42 is not an instance of Foo::Bar');
45 ok(!$isa_foo_bar->verify(exists => 1, got => $foo), '$foo is not an instance of Foo::Bar');
46 ok($isa_foo_bar->verify(exists => 1, got => $foo_bar), '$foo_bar is an instance of Foo::Bar');
47 ok(!$isa_foo_bar->verify(exists => 1, got => $baz), '$baz is not an instance of Foo::Bar');
48
49 ok(!$not_isa_foo_bar->verify(exists => 0, got => undef), 'does not verify against DNE');
50 ok($not_isa_foo_bar->verify(exists => 1, got => undef), 'undef is not an instance of Foo::Bar');
51 ok($not_isa_foo_bar->verify(exists => 1, got => 42), '42 is not an instance of Foo::Bar');
52 ok($not_isa_foo_bar->verify(exists => 1, got => $foo), '$foo is not an instance of Foo::Bar');
53 ok(!$not_isa_foo_bar->verify(exists => 1, got => $foo_bar), '$foo_bar is an instance of Foo::Bar');
54 ok($not_isa_foo_bar->verify(exists => 1, got => $baz), '$baz is not an instance of Foo::Bar');
55 };
56
57 like(
58 dies { $CLASS->new() },
59 qr/input must be defined for 'Isa' check/,
60 "Cannot use undef as a class name"
61 );
62
63 done_testing;