File: inline-environment.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 (77 lines) | stat: -rw-r--r-- 2,157 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
use strict;
use warnings;

use Test::Fatal;
use Test::More 0.96;

use Specio::Constraint::Simple;
use Specio::DeclaredAt;
use Specio::Library::Builtins;

{
    my $t = Specio::Constraint::Simple->new(
        name               => 'Foo',
        parent             => t('Str'),
        inline_generator   => sub {'1'},
        inline_environment => { '$scalar' => 42 },
        declared_at        => Specio::DeclaredAt->new_from_caller(0),
    );

    my $ref = Specio::Constraint::Simple->new(
        name               => 'Bar',
        parent             => t('Ref'),
        inline_generator   => sub {'1'},
        inline_environment => { '$scalar_from' => 77 },
        declared_at        => Specio::DeclaredAt->new_from_caller(0),
    );

    my $from_int = Specio::Coercion->new(
        from               => t('Int'),
        to                 => $t,
        inline_generator   => sub {'1'},
        inline_environment => {
            '%hash' => { y => 84 },
        },
        declared_at => Specio::DeclaredAt->new_from_caller(0),
    );

    my $from_num = Specio::Coercion->new(
        from               => t('Num'),
        to                 => $t,
        inline_generator   => sub {'1'},
        inline_environment => {
            '@array' => [ 1, 2, 3 ],
        },
        declared_at => Specio::DeclaredAt->new_from_caller(0),
    );

    my $from_ref = Specio::Coercion->new(
        from             => $ref,
        to               => $t,
        inline_generator => sub {'1'},
        declared_at      => Specio::DeclaredAt->new_from_caller(0),
    );

    $t->add_coercion($from_int);
    $t->add_coercion($from_num);
    $t->add_coercion($from_ref);

    my ( undef, $env ) = $t->inline_coercion_and_check('$var');

    my %expect = (
        '$scalar'      => 42,
        '$scalar_from' => 77,
        '%hash'        => { y => 84 },
        '@array'       => [ 1, 2, 3 ],
    );

    for my $key ( sort keys %expect ) {
        is_deeply(
            $env->{$key},
            $expect{$key},
            "inline_coercion_and_check merges all inline environment hashes together - $key",
        );
    }
}

done_testing();