use Carp 'confess';
use Scalar::Util 'blessed';
-our $VERSION = '0.04';
+our $VERSION = '0.05';
use Moose::Meta::TypeConstraint;
use Moose::Meta::TypeCoercion;
subtype 'Value' => as 'Any' => where { !ref($_) };
subtype 'Ref' => as 'Any' => where { ref($_) };
+subtype 'Bool' => as 'Any' => where { "$_" eq '1' || "$_" eq '0' };
+
subtype 'Int' => as 'Value' => where { Scalar::Util::looks_like_number($_) };
subtype 'Str' => as 'Value' => where { !Scalar::Util::looks_like_number($_) };
subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' };
-subtype 'ArrayRef' => as 'Ref' => where { ref($_) eq 'ARRAY' };
-subtype 'HashRef' => as 'Ref' => where { ref($_) eq 'HASH' };
+
+subtype 'CollectionRef' => as 'Ref' => where { ref($_) eq 'ARRAY' || ref($_) eq 'HASH' };
+
+subtype 'ArrayRef' => as 'CollectionRef' => where { ref($_) eq 'ARRAY' };
+subtype 'HashRef' => as 'CollectionRef' => where { ref($_) eq 'HASH' };
+
subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' };
subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' };
could probably use some work, but it works for me at the moment.
Any
+ Bool
Value
Int
Str
Ref
ScalarRef
- ArrayRef
- HashRef
+ CollectionRef
+ ArrayRef
+ HashRef
CodeRef
RegexpRef
Object
- Role
+ Role
Suggestions for improvement are welcome.
use strict;
use warnings;
-use Test::More tests => 122;
+use Test::More tests => 143;
use Test::Exception;
use Scalar::Util ();
ok(defined Any(qr/../), '... Any accepts anything');
ok(defined Any(bless {}, 'Foo'), '... Any accepts anything');
+ok(defined Bool(0), '... Bool rejects anything which is not a 1 or 0');
+ok(defined Bool(1), '... Bool rejects anything which is not a 1 or 0');
+ok(!defined Bool(100), '... Bool rejects anything which is not a 1 or 0');
+ok(!defined Bool(''), '... Bool rejects anything which is not a 1 or 0');
+ok(!defined Bool('Foo'), '... Bool rejects anything which is not a 1 or 0');
+ok(!defined Bool([]), '... Bool rejects anything which is not a 1 or 0');
+ok(!defined Bool({}), '... Bool rejects anything which is not a 1 or 0');
+ok(!defined Bool(sub {}), '... Bool rejects anything which is not a 1 or 0');
+ok(!defined Bool($SCALAR_REF), '... Bool rejects anything which is not a 1 or 0');
+ok(!defined Bool(qr/../), '... Bool rejects anything which is not a 1 or 0');
+ok(!defined Bool(bless {}, 'Foo'), '... Bool rejects anything which is not a 1 or 0');
+
ok(defined Value(0), '... Value accepts anything which is not a Ref');
ok(defined Value(100), '... Value accepts anything which is not a Ref');
ok(defined Value(''), '... Value accepts anything which is not a Ref');
ok(!defined ScalarRef(qr/../), '... ScalarRef rejects anything which is not a ScalarRef');
ok(!defined ScalarRef(bless {}, 'Foo'), '... ScalarRef rejects anything which is not a ScalarRef');
+ok(!defined CollectionRef(0), '... CollectionRef rejects anything which is not a HASH or ARRAY');
+ok(!defined CollectionRef(100), '... CollectionRef rejects anything which is not a HASH or ARRAY');
+ok(!defined CollectionRef(''), '... CollectionRef rejects anything which is not a HASH or ARRAY');
+ok(!defined CollectionRef('Foo'), '... CollectionRef rejects anything which is not a HASH or ARRAY');
+ok(defined CollectionRef([]), '... CollectionRef accepts anything which is not a HASH or ARRAY');
+ok(defined CollectionRef({}), '... CollectionRef rejects anything which is not a HASH or ARRAY');
+ok(!defined CollectionRef(sub {}), '... CollectionRef rejects anything which is not a HASH or ARRAY');
+ok(!defined CollectionRef($SCALAR_REF), '... CollectionRef rejects anything which is not a HASH or ARRAY');
+ok(!defined CollectionRef(qr/../), '... CollectionRef rejects anything which is not a HASH or ARRAY');
+ok(!defined CollectionRef(bless {}, 'Foo'), '... CollectionRef rejects anything which is not a HASH or ARRAY');
+
ok(!defined ArrayRef(0), '... ArrayRef rejects anything which is not a ArrayRef');
ok(!defined ArrayRef(100), '... ArrayRef rejects anything which is not a ArrayRef');
ok(!defined ArrayRef(''), '... ArrayRef rejects anything which is not a ArrayRef');