use strict;
use warnings;
-use Test::More 'no_plan';
-use Test::Exception;
+use Test::More;
+use Test::Fatal;
+
+use Test::Requires {
+ 'Test::Output' => '0.01', # skip all if not installed
+};
+
+{
+ package HasOwnImmutable;
+
+ use Moose;
+
+ no Moose;
+
+ ::stderr_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' );
+}
-# All the BEGIN blocks are necessary to emulate the behavior of
-# loading modules via use and the similar compile-time effect of "no
-# ..."
{
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' );
+
}
{
use Moose ();
sub wrapped1 {
- my $caller = shift;
- return $caller . ' called wrapped1';
+ my $meta = shift;
+ return $meta->name . ' called wrapped1';
}
- BEGIN {
- Moose::Exporter->build_import_methods(
- with_caller => ['wrapped1'],
- also => 'Moose',
- );
- }
+ Moose::Exporter->setup_import_methods(
+ with_meta => ['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();
}
{
use Moose ();
sub wrapped2 {
- my $caller = shift;
+ my $caller = shift->name;
return $caller . ' called wrapped2';
}
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_meta => ['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();
}
{
);
}
- BEGIN { Moose::Exporter->build_import_methods( also => 'Moose' ); }
+ Moose::Exporter->setup_import_methods( also => 'Moose' );
}
{
package NewMeta;
- BEGIN { HasInitMeta->import() }
+ HasInitMeta->import();
}
{
use Moose ();
- ::dies_ok(
- sub {
- Moose::Exporter->build_import_methods(
+ ::like(
+ ::exception{ Moose::Exporter->setup_import_methods(
also => [ 'Moose', 'MooseX::CircularAlso' ],
);
- },
+ },
+ qr/\QCircular reference in 'also' parameter to Moose::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/,
'a circular reference in also dies with an error'
);
+}
+
+{
+ package MooseX::NoAlso;
+
+ use Moose ();
::like(
- $@,
- qr/\QCircular reference in also parameter to MooseX::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/,
- 'got the expected error from circular reference in also'
+ ::exception{ Moose::Exporter->setup_import_methods(
+ also => ['NoSuchThing'],
+ );
+ },
+ qr/\QPackage in also (NoSuchThing) does not seem to use Moose::Exporter (is it loaded?) at /,
+ 'a package which does not use Moose::Exporter in also dies with an error'
);
}
{
- package MooseX::CircularAlso;
+ package MooseX::NotExporter;
use Moose ();
- ::dies_ok(
- sub {
- Moose::Exporter->build_import_methods(
- also => [ 'NoSuchThing' ],
+ ::like(
+ ::exception{ Moose::Exporter->setup_import_methods(
+ also => ['Moose::Meta::Method'],
);
- },
+ },
+ qr/\QPackage in also (Moose::Meta::Method) does not seem to use Moose::Exporter at /,
'a package which does not use Moose::Exporter in also dies with an error'
);
+}
- ::like(
- $@,
- qr/\QPackage in also (NoSuchThing) does not seem to use MooseX::Exporter/,
- 'got the expected error from a reference in also to a package which does not use Moose::Exporter'
+{
+ package MooseX::OverridingSugar;
+
+ use Moose ();
+
+ sub has {
+ my $caller = shift->name;
+ return $caller . ' called has';
+ }
+
+ Moose::Exporter->setup_import_methods(
+ with_meta => ['has'],
+ also => 'Moose',
+ );
+}
+
+{
+ package WantsOverridingSugar;
+
+ MooseX::OverridingSugar->import();
+
+ ::can_ok( 'WantsOverridingSugar', 'has' );
+ ::can_ok( 'WantsOverridingSugar', 'with' );
+ ::is( has('foo'), 'WantsOverridingSugar called has',
+ 'has from MooseX::OverridingSugar is called, not has from Moose' );
+
+ MooseX::OverridingSugar->unimport();
+}
+
+{
+ ok( ! WantsSugar->can('has'), 'WantsSugar::has() has been cleaned' );
+ ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );
+}
+
+{
+ package NonExistentExport;
+
+ use Moose ();
+
+ ::stderr_like {
+ Moose::Exporter->setup_import_methods(
+ also => ['Moose'],
+ with_meta => ['does_not_exist'],
+ );
+ } qr/^Trying to export undefined sub NonExistentExport::does_not_exist/,
+ "warns when a non-existent method is requested to be exported";
+}
+
+{
+ package WantsNonExistentExport;
+
+ NonExistentExport->import;
+
+ ::ok(!__PACKAGE__->can('does_not_exist'),
+ "undefined subs do not get exported");
+}
+
+{
+ package AllOptions;
+ use Moose ();
+ use Moose::Deprecated -api_version => '0.88';
+ use Moose::Exporter;
+
+ Moose::Exporter->setup_import_methods(
+ also => ['Moose'],
+ with_meta => [ 'with_meta1', 'with_meta2' ],
+ with_caller => [ 'with_caller1', 'with_caller2' ],
+ as_is => ['as_is1'],
+ );
+
+ sub with_caller1 {
+ return @_;
+ }
+
+ sub with_caller2 (&) {
+ return @_;
+ }
+
+ sub as_is1 {2}
+
+ sub with_meta1 {
+ return @_;
+ }
+
+ sub with_meta2 (&) {
+ return @_;
+ }
+}
+
+{
+ package UseAllOptions;
+
+ AllOptions->import();
+}
+
+{
+ can_ok( 'UseAllOptions', $_ )
+ for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );
+
+ {
+ my ( $caller, $arg1 ) = UseAllOptions::with_caller1(42);
+ is( $caller, 'UseAllOptions', 'with_caller wrapped sub gets the right caller' );
+ is( $arg1, 42, 'with_caller wrapped sub returns argument it was passed' );
+ }
+
+ {
+ my ( $meta, $arg1 ) = UseAllOptions::with_meta1(42);
+ isa_ok( $meta, 'Moose::Meta::Class', 'with_meta first argument' );
+ is( $arg1, 42, 'with_meta1 returns argument it was passed' );
+ }
+
+ is(
+ prototype( UseAllOptions->can('with_caller2') ),
+ prototype( AllOptions->can('with_caller2') ),
+ 'using correct prototype on with_meta function'
);
+
+ is(
+ prototype( UseAllOptions->can('with_meta2') ),
+ prototype( AllOptions->can('with_meta2') ),
+ 'using correct prototype on with_meta function'
+ );
+}
+
+{
+ package UseAllOptions;
+ AllOptions->unimport();
+}
+
+{
+ ok( ! UseAllOptions->can($_), "UseAllOptions::$_ has been unimported" )
+ for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );
}
+
+done_testing;