Test coercion of lazy defaults
[gitmo/Moose.git] / t / 050_metaclasses / 012_moose_exporter.t
index ef9db42..98bde56 100644 (file)
@@ -4,13 +4,11 @@ use strict;
 use warnings;
 
 use Test::More;
-use Test::Exception;
-BEGIN {
-    eval "use Test::Output;";
-    plan skip_all => "Test::Output is required for this test" if $@;
-    plan tests => 63;
-}
+use Test::Fatal;
 
+use Test::Requires {
+    'Test::Output' => '0.01', # skip all if not installed
+};
 
 {
     package HasOwnImmutable;
@@ -112,7 +110,7 @@ BEGIN {
     use Moose ();
 
     sub wrapped2 {
-        my $caller = shift;
+        my $caller = shift->name;
         return $caller . ' called wrapped2';
     }
 
@@ -121,9 +119,9 @@ BEGIN {
     }
 
     Moose::Exporter->setup_import_methods(
-        with_caller => ['wrapped2'],
-        as_is       => ['as_is1'],
-        also        => 'MooseX::Sugar',
+        with_meta => ['wrapped2'],
+        as_is     => ['as_is1'],
+        also      => 'MooseX::Sugar',
     );
 }
 
@@ -199,20 +197,14 @@ BEGIN {
 
     use Moose ();
 
-    ::dies_ok(
-        sub {
-            Moose::Exporter->setup_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'
     );
-
-    ::like(
-        $@,
-        qr/\QCircular reference in also parameter to Moose::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/,
-        'got the expected error from circular reference in also'
-    );
 }
 
 {
@@ -220,19 +212,13 @@ BEGIN {
 
     use Moose ();
 
-    ::dies_ok(
-        sub {
-            Moose::Exporter->setup_import_methods(
-                also => [ 'NoSuchThing' ],
-            );
-        },
-        'a package which does not use Moose::Exporter in also dies with an error'
-    );
-
     ::like(
-        $@,
+        ::exception{ Moose::Exporter->setup_import_methods(
+                also => ['NoSuchThing'],
+            );
+            },
         qr/\QPackage in also (NoSuchThing) does not seem to use Moose::Exporter (is it loaded?) at /,
-        'got the expected error from a reference in also to a package which is not loaded'
+        'a package which does not use Moose::Exporter in also dies with an error'
     );
 }
 
@@ -241,19 +227,13 @@ BEGIN {
 
     use Moose ();
 
-    ::dies_ok(
-        sub {
-            Moose::Exporter->setup_import_methods(
-                also => [ 'Moose::Meta::Method' ],
-            );
-        },
-        'a package which does not use Moose::Exporter in also dies with an error'
-    );
-
     ::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 /,
-        'got the expected error from a reference in also to a package which does not use Moose::Exporter'
+        'a package which does not use Moose::Exporter in also dies with an error'
     );
 }
 
@@ -263,13 +243,13 @@ BEGIN {
     use Moose ();
 
     sub has {
-        my $caller = shift;
+        my $caller = shift->name;
         return $caller . ' called has';
     }
 
     Moose::Exporter->setup_import_methods(
-        with_caller => ['has'],
-        also        => 'Moose',
+        with_meta => ['has'],
+        also      => 'Moose',
     );
 }
 
@@ -292,8 +272,32 @@ BEGIN {
 }
 
 {
+    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(
@@ -355,12 +359,16 @@ BEGIN {
         prototype( AllOptions->can('with_meta2') ),
         'using correct prototype on with_meta function'
     );
+}
 
-    {
-        package UseAllOptions;
-        AllOptions->unimport();
-    }
+{
+    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;