File: coerce.t

package info (click to toggle)
libtest-moose-more-perl 0.024-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 312 kB
  • ctags: 25
  • sloc: perl: 303; makefile: 2
file content (81 lines) | stat: -rw-r--r-- 2,546 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;

use Test::Builder::Tester;
use Test::More;
use Test::Moose::More;
use TAP::SimpleOutput 'counters';

use Moose::Util::TypeConstraints;
subtype 'AllCaps', as 'Str', where { !m/[a-z]/ }, message { 'String contains some lower-case chars' };
coerce 'AllCaps', from 'Str', via { tr/[a-z]/A-Z]/ };

{
    package TestRole;
    use Moose::Role;
    use Moose::Deprecated -api_version => '1.07'; # don't complain
    use namespace::autoclean;

    has yes_coerce  => (is => 'ro', isa => 'AllCaps', coerce => 1);
    has no_coerce   => (is => 'ro', isa => 'AllCaps', coerce => 0);
    has null_coerce => (is => 'ro', isa => 'AllCaps');
}
{
    package TestClass;
    use Moose;
    use Moose::Deprecated -api_version => '1.07'; # don't complain
    use namespace::autoclean;

    has yes_coerce  => (is => 'ro', isa => 'AllCaps', coerce => 1);
    has no_coerce   => (is => 'ro', isa => 'AllCaps', coerce => 0);
    has null_coerce => (is => 'ro', isa => 'AllCaps');
}

note 'finds coercion correctly';
for my $thing (qw{ TestClass TestRole }) {
    my ($_ok, $_nok, $_skip) = counters();
    my $name = 'yes_coerce';
    test_out $_ok->("$thing has an attribute named $name");
    test_out $_ok->("$name should coerce");
    test_out $_ok->("$thing has an attribute named $name");
    test_out $_nok->("$name should not coerce");
    test_fail 7;
    test_out $_ok->("$thing has an attribute named $name");
    test_out $_nok->("$name should not coerce");
    test_fail 7;
    validate_attribute $thing => $name => (
        coerce => 1,
    );
    validate_attribute $thing => $name => (
        coerce => 0,
    );
    validate_attribute $thing => $name => (
        coerce => undef,
    );
    test_test "finds coercion correctly in $thing";
}

note 'finds no coercion correctly';
for my $thing (qw{ TestClass TestRole}) {
    my ($_ok, $_nok, $_skip) = counters();
    my $name = 'no_coerce';
    test_out $_ok->("$thing has an attribute named $name");
    test_out $_nok->("$name should coerce");
    test_fail 5;
    test_out $_ok->("$thing has an attribute named $name");
    test_out $_ok->("$name should not coerce");
    test_out $_ok->("$thing has an attribute named $name");
    test_out $_ok->("$name should not coerce");
    validate_attribute $thing => $name => (
        coerce => 1,
    );
    validate_attribute $thing => $name => (
        coerce => 0,
    );
    validate_attribute $thing => $name => (
        coerce => undef,
    );
    test_test "finds no coercion correctly in $thing";
}

done_testing;