File: basic.t

package info (click to toggle)
libdispatch-class-perl 0.04-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 140 kB
  • sloc: perl: 115; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 1,660 bytes parent folder | download | duplicates (4)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!perl
use warnings FATAL => 'all';
use strict;

use Test::More tests => 17;

use Dispatch::Class qw(class_case dispatch);
use IO::Handle ();

{
    package DummyClass;
    sub new { bless {}, $_[0] }
    sub subclass {
        my $class = shift;
        for my $subclass (@_) {
            no strict 'refs';
            push @{$subclass . '::ISA'}, $class;
        }
    }
}

DummyClass->subclass(qw(Some::Class Other::Class));

my $analyze = class_case(
    'Some::Class'  => 1,
    'Other::Class' => 2,
    'UNIVERSAL'    => "???",
);
is $analyze->(Other::Class->new), 2;
is $analyze->(IO::Handle->new), "???";
is $analyze->(["not an object"]), undef;
is +() = $analyze->(["not an object"]), 0;

DummyClass->subclass(qw(Mammal Tree));
Mammal->subclass(qw(Dog Bunny));
Dog->subclass(qw(Dog::Tiny Barky Setter));
Tree->subclass(qw(Barky));

my @trace;

my $dispatch = dispatch(
    map {
        my $class = $_;
        $_ => sub {
            push @trace, $class;
            return $class, $_[0];
        }
    } qw(
        Tree
        Dog::Tiny
        Dog
        ARRAY
        Mammal
        :str
        HASH
        *
    )
);

my @prep = (
    'Tree' => Tree->new,
    'Mammal' => Mammal->new,
    'Dog' => Dog->new,
    'Mammal' => Bunny->new,
    'Dog::Tiny' => Dog::Tiny->new,
    'Tree' => Barky->new,
    'Dog' => Setter->new,
    'ARRAY' => [1, 2, 3],
    'HASH' => {A => 'b'},
    ':str' => "foo bar",
    ':str' => 5,
    '*' => IO::Handle->new,
);

my @ks;
for (my $i = 0; $i < @prep; $i += 2) {
    my ($k, $v) = @prep[$i, $i + 1];
    my @got = $dispatch->($v);
    is_deeply \@got, [$k, $v];
    push @ks, $k;
}
is_deeply \@trace, \@ks;