From: Stevan Little Date: Sat, 26 Aug 2006 06:28:48 +0000 (+0000) Subject: lost of misc changes; fixed the &create method; test cleanup X-Git-Tag: 0_34~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=88dd563c8b2edea359b8500535b8b46a8bbe6340;hp=c4260b45e76ce008e4c51987b243f2b0ae4313bb;p=gitmo%2FClass-MOP.git lost of misc changes; fixed the &create method; test cleanup --- diff --git a/Changes b/Changes index 9f6b96d..d223792 100644 --- a/Changes +++ b/Changes @@ -6,6 +6,11 @@ Revision history for Perl extension Class-MOP. the $:version and such just actually goes to the symbol table to get it's stuff. However, it makes the MOP more complete. + ** API CHANGE ** + - The &create method now requires that all + but the package name now is passed in as + named parameters. See docs for more info. + - updated docs and tests for this * Class::MOP::Object - added &dump method to easily Data::Dumper @@ -16,6 +21,8 @@ Revision history for Perl extension Class-MOP. which do not store things in the instance - added the %:methods attribute definition to the bootstrap + + ~ lots of misc. test cleanup 0.33 Sat. Aug. 19, 2006 * Class::MOP::Class diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 88842f3..3dd162c 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -179,7 +179,7 @@ sub check_metaclass_compatability { sub create_anon_class { my ($class, %options) = @_; my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL; - return $class->create($package_name, '0.00', %options); + return $class->create($package_name, %options); } # NOTE: @@ -204,14 +204,27 @@ sub check_metaclass_compatability { # creating classes with MOP ... sub create { - my ($class, $package_name, $package_version, %options) = @_; + my $class = shift; + my $package_name = shift; + (defined $package_name && $package_name) || confess "You must pass a package name"; + + (scalar @_ % 2 == 0) + || confess "You much pass all parameters as name => value pairs " . + "(I found an uneven number of params in \@_)"; + + my (%options) = @_; + my $code = "package $package_name;"; - $code .= "\$$package_name\:\:VERSION = '$package_version';" - if defined $package_version; + $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';" + if exists $options{version}; + $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';" + if exists $options{authority}; + eval $code; confess "creation of $package_name failed : $@" if $@; + my $meta = $class->initialize($package_name); $meta->add_method('meta' => sub { @@ -701,7 +714,8 @@ Class::MOP::Class - Class Meta Object # or use this to actually create classes ... - Class::MOP::Class->create('Bar' => '0.01' => ( + Class::MOP::Class->create('Bar' => ( + version => '0.01', superclasses => [ 'Foo' ], attributes => [ Class::MOP:::Attribute->new('$bar'), @@ -752,15 +766,17 @@ created any more than nessecary. Basically, they are singletons. =over 4 -=item B ?$version, + authority =E ?$authority, superclasses =E ?@superclasses, methods =E ?%methods, attributes =E ?%attributes)> This returns a B object, bringing the specified -C<$package_name> into existence and adding any of the -C<$package_version>, C<@superclasses>, C<%methods> and C<%attributes> -to it. +C<$package_name> into existence and adding any of the C<$version>, +C<$authority>, C<@superclasses>, C<%methods> and C<%attributes> to +it. =item B ?@superclasses, methods =E ?%methods, diff --git a/lib/Class/MOP/Class/Immutable.pm b/lib/Class/MOP/Class/Immutable.pm index 574257d..5906784 100644 --- a/lib/Class/MOP/Class/Immutable.pm +++ b/lib/Class/MOP/Class/Immutable.pm @@ -105,20 +105,21 @@ sub _generate_slot_initializer { my $attr = $attrs->[$index]; my $default; if ($attr->has_default) { + # NOTE: + # default values can either be CODE refs + # in which case we need to call them. Or + # they can be scalars (strings/numbers) + # in which case we can just deal with them + # in the code we eval. if ($attr->is_default_a_coderef) { $default = '$attrs->[' . $index . ']->default($instance)'; } else { $default = $attrs->[$index]->default; + # make sure to quote strings ... unless (looks_like_number($default)) { $default = "'$default'"; } - # TODO: - # we should use Data::Dumper to - # output any ref's here, obviously - # we cannot handle Scalar refs, but - # it should work for Array and Hash - # refs pretty well. } } $meta_instance->inline_set_slot_value( diff --git a/lib/Class/MOP/Object.pm b/lib/Class/MOP/Object.pm index 1535cf7..e20e299 100644 --- a/lib/Class/MOP/Object.pm +++ b/lib/Class/MOP/Object.pm @@ -66,7 +66,7 @@ and really just exists to make the Class::MOP metamodel complete. legend: ..(is an instance of)..> - --(is a subclass of)--> + --(is a subclass of)---> A deeper discussion of this model is currently beyond the scope of this documenation. @@ -79,6 +79,12 @@ this documenation. =item B +This will C the L module and then dump a +representation of your object. It passed the C<$max_depth> arg +to C<$Data::Dumper::Maxdepth>. The default C<$max_depth> is 1, +so it will not go crazy and print a massive bunch of stuff. +Adjust this as nessecary. + =back =head1 AUTHORS @@ -94,4 +100,4 @@ L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -=cut \ No newline at end of file +=cut diff --git a/t/000_load.t b/t/000_load.t index 31bd482..35c93e8 100644 --- a/t/000_load.t +++ b/t/000_load.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 16; +use Test::More tests => 17; BEGIN { use_ok('Class::MOP'); @@ -57,4 +57,20 @@ is_deeply( Class::MOP::Object Class::MOP::Package / ], - '... got all the metaclass names'); \ No newline at end of file + '... got all the metaclass names'); + +is_deeply( + [ map { $_->meta->identifier } sort { $a cmp $b } Class::MOP::get_all_metaclass_names() ], + [ + "Class::MOP::Attribute-" . $Class::MOP::Attribute::VERSION . "-cpan:STEVAN", + "Class::MOP::Class-" . $Class::MOP::Class::VERSION . "-cpan:STEVAN", + "Class::MOP::Instance-" . $Class::MOP::Instance::VERSION . "-cpan:STEVAN", + "Class::MOP::Method-" . $Class::MOP::Method::VERSION . "-cpan:STEVAN", + "Class::MOP::Module-" . $Class::MOP::Module::VERSION . "-cpan:STEVAN", + "Class::MOP::Object-" . $Class::MOP::Object::VERSION . "-cpan:STEVAN", + "Class::MOP::Package-" . $Class::MOP::Package::VERSION . "-cpan:STEVAN", + ], + '... got all the metaclass identifiers'); + + + \ No newline at end of file diff --git a/t/001_basic.t b/t/001_basic.t index a4b837c..f4e65d0 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 19; +use Test::More tests => 25; use Test::Exception; BEGIN { @@ -18,6 +18,8 @@ BEGIN { package Bar; our @ISA = ('Foo'); + + our $AUTHORITY = 'cpan:JRANDOM'; } my $Foo = Foo->meta; @@ -32,6 +34,12 @@ is($Bar->name, 'Bar', '... Bar->name == Bar'); is($Foo->version, '0.01', '... Foo->version == 0.01'); is($Bar->version, undef, '... Bar->version == undef'); +is($Foo->authority, undef, '... Foo->authority == undef'); +is($Bar->authority, 'cpan:JRANDOM', '... Bar->authority == cpan:JRANDOM'); + +is($Foo->identifier, 'Foo-0.01', '... Foo->identifier == Foo-0.01'); +is($Bar->identifier, 'Bar-cpan:JRANDOM', '... Bar->identifier == Bar-cpan:JRANDOM'); + is_deeply([$Foo->superclasses], [], '... Foo has no superclasses'); is_deeply([$Bar->superclasses], ['Foo'], '... Bar->superclasses == (Foo)'); @@ -52,7 +60,9 @@ is_deeply( # create a class using Class::MOP::Class ... my $Baz = Class::MOP::Class->create( - 'Baz' => '0.10' => ( + 'Baz' => ( + version => '0.10', + authority => 'cpan:YOMAMA', superclasses => [ 'Bar' ] )); isa_ok($Baz, 'Class::MOP::Class'); @@ -60,6 +70,9 @@ is(Baz->meta, $Baz, '... our metaclasses are singletons'); is($Baz->name, 'Baz', '... Baz->name == Baz'); is($Baz->version, '0.10', '... Baz->version == 0.10'); +is($Baz->authority, 'cpan:YOMAMA', '... Baz->authority == YOMAMA'); + +is($Baz->identifier, 'Baz-0.10-cpan:YOMAMA', '... Baz->identifier == Baz-0.10-cpan:YOMAMA'); is_deeply([$Baz->superclasses], ['Bar'], '... Baz->superclasses == (Bar)'); diff --git a/t/003_methods.t b/t/003_methods.t index d807876..2b0b527 100644 --- a/t/003_methods.t +++ b/t/003_methods.t @@ -174,7 +174,7 @@ is_deeply( # ... test our class creator my $Bar = Class::MOP::Class->create( - 'Bar' => '0.10' => ( + 'Bar' => ( superclasses => [ 'Foo' ], methods => { foo => sub { 'Bar::foo' }, diff --git a/t/011_create_class.t b/t/011_create_class.t index 15252b6..1557927 100644 --- a/t/011_create_class.t +++ b/t/011_create_class.t @@ -10,7 +10,8 @@ BEGIN { use_ok('Class::MOP'); } -my $Point = Class::MOP::Class->create('Point' => '0.01' => ( +my $Point = Class::MOP::Class->create('Point' => ( + version => '0.01', attributes => [ Class::MOP::Attribute->new('$.x' => ( reader => 'x', @@ -35,7 +36,8 @@ my $Point = Class::MOP::Class->create('Point' => '0.01' => ( } )); -my $Point3D = Class::MOP::Class->create('Point3D' => '0.01' => ( +my $Point3D = Class::MOP::Class->create('Point3D' => ( + version => '0.01', superclasses => [ 'Point' ], attributes => [ Class::MOP::Attribute->new('$:z' => ( diff --git a/t/015_metaclass_inheritance.t b/t/015_metaclass_inheritance.t index d8ea970..f8ac55d 100644 --- a/t/015_metaclass_inheritance.t +++ b/t/015_metaclass_inheritance.t @@ -44,4 +44,5 @@ isa_ok($baz_meta, 'Class::MOP::Class'); is($baz_meta->name, 'Baz', '... baz_meta->name == Baz'); isnt($baz_meta, $bar_meta, '... Baz->meta != Bar->meta'); -isnt($baz_meta, $foo_meta, '... Baz->meta != Foo->meta'); \ No newline at end of file +isnt($baz_meta, $foo_meta, '... Baz->meta != Foo->meta'); + diff --git a/t/016_class_errors_and_edge_cases.t b/t/016_class_errors_and_edge_cases.t index c92ccf4..2d2eb7f 100644 --- a/t/016_class_errors_and_edge_cases.t +++ b/t/016_class_errors_and_edge_cases.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 52; +use Test::More tests => 49; use Test::Exception; BEGIN { @@ -216,10 +216,6 @@ BEGIN { dies_ok { Class::MOP::Class->has_package_symbol('foo'); } '... has_package_symbol dies as expected'; - - dies_ok { - Class::MOP::Class->has_package_symbol('&foo'); - } '... has_package_symbol dies as expected'; } { @@ -233,11 +229,7 @@ BEGIN { dies_ok { Class::MOP::Class->get_package_symbol('foo'); - } '... get_package_symbol dies as expected'; - - dies_ok { - Class::MOP::Class->get_package_symbol('&foo'); - } '... get_package_symbol dies as expected'; + } '... get_package_symbol dies as expected'; } { @@ -252,9 +244,5 @@ BEGIN { dies_ok { Class::MOP::Class->remove_package_symbol('foo'); } '... remove_package_symbol dies as expected'; - - dies_ok { - Class::MOP::Class->remove_package_symbol('&foo'); - } '... remove_package_symbol dies as expected'; } diff --git a/t/021_attribute_errors_and_edge_cases.t b/t/021_attribute_errors_and_edge_cases.t index 2974b8d..7e948ab 100644 --- a/t/021_attribute_errors_and_edge_cases.t +++ b/t/021_attribute_errors_and_edge_cases.t @@ -118,7 +118,7 @@ BEGIN { predicate => 'has_test' )); - my $Bar = Class::MOP::Class->create('Bar' => '0.01'); + my $Bar = Class::MOP::Class->create('Bar'); isa_ok($Bar, 'Class::MOP::Class'); $Bar->add_attribute($attr); diff --git a/t/102_InsideOutClass_test.t b/t/102_InsideOutClass_test.t index 4788dfc..b8c5d62 100644 --- a/t/102_InsideOutClass_test.t +++ b/t/102_InsideOutClass_test.t @@ -3,8 +3,9 @@ use strict; use warnings; -use Test::More tests => 85; +use Test::More tests => 89; use File::Spec; +use Scalar::Util 'reftype'; BEGIN { use_ok('Class::MOP'); @@ -75,6 +76,8 @@ BEGIN { my $foo = Foo->new(); isa_ok($foo, 'Foo'); +is(reftype($foo), 'SCALAR', '... Foo is made with SCALAR'); + can_ok($foo, 'foo'); can_ok($foo, 'has_foo'); can_ok($foo, 'get_bar'); @@ -95,6 +98,8 @@ is($foo->get_bar(), 42, '... Foo::bar == 42'); my $foo2 = Foo->new(); isa_ok($foo2, 'Foo'); +is(reftype($foo2), 'SCALAR', '... Foo is made with SCALAR'); + ok(!$foo2->has_foo, '... Foo2::foo is not defined yet'); is($foo2->foo(), undef, '... Foo2::foo is not defined yet'); is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized'); @@ -110,6 +115,8 @@ my $bar = Bar->new(); isa_ok($bar, 'Bar'); isa_ok($bar, 'Foo'); +is(reftype($bar), 'SCALAR', '... Bar is made with SCALAR'); + can_ok($bar, 'foo'); can_ok($bar, 'has_foo'); can_ok($bar, 'get_bar'); @@ -144,6 +151,8 @@ isa_ok($baz, 'Bar'); isa_ok($baz, 'Foo'); isa_ok($baz, 'Baz'); +is(reftype($baz), 'SCALAR', '... Bar::Baz is made with SCALAR'); + can_ok($baz, 'foo'); can_ok($baz, 'has_foo'); can_ok($baz, 'get_bar'); diff --git a/t/108_ArrayBasedStorage_test.t b/t/108_ArrayBasedStorage_test.t index 17add18..c36a111 100644 --- a/t/108_ArrayBasedStorage_test.t +++ b/t/108_ArrayBasedStorage_test.t @@ -3,8 +3,9 @@ use strict; use warnings; -use Test::More tests => 65; +use Test::More tests => 69; use File::Spec; +use Scalar::Util 'reftype'; BEGIN { use_ok('Class::MOP'); @@ -72,6 +73,8 @@ BEGIN { my $foo = Foo->new(); isa_ok($foo, 'Foo'); +is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY'); + can_ok($foo, 'foo'); can_ok($foo, 'has_foo'); can_ok($foo, 'get_bar'); @@ -92,6 +95,8 @@ is($foo->get_bar(), 42, '... Foo::bar == 42'); my $foo2 = Foo->new(); isa_ok($foo2, 'Foo'); +is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY'); + ok(!$foo2->has_foo, '... Foo2::foo is not defined yet'); is($foo2->foo(), undef, '... Foo2::foo is not defined yet'); is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized'); @@ -107,6 +112,8 @@ my $bar = Bar->new(); isa_ok($bar, 'Bar'); isa_ok($bar, 'Foo'); +is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY'); + can_ok($bar, 'foo'); can_ok($bar, 'has_foo'); can_ok($bar, 'get_bar'); @@ -141,6 +148,8 @@ isa_ok($baz, 'Bar'); isa_ok($baz, 'Foo'); isa_ok($baz, 'Baz'); +is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY'); + can_ok($baz, 'foo'); can_ok($baz, 'has_foo'); can_ok($baz, 'get_bar');