be more verbose in the required method error (RT#60583)
[gitmo/Moose.git] / t / roles / imported_required_method.t
diff --git a/t/roles/imported_required_method.t b/t/roles/imported_required_method.t
new file mode 100644 (file)
index 0000000..ce44640
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+use Test::Fatal;
+use Test::Moose;
+
+BEGIN {
+    package ExportsFoo;
+    use Sub::Exporter -setup => {
+        exports => ['foo'],
+    };
+
+    sub foo { 'FOO' }
+
+    $INC{'ExportsFoo.pm'} = 1;
+}
+
+{
+    package Foo;
+    use Moose::Role;
+    requires 'foo';
+}
+
+{
+    package Bar;
+    use Moose::Role;
+    requires 'bar';
+}
+
+{
+    package Class;
+    use Moose;
+    use ExportsFoo 'foo';
+    ::like(
+        ::exception { with 'Foo' },
+        qr/^\Q'Foo' requires the method 'foo' to be implemented by 'Class'. If you imported functions intending to use them as methods, you need to explicitly mark them as such, via Class->meta->add_method(foo => \&foo)/,
+        "imported 'method' isn't seen"
+    );
+    Class->meta->add_method(foo => \&foo);
+    ::is(
+        ::exception { with 'Foo' },
+        undef,
+        "now it's a method"
+    );
+
+    ::like(
+        ::exception { with 'Bar' },
+        qr/^\Q'Bar' requires the method 'bar' to be implemented by 'Class' at/,
+        "requirement isn't imported, so don't give the extra info in the error"
+    );
+}
+
+does_ok('Class', 'Foo');
+
+done_testing;