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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
{
package MooX::Types::MooseLike::Test;
use strict;
use warnings FATAL => 'all';
use Moo;
use MooX::Types::MooseLike::Base qw/
ArrayRef Int HashRef Str ScalarRef Maybe AnyOf Undef
/;
has an_array_of_integers => (
is => 'ro',
isa => ArrayRef [Int],
);
has an_array_of_hash => (
is => 'ro',
isa => ArrayRef [HashRef],
);
has a_hash_of_strings => (
is => 'ro',
isa => HashRef [Str],
);
has an_array_of_hash_of_int => (
is => 'ro',
isa => ArrayRef [ HashRef [Int] ],
);
has a_scalar_ref_of_int => (
is => 'ro',
isa => ScalarRef [Int],
);
has maybe_an_int => (
is => 'ro',
isa => Maybe [Int],
);
has array_maybe_a_hash => (
is => 'ro',
isa => ArrayRef [ Maybe [HashRef] ],
);
has array_maybe_a_hash_of_int => (
is => 'ro',
isa => ArrayRef [ Maybe [ HashRef [Int] ] ],
);
has maybe_an_int_or_hash => (
is => 'ro',
isa => AnyOf[Int, HashRef, Undef],
);
}
package main;
use strict;
use warnings FATAL => 'all';
use Test::More;
use Test::Fatal;
use IO::Handle;
# ArrayRef[Int]
ok(MooX::Types::MooseLike::Test->new(an_array_of_integers => [ 6, 7, 10, -1 ]),
'ArrayRef[Int]');
like(
exception {
MooX::Types::MooseLike::Test->new(an_array_of_integers => [ 1.5, 2 ]);
},
qr/is not an integer/,
'an ArrayRef of Floats is an exception when we want an ArrayRef[Int]'
);
# ArrayRef[HashRef]
ok(
MooX::Types::MooseLike::Test->new(
an_array_of_hash => [ { a => 1 }, { b => 2 } ]
),
'ArrayRef[HashRef]'
);
ok(
MooX::Types::MooseLike::Test->new(
an_array_of_hash => [ { a => 1 }, { b => undef } ]
),
'ArrayRef[HashRef] with undef'
);
like(
exception {
MooX::Types::MooseLike::Test->new(
an_array_of_hash => [ { x => 1 }, [ 1, 2, 3 ] ]);
},
qr/is not a HashRef/,
'an ArrayRef of integers is an exception when we want an ArrayRef[HashRef]'
);
# HashRef[Str]
ok(MooX::Types::MooseLike::Test->new(a_hash_of_strings => { a => 1, b => 'x' }),
'HashRef[Str]');
ok(MooX::Types::MooseLike::Test->new(a_hash_of_strings => {}),
'Empty HashRef');
like(
exception {
MooX::Types::MooseLike::Test->new(a_hash_of_strings =>
{ a => MooX::Types::MooseLike::Test->new(), b => 'x' });
},
qr/is not a string/,
'an HashRef with Objects is an exception when we want an HashRef[Str]'
);
# ArrayRef[HashRef[Int]]
ok(
MooX::Types::MooseLike::Test->new(
an_array_of_hash_of_int => [ { a => 1 }, { b => 2 } ]
),
'ArrayRef[HashRef[Int]]'
);
like(
exception {
MooX::Types::MooseLike::Test->new(
an_array_of_hash_of_int => [ { x => 1 }, { x => 1.1 } ]);
},
qr/is not an integer/,
'an ArrayRef of HashRef of Floats is an exception when we want an ArrayRef[HashRef[Int]]'
);
# ScalarRef[Int]
ok(MooX::Types::MooseLike::Test->new(a_scalar_ref_of_int => \1),
'ScalarRef[Int]');
like(
exception { MooX::Types::MooseLike::Test->new(a_scalar_ref_of_int => \'x') },
qr/is not an integer/,
'a ScalarRef of Str is an exception when we want an ScalarRef[Int]'
);
# Maybe[Int]
ok(MooX::Types::MooseLike::Test->new(maybe_an_int => 41),
'Maybe[Int] as an integer');
ok(MooX::Types::MooseLike::Test->new(maybe_an_int => undef),
'Maybe[Int] as undef');
ok(MooX::Types::MooseLike::Test->new(maybe_an_int => -24),
'Maybe[Int] as undef');
like(
exception { MooX::Types::MooseLike::Test->new(maybe_an_int => 'x') },
qr/is not an integer/,
'a Str is an exception when we want a Maybe[Int]'
);
# AnyOf[Int, HashRef, Undef] - a stand in for Maybe[Int, HashRef]
ok(MooX::Types::MooseLike::Test->new(maybe_an_int_or_hash => {nbr => 41} ),
'Maybe[Int, HahsRef, Undef] as a HashRef');
ok(MooX::Types::MooseLike::Test->new(maybe_an_int_or_hash => 41 ),
'Maybe[Int, HahsRef, Undef] as an integer');
ok(MooX::Types::MooseLike::Test->new(maybe_an_int_or_hash => undef ),
'Maybe[Int, HahsRef, Undef] as an undef');
like(
exception { MooX::Types::MooseLike::Test->new(maybe_an_int_or_hash => [] ) },
qr/is not any of/,
'an ArrayRef is an exception when we want a AnyOf[Int, HashRef, Undef]'
);
like(
exception { MooX::Types::MooseLike::Test->new(maybe_an_int_or_hash => sub {} ) },
qr/is not any of/,
'a CodeRef is an exception when we want a AnyOf[Int, HashRef, Undef]'
);
# ArrayRef[Maybe[HashRef]]
ok(MooX::Types::MooseLike::Test->new(array_maybe_a_hash => [ { a => 41 } ]),
'ArrayRef[Maybe[HashRef]] as an HashRef');
ok(
MooX::Types::MooseLike::Test->new(
array_maybe_a_hash => [ undef, { a => 41 } ]
),
'ArrayRef[Maybe[HashRef]] with an undef'
);
like(
exception { MooX::Types::MooseLike::Test->new(array_maybe_a_hash => ['x']) },
qr/is not a HashRef/,
'a Str is an exception when we want a Maybe[HashRef]'
);
# ArrayRef[Maybe[HashRef[Int]]]
ok(
MooX::Types::MooseLike::Test->new(
array_maybe_a_hash_of_int => [ { a => 41 } ]
),
'ArrayRef[Maybe[HashRef]] as a HashRef[Int]'
);
ok(
MooX::Types::MooseLike::Test->new(
array_maybe_a_hash_of_int => [ undef, { a => -1 } ]
),
'ArrayRef[Maybe[HashRef]] with an undef'
);
like(
exception {
MooX::Types::MooseLike::Test->new(
array_maybe_a_hash_of_int => [ { b => 'x' } ]);
},
qr/is not an integer/,
'a Str is an exception when we want a Maybe[HashRef[Int]]'
);
done_testing;
|