File: inline_coercion-back-compat.t

package info (click to toggle)
libmoosex-attributeshortcuts-perl 0.024-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 312 kB
  • ctags: 9
  • sloc: perl: 326; makefile: 2
file content (92 lines) | stat: -rw-r--r-- 2,281 bytes parent folder | download
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
92
use strict;
use warnings;

# small, minimal test to ensure our hashref-based attribute matching is still
# working as expected
#
# That is to say, working unless you hit a nasty bug.

{ package TestClass::From; use Moose; }

my $i = 0;
my $sc_trait;

{
    package TestClass;

    use Moose;
    use namespace::autoclean;
    use MooseX::AttributeShortcuts;
    use Path::Class;
    use MooseX::Types::Path::Class ':all';

    $sc_trait = Shortcuts;

    has bar => (
        is     => 'rw',
        isa    => File,
        coerce => {
            'TestClass::From' => sub { $i++; return file('foo') },
            'Str'             => sub { $i++; file $_ },
        },
    );
}

use Test::More;
use Test::Moose::More 0.018;
use Test::Fatal;
use Path::Class;
use MooseX::Types::Path::Class ':all';

# TODO shift the constraint checking out into TMM?

validate_class TestClass => (
    attributes => [
        bar => {
            -does        => [ $sc_trait ],
            reader       => undef,
            writer       => undef,
            accessor     => 'bar',
            isa          => File,
            original_isa => 'MooseX::Types::Path::Class::File',
            coerce       => 1,
            required     => undef,
        },
    ],
);

subtest 'Str coercion OK' => sub {

    my $tc;
    my $msg = exception { $tc = TestClass->new(bar => 'foo') };
    is $msg, undef, 'does not die on construction';
    my $bar = $tc->bar;
    isa_ok $bar, 'Path::Class::File';
    is "$bar", 'foo', 'value is correct';

    $msg = exception { $tc->bar('baz') };
    is $msg, undef, 'does not die on setting';
    $bar = $tc->bar;
    isa_ok $bar, 'Path::Class::File';
    is "$bar", 'baz', 'value is correct';
};

subtest 'TestClass::From coercion OK' => sub {

    my $tc;
    my $tf = TestClass::From->new();
    my $msg = exception { $tc = TestClass->new(bar => $tf) };
    is $msg, undef, 'does not die on construction';
    my $bar = $tc->bar;
    isa_ok $bar, 'Path::Class::File';
    is "$bar", 'foo', 'value is correct';

    $msg = exception { $tc->bar($tf) };
    is $msg, undef, 'does not die on setting';
    $bar = $tc->bar;
    isa_ok $bar, 'Path::Class::File';
    # yeah, I know, just go with it for now
    is "$bar", 'foo', 'value is correct';
};

done_testing;