File: structured.t

package info (click to toggle)
libmoosex-types-varianttable-perl 0.04-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 124 kB
  • sloc: perl: 384; makefile: 2
file content (87 lines) | stat: -rw-r--r-- 2,855 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
83
84
85
86
87
use strict;
use warnings;
use Test::More;
use Test::Exception;
use MooseX::Types::VariantTable;
use Moose::Util::TypeConstraints;

BEGIN {
    eval q[
        use MooseX::Types::Structured;
        use MooseX::Types::Moose;
        1;
    ] or plan skip_all => 'requires MooseX::Types and MooseX::Types::Structured';
}

use MooseX::Types::Structured qw/Tuple Dict/;
use MooseX::Types::Moose qw/Num Int Str Item/;

plan tests => 16;

{
    my $t = MooseX::Types::VariantTable->new;
    $t->add_variant( Num => 'Num' );
    $t->add_variant( Str => 'Str' );

    is($t->find_variant(21), 'Num');
    is($t->find_variant('hey'), 'Str');
}

{
    my $t = MooseX::Types::VariantTable->new;
    $t->add_variant( Tuple[Tuple[Num], Dict[]] => 'Num' );
    $t->add_variant( Tuple[Tuple[Str], Dict[]] => 'Str' );

    is($t->find_variant([[21], {}]), 'Num');
    is($t->find_variant([['hey'], {}]), 'Str');
}

{
    package Paper;
    use Moose;

    package Scissors;
    use Moose;

    package Stone;
    use Moose;
}

{
    my $t = MooseX::Types::VariantTable->new;
    $t->add_variant( Tuple[Tuple[ class_type('Paper'),    class_type('Paper')    ], Dict[]] => 0 );
    $t->add_variant( Tuple[Tuple[ class_type('Stone'),    class_type('Stone')    ], Dict[]] => 0 );
    $t->add_variant( Tuple[Tuple[ class_type('Scissors'), class_type('Scissors') ], Dict[]] => 0 );
    $t->add_variant( Tuple[Tuple[ class_type('Paper'),    class_type('Stone')    ], Dict[]] => 1 );
    $t->add_variant( Tuple[Tuple[ class_type('Scissors'), class_type('Paper')    ], Dict[]] => 1 );
    $t->add_variant( Tuple[Tuple[ class_type('Stone'),    class_type('Scissors') ], Dict[]] => 1 );
    $t->add_variant( Tuple[Tuple[ Item, Item ], Dict[]] => -1);

    is( $t->find_variant([[ Paper->new, Scissors->new ], {}]), -1 );
    is( $t->find_variant([[ Paper->new, Stone->new    ], {}]), 1 );
    is( $t->find_variant([[ Stone->new, Stone->new    ], {}]), 0 );
    is( $t->find_variant([[ Paper->new, Paper->new    ], {}]), 0 );
    is( $t->find_variant([[ Stone->new, Paper->new    ], {}]), -1 );
    is( $t->find_variant([[ Paper->new, Stone->new    ], {}]), 1 );
}

{
    my $t = MooseX::Types::VariantTable->new;
    $t->add_variant( Tuple[Tuple[ Int, Num ], Dict[]] => 'first' );
    $t->add_variant( Tuple[Tuple[ Num, Int ], Dict[]] => 'second' );

    dies_ok { $t->find_variant([[ 42, 23 ], {}]) };

    is($t->find_variant([[ 42, 23.3 ], {}]), 'first');
    is($t->find_variant([[ 42.2, 23 ], {}]), 'second');
}

{
    my $t = MooseX::Types::VariantTable->new;
    $t->add_variant( Tuple[Tuple[ Int ], Dict[foo => Int]] => 'first'  );
    $t->add_variant( Tuple[Tuple[ Int ], Dict[          ]] => 'second' );

    is($t->find_variant([[ 23 ], { foo => 42 }]), 'first');
    is($t->find_variant([[ 42 ], { }]), 'second');
    ok(!$t->find_variant([[ 23 ], { foo => 'bar' }]));
}