Permalink
Browse files

add requires_method_ok

  • Loading branch information...
1 parent 653a43d commit c04ab7c58ff616f86f5860be98f2364d714368ea @rsrchboy committed Aug 24, 2012
Showing with 56 additions and 2 deletions.
  1. +29 −2 lib/Test/Moose/More.pm
  2. +27 −0 t/requires_method_ok.t
View
@@ -7,7 +7,9 @@ use warnings;
use Sub::Exporter -setup => {
exports => [ qw{
- has_method_ok is_role is_class
+ is_role is_class
+ has_method_ok
+ requires_method_ok
check_sugar_ok check_sugar_removed_ok
validate_class validate_role
meta_ok does_ok does_not_ok
@@ -150,7 +152,32 @@ sub has_method_ok {
my $name = $meta->name;
### @methods
- $tb->ok(!!$meta->has_method($_), "$name has method $_") for @methods;
+ $tb->ok(!!$meta->has_method($_), "$name has method $_")
+ for @methods;
+
+ return;
+}
+
+=test requires_method_ok $thing, @methods
+
+Queries $thing's metaclass to see if $thing requires the methods named in
+@methods.
+
+Note that this really only makes sense if $thing is a role.
+
+=cut
+
+sub requires_method_ok {
+ my ($thing, @methods) = @_;
+
+ ### $thing
+ my $meta = find_meta($thing);
+ my $name = $meta->name;
+
+ ### @methods
+ $tb->ok(!!$meta->requires_method($_), "$name requires method $_")
+ for @methods;
+
return;
}
View
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+
+{
+ package TestRole;
+ use Moose::Role;
+
+ requires 'foo';
+}
+
+use Test::Builder::Tester;
+use Test::More;
+use Test::Moose::More;
+
+my $THING = 'TestRole';
+
+test_out "ok 1 - $THING requires method foo";
+requires_method_ok $THING, 'foo';
+test_test 'requires_method_ok works correctly with methods';
+
+# is_role vs plain-old-package
+test_out "not ok 1 - $THING requires method bar";
+test_fail(1);
+requires_method_ok $THING, 'bar';
+test_test 'requires_method_ok works correctly with methods not required';
+
+done_testing;

0 comments on commit c04ab7c

Please sign in to comment.