From: Stevan Little Date: Fri, 22 Sep 2006 21:19:46 +0000 (+0000) Subject: foo X-Git-Tag: 0_14~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3f7376b0954bb3d5862741dd90dcf20ec16a0c18;p=gitmo%2FMoose.git foo --- diff --git a/Changes b/Changes index 94a6e63..31a5682 100644 --- a/Changes +++ b/Changes @@ -18,6 +18,14 @@ Revision history for Perl extension Moose - added a FAQ and WTF files to document frequently asked questions and common problems + * Moose::Util::TypeConstraints + - added GlobRef type constraint + - added tests for this + + * Moose::Meta::Attribute + - if your attribute 'isa' ArrayRef of HashRef, and you have + not explicitly set a default, then make the default DWIM. + * Moose::Meta::Role - added basic support for runtime role composition but this is still highly experimental diff --git a/MANIFEST b/MANIFEST index f6ed41e..f2451a2 100644 --- a/MANIFEST +++ b/MANIFEST @@ -75,6 +75,7 @@ t/056_util_more_type_coercion.t t/057_union_types.t t/060_moose_for_meta.t t/070_more_attr_delegation.t +t/071_misc_attribute_tests.t t/100_subtype_quote_bug.t t/101_subtype_conflict_bug.t t/102_Moose_Object_error.t diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 5feaf85..2c5e01c 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -174,6 +174,13 @@ sub _process_options { || confess "You cannot auto-dereference anything other than a ArrayRef or HashRef"; } + if (exists $options->{type_constraint} && $options->{type_constraint}->name =~ /^ArrayRef|HashRef$/) { + unless (exists $options->{default}) { + $options->{default} = sub { [] } if $options->{type_constraint}->name eq 'ArrayRef'; + $options->{default} = sub { {} } if $options->{type_constraint}->name eq 'HashRef'; + } + } + if (exists $options->{lazy} && $options->{lazy}) { (exists $options->{default}) || confess "You cannot have lazy attribute without specifying a default value for it"; diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 00a0fd9..dbd4844 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -7,7 +7,7 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed'; -our $VERSION = '0.07'; +our $VERSION = '0.08'; use Moose::Meta::TypeConstraint; use Moose::Meta::TypeCoercion; @@ -142,6 +142,7 @@ subtype 'ArrayRef' => as 'Ref' => where { ref($_) eq 'ARRAY' }; subtype 'HashRef' => as 'Ref' => where { ref($_) eq 'HASH' }; subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' }; subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' }; +subtype 'GlobRef' => as 'Ref' => where { ref($_) eq 'GLOB' }; # NOTE: # blessed(qr/.../) returns true,.. how odd @@ -239,6 +240,7 @@ could probably use some work, but it works for me at the moment. HashRef CodeRef RegexpRef + GlobRef Object Role diff --git a/t/032_attribute_accessor_generation.t b/t/032_attribute_accessor_generation.t index f86e264..1a24860 100644 --- a/t/032_attribute_accessor_generation.t +++ b/t/032_attribute_accessor_generation.t @@ -155,7 +155,7 @@ BEGIN { ok(isweak($foo->{foo_weak}), '... it is a weak reference'); can_ok( $foo, 'foo_deref'); - is( $foo->foo_deref(), undef, '... unset value'); + is_deeply( [$foo->foo_deref()], [], '... default default value'); my @list; lives_ok { @list = $foo->foo_deref(); @@ -174,7 +174,7 @@ BEGIN { can_ok( $foo, 'foo_deref' ); - is( $foo->foo_deref_ro(), undef, "... unset value" ); + is_deeply( [$foo->foo_deref_ro()], [], "... default default value" ); dies_ok { $foo->foo_deref_ro( [] ); @@ -186,7 +186,7 @@ BEGIN { is_deeply( [ $foo->foo_deref_ro() ], [qw/la la la/], "list context ro" ); can_ok( $foo, 'foo_deref_hash' ); - is( $foo->foo_deref_hash(), undef, "... unset value" ); + is_deeply( { $foo->foo_deref_hash() }, {}, "... default default value" ); my %hash; lives_ok { diff --git a/t/052_util_std_type_constraints.t b/t/052_util_std_type_constraints.t index 4a97f6c..acd2ab1 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 => 194; +use Test::More tests => 223; use Test::Exception; use Scalar::Util (); @@ -13,6 +13,7 @@ BEGIN { } my $SCALAR_REF = \(my $var); +my $GLOB_REF = \*GLOB_REF; Moose::Util::TypeConstraints->export_type_contstraints_as_functions(); @@ -24,6 +25,7 @@ ok(defined Any([]), '... Any accepts anything'); ok(defined Any({}), '... Any accepts anything'); ok(defined Any(sub {}), '... Any accepts anything'); ok(defined Any($SCALAR_REF), '... Any accepts anything'); +ok(defined Any($GLOB_REF), '... Any accepts anything'); ok(defined Any(qr/../), '... Any accepts anything'); ok(defined Any(bless {}, 'Foo'), '... Any accepts anything'); ok(defined Any(undef), '... Any accepts anything'); @@ -36,6 +38,7 @@ ok(defined Item([]), '... Item is the base type, so accepts anythin 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($GLOB_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'); @@ -48,6 +51,7 @@ ok(defined Defined([]), '... Defined accepts anything which is defi 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($GLOB_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'); @@ -60,6 +64,7 @@ ok(!defined Undef([]), '... Undef accepts anything which is not def 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($GLOB_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'); @@ -73,6 +78,7 @@ ok(!defined Bool([]), '... Bool rejects anything which is not a 1 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($GLOB_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'); @@ -85,6 +91,7 @@ ok(!defined Value([]), '... Value rejects anything which is not a ok(!defined Value({}), '... Value rejects anything which is not a Value'); ok(!defined Value(sub {}), '... Value rejects anything which is not a Value'); ok(!defined Value($SCALAR_REF), '... Value rejects anything which is not a Value'); +ok(!defined Value($GLOB_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'); @@ -97,6 +104,7 @@ ok(defined Ref([]), '... Ref rejects anything which is not a Ref') ok(defined Ref({}), '... Ref rejects anything which is not a Ref'); 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($GLOB_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'); @@ -111,6 +119,7 @@ ok(!defined Int([]), '... Int rejects anything which is not a Int' ok(!defined Int({}), '... Int rejects anything which is not a Int'); 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($GLOB_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 Int(undef), '... Int rejects anything which is not a Int'); @@ -125,6 +134,7 @@ 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($GLOB_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'); @@ -137,6 +147,7 @@ ok(!defined Str([]), '... Str rejects anything which is not a Str' ok(!defined Str({}), '... Str rejects anything which is not a Str'); 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($GLOB_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'); @@ -149,6 +160,7 @@ ok(!defined ScalarRef([]), '... ScalarRef rejects anything which i ok(!defined ScalarRef({}), '... ScalarRef rejects anything which is not a ScalarRef'); ok(!defined ScalarRef(sub {}), '... ScalarRef rejects anything which is not a ScalarRef'); ok(defined ScalarRef($SCALAR_REF), '... ScalarRef accepts anything which is a ScalarRef'); +ok(!defined ScalarRef($GLOB_REF), '... ScalarRef rejects anything which is not 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'); @@ -161,6 +173,7 @@ ok(defined ArrayRef([]), '... ArrayRef accepts anything which is ok(!defined ArrayRef({}), '... ArrayRef rejects anything which is not a ArrayRef'); ok(!defined ArrayRef(sub {}), '... ArrayRef rejects anything which is not a ArrayRef'); ok(!defined ArrayRef($SCALAR_REF), '... ArrayRef rejects anything which is not a ArrayRef'); +ok(!defined ArrayRef($GLOB_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'); @@ -173,6 +186,7 @@ ok(!defined HashRef([]), '... HashRef rejects anything which is no ok(defined HashRef({}), '... HashRef accepts anything which is a HashRef'); ok(!defined HashRef(sub {}), '... HashRef rejects anything which is not a HashRef'); ok(!defined HashRef($SCALAR_REF), '... HashRef rejects anything which is not a HashRef'); +ok(!defined HashRef($GLOB_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'); @@ -185,6 +199,7 @@ ok(!defined CodeRef([]), '... CodeRef rejects anything which is no ok(!defined CodeRef({}), '... CodeRef rejects anything which is not a CodeRef'); ok(defined CodeRef(sub {}), '... CodeRef accepts anything which is a CodeRef'); ok(!defined CodeRef($SCALAR_REF), '... CodeRef rejects anything which is not a CodeRef'); +ok(!defined CodeRef($GLOB_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'); @@ -197,10 +212,24 @@ ok(!defined RegexpRef([]), '... RegexpRef rejects anything which i ok(!defined RegexpRef({}), '... RegexpRef rejects anything which is not a RegexpRef'); ok(!defined RegexpRef(sub {}), '... RegexpRef rejects anything which is not a RegexpRef'); ok(!defined RegexpRef($SCALAR_REF), '... RegexpRef rejects anything which is not a RegexpRef'); +ok(!defined RegexpRef($GLOB_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 GlobRef(0), '... GlobRef rejects anything which is not a GlobRef'); +ok(!defined GlobRef(100), '... GlobRef rejects anything which is not a GlobRef'); +ok(!defined GlobRef(''), '... GlobRef rejects anything which is not a GlobRef'); +ok(!defined GlobRef('Foo'), '... GlobRef rejects anything which is not a GlobRef'); +ok(!defined GlobRef([]), '... GlobRef rejects anything which is not a GlobRef'); +ok(!defined GlobRef({}), '... GlobRef rejects anything which is not a GlobRef'); +ok(!defined GlobRef(sub {}), '... GlobRef rejects anything which is not a GlobRef'); +ok(!defined GlobRef($SCALAR_REF), '... GlobRef rejects anything which is not a GlobRef'); +ok(defined GlobRef($GLOB_REF), '... GlobRef rejects anything which is a GlobRef'); +ok(!defined GlobRef(qr/../), '... GlobRef accepts anything which is not a GlobRef'); +ok(!defined GlobRef(bless {}, 'Foo'), '... GlobRef rejects anything which is not a GlobRef'); +ok(!defined GlobRef(undef), '... GlobRef rejects anything which is not a GlobRef'); + ok(!defined Object(0), '... Object rejects anything which is not blessed'); ok(!defined Object(100), '... Object rejects anything which is not blessed'); ok(!defined Object(''), '... Object rejects anything which is not blessed'); @@ -209,6 +238,7 @@ ok(!defined Object([]), '... Object rejects anything which is not ok(!defined Object({}), '... Object rejects anything which is not blessed'); ok(!defined Object(sub {}), '... Object rejects anything which is not blessed'); ok(!defined Object($SCALAR_REF), '... Object rejects anything which is not blessed'); +ok(!defined Object($GLOB_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'); @@ -226,6 +256,7 @@ ok(!defined Role([]), '... Role rejects anything which is not 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($GLOB_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');