File: overloading.t

package info (click to toggle)
libspecio-perl 0.52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,132 kB
  • sloc: perl: 5,200; sh: 23; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,256 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
88
89
90
91
use strict;
use warnings;

use Test::More 0.96;

use Specio::Declare;
use Specio::Library::Builtins qw( HashRef Int Str );
use Specio::Library::Structured;

my @types = (
    t('Int'),
    t('HashRef'),
    t( 'HashRef', of => t('Int') ),
    declare(
        'Tuple[ Int, Str ]',
        parent => t(
            'Tuple',
            of => [
                t('Int'),
                t('Str'),
            ],
        ),
    ),
    declare(
        'Dict{ bar => Int, foo => Str }',
        parent => t(
            'Dict',
            of => {
                kv => {
                    foo => t('Str'),
                    bar => t('Int'),
                },
            },
        ),
    ),
    union( 'IntOrStr', of => [ t('Int'), t('Str') ] ),
    intersection( 'IntAndStr', of => [ t('Int'), t('Str') ] ),
    enum( 'Colors', values => [qw( red blue )] ),
    object_does_type('Foo'),
    any_does_type('Foo'),
    object_isa_type('Specio::Constraint::Simple'),
    any_isa_type('Specio::Constraint::Simple'),
    anon( parent => t( 'HashRef', of => t('Str') ) ),
);

for my $type (@types) {
    my $test_name = sprintf( "%s - $type", ref $type );
    subtest(
        $test_name,
        sub {

            unless ( $type->is_anon ) {
                is(
                    "$type", $type->name,
                    sprintf(
                        'stringifying a %s returns its name - %s', ref $type,
                        $type->name,
                    ),
                );
            }
            cmp_ok(
                $type, 'eq', $type,
                'type overloads eq so it is equal to itself'
            );
        }
    );
}

{
    my $anon1 = anon( parent => t( 'HashRef', of => t('Str') ) );
    is(
        "$anon1", '__ANON__(HashRef[Str])',
        "anonymous type stringification of $anon1"
    );

    my $anon2
        = anon( parent => anon( parent => t( 'HashRef', of => t('Str') ) ) );
    is(
        "$anon2", '__ANON__(__ANON__(HashRef[Str]))',
        "anonymous type stringification of $anon2"
    );

    my $anon3
        = anon( parent => enum( values => [qw( red blue )] ) );
    is(
        "$anon3", '__ANON__(__ANON__(Str))',
        "anonymous type stringification of $anon3"
    );
}

done_testing();