From: Dave Rolsky Date: Wed, 22 Oct 2008 20:46:02 +0000 (+0000) Subject: Tests for resolve_meta{class,trait}_alias in Moose::Util. X-Git-Tag: 0.60~20 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=50fbbf3d01c2e3c1638cf8e22498d9dbe9f84791;p=gitmo%2FMoose.git Tests for resolve_meta{class,trait}_alias in Moose::Util. Moved caching to resolve_metaclass_alias. --- diff --git a/Changes b/Changes index 2ea489a..14621c7 100644 --- a/Changes +++ b/Changes @@ -7,12 +7,13 @@ Revision history for Perl extension Moose - Tests for this bug. (t0m) * Moose::Util - - Change resolve_metaclass alias to use the new + - Change resolve_metaclass alias to use the new load_first_existing_class function. This makes it a lot simpler, and also around 5 times faster. (t0m) - - Add caching to resolve_metatrait_alias, which gives an - order of magnitude speedup to things which repeatedly call - the does method. E.g. MooseX::Storage (t0m) + - Add caching to resolve_metaclass_alias, which gives an order + of magnitude speedup to things which repeatedly call the + Moose::Meta::Attribute->does method. E.g. MooseX::Storage + (t0m) 0.59 Tue October 14, 2008 * Moose diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index 2f66c0d..7c3a683 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -118,28 +118,39 @@ sub get_all_init_args { }; } -{ my %cache; - sub resolve_metatrait_alias { - my ( $type, $metaclass_name ) = @_; - - return $cache{$type}{$metaclass_name} if $cache{$type}{$metaclass_name}; - - my $class = resolve_metaclass_alias( @_, trait => 1 ); - $cache{$type}{$metaclass_name} = $class if $class; - - return $class; - } +sub resolve_metatrait_alias { + my ( $type, $metaclass_name ) = @_; + + return resolve_metaclass_alias( @_, trait => 1 ); } -sub resolve_metaclass_alias { - my ( $type, $metaclass_name, %options ) = @_; +{ + my %cache; - my $possible_full_name = 'Moose::Meta::' . $type . '::Custom::' . ( $options{trait} ? "Trait::" : "" ) . $metaclass_name; - my $loaded_class = Class::MOP::load_first_existing_class($possible_full_name, $metaclass_name); + sub resolve_metaclass_alias { + my ( $type, $metaclass_name, %options ) = @_; - $loaded_class->can('register_implementation') - ? $loaded_class->register_implementation - : $loaded_class; + my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' ); + return $cache{$cache_key}{$metaclass_name} + if $cache{$cache_key}{$metaclass_name}; + + my $possible_full_name + = 'Moose::Meta::' + . $type + . '::Custom::' + . ( $options{trait} ? "Trait::" : "" ) + . $metaclass_name; + + my $loaded_class = Class::MOP::load_first_existing_class( + $possible_full_name, + $metaclass_name + ); + + return $cache{$cache_key}{$metaclass_name} + = $loaded_class->can('register_implementation') + ? $loaded_class->register_implementation + : $loaded_class; + } } sub add_method_modifier { diff --git a/t/400_moose_util/004_resolve_alias.t b/t/400_moose_util/004_resolve_alias.t new file mode 100644 index 0000000..f5c55a5 --- /dev/null +++ b/t/400_moose_util/004_resolve_alias.t @@ -0,0 +1,77 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 16; + +use Moose::Util qw( resolve_metaclass_alias resolve_metatrait_alias ); + +use lib 't/lib'; + +# Doing each test twice is intended to make sure that the caching +# doesn't break name resolution. It doesn't actually test that +# anything is cached. +is( resolve_metaclass_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Foo' ), + 'Moose::Meta::Attribute::Custom::Foo', + 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Foo' ); + +is( resolve_metaclass_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Foo' ), + 'Moose::Meta::Attribute::Custom::Foo', + 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Foo second time' ); + +is( resolve_metaclass_alias( 'Attribute', 'Foo' ), + 'Moose::Meta::Attribute::Custom::Foo', + 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Foo via alias (Foo)' ); + +is( resolve_metaclass_alias( 'Attribute', 'Foo' ), + 'Moose::Meta::Attribute::Custom::Foo', + 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Foo via alias (Foo) a second time' ); + +is( resolve_metaclass_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Bar' ), + 'My::Bar', + 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Bar as My::Bar' ); + +is( resolve_metaclass_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Bar' ), + 'My::Bar', + 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Bar as My::Bar a second time' ); + +is( resolve_metaclass_alias( 'Attribute', 'Bar' ), + 'My::Bar', + 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Bar as My::Bar via alias (Bar)' ); + +is( resolve_metaclass_alias( 'Attribute', 'Bar' ), + 'My::Bar', + 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Bar as My::Bar via alias (Bar) a second time' ); + +is( resolve_metatrait_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Trait::Foo' ), + 'Moose::Meta::Attribute::Custom::Trait::Foo', + 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Foo' ); + +is( resolve_metatrait_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Trait::Foo' ), + 'Moose::Meta::Attribute::Custom::Trait::Foo', + 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Foo second time' ); + +is( resolve_metatrait_alias( 'Attribute', 'Foo' ), + 'Moose::Meta::Attribute::Custom::Trait::Foo', + 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Foo via alias (Foo)' ); + +is( resolve_metatrait_alias( 'Attribute', 'Foo' ), + 'Moose::Meta::Attribute::Custom::Trait::Foo', + 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Foo via alias (Foo) a second time' ); + +is( resolve_metatrait_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Trait::Bar' ), + 'My::Trait::Bar', + 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Bar as My::Trait::Bar' ); + +is( resolve_metatrait_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Trait::Bar' ), + 'My::Trait::Bar', + 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Bar as My::Trait::Bar a second time' ); + +is( resolve_metatrait_alias( 'Attribute', 'Bar' ), + 'My::Trait::Bar', + 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Bar as My::Trait::Bar via alias (Bar)' ); + +is( resolve_metatrait_alias( 'Attribute', 'Bar' ), + 'My::Trait::Bar', + 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Bar as My::Trait::Bar via alias (Bar) a second time' ); diff --git a/t/lib/Moose/Meta/Attribute/Custom/Bar.pm b/t/lib/Moose/Meta/Attribute/Custom/Bar.pm new file mode 100644 index 0000000..64dd230 --- /dev/null +++ b/t/lib/Moose/Meta/Attribute/Custom/Bar.pm @@ -0,0 +1,10 @@ +package Moose::Meta::Attribute::Custom::Bar; + +sub register_implementation { 'My::Bar' } + + +package My::Bar; + +use Moose::Role; + +1; diff --git a/t/lib/Moose/Meta/Attribute/Custom/Foo.pm b/t/lib/Moose/Meta/Attribute/Custom/Foo.pm new file mode 100644 index 0000000..49f7a01 --- /dev/null +++ b/t/lib/Moose/Meta/Attribute/Custom/Foo.pm @@ -0,0 +1,5 @@ +package Moose::Meta::Attribute::Custom::Foo; + +use Moose::Role; + +1; diff --git a/t/lib/Moose/Meta/Attribute/Custom/Trait/Bar.pm b/t/lib/Moose/Meta/Attribute/Custom/Trait/Bar.pm new file mode 100644 index 0000000..17412c1 --- /dev/null +++ b/t/lib/Moose/Meta/Attribute/Custom/Trait/Bar.pm @@ -0,0 +1,10 @@ +package Moose::Meta::Attribute::Custom::Trait::Bar; + +sub register_implementation { 'My::Trait::Bar' } + + +package My::Trait::Bar; + +use Moose::Role; + +1; diff --git a/t/lib/Moose/Meta/Attribute/Custom/Trait/Foo.pm b/t/lib/Moose/Meta/Attribute/Custom/Trait/Foo.pm new file mode 100644 index 0000000..682b61f --- /dev/null +++ b/t/lib/Moose/Meta/Attribute/Custom/Trait/Foo.pm @@ -0,0 +1,5 @@ +package Moose::Meta::Attribute::Custom::Trait::Foo; + +use Moose::Role; + +1;