File: 03-constraints-oo.t

package info (click to toggle)
libdeclare-constraints-simple-perl 0.03-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 300 kB
  • sloc: perl: 2,068; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl
use warnings;
use strict;

use Test::More;

use Declare::Constraints::Simple
    only => qw(IsA IsClass HasMethods IsObject);

{
    package TestA;
    sub foo { }
    sub bar { }
    package TestB;
    use base 'TestA';
    sub foo { }
}
my $testA = bless {} => 'TestA';
my $testB = bless {} => 'TestB';

my @test_sets = (
    [IsA(qw(TestNone TestA)),       $testA,     1,  'IsA multiple true'],
    [IsA('TestB'),                  $testA,     0,  'IsA false'],
    [IsA('TestA'),                  undef,      0,  'IsA undef'],
    [IsA(),                         $testA,     0,  'IsA empty'],
    [IsA('TestA'),                  'TestB',    1,  'IsA class true'],
    [IsA('TestA'),                  'Foo',      0,  'IsA class unknown'],
    [IsA('TestB'),                  'TestA',    0,  'IsA class false'],

    [IsClass,                       'Foo',      0,  'IsClass false'],
    [IsClass,                       undef,      0,  'IsClass undef'],
    [IsClass,                       'TestA',    1,  'IsClass true'],

    [IsObject,                      undef,      0,  'IsObject undef'],
    [IsObject,                      "foo",      0,  'IsObject string'],
    [IsObject,                      {},         0,  'IsObject hash ref'],
    [IsObject,                      $testA,     1,  'IsObject true'],

    [HasMethods(qw(foo)),           $testA,     1,  'HasMethods true'],
    [HasMethods(qw(foo bar)),       $testA,     1,  'HasMethods multiple true'],
    [HasMethods(qw(foo baz)),       $testA,     0,  'HasMethods half false'],
    [HasMethods(qw(baz)),           $testA,     0,  'HasMethods all false'],
    [HasMethods(qw(bar)),           $testB,     1,  'HasMethods inherited true'],
    [HasMethods(),                  $testB,     1,  'HasMethods no list true'],
    [HasMethods(),                  "foo",      0,  'HasMethods no list no class'],
    [HasMethods(qw(foo)),           undef,      0,  'HasMethods undef'],
);

plan tests => scalar(@test_sets);

for (@test_sets) {
    my ($check, $value, $expect, $title) = @$_;
    my $result = $check->($value);
    is(($result ? 1 : 0), $expect, $title);
}