X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F050_metaclasses%2F012_moose_exporter.t;h=3357ae1938ab39d9e95be2235c6a3a524b88bd42;hb=7176be9beca87b047f077bd71019e05b42e18713;hp=efeb1d645c948be5e05b10446833572a796f3cfe;hpb=4403da902109164d2674c1169055c369500080ec;p=gitmo%2FMoose.git diff --git a/t/050_metaclasses/012_moose_exporter.t b/t/050_metaclasses/012_moose_exporter.t index efeb1d6..3357ae1 100644 --- a/t/050_metaclasses/012_moose_exporter.t +++ b/t/050_metaclasses/012_moose_exporter.t @@ -3,39 +3,71 @@ use strict; use warnings; -use Test::More 'no_plan'; +use Test::More; use Test::Exception; -# All the BEGIN blocks are necessary to emulate the behavior of -# loading modules via use and the similar compile-time effect of "no -# ..." +BEGIN { + unless ( eval 'use Test::Warn 0.10; 1' ) { + plan skip_all => 'These tests require Test::Warn 0.10'; + } + else { + plan tests => 40; + } +} + + +{ + package HasOwnImmutable; + + use Moose; + + no Moose; + + ::warning_is( sub { eval q[sub make_immutable { return 'foo' }] }, + '', + 'no warning when defining our own make_immutable sub' ); +} + +{ + is( HasOwnImmutable->make_immutable(), 'foo', + 'HasOwnImmutable->make_immutable does not get overwritten' ); +} + { package MooseX::Empty; use Moose (); - BEGIN { Moose::Exporter->build_import_methods( also => 'Moose' ); } + Moose::Exporter->setup_import_methods( also => 'Moose' ); } { package WantsMoose; - BEGIN { MooseX::Empty->import(); } + MooseX::Empty->import(); sub foo { 1 } - BEGIN { - ::can_ok( 'WantsMoose', 'has' ); - ::can_ok( 'WantsMoose', 'with' ); - ::can_ok( 'WantsMoose', 'foo' ); - } + ::can_ok( 'WantsMoose', 'has' ); + ::can_ok( 'WantsMoose', 'with' ); + ::can_ok( 'WantsMoose', 'foo' ); - BEGIN{ MooseX::Empty->unimport();} + MooseX::Empty->unimport(); } { + # Note: it's important that these methods be out of scope _now_, + # after unimport was called. We tried a + # namespace::clean(0.08)-based solution, but had to abandon it + # because it cleans the namespace _later_ (when the file scope + # ends). ok( ! WantsMoose->can('has'), 'WantsMoose::has() has been cleaned' ); ok( ! WantsMoose->can('with'), 'WantsMoose::with() has been cleaned' ); can_ok( 'WantsMoose', 'foo' ); + + # This makes sure that Moose->init_meta() happens properly + isa_ok( WantsMoose->meta(), 'Moose::Meta::Class' ); + isa_ok( WantsMoose->new(), 'Moose::Object' ); + } { @@ -48,31 +80,27 @@ use Test::Exception; return $caller . ' called wrapped1'; } - BEGIN { - Moose::Exporter->build_import_methods( - with_caller => ['wrapped1'], - also => 'Moose', - ); - } + Moose::Exporter->setup_import_methods( + with_caller => ['wrapped1'], + also => 'Moose', + ); } { package WantsSugar; - BEGIN { MooseX::Sugar->import() } + MooseX::Sugar->import(); sub foo { 1 } - BEGIN { - ::can_ok( 'WantsSugar', 'has' ); - ::can_ok( 'WantsSugar', 'with' ); - ::can_ok( 'WantsSugar', 'wrapped1' ); - ::can_ok( 'WantsSugar', 'foo' ); - ::is( wrapped1(), 'WantsSugar called wrapped1', - 'wrapped1 identifies the caller correctly' ); - } + ::can_ok( 'WantsSugar', 'has' ); + ::can_ok( 'WantsSugar', 'with' ); + ::can_ok( 'WantsSugar', 'wrapped1' ); + ::can_ok( 'WantsSugar', 'foo' ); + ::is( wrapped1(), 'WantsSugar called wrapped1', + 'wrapped1 identifies the caller correctly' ); - BEGIN{ MooseX::Sugar->unimport();} + MooseX::Sugar->unimport(); } { @@ -96,38 +124,34 @@ use Test::Exception; return 'as_is1'; } - BEGIN { - Moose::Exporter->build_import_methods( - with_caller => ['wrapped2'], - as_is => ['as_is1'], - also => 'MooseX::Sugar', - ); - } + Moose::Exporter->setup_import_methods( + with_caller => ['wrapped2'], + as_is => ['as_is1'], + also => 'MooseX::Sugar', + ); } { package WantsMoreSugar; - BEGIN { MooseX::MoreSugar->import() } + MooseX::MoreSugar->import(); sub foo { 1 } - BEGIN { - ::can_ok( 'WantsMoreSugar', 'has' ); - ::can_ok( 'WantsMoreSugar', 'with' ); - ::can_ok( 'WantsMoreSugar', 'wrapped1' ); - ::can_ok( 'WantsMoreSugar', 'wrapped2' ); - ::can_ok( 'WantsMoreSugar', 'as_is1' ); - ::can_ok( 'WantsMoreSugar', 'foo' ); - ::is( wrapped1(), 'WantsMoreSugar called wrapped1', - 'wrapped1 identifies the caller correctly' ); - ::is( wrapped2(), 'WantsMoreSugar called wrapped2', - 'wrapped2 identifies the caller correctly' ); - ::is( as_is1(), 'as_is1', - 'as_is1 works as expected' ); - } - - BEGIN{ MooseX::MoreSugar->unimport();} + ::can_ok( 'WantsMoreSugar', 'has' ); + ::can_ok( 'WantsMoreSugar', 'with' ); + ::can_ok( 'WantsMoreSugar', 'wrapped1' ); + ::can_ok( 'WantsMoreSugar', 'wrapped2' ); + ::can_ok( 'WantsMoreSugar', 'as_is1' ); + ::can_ok( 'WantsMoreSugar', 'foo' ); + ::is( wrapped1(), 'WantsMoreSugar called wrapped1', + 'wrapped1 identifies the caller correctly' ); + ::is( wrapped2(), 'WantsMoreSugar called wrapped2', + 'wrapped2 identifies the caller correctly' ); + ::is( as_is1(), 'as_is1', + 'as_is1 works as expected' ); + + MooseX::MoreSugar->unimport(); } { @@ -140,13 +164,48 @@ use Test::Exception; } { + package My::Metaclass; + use Moose; + BEGIN { extends 'Moose::Meta::Class' } + + package My::Object; + use Moose; + BEGIN { extends 'Moose::Object' } + + package HasInitMeta; + + use Moose (); + + sub init_meta { + shift; + return Moose->init_meta( @_, + metaclass => 'My::Metaclass', + base_class => 'My::Object', + ); + } + + Moose::Exporter->setup_import_methods( also => 'Moose' ); +} + +{ + package NewMeta; + + HasInitMeta->import(); +} + +{ + isa_ok( NewMeta->meta(), 'My::Metaclass' ); + isa_ok( NewMeta->new(), 'My::Object' ); +} + +{ package MooseX::CircularAlso; use Moose (); ::dies_ok( sub { - Moose::Exporter->build_import_methods( + Moose::Exporter->setup_import_methods( also => [ 'Moose', 'MooseX::CircularAlso' ], ); }, @@ -155,7 +214,7 @@ use Test::Exception; ::like( $@, - qr/\QCircular reference in also parameter to MooseX::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/, + qr/\QCircular reference in also parameter to Moose::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/, 'got the expected error from circular reference in also' ); } @@ -167,7 +226,7 @@ use Test::Exception; ::dies_ok( sub { - Moose::Exporter->build_import_methods( + Moose::Exporter->setup_import_methods( also => [ 'NoSuchThing' ], ); }, @@ -176,7 +235,7 @@ use Test::Exception; ::like( $@, - qr/\QPackage in also (NoSuchThing) does not seem to use MooseX::Exporter/, + qr/\QPackage in also (NoSuchThing) does not seem to use Moose::Exporter/, 'got the expected error from a reference in also to a package which does not use Moose::Exporter' ); }