From: Stevan Little Date: Fri, 21 Apr 2006 18:49:41 +0000 (+0000) Subject: more-tests X-Git-Tag: 0_05~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5a4c549307725560117d91a2d356645082371ee8;p=gitmo%2FMoose.git more-tests --- diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index fc9a14e..61c2b62 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -46,7 +46,7 @@ sub compile_type_constraint () { $self->_compiled_type_constraint(subname $self->name => sub { local $_ = $_[0]; return undef unless defined $parent->($_[0]) && $check->($_[0]); - $_[0]; + 1; }); } else { @@ -54,7 +54,7 @@ sub compile_type_constraint () { $self->_compiled_type_constraint(subname $self->name => sub { local $_ = $_[0]; return undef unless $check->($_[0]); - $_[0]; + 1; }); } } diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 0a81da0..4a9e4aa 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -109,12 +109,12 @@ type 'Item' => where { 1 }; # base-type subtype 'Undef' => as 'Item' => where { !defined($_) }; subtype 'Defined' => as 'Item' => where { defined($_) }; -subtype 'Value' => as 'Item' => where { !ref($_) }; -subtype 'Ref' => as 'Item' => where { ref($_) }; - subtype 'Bool' => as 'Item' => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' }; -subtype 'Str' => as 'Value' => where { defined($_) }; +subtype 'Value' => as 'Defined' => where { !ref($_) }; +subtype 'Ref' => as 'Defined' => where { ref($_) }; + +subtype 'Str' => as 'Value' => where { 1 }; subtype 'Num' => as 'Value' => where { Scalar::Util::looks_like_number($_) }; subtype 'Int' => as 'Num' => where { "$_" =~ /^[0-9]+$/ }; @@ -185,23 +185,23 @@ This module also provides a simple hierarchy for Perl 5 types, this could probably use some work, but it works for me at the moment. Any - Item + Bool Undef Defined - Bool - Value - Int - Str - Ref - ScalarRef - CollectionRef - ArrayRef - HashRef - CodeRef - RegexpRef - Object - Role + Value + Num + Int + Str + Ref + ScalarRef + CollectionRef + ArrayRef + HashRef + CodeRef + RegexpRef + Object + Role Suggestions for improvement are welcome. diff --git a/t/050_util_type_constraints.t b/t/050_util_type_constraints.t index c651487..5d9e6d1 100644 --- a/t/050_util_type_constraints.t +++ b/t/050_util_type_constraints.t @@ -12,11 +12,11 @@ BEGIN { use_ok('Moose::Util::TypeConstraints'); } -type Num => where { Scalar::Util::looks_like_number($_) }; +type Number => where { Scalar::Util::looks_like_number($_) }; type String => where { !ref($_) && !Num($_) }; subtype Natural - => as Num + => as Number => where { $_ > 0 }; subtype NaturalLessThanTen @@ -26,28 +26,28 @@ subtype NaturalLessThanTen Moose::Util::TypeConstraints->export_type_contstraints_as_functions(); -is(Num(5), 5, '... this is a Num'); -ok(!defined(Num('Foo')), '... this is not a Num'); +ok(Number(5), '... this is a Num'); +ok(!defined(Number('Foo')), '... this is not a Num'); -is(String('Foo'), 'Foo', '... this is a Str'); +ok(String('Foo'), '... this is a Str'); ok(!defined(String(5)), '... this is not a Str'); -is(Natural(5), 5, '... this is a Natural'); +ok(Natural(5), '... this is a Natural'); is(Natural(-5), undef, '... this is not a Natural'); is(Natural('Foo'), undef, '... this is not a Natural'); -is(NaturalLessThanTen(5), 5, '... this is a NaturalLessThanTen'); +ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen'); is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen'); is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen'); is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen'); # anon sub-typing -my $negative = subtype Num => where { $_ < 0 }; +my $negative = subtype Number => where { $_ < 0 }; ok(defined $negative, '... got a value back from negative'); isa_ok($negative, 'Moose::Meta::TypeConstraint'); -is($negative->check(-5), -5, '... this is a negative number'); +ok($negative->check(-5), '... this is a negative number'); ok(!defined($negative->check(5)), '... this is not a negative number'); is($negative->check('Foo'), undef, '... this is not a negative number'); diff --git a/t/052_util_std_type_constraints.t b/t/052_util_std_type_constraints.t index df63a1b..e340b63 100644 --- a/t/052_util_std_type_constraints.t +++ b/t/052_util_std_type_constraints.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 143; +use Test::More tests => 205; use Test::Exception; use Scalar::Util (); @@ -26,18 +26,56 @@ ok(defined Any(sub {}), '... Any accepts anything'); ok(defined Any($SCALAR_REF), '... Any accepts anything'); 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 Any(undef), '... Any accepts anything'); + +ok(defined Item(0), '... Item is the base type, so accepts anything'); +ok(defined Item(100), '... Item is the base type, so accepts anything'); +ok(defined Item(''), '... Item is the base type, so accepts anything'); +ok(defined Item('Foo'), '... Item is the base type, so accepts anything'); +ok(defined Item([]), '... Item is the base type, so accepts anything'); +ok(defined Item({}), '... Item is the base type, so accepts anything'); +ok(defined Item(sub {}), '... Item is the base type, so accepts anything'); +ok(defined Item($SCALAR_REF), '... Item is the base type, so accepts anything'); +ok(defined Item(qr/../), '... Item is the base type, so accepts anything'); +ok(defined Item(bless {}, 'Foo'), '... Item is the base type, so accepts anything'); +ok(defined Item(undef), '... Item is the base type, so accepts anything'); + +ok(defined Defined(0), '... Defined accepts anything which is defined'); +ok(defined Defined(100), '... Defined accepts anything which is defined'); +ok(defined Defined(''), '... Defined accepts anything which is defined'); +ok(defined Defined('Foo'), '... Defined accepts anything which is defined'); +ok(defined Defined([]), '... Defined accepts anything which is defined'); +ok(defined Defined({}), '... Defined accepts anything which is defined'); +ok(defined Defined(sub {}), '... Defined accepts anything which is defined'); +ok(defined Defined($SCALAR_REF), '... Defined accepts anything which is defined'); +ok(defined Defined(qr/../), '... Defined accepts anything which is defined'); +ok(defined Defined(bless {}, 'Foo'), '... Defined accepts anything which is defined'); +ok(!defined Defined(undef), '... Defined accepts anything which is defined'); + +ok(!defined Undef(0), '... Undef accepts anything which is not defined'); +ok(!defined Undef(100), '... Undef accepts anything which is not defined'); +ok(!defined Undef(''), '... Undef accepts anything which is not defined'); +ok(!defined Undef('Foo'), '... Undef accepts anything which is not defined'); +ok(!defined Undef([]), '... Undef accepts anything which is not defined'); +ok(!defined Undef({}), '... Undef accepts anything which is not defined'); +ok(!defined Undef(sub {}), '... Undef accepts anything which is not defined'); +ok(!defined Undef($SCALAR_REF), '... Undef accepts anything which is not defined'); +ok(!defined Undef(qr/../), '... Undef accepts anything which is not defined'); +ok(!defined Undef(bless {}, 'Foo'), '... Undef accepts anything which is not defined'); +ok(defined Undef(undef), '... Undef accepts anything which is not defined'); + +ok(defined Bool(0), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(defined Bool(1), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(!defined Bool(100), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(defined Bool(''), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(!defined Bool('Foo'), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(!defined Bool([]), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(!defined Bool({}), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(!defined Bool(sub {}), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(!defined Bool($SCALAR_REF), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(!defined Bool(qr/../), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(!defined Bool(bless {}, 'Foo'), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); +ok(defined Bool(undef), '... Bool rejects anything which is not a 1 or 0 or "" or undef'); ok(defined Value(0), '... Value accepts anything which is not a Ref'); ok(defined Value(100), '... Value accepts anything which is not a Ref'); @@ -49,6 +87,7 @@ ok(!defined Value(sub {}), '... Value rejects anything which is not a ok(!defined Value($SCALAR_REF), '... Value rejects anything which is not a Value'); ok(!defined Value(qr/../), '... Value rejects anything which is not a Value'); ok(!defined Value(bless {}, 'Foo'), '... Value rejects anything which is not a Value'); +ok(!defined Value(undef), '... Value rejects anything which is not a Value'); ok(!defined Ref(0), '... Ref accepts anything which is not a Value'); ok(!defined Ref(100), '... Ref accepts anything which is not a Value'); @@ -60,9 +99,12 @@ ok(defined Ref(sub {}), '... Ref rejects anything which is not a Ref') ok(defined Ref($SCALAR_REF), '... Ref rejects anything which is not a Ref'); ok(defined Ref(qr/../), '... Ref rejects anything which is not a Ref'); ok(defined Ref(bless {}, 'Foo'), '... Ref rejects anything which is not a Ref'); +ok(!defined Ref(undef), '... Ref rejects anything which is not a Ref'); ok(defined Int(0), '... Int accepts anything which is an Int'); ok(defined Int(100), '... Int accepts anything which is an Int'); +ok(!defined Int(0.5), '... Int accepts anything which is not a Int'); +ok(!defined Int(100.01), '... Int accepts anything which is not a Int'); ok(!defined Int(''), '... Int rejects anything which is not a Int'); ok(!defined Int('Foo'), '... Int rejects anything which is not a Int'); ok(!defined Int([]), '... Int rejects anything which is not a Int'); @@ -71,9 +113,24 @@ ok(!defined Int(sub {}), '... Int rejects anything which is not a Int' ok(!defined Int($SCALAR_REF), '... Int rejects anything which is not a Int'); ok(!defined Int(qr/../), '... Int rejects anything which is not a Int'); ok(!defined Int(bless {}, 'Foo'), '... Int rejects anything which is not a Int'); - -ok(!defined Str(0), '... Str rejects anything which is not a Str'); -ok(!defined Str(100), '... Str rejects anything which is not a Str'); +ok(!defined Int(undef), '... Int rejects anything which is not a Int'); + +ok(defined Num(0), '... Num accepts anything which is an Num'); +ok(defined Num(100), '... Num accepts anything which is an Num'); +ok(defined Num(0.5), '... Num accepts anything which is an Num'); +ok(defined Num(100.01), '... Num accepts anything which is an Num'); +ok(!defined Num(''), '... Num rejects anything which is not a Num'); +ok(!defined Num('Foo'), '... Num rejects anything which is not a Num'); +ok(!defined Num([]), '... Num rejects anything which is not a Num'); +ok(!defined Num({}), '... Num rejects anything which is not a Num'); +ok(!defined Num(sub {}), '... Num rejects anything which is not a Num'); +ok(!defined Num($SCALAR_REF), '... Num rejects anything which is not a Num'); +ok(!defined Num(qr/../), '... Num rejects anything which is not a Num'); +ok(!defined Num(bless {}, 'Foo'), '... Num rejects anything which is not a Num'); +ok(!defined Num(undef), '... Num rejects anything which is not a Num'); + +ok(defined Str(0), '... Str accepts anything which is a Str'); +ok(defined Str(100), '... Str accepts anything which is a Str'); ok(defined Str(''), '... Str accepts anything which is a Str'); ok(defined Str('Foo'), '... Str accepts anything which is a Str'); ok(!defined Str([]), '... Str rejects anything which is not a Str'); @@ -82,6 +139,7 @@ ok(!defined Str(sub {}), '... Str rejects anything which is not a Str' ok(!defined Str($SCALAR_REF), '... Str rejects anything which is not a Str'); ok(!defined Str(qr/../), '... Str rejects anything which is not a Str'); ok(!defined Str(bless {}, 'Foo'), '... Str rejects anything which is not a Str'); +ok(!defined Str(undef), '... Str rejects anything which is not a Str'); ok(!defined ScalarRef(0), '... ScalarRef rejects anything which is not a ScalarRef'); ok(!defined ScalarRef(100), '... ScalarRef rejects anything which is not a ScalarRef'); @@ -93,6 +151,7 @@ ok(!defined ScalarRef(sub {}), '... ScalarRef rejects anything which i ok(defined ScalarRef($SCALAR_REF), '... ScalarRef accepts anything which is a ScalarRef'); 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 ScalarRef(undef), '... 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'); @@ -104,6 +163,7 @@ ok(!defined CollectionRef(sub {}), '... CollectionRef rejects anything 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 CollectionRef(undef), '... 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'); @@ -115,6 +175,7 @@ ok(!defined ArrayRef(sub {}), '... ArrayRef rejects anything which is ok(!defined ArrayRef($SCALAR_REF), '... ArrayRef rejects anything which is not a ArrayRef'); ok(!defined ArrayRef(qr/../), '... ArrayRef rejects anything which is not a ArrayRef'); ok(!defined ArrayRef(bless {}, 'Foo'), '... ArrayRef rejects anything which is not a ArrayRef'); +ok(!defined ArrayRef(undef), '... ArrayRef rejects anything which is not a ArrayRef'); ok(!defined HashRef(0), '... HashRef rejects anything which is not a HashRef'); ok(!defined HashRef(100), '... HashRef rejects anything which is not a HashRef'); @@ -126,6 +187,7 @@ ok(!defined HashRef(sub {}), '... HashRef rejects anything which is no ok(!defined HashRef($SCALAR_REF), '... HashRef rejects anything which is not a HashRef'); ok(!defined HashRef(qr/../), '... HashRef rejects anything which is not a HashRef'); ok(!defined HashRef(bless {}, 'Foo'), '... HashRef rejects anything which is not a HashRef'); +ok(!defined HashRef(undef), '... HashRef rejects anything which is not a HashRef'); ok(!defined CodeRef(0), '... CodeRef rejects anything which is not a CodeRef'); ok(!defined CodeRef(100), '... CodeRef rejects anything which is not a CodeRef'); @@ -137,6 +199,7 @@ ok(defined CodeRef(sub {}), '... CodeRef accepts anything which is a ok(!defined CodeRef($SCALAR_REF), '... CodeRef rejects anything which is not a CodeRef'); ok(!defined CodeRef(qr/../), '... CodeRef rejects anything which is not a CodeRef'); ok(!defined CodeRef(bless {}, 'Foo'), '... CodeRef rejects anything which is not a CodeRef'); +ok(!defined CodeRef(undef), '... CodeRef rejects anything which is not a CodeRef'); ok(!defined RegexpRef(0), '... RegexpRef rejects anything which is not a RegexpRef'); ok(!defined RegexpRef(100), '... RegexpRef rejects anything which is not a RegexpRef'); @@ -148,6 +211,7 @@ ok(!defined RegexpRef(sub {}), '... RegexpRef rejects anything which i ok(!defined RegexpRef($SCALAR_REF), '... RegexpRef rejects anything which is not a RegexpRef'); ok(defined RegexpRef(qr/../), '... RegexpRef accepts anything which is a RegexpRef'); ok(!defined RegexpRef(bless {}, 'Foo'), '... RegexpRef rejects anything which is not a RegexpRef'); +ok(!defined RegexpRef(undef), '... RegexpRef rejects anything which is not a RegexpRef'); ok(!defined Object(0), '... Object rejects anything which is not blessed'); ok(!defined Object(100), '... Object rejects anything which is not blessed'); @@ -159,22 +223,24 @@ ok(!defined Object(sub {}), '... Object rejects anything which is not ok(!defined Object($SCALAR_REF), '... Object rejects anything which is not blessed'); ok(!defined Object(qr/../), '... Object rejects anything which is not blessed'); ok(defined Object(bless {}, 'Foo'), '... Object accepts anything which is blessed'); +ok(!defined Object(undef), '... Object accepts anything which is blessed'); { package My::Role; sub does { 'fake' } } -ok(!defined Role(0), '... Role rejects anything which is not a Role'); -ok(!defined Role(100), '... Role rejects anything which is not a Role'); -ok(!defined Role(''), '... Role rejects anything which is not a Role'); -ok(!defined Role('Foo'), '... Role rejects anything which is not a Role'); -ok(!defined Role([]), '... Role rejects anything which is not a Role'); -ok(!defined Role({}), '... Role rejects anything which is not a Role'); -ok(!defined Role(sub {}), '... Role rejects anything which is not a Role'); -ok(!defined Role($SCALAR_REF), '... Role rejects anything which is not a Role'); -ok(!defined Role(qr/../), '... Role rejects anything which is not a Role'); -ok(!defined Role(bless {}, 'Foo'), '... Role accepts anything which is not a Role'); +ok(!defined Role(0), '... Role rejects anything which is not a Role'); +ok(!defined Role(100), '... Role rejects anything which is not a Role'); +ok(!defined Role(''), '... Role rejects anything which is not a Role'); +ok(!defined Role('Foo'), '... Role rejects anything which is not a Role'); +ok(!defined Role([]), '... Role rejects anything which is not a Role'); +ok(!defined Role({}), '... Role rejects anything which is not a Role'); +ok(!defined Role(sub {}), '... Role rejects anything which is not a Role'); +ok(!defined Role($SCALAR_REF), '... Role rejects anything which is not a Role'); +ok(!defined Role(qr/../), '... Role rejects anything which is not a Role'); +ok(!defined Role(bless {}, 'Foo'), '... Role accepts anything which is not a Role'); ok(defined Role(bless {}, 'My::Role'), '... Role accepts anything which is not a Role'); +ok(!defined Role(undef), '... Role accepts anything which is not a Role');