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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
package MooX::Types::MooseLike::Test;
use strict;
use warnings FATAL => 'all';
use Moo;
use MooX::Types::MooseLike::Base qw(:all);
has 'a_bool' => (
is => 'ro',
isa => Bool,
);
has 'an_undef' => (
is => 'ro',
isa => Undef,
);
has 'a_defined' => (
is => 'ro',
isa => Defined,
);
has 'a_value' => (
is => 'ro',
isa => Value,
);
has 'a_string' => (
is => 'ro',
isa => Str,
);
has 'a_number' => (
is => 'ro',
isa => Num,
);
has 'an_integer' => (
is => 'ro',
isa => Int,
);
has 'a_ref' => (
is => 'ro',
isa => Ref,
);
has 'an_array' => (
is => 'ro',
isa => ArrayRef,
);
has 'a_hash' => (
is => 'ro',
isa => HashRef,
);
has 'a_code' => (
is => 'ro',
isa => CodeRef,
);
has 'a_regex' => (
is => 'ro',
isa => RegexpRef,
);
has 'a_glob' => (
is => 'ro',
isa => GlobRef,
);
has 'a_filehandle' => (
is => 'ro',
isa => FileHandle,
);
has 'an_object' => (
is => 'ro',
isa => Object,
);
has 'legal_age' => (
is => 'ro',
isa => sub {
die "$_[0] is not of legal age"
unless (is_Int($_[0]) && $_[0] > 17);
},
);
package main;
use strict;
use warnings FATAL => 'all';
use Test::More;
use Test::Fatal;
use IO::Handle;
use MooX::Types::MooseLike::Base qw(:all);
ok(is_Str('x'), 'is_Str');
ok(!is_Str([]), 'is_Str fails on ArrayRef');
ok(is_Num(3.142), 'is_Num');
ok(is_Num('9'), 'is_Num');
ok(!is_Num('xxx'), 'is_Num fails on a non-numeric string');
ok(is_Int(-3), 'is_Int');
ok(!is_Int(3.142), 'is_Int fails on a Float');
ok(is_Bool('0'), '0 is_Bool');
ok(is_Bool(''), 'empty string is_Bool');
ok(is_Bool(), 'undef is_Bool');
ok(is_Bool(1), '1 is_Bool');
ok(!is_Bool(-1), 'is_Bool fails on -1');
ok(is_ArrayRef([]), 'is_ArrayRef');
ok(!is_ArrayRef('1'), 'is_ArrayRef fails on 1');
ok(is_HashRef({}), 'is_HashRef');
ok(!is_HashRef([]), 'is_HashRef fails on []');
ok(is_CodeRef(sub { }), 'is_CodeRef');
ok(!is_CodeRef({}), 'is_CodeRef fails on {}');
ok(is_Object(IO::Handle->new), 'Object');
ok(!is_Object({}), 'is_Object fails on HashRef');
# Test Bool
ok(MooX::Types::MooseLike::Test->new(a_bool => undef), 'undef is a Bool');
ok(MooX::Types::MooseLike::Test->new(a_bool => 0), '0 is a Bool');
my $false_boolean_value = 0.001;
like(
exception {
MooX::Types::MooseLike::Test->new(a_bool => $false_boolean_value);
},
qr/$false_boolean_value is not a Boolean/,
'a non-boolean is an exception when we want a Bool'
);
# Undef
ok(MooX::Types::MooseLike::Test->new(an_undef => undef), 'Undef');
like(
exception { MooX::Types::MooseLike::Test->new(an_undef => '') },
qr/is not undef/,
'empty string is an exception when we want an Undef type'
);
# Defined
ok(MooX::Types::MooseLike::Test->new(a_defined => ''), 'Defined');
like(
exception { MooX::Types::MooseLike::Test->new(a_defined => undef) },
qr/is not defined/,
'undef is an exception when we want a Defined type'
);
# Test Value
ok(MooX::Types::MooseLike::Test->new(a_value => ''), 'empty string Value');
ok(MooX::Types::MooseLike::Test->new(a_value => 0), 'zero Value');
ok(MooX::Types::MooseLike::Test->new(a_value => 'yarn'), 'word Value');
like(
exception { MooX::Types::MooseLike::Test->new(a_value => undef) },
qr/undef is not a value/,
'undef is an exception when we want a Value'
);
like(
exception { MooX::Types::MooseLike::Test->new(a_value => []) },
qr/ARRAY.*?is not a value/,
'an ArrayRef is an exception when we want a Value'
);
# Test Str
ok(MooX::Types::MooseLike::Test->new(a_string => ''), 'Empty string');
ok(MooX::Types::MooseLike::Test->new(a_string => '0'), 'Zero as a string');
ok(MooX::Types::MooseLike::Test->new(a_string => 'barn'), 'Word string');
like(
exception { MooX::Types::MooseLike::Test->new(a_string => undef) },
qr/undef is not a string/,
'undef is an exception when we want a Str'
);
like(
exception { MooX::Types::MooseLike::Test->new(a_string => []) },
qr/ARRAY.*?is not a string/,
'an ArrayRef is an exception when we want a Str'
);
# Test Num
ok(MooX::Types::MooseLike::Test->new(a_number => 0), 'Num zero');
ok(MooX::Types::MooseLike::Test->new(a_number => 3.14), 'Num');
like(
exception { MooX::Types::MooseLike::Test->new(a_number => undef) },
qr/undef is not a number/,
'undef is an exception when we want a number'
);
like(
exception { MooX::Types::MooseLike::Test->new(a_number => '') },
qr/The empty string is not a number/,
'The empty string is an exception when we want a number'
);
like(
exception { MooX::Types::MooseLike::Test->new(a_number => '5x5') },
qr/is not a number/,
'a non number is an exception when we want a number'
);
# Test Int
ok(MooX::Types::MooseLike::Test->new(an_integer => -1), 'Int');
like(
exception { MooX::Types::MooseLike::Test->new(an_integer => undef) },
qr/undef is not an integer/,
'undef is an exception when we want an Integer'
);
like(
exception { MooX::Types::MooseLike::Test->new(an_integer => '') },
qr/The empty string is not an integer/,
'The empty string is an exception when we want an Integer'
);
like(
exception { MooX::Types::MooseLike::Test->new(an_integer => 1.01) },
qr/is not an integer/,
'a non-integer is an exception when we want an Integer'
);
# Test Ref
ok(MooX::Types::MooseLike::Test->new(a_ref => []), 'Ref: ArrayRef');
like(
exception { MooX::Types::MooseLike::Test->new(a_ref => undef) },
qr/is not a reference/,
'undef is an exception when we want a reference'
);
like(
exception { MooX::Types::MooseLike::Test->new(a_ref => '') },
qr/is not a reference/,
'The empty string is an exception when we want a reference'
);
like(
exception { MooX::Types::MooseLike::Test->new(a_ref => 0) },
qr/is not a reference/,
'zero is an exception when we want an reference'
);
# Test ArrayRef
ok(MooX::Types::MooseLike::Test->new(an_array => []), 'ArrayRef');
like(
exception { MooX::Types::MooseLike::Test->new(an_array => undef) },
qr/is not an ArrayRef/,
'undef is an exception when we want an ArrayRef'
);
like(
exception { MooX::Types::MooseLike::Test->new(an_array => '') },
qr/is not an ArrayRef/,
'The empty string is an exception when we want an ArrayRef'
);
like(
exception { MooX::Types::MooseLike::Test->new(an_array => 0) },
qr/is not an ArrayRef/,
'zero is an exception when we want an ArrayRef'
);
like(
exception { MooX::Types::MooseLike::Test->new(an_array => {}) },
qr/HASH.*?is not an ArrayRef/,
'a HashRef is an exception when we want an ArrayRef'
);
like(
exception { MooX::Types::MooseLike::Test->new(an_array => 'string') },
qr/string is not an ArrayRef/,
'a String is an exception when we want an ArrayRef'
);
# Test HashRef
ok(MooX::Types::MooseLike::Test->new(a_hash => {}), 'HashRef');
like(
exception { MooX::Types::MooseLike::Test->new(a_hash => undef) },
qr/is not a HashRef/,
'undef is an exception when we want a HashRef'
);
like(
exception { MooX::Types::MooseLike::Test->new(a_hash => '') },
qr/is not a HashRef/,
'The empty string is an exception when we want a HashRef'
);
like(
exception { MooX::Types::MooseLike::Test->new(a_hash => []) },
qr/ARRAY.*?is not a HashRef/,
'an ArrayRef is an exception when we want a HashRef'
);
like(
exception { MooX::Types::MooseLike::Test->new(a_hash => 'string') },
qr/string is not a HashRef/,
'a String is an exception when we want a HashRef'
);
# Test CodeRef
ok(MooX::Types::MooseLike::Test->new(a_code => sub { }), 'CodeRef');
like(
exception { MooX::Types::MooseLike::Test->new(a_code => []) },
qr/ARRAY.*?is not a CodeRef/,
'an ArrayRef is an exception when we want a CodeRef'
);
# Test RegexpRef
ok(MooX::Types::MooseLike::Test->new(a_regex => qr{}), 'RegexpRef');
like(
exception { MooX::Types::MooseLike::Test->new(a_regex => []) },
qr/ARRAY.*?is not a RegexpRef/,
'an ArrayRef is an exception when we want a RegexpRef'
);
# Test GlobRef
# avoid warning for using FOO only once
no warnings 'once';
ok(MooX::Types::MooseLike::Test->new(a_glob => \*FOO), 'GlobRef');
like(
exception { MooX::Types::MooseLike::Test->new(a_glob => []) },
qr/ARRAY.*?is not a GlobRef/,
'an ArrayRef is an exception when we want a GlobRef'
);
# Test FileHandle
ok(MooX::Types::MooseLike::Test->new(a_filehandle => IO::Handle->new),
'FileHandle');
like(
exception { MooX::Types::MooseLike::Test->new(a_filehandle => []) },
qr/ARRAY.*?is not a FileHandle/,
'an ArrayRef is an exception when we want a FileHandle'
);
# Test Object
ok(MooX::Types::MooseLike::Test->new(an_object => IO::Handle->new), 'Object');
like(
exception { MooX::Types::MooseLike::Test->new(an_object => []) },
qr/ARRAY.*?is not an Object/,
'an ArrayRef is an exception when we want an Object'
);
# Test legal_age attribute which has an 'isa' that uses 'is_Int'
ok(MooX::Types::MooseLike::Test->new(legal_age => 18), 'Legal Age');
my $minor_age = 17;
like(
exception { MooX::Types::MooseLike::Test->new(legal_age => $minor_age) },
qr/$minor_age is not of legal age/,
'an integer less than 18 is an exception when we want a legal age'
);
like(
exception { MooX::Types::MooseLike::Test->new(an_undef => '') },
qr/is not undef.*\n.*MooX::Types::MooseLike::Test::new.*basic\.t/s,
'The error looks like a useful stacktrace'
);
done_testing;
|