File Coverage

blib/lib/Test/Moose/More.pm
Criterion Covered Total %
statement 324 330 98.1
branch 133 152 87.5
condition 34 56 60.7
subroutine 74 79 93.6
pod 36 40 90.0
total 601 657 91.4


line stmt bran cond sub pod time code
1             #
2             # This file is part of Test-Moose-More
3             #
4             # This software is Copyright (c) 2017, 2016, 2015, 2014, 2013, 2012 by Chris Weyl.
5             #
6             # This is free software, licensed under:
7             #
8             # The GNU Lesser General Public License, Version 2.1, February 1999
9             #
10             package Test::Moose::More;
11             our $AUTHORITY = 'cpan:RSRCHBOY';
12             # git description: 0.048-9-ge770325
13             $Test::Moose::More::VERSION = '0.049'; # TRIAL
14              
15             # ABSTRACT: More tools for testing Moose packages
16              
17 42     42   13942204 use strict;
  42         124  
  42         1423  
18 42     42   263 use warnings;
  42         102  
  42         4311  
19              
20             use Sub::Exporter::Progressive -setup => {
21             exports => [ qw{
22             attribute_options_ok
23             check_sugar_ok
24             check_sugar_removed_ok
25             definition_context_ok
26             does_metaroles_ok
27             does_not_metaroles_ok
28             does_not_ok
29             does_not_require_method_ok
30             does_ok
31             has_attribute_ok
32             has_method_from_anywhere_ok
33             has_method_ok
34             has_no_method_from_anywhere_ok
35             has_no_method_ok
36             is_anon
37             is_anon_ok
38             is_class
39             is_class_ok
40             is_immutable_ok
41             is_not_anon
42             is_not_anon_ok
43             is_not_immutable_ok
44             is_not_pristine_ok
45             is_pristine_ok
46             is_role
47             is_role_ok
48             meta_ok
49             method_from_pkg_ok
50             method_is_accessor_ok
51             method_is_not_accessor_ok
52             method_not_from_pkg_ok
53             no_meta_ok
54             requires_method_ok
55             role_wraps_after_method_ok
56             role_wraps_around_method_ok
57             role_wraps_before_method_ok
58             validate_attribute
59             validate_class
60             validate_role
61             validate_thing
62             with_immutable
63             } ],
64             groups => {
65             default => [ ':all' ],
66 42         442 validate => [ map { "validate_$_" } qw{ attribute class role thing } ],
  168         1016  
67             },
68 42     42   6224 };
  42         18628  
69              
70 42     42   17570 use Test::Builder;
  42         106  
  42         964  
71 42     42   233 use Test::More;
  42         93  
  42         239  
72 42     42   23771 use Test::Moose 'with_immutable';
  42         2437631  
  42         370  
73 42     42   27918 use List::MoreUtils 'apply';
  42         347034  
  42         517  
74 42     42   30395 use Scalar::Util 'blessed';
  42         127  
  42         3267  
75 42     42   13786 use Syntax::Keyword::Junction 'any';
  42         307299  
  42         796  
76 42     42   5144 use Moose::Util 'resolve_metatrait_alias', 'does_role', 'find_meta';
  42         92  
  42         464  
77 42     42   31392 use Moose::Util::TypeConstraints;
  42         3418735  
  42         497  
78 42     42   90771 use Carp 'confess';
  42         114  
  42         2681  
79 42     42   272 use Data::OptList;
  42         101  
  42         387  
80              
81 42     42   18624 use Test::Moose::More::Utils;
  42         120  
  42         294  
82              
83             # debugging...
84             #use Smart::Comments;
85              
86             my $tb = Test::Builder->new();
87              
88             our $THING_NAME;
89              
90             sub _thing_name {
91 587     587   1293 my ($thing, $thing_meta) = @_;
92              
93 587 100       1934 return $THING_NAME if $THING_NAME;
94              
95 385   100     1674 $thing_meta ||= find_meta($thing);
96              
97             # try very hard to come up with a meaningful name
98 385 50       3910 my $desc
    50          
    100          
99             = !!$thing_meta ? $thing_meta->name
100             : blessed $thing ? ref $thing
101             : ref $thing ? 'The object'
102             : $thing
103             ;
104              
105 385         1439 return $desc;
106             }
107              
108              
109             {
110             my $_yes = sub { $tb->ok(!!shift, shift . ' has a meta') };
111             my $_no = sub { $tb->ok( !shift, shift . ' does not have a meta') };
112 3     3 1 5599 sub meta_ok ($;$) { _method_ok_guts($_yes, $_[0], @_) }
113 3     3 1 7356 sub no_meta_ok ($;$) { _method_ok_guts($_no, $_[0], @_) }
114             }
115              
116              
117             sub does_ok ($$;$) {
118 81     81 1 65782 my ($thing, $roles, $message) = @_;
119              
120 81         264 my $thing_meta = find_meta($thing);
121              
122 81 100       1080 $roles = [ $roles ] unless ref $roles;
123 81   66     352 $message ||= _thing_name($thing, $thing_meta) . ' does %s';
124              
125             # this generally happens when we're checking a vanilla attribute
126             # metaclass, which turns out to be a
127             # Class::MOP::Class::Immutable::Class::MOP::Class. If our metaclass does
128             # not have a does_role() method, then by definition the metaclass cannot
129             # do the role (that is, it's a Class::MOP metaclass).
130 81   100 1   462 my $_does = $thing_meta->can('does_role') || sub { 0 };
  1         6  
131              
132 42 50   42   25340 BEGIN { warnings::unimport 'redundant' if $^V gt v5.21.1 }
133             $tb->ok(!!$thing_meta->$_does($_), sprintf($message, $_))
134 81         380 for @$roles;
135              
136 81         60649 return;
137             }
138              
139              
140             sub does_not_ok ($$;$) {
141 66     66 1 64665 my ($thing, $roles, $message) = @_;
142              
143 66         231 my $thing_meta = find_meta($thing);
144              
145 66 100       778 $roles = [ $roles ] unless ref $roles;
146 66   66     277 $message ||= _thing_name($thing, $thing_meta) . ' does not do %s';
147              
148 66   100 1   322 my $_does = $thing_meta->can('does_role') || sub { 0 };
  1         11  
149              
150 42 50   42   199536 BEGIN { warnings::unimport 'redundant' if $^V gt v5.21.1 }
151             $tb->ok(!$thing_meta->$_does($_), sprintf($message, $_))
152 66         306 for @$roles;
153              
154 66         71873 return;
155             }
156              
157              
158             # helper to dig for an attribute
159             sub _find_attribute {
160 121     121   261 my ($thing, $attr_name) = @_;
161              
162 121         292 my $meta = find_meta($thing);
163              
164             # if $thing is a role, find_attribute_by_name() is not available to us
165 121 100       1712 return $meta->isa('Moose::Meta::Role')
166             ? $meta->get_attribute($attr_name)
167             : $meta->find_attribute_by_name($attr_name)
168             ;
169             }
170              
171             sub has_attribute_ok ($$;$) {
172 71     71 1 33761 my ($thing, $attr_name, $message) = @_;
173              
174 71   66     311 $message ||= _thing_name($thing) . " has an attribute named $attr_name";
175 71         204 return $tb->ok(!!_find_attribute($thing => $attr_name), $message);
176             }
177              
178              
179             {
180             my $_has_test = sub { $tb->ok(!!$_[0]->has_method($_), "$_[1] has method $_") };
181             my $_no_test = sub { $tb->ok( !$_[0]->has_method($_), "$_[1] does not have method $_") };
182              
183 13     13 1 13489 sub has_no_method_ok ($@) { _method_ok_guts($_no_test, @_) }
184 33     33 1 36042 sub has_method_ok ($@) { _method_ok_guts($_has_test, @_) }
185             }
186             {
187             my $_has_test = sub { $tb->ok(!!$_[0]->find_method_by_name($_), "$_[1] has method $_") };
188             my $_no_test = sub { $tb->ok( !$_[0]->find_method_by_name($_), "$_[1] does not have method $_") };
189              
190 3     3 1 23 sub has_no_method_from_anywhere_ok ($@) { _method_ok_guts($_no_test, @_) }
191 4     4 1 5435 sub has_method_from_anywhere_ok ($@) { _method_ok_guts($_has_test, @_) }
192             }
193              
194             sub _method_ok_guts {
195 70     70   214 my ($_test, $thing, @methods) = @_;
196              
197             ### $thing
198 70         256 my $meta = find_meta($thing);
199 70         822 my $name = _thing_name($thing, $meta);
200              
201             # the test below is run two stack frame up (down?), so let's handle that
202 70         170 local $Test::Builder::Level = $Test::Builder::Level + 2;
203              
204             # "tiny evil?" -- Eleanor Weyl
205              
206             ### @methods
207             $_test->($meta => $name)
208 70         237 for @methods;
209              
210 70         38153 return;
211             }
212              
213              
214             {
215             my $_yes = sub { $tb->ok($_[0]->original_package_name eq $_[1], "$_[3] is from $_[1]") };
216             my $_no = sub { $tb->ok($_[0]->original_package_name ne $_[1], "$_[3] is not from $_[1]") };
217 6     6 1 16196 sub method_from_pkg_ok($$$) { _method_from_pkg_ok($_yes, @_) }
218 6     6 1 16235 sub method_not_from_pkg_ok($$$) { _method_from_pkg_ok($_no, @_) }
219              
220             my $_yes_acc = sub { $tb->ok( $_[0]->isa('Class::MOP::Method::Accessor'), "$_[3] is an accessor method") };
221             my $_no_acc = sub { $tb->ok(!$_[0]->isa('Class::MOP::Method::Accessor'), "$_[3] is not an accessor method") };
222 6     6 1 18676 sub method_is_accessor_ok($$) { _method_from_pkg_ok($_yes_acc, @_) }
223 7     7 1 20607 sub method_is_not_accessor_ok($$) { _method_from_pkg_ok($_no_acc, @_) }
224             }
225              
226             sub _method_from_pkg_ok {
227 25     25   72 my ($test, $thing, $method, $orig_pkg) = @_;
228              
229             ### $thing
230 25         84 my $meta = find_meta($thing);
231 25         274 my $name = _thing_name($thing, $meta);
232              
233 25         51 local $Test::Builder::Level = $Test::Builder::Level + 1;
234 25 100       102 my $mmeta = $meta->find_method_by_name($method)
235             or return $tb->ok(0, "$name has no method $method");
236              
237 21         3246 local $Test::Builder::Level = $Test::Builder::Level + 1;
238 21         75 return $test->($mmeta, $orig_pkg, $meta, "${name}'s method $method");
239             }
240              
241              
242             sub definition_context_ok ($$) {
243 5     5 1 27410 my ($meta, $dc) = @_;
244              
245 5         27 my $name = _thing_name($meta, $meta);
246              
247 5 100       63 return unless $tb->ok(
248             $meta->can('definition_context'),
249             "$name can definition_context()",
250             );
251              
252 4         1947 my $meta_dc = $meta->definition_context;
253              
254             ### $dc
255             ### $meta_dc
256              
257 4         16 local $Test::Builder::Level = $Test::Builder::Level + 1;
258 4         31 return is_deeply $meta_dc => $dc,
259             "$name definition context is strictly correct";
260             }
261              
262              
263 4     4 1 4798 sub role_wraps_around_method_ok ($@) { _role_wraps(around => @_) }
264 4     4 1 2826 sub role_wraps_before_method_ok ($@) { _role_wraps(before => @_) }
265 4     4 1 2875 sub role_wraps_after_method_ok ($@) { _role_wraps(after => @_) }
266              
267             sub _role_wraps {
268 12     12   42 my ($style, $thing, @methods) = @_;
269              
270 12         30 my $meta_method = "get_${style}_method_modifiers";
271              
272             ### $thing
273 12         40 my $meta = find_meta($thing);
274 12         142 my $name = _thing_name($thing, $meta);
275              
276             ### @methods
277 12         24 local $Test::Builder::Level = $Test::Builder::Level + 2;
278             $tb->ok(!!$meta->$meta_method($_), "$name wraps $style method $_")
279 12         68 for @methods;
280              
281 12         6416 return;
282             }
283              
284              
285             {
286             my $_is_test = sub { $tb->ok( $_[0]->requires_method($_), "$_[1] requires method $_") };
287             my $_not_test = sub { $tb->ok(!$_[0]->requires_method($_), "$_[1] does not require method $_") };
288              
289 5     5 1 6323 sub requires_method_ok ($@) { _method_ok_guts($_is_test, @_) }
290 3     3 1 4022 sub does_not_require_method_ok ($@) { _method_ok_guts($_not_test, @_) }
291             }
292              
293              
294             sub is_immutable_ok ($;$) {
295 2     2 1 25503 my ($thing, $message) = @_;
296              
297             ### $thing
298 2         14 my $meta = find_meta($thing);
299              
300 2   33     39 $message ||= _thing_name($thing, $meta) . ' is immutable';
301 2         14 return $tb->ok($meta->is_immutable, $message);
302             }
303              
304             sub is_not_immutable_ok ($;$) {
305 4     4 1 9199 my ($thing, $message) = @_;
306              
307             ### $thing
308 4         20 my $meta = find_meta($thing);
309              
310 4   33     71 $message ||= _thing_name($thing, $meta) . ' is not immutable';
311 4         28 return $tb->ok(!$meta->is_immutable, $message);
312             }
313              
314              
315              
316             {
317             my $_is_test = sub { $tb->ok( $_[0]->is_pristine(), "$_[1] is pristine") };
318             my $_not_test = sub { $tb->ok(!$_[0]->is_pristine(), "$_[1] is not pristine") };
319              
320             # FIXME should probably rename _method_ok_guts()...
321 2     2 1 8623 sub is_pristine_ok ($) { _method_ok_guts($_is_test, @_, q{}) }
322 1     1 1 10 sub is_not_pristine_ok ($) { _method_ok_guts($_not_test, @_, q{}) }
323             }
324              
325              
326             # NOTE: deprecate at some point late 2015
327 0     0 0 0 sub is_role ($) { goto \&is_role_ok }
328 0     0 0 0 sub is_class ($) { goto \&is_class_ok }
329              
330 13     13 1 8205 sub is_role_ok ($) { unshift @_, 'Role'; goto \&_is_moosey_ok }
  13         51  
331 42     42 1 17110 sub is_class_ok ($) { unshift @_, 'Class'; goto \&_is_moosey_ok }
  42         192  
332              
333             sub _is_moosey_ok {
334 55     55   165 my ($type, $thing) = @_;
335              
336 55         182 my $thing_name = _thing_name($thing);
337              
338 55         204 my $meta = find_meta($thing);
339 55         822 $tb->ok(!!$meta, "$thing_name has a metaclass");
340 55 100       24724 return unless !!$meta;
341              
342 51         558 my $is_moosey = $meta->isa("Moose::Meta::$type");
343              
344             # special check for class -- this will happen when, say, you're validating
345             # an attribute and it's a bog standard Moose::Meta::Attribute: strictly
346             # speaking its metaclass is Class::MOPish, but really,
347             # a Moose::Meta::Attribute is a Moose class. Or arguably so. Certainly
348             # in the context of what we're asking about here. Better approaches to
349             # this welcomed as pull requests :)
350              
351 51 100 50     353 $is_moosey ||= ($meta->name || q{}) =~ /^Moose::Meta::/
      100        
352             if $type eq 'Class';
353              
354 51         322 return $tb->ok($is_moosey, "$thing_name is a Moose " . lc $type);
355             }
356              
357              
358             # NOTE: deprecate at some point late 2015
359 0     0 0 0 sub is_anon ($) { goto \&is_anon_ok }
360 0     0 0 0 sub is_not_anon ($) { goto \&is_not_anon_ok }
361              
362             sub is_anon_ok ($) {
363 5     5 1 22062 my ($thing, $message) = @_;
364              
365 5         33 my $thing_meta = find_meta($thing);
366 5   33     104 $message ||= _thing_name($thing, $thing_meta) . ' is anonymous';
367              
368 5         51 return $tb->ok(!!$thing_meta->is_anon, $message);
369             }
370              
371             sub is_not_anon_ok ($) {
372 7     7 1 21109 my ($thing, $message) = @_;
373              
374 7         33 my $thing_meta = find_meta($thing);
375 7   33     108 $message ||= _thing_name($thing, $thing_meta) . ' is not anonymous';
376              
377 7         65 return $tb->ok(!$thing_meta->is_anon, $message);
378             }
379              
380              
381             sub check_sugar_removed_ok ($) {
382 4     4 1 3706 my $t = shift @_;
383              
384             # check some (not all) Moose sugar to make sure it has been cleared
385 4         15 $tb->ok(!$t->can($_) => "$t cannot $_") for known_sugar;
386              
387 4         15129 return;
388             }
389              
390              
391             sub check_sugar_ok ($) {
392 4     4 1 3470 my $t = shift @_;
393              
394             # check some (not all) Moose sugar to make sure it has been cleared
395 4         20 $tb->ok($t->can($_) => "$t can $_") for known_sugar;
396              
397 4         14236 return;
398             }
399              
400              
401 10     10 1 28174 sub does_metaroles_ok($$) { push @_, \&does_ok; goto &_does_metaroles }
  10         64  
402 10     10 1 23500 sub does_not_metaroles_ok($$) { push @_, \&does_not_ok; goto &_does_metaroles }
  10         43  
403              
404             sub _does_metaroles {
405 20     20   74 my ($thing, $metaroles, $test_func) = @_;
406              
407 20         55 local $Test::Builder::Level = $Test::Builder::Level + 1;
408              
409 20         102 my $meta = find_meta($thing);
410 20         298 my $name = _thing_name($thing, $meta);
411              
412 20         120 for my $mop (sort keys %$metaroles) {
413              
414 80         281 my $mop_metaclass = get_mop_metaclass_for $mop => $meta;
415              
416 80         548 local $THING_NAME = "${name}'s $mop metaclass $mop_metaclass";
417 80         233 $test_func->($mop_metaclass => $metaroles->{$mop});
418             }
419              
420 20         96 return;
421             }
422              
423              
424 53     53 1 25679 sub validate_thing ($@) { _validate_subtest_wrapper(\&_validate_thing_guts, @_) }
425 37     37 1 198645 sub validate_class ($@) { _validate_subtest_wrapper(\&_validate_class_guts, @_) }
426 10     10 1 54943 sub validate_role ($@) { _validate_subtest_wrapper(\&_validate_role_guts, @_) }
427              
428             sub _validate_subtest_wrapper {
429 191     191   689 my ($func, $thing, %args) = @_;
430              
431             # note incrementing by 2 because of our upper curried function
432 191         423 local $Test::Builder::Level = $Test::Builder::Level + 2;
433              
434             # run tests w/o a subtest wrapper...
435             return $func->($thing => %args)
436 191 100       998 unless $args{-subtest};
437              
438 25 100       105 $args{-subtest} = _thing_name($thing)
439             if "$args{-subtest}" eq '1';
440              
441             # ...or with one.
442 25     25   237 return $tb->subtest(delete $args{-subtest} => sub { $func->($thing => %args) });
  25         23897  
443             }
444              
445             sub _validate_thing_guts {
446 53     53   294 my ($thing, %args) = @_;
447              
448 53         122 local $Test::Builder::Level = $Test::Builder::Level + 1;
449              
450 53         192 my $meta = find_meta($thing);
451 53         672 my $name = _thing_name($thing, $meta);
452              
453             ### anonymous...
454             $args{anonymous} ? is_anon_ok $thing : is_not_anon_ok $thing
455 53 100       214 if exists $args{anonymous};
    100          
456              
457             ### sugar checking...
458             $args{sugar} ? check_sugar_ok $thing : check_sugar_removed_ok $thing
459 53 100       2900 if exists $args{sugar};
    100          
460              
461             # metaclass checking
462 53 100       111 for my $mop (sort keys %{ $args{metaclasses} || {} }) {
  53         423  
463              
464 14         12065 my $mop_metaclass = get_mop_metaclass_for $mop => $meta;
465              
466 14         73 local $THING_NAME = "${name}'s $mop metaclass";
467             validate_class $mop_metaclass => (
468             -subtest => "Checking the $mop metaclass, $mop_metaclass",
469 14         44 %{ $args{metaclasses}->{$mop} },
  14         86  
470             );
471             }
472              
473             ### roles...
474 21         54 do { does_ok($thing, $_) for @{$args{does}} }
  21         113  
475 53 100       8646 if exists $args{does};
476 7         15 do { does_not_ok($thing, $_) for @{$args{does_not}} }
  7         33  
477 53 100       190 if exists $args{does_not};
478              
479             ### methods...
480 12         25 do { has_method_ok($thing, $_) for @{$args{methods}} }
  12         58  
481 53 100       161 if exists $args{methods};
482 4         10 do { has_no_method_ok($thing, $_) for @{$args{no_methods}} }
  4         19  
483 53 100       158 if exists $args{no_methods};
484              
485             ### attributes...
486             ATTRIBUTE_LOOP:
487 53   100     110 for my $attribute (@{Data::OptList::mkopt($args{attributes} || [])}) {
  53         367  
488              
489 12         1636 my ($name, $opts) = @$attribute;
490 12 50       34 has_attribute_ok($thing, $name)
491             or next ATTRIBUTE_LOOP;
492              
493 12 100       3733 if (!!$opts) {
494              
495             SKIP: {
496 4 100       9 skip 'Cannot examine attribute metaclass in roles', 1
  4         18  
497             if (find_meta($thing)->isa('Moose::Meta::Role'));
498              
499 2         36 local $THING_NAME = _thing_name($thing) . "'s attribute $name";
500 2         8 _validate_attribute(_find_attribute($thing, $name) => (
501             -subtest => "checking $THING_NAME",
502             %$opts,
503             ));
504             }
505             }
506             }
507              
508 53         4733 return;
509             }
510              
511             sub _validate_class_guts {
512 37     37   155 my ($class, %args) = @_;
513              
514 37         89 local $Test::Builder::Level = $Test::Builder::Level + 1;
515 37 100       134 return unless is_class_ok $class;
516              
517 36         13522 my $meta = find_meta($class);
518 36         457 my $name = _thing_name($class, $meta);
519              
520 19         42 do { ok($class->isa($_), "$name isa $_") for @{$args{isa}} }
  19         274  
521 36 100       180 if exists $args{isa};
522              
523             # check our mutability
524 0         0 do { is_immutable_ok $class }
525 36 50 66     7053 if exists $args{immutable} && $args{immutable};
526 2         7 do { is_not_immutable_ok $class }
527 36 100 66     148 if exists $args{immutable} && !$args{immutable};
528              
529             # metaclass / metarole checking
530 2         13 do { does_metaroles_ok $class => $args{class_metaroles} }
531 36 100       695 if exists $args{class_metaroles};
532 2         10 do { does_not_metaroles_ok $class => $args{no_class_metaroles} }
533 36 100       115 if exists $args{no_class_metaroles};
534              
535             confess 'Cannot specify both a metaclasses and class_metaclasses to validate_class()!'
536 36 50 66     132 if $args{class_metaclasses} && $args{metaclasses};
537              
538             $args{metaclasses} = $args{class_metaclasses}
539 36 100       118 if exists $args{class_metaclasses};
540              
541 36         167 return validate_thing $class => %args;
542             }
543              
544             # _validate_role_guts() is where the main logic of validate_role() lives;
545             # we're broken out here so as to allow it all to be easily wrapped -- or not
546             # -- in a subtest.
547              
548             sub _validate_role_guts {
549 10     10   34 my ($role, %args) = @_;
550              
551 10         25 local $Test::Builder::Level = $Test::Builder::Level + 1;
552              
553             # basic role validation
554 10 100       30 return unless is_role_ok $role;
555              
556 2         14 requires_method_ok($role => @{ $args{required_methods} })
557 9 100       2760 if defined $args{required_methods};
558              
559 1         9 role_wraps_before_method_ok($role => @{ $args{before} })
560 9 100       35 if defined $args{before};
561 1         4 role_wraps_around_method_ok($role => @{ $args{around} })
562 9 100       32 if defined $args{around};
563 1         4 role_wraps_after_method_ok($role => @{ $args{after} })
564 9 100       31 if defined $args{after};
565              
566             # metarole checking
567 2         11 do { does_metaroles_ok $role => $args{role_metaroles} }
568 9 100       30 if exists $args{role_metaroles};
569 2         7 do { does_not_metaroles_ok $role => $args{no_role_metaroles} }
570 9 100       27 if exists $args{no_role_metaroles};
571              
572              
573             confess 'Cannot specify both a metaclasses and role_metaclasses to validate_class()!'
574 9 50 66     32 if $args{role_metaclasses} && $args{metaclasses};
575              
576             $args{metaclasses} = $args{role_metaclasses}
577 9 100       30 if exists $args{role_metaclasses};
578              
579             # if we've been asked to compose ourselves, then do that -- otherwise return
580             $args{-compose}
581 9 100       57 ? validate_thing $role => %args
582             : return validate_thing $role => %args
583             ;
584              
585             # compose it and validate that class.
586             my $anon = Moose::Meta::Class->create_anon_class(
587             roles => [$role],
588 1 50   0   5 methods => { map { $_ => sub {} } @{ $args{required_methods} || [] } },
  1         28  
  1         8  
589             );
590              
591             # take anything in required_methods and put it in methods for this test
592             $args{methods}
593             = defined $args{methods}
594 1 50       5 ? [ @{$args{methods}}, @{$args{required_methods} || []} ]
  1         9  
595 1 0       143344 : [ @{$args{required_methods} || []} ]
  0 50       0  
596             ;
597 1         4 delete $args{required_methods};
598             # and add a test for the role we're actually testing...
599 1 50       4 $args{does} = [ $role, @{ $args{does} || [] } ];
  1         7  
600              
601             # aaaand a subtest wrapper to make it easier to read...
602 1         6 local $THING_NAME = _thing_name($role) . q{'s composed class};
603 1         44 return validate_class $anon->name => (
604             -subtest => 'role composed into ' . $anon->name,
605             %args,
606             );
607             }
608              
609 42     42   230 sub _validate_attribute { _validate_subtest_wrapper(\&__validate_attribute_guts, @_) }
610 41     41 1 85805 sub validate_attribute ($$@) { _validate_subtest_wrapper( \&_validate_attribute_guts, [shift, shift], @_) }
611              
612             sub _validate_attribute_guts {
613 41     41   108 my ($thingname, %opts) = @_;
614 41         129 my ($thing, $name) = @$thingname;
615              
616 41         73 local $Test::Builder::Level = $Test::Builder::Level + 1;
617 41 100       107 return unless has_attribute_ok($thing, $name);
618 40         11003 my $att = _find_attribute($thing => $name);
619              
620 40         959 local $THING_NAME = _thing_name($thing) . "'s attribute $name";
621 40         131 return _validate_attribute($att, %opts);
622             }
623              
624             sub __validate_attribute_guts {
625 42     42   104 my ($att, %opts) = @_;
626              
627 42         70 local $Test::Builder::Level = $Test::Builder::Level + 1;
628             my %thing_opts =
629 7         51 map { $_ => delete $opts{"-$_"} }
630 7     7   53 apply { s/^-// }
631 42         250 grep { /^-/ }
  73         301  
632             sort keys %opts
633             ;
634              
635 4         20 $thing_opts{does} = [ map { resolve_metatrait_alias(Attribute => $_) } @{$thing_opts{does}} ]
  4         13  
636 42 100       422 if $thing_opts{does};
637              
638             ### %thing_opts
639             {
640             # If $THING_NAME is set, we're processing an attribute metaclass via
641             # _validate_attribute_guts() or _validate_thing_guts()
642 42 50       134 local $THING_NAME = "${THING_NAME}'s metaclass"
  42         144  
643             if !!$THING_NAME;
644 42 100       127 validate_class $att => %thing_opts
645             if keys %thing_opts;
646             }
647              
648 42         125 return _attribute_options_ok($att, %opts);
649             }
650              
651             sub attribute_options_ok ($$@) {
652 8     8 1 45217 my ($thing, $name, %opts) = @_;
653              
654 8         24 local $Test::Builder::Level = $Test::Builder::Level + 1;
655 8 50       30 return unless has_attribute_ok($thing, $name);
656 8         3694 my $att = _find_attribute($thing => $name);
657              
658 8         309 return _validate_subtest_wrapper(\&_attribute_options_ok => ($att, %opts));
659             }
660              
661             sub _attribute_options_ok {
662 50     50   141 my ($att, %opts) = @_;
663              
664 50 100       338 goto \&_role_attribute_options_ok
665             if $att->isa('Moose::Meta::Role::Attribute');
666 28         116 goto \&_class_attribute_options_ok;
667             }
668              
669             sub _role_attribute_options_ok {
670 22     22   62 my ($att, %opts) = @_;
671              
672 22         46 local $Test::Builder::Level = $Test::Builder::Level + 1;
673 22         93 my $name = $att->name;
674 22         52 my $thing_name = _thing_name($name, $att);
675              
676             exists $opts{required} and delete $opts{required}
677 22 100       391 ? ok($att->is_required, "$thing_name is required")
    100          
678             : ok(!$att->is_required, "$thing_name is not required")
679             ;
680              
681             exists $opts{lazy} and delete $opts{lazy}
682 22 100       4188 ? ok($att->is_lazy, "$thing_name is lazy")
    100          
683             : ok(!$att->is_lazy, "$thing_name is not lazy")
684             ;
685              
686             exists $opts{coerce} and delete $opts{coerce}
687 22 100       3948 ? ok( $att->should_coerce, "$thing_name should coerce")
    100          
688             : ok(!$att->should_coerce, "$thing_name should not coerce")
689             ;
690              
691             ### for now, skip role attributes: blessed $att
692 22 100       3355 return $tb->skip('cannot yet test role attribute layouts')
693             if keys %opts;
694             }
695              
696             sub _class_attribute_options_ok {
697 28     28   82 my ($att, %opts) = @_;
698              
699 28         92 my @check_opts =
700             qw{ reader writer accessor predicate default builder clearer };
701 28         76 my @unhandled_opts = qw{ isa does handles traits };
702              
703 28         59 local $Test::Builder::Level = $Test::Builder::Level + 1;
704 28         95 my $name = $att->name;
705              
706 28         73 my $thing_name = _thing_name($name, $att);
707              
708             # XXX do we really want to do this?
709 28 100       97 if (my $is = delete $opts{is}) {
710 5 50 33     23 $opts{accessor} = $name if $is eq 'rw' && ! exists $opts{accessor};
711 5 50 33     44 $opts{reader} = $name if $is eq 'ro' && ! exists $opts{reader};
712             }
713              
714             # helper to check an attribute option we expect to be a string, !exist, or
715             # undef
716             my $check = sub {
717 25   33 25   4046 my $property = shift || $_;
718 25         57 my $value = delete $opts{$property};
719 25         64 my $has = "has_$property";
720              
721             # deeper and deeper down the rabbit hole...
722 25         54 local $Test::Builder::Level = $Test::Builder::Level + 1;
723              
724 25 100       257 defined $value
725             ? ok($att->$has, "$thing_name has a $property")
726             : ok(!$att->$has, "$thing_name does not have a $property")
727             ;
728 25         7873 is($att->$property, $value, "$thing_name option $property correct")
729 28         145 };
730              
731             exists $opts{required} and delete $opts{required}
732 28 100       469 ? ok($att->is_required, "$thing_name is required")
    100          
733             : ok(!$att->is_required, "$thing_name is not required")
734             ;
735              
736 28         5232 $check->($_) for grep { any(@check_opts) eq $_ } sort keys %opts;
  60         933  
737              
738 16         90 do { $tb->skip("cannot test '$_' options yet", 1); delete $opts{$_} }
  16         5482  
739 28         5895 for grep { exists $opts{$_} } @unhandled_opts;
  112         268  
740              
741 28 100       93 if (exists $opts{init_arg}) {
742              
743             $opts{init_arg}
744 4 50       21 ? $check->('init_arg')
745             : ok(!$att->has_init_arg, "$thing_name has no init_arg")
746             ;
747 4         2001 delete $opts{init_arg};
748             }
749              
750             exists $opts{lazy} and delete $opts{lazy}
751 28 100       523 ? ok($att->is_lazy, "$thing_name is lazy")
    100          
752             : ok(!$att->is_lazy, "$thing_name is not lazy")
753             ;
754              
755             exists $opts{coerce} and delete $opts{coerce}
756 28 100       5985 ? ok( $att->should_coerce, "$thing_name should coerce")
    100          
757             : ok(!$att->should_coerce, "$thing_name should not coerce")
758             ;
759              
760 28         3419 for my $opt (sort keys %opts) {
761              
762 5 100       26 do { fail "unknown attribute option: $opt"; next }
  2         339  
  2         2082  
763             unless $att->meta->find_attribute_by_name($opt);
764              
765 3         199 $check->($opt);
766             }
767              
768             #fail "unknown attribute option: $_"
769             #for sort keys %opts;
770              
771 28         1832 return;
772             }
773              
774             !!42;
775              
776             __END__
777              
778             =pod
779              
780             =encoding UTF-8
781              
782             =for :stopwords Chris Weyl Chad Etheridge Granum Karen subtest MOPs metaroles
783              
784             =head1 NAME
785              
786             Test::Moose::More - More tools for testing Moose packages
787              
788             =head1 VERSION
789              
790             This document describes version 0.049 of Test::Moose::More - released July 30, 2017 as part of Test-Moose-More.
791              
792             =head1 SYNOPSIS
793              
794             use Test::Moose::More;
795              
796             is_class_ok 'Some::Class';
797             is_role_ok 'Some::Role';
798             has_method_ok 'Some::Class', 'foo';
799              
800             # ... etc
801              
802             =head1 DESCRIPTION
803              
804             This package contains a number of additional tests that can be employed
805             against Moose classes/roles. It is intended to replace L<Test::Moose> in your
806             tests, and re-exports any tests that it has and we do not, yet.
807              
808             =head2 Export Groups
809              
810             By default, this package exports all test functions. You can be more
811             selective, however, and there are a number of export groups (aside from the
812             default C<:all>) to help you achieve those dreams!
813              
814             =over 4
815              
816             =item :all
817              
818             All exportable functions.
819              
820             =item :validate
821              
822             L</validate_attribute>, L</validate_class>, L</validate_role>, L</validate_thing>
823              
824             =back
825              
826             =head1 TEST FUNCTIONS
827              
828             =head2 meta_ok $thing
829              
830             Tests C<$thing> to see if it has a metaclass; C<$thing> may be the class name or
831             instance of the class you wish to check. Passes if C<$thing> has a metaclass.
832              
833             =head2 no_meta_ok $thing
834              
835             Tests C<$thing> to see if it does not have a metaclass; C<$thing> may be the class
836             name or instance of the class you wish to check. Passes if C<$thing> does not
837             have a metaclass.
838              
839             =head2 does_ok $thing, < $role | \@roles >, [ $message ]
840              
841             Checks to see if C<$thing> does the given roles. C<$thing> may be the class name or
842             instance of the class you wish to check.
843              
844             Note that the message will be taken verbatim unless it contains C<%s>
845             somewhere; this will be replaced with the name of the role being tested for.
846              
847             =head2 does_not_ok $thing, < $role | \@roles >, [ $message ]
848              
849             Checks to see if C<$thing> does not do the given roles. C<$thing> may be the
850             class name or instance of the class you wish to check.
851              
852             Note that the message will be taken verbatim unless it contains C<%s>
853             somewhere; this will be replaced with the name of the role being tested for.
854              
855             =head2 has_attribute_ok $thing, $attribute_name, [ $message ]
856              
857             Checks C<$thing> for an attribute named C<$attribute_name>; C<$thing> may be a
858             class name, instance, or role name.
859              
860             =head2 has_method_ok $thing, @methods
861              
862             Queries C<$thing>'s metaclass to see if C<$thing> has the methods named in
863             C<@methods>.
864              
865             Note: This does B<not> include inherited methods; see
866             L<Class::MOP::Class/has_method>.
867              
868             =head2 has_no_method_ok $thing, @methods
869              
870             Queries C<$thing>'s metaclass to ensure C<$thing> does not provide the methods named
871             in C<@methods>.
872              
873             Note: This does B<not> include inherited methods; see
874             L<Class::MOP::Class/has_method>.
875              
876             =head2 has_method_from_anywhere_ok $thing, @methods
877              
878             Queries C<$thing>'s metaclass to see if C<$thing> has the methods named in
879             C<@methods>.
880              
881             Note: This B<does> include inherited methods; see
882             L<Class::MOP::Class/find_method_by_name>.
883              
884             =head2 has_no_method_from_anywhere_ok $thing, @methods
885              
886             Queries C<$thing>'s metaclass to ensure C<$thing> does not provide the methods
887             named in C<@methods>.
888              
889             Note: This B<does> include inherited methods; see
890             L<Class::MOP::Class/find_method_by_name>.
891              
892             =head2 method_from_pkg_ok $thing, $method, $orig_pkg
893              
894             Given a thing (role, class, etc) and a method, test that it originally came
895             from C<$orig_pkg>.
896              
897             =head2 method_not_from_pkg_ok $thing, $method, $orig_pkg
898              
899             Given a thing (role, class, etc) and a method, test that it did not come from
900             C<$orig_pkg>.
901              
902             =head2 method_is_accessor_ok $thing, $method
903              
904             Given a thing (role, class, etc) and a method, test that the method is an
905             accessor -- that is, it descends from L<Class::MOP::Method::Accessor>.
906              
907             =head2 method_is_not_accessor_ok $thing, $method
908              
909             Given a thing (role, class, etc) and a method, test that the method is B<not>
910             an accessor -- that is, it does not descend from L<Class::MOP::Method::Accessor>.
911              
912             =head2 definition_context_ok $meta, \%dc
913              
914             Validates the definition context of a metaclass instance. This is a strict
915             comparison.
916              
917             =head2 role_wraps_around_method_ok $role, @methods
918              
919             Queries C<$role>'s metaclass to see if C<$role> wraps the methods named in
920             C<@methods> with an around method modifier.
921              
922             =head2 role_wraps_before_method_ok $role, @methods
923              
924             Queries C<$role>'s metaclass to see if C<$role> wraps the methods named in
925             C<@methods> with an before method modifier.
926              
927             =head2 role_wraps_after_method_ok $role, @methods
928              
929             Queries C<$role>'s metaclass to see if C<$role> wraps the methods named in
930             C<@methods> with an after method modifier.
931              
932             =head2 requires_method_ok $thing, @methods
933              
934             Queries C<$thing>'s metaclass to see if C<$thing> requires the methods named in
935             C<@methods>.
936              
937             Note that this really only makes sense if C<$thing> is a role.
938              
939             =head2 does_not_require_method_ok $thing, @methods
940              
941             Queries C<$thing>'s metaclass to ensure C<$thing> does not require the methods named
942             in C<@methods>.
943              
944             Note that this really only makes sense if C<$thing> is a role.
945              
946             =head2 is_immutable_ok $thing
947              
948             Passes if C<$thing> is immutable.
949              
950             =head2 is_not_immutable_ok $thing
951              
952             Passes if C<$thing> is not immutable; that is, is mutable.
953              
954             =head2 is_pristine_ok $thing
955              
956             Passes if C<$thing> is pristine. See L<Class::MOP::Class/is_pristine>.
957              
958             =head2 is_not_pristine_ok $thing
959              
960             Passes if C<$thing> is not pristine. See L<Class::MOP::Class/is_pristine>.
961              
962             =head2 is_role_ok $thing
963              
964             Passes if C<C<$thing>'s> metaclass is a L<Moose::Meta::Role>.
965              
966             =head2 is_class_ok $thing
967              
968             Passes if C<C<$thing>'s> metaclass is a L<Moose::Meta::Class>.
969              
970             =head2 is_anon_ok $thing
971              
972             Passes if C<$thing> is "anonymous".
973              
974             =head2 is_not_anon_ok $thing
975              
976             Passes if C<$thing> is not "anonymous".
977              
978             =head2 check_sugar_removed_ok $thing
979              
980             Ensures that all the standard Moose sugar is no longer directly callable on a
981             given package.
982              
983             =head2 check_sugar_ok $thing
984              
985             Checks and makes sure a class/etc can still do all the standard Moose sugar.
986              
987             =head2 does_metaroles_ok $thing => { $mop => [ @traits ], ... };
988              
989             Validate the metaclasses associated with a class/role metaclass.
990              
991             e.g., if I wanted to validate that the attribute trait for
992             L<MooseX::AttributeShortcuts> is actually applied, I could do this:
993              
994             { package TestClass; use Moose; use MooseX::AttributeShortcuts; }
995             use Test::Moose::More;
996             use Test::More;
997              
998             does_metaroles_ok TestClass => {
999             attribute => ['MooseX::AttributeShortcuts::Trait::Attribute'],
1000             };
1001             done_testing;
1002              
1003             This function will accept either class or role metaclasses for C<$thing>.
1004              
1005             The MOPs available for classes (L<Moose::Meta::Class>) are:
1006              
1007             =over 4
1008              
1009             =item class
1010              
1011             =item attribute
1012              
1013             =item method
1014              
1015             =item wrapped_method
1016              
1017             =item instance
1018              
1019             =item constructor
1020              
1021             =item destructor
1022              
1023             =back
1024              
1025             The MOPs available for roles (L<Moose::Meta::Role>) are:
1026              
1027             =over 4
1028              
1029             =item role
1030              
1031             =item attribute
1032              
1033             =item method
1034              
1035             =item required_method
1036              
1037             =item wrapped_method
1038              
1039             =item conflicting_method
1040              
1041             =item application_to_class
1042              
1043             =item application_to_role
1044              
1045             =item application_to_instance
1046              
1047             =item applied_attribute
1048              
1049             =back
1050              
1051             Note! Neither this function nor C<does_not_metaroles_ok()> attempts to
1052             validate that the MOP type passed in is a member of the above lists. There's
1053             no gain here in implementing such a check, and a negative to be had:
1054             specifying an invalid MOP type will result in immediate explosions, while it's
1055             entirely possible other MOP types will be added (either to core, via traits,
1056             or "let's subclass Moose::Meta::Class/etc and implement something new").
1057              
1058             =head2 does_not_metaroles_ok $thing => { $mop => [ @traits ], ... };
1059              
1060             As with L</does_metaroles_ok>, but test that the metaroles are not consumed, a
1061             la L</does_not_ok>.
1062              
1063             =head2 attribute_options_ok
1064              
1065             Validates that an attribute is set up as expected; like
1066             C<validate_attribute()>, but only concerns itself with attribute options.
1067              
1068             Note that some of these options will skip if used against attributes defined
1069             in a role.
1070              
1071             =over 4
1072              
1073             =item *
1074              
1075             C<< -subtest => 'subtest name...' >>
1076              
1077             If set, all tests run (save the first, "does this thing even have this
1078             attribute?" test) will be wrapped in a subtest, the name of which will be
1079             whatever C<-subtest> is set to.
1080              
1081             =item *
1082              
1083             C<< is => ro|rw >>
1084              
1085             Tests for reader/writer options set as one would expect.
1086              
1087             =item *
1088              
1089             C<< isa => ... >>
1090              
1091             Validates that the attribute requires its value to be a given type.
1092              
1093             =item *
1094              
1095             C<< does => ... >>
1096              
1097             Validates that the attribute requires its value to do a given role.
1098              
1099             =item *
1100              
1101             C<< builder => '...' >>
1102              
1103             Validates that the attribute expects the method name given to be its builder.
1104              
1105             =item *
1106              
1107             C<< default => ... >>
1108              
1109             Validates that the attribute has the given default.
1110              
1111             =item *
1112              
1113             C<< init_arg => '...' >>
1114              
1115             Validates that the attribute has the given initial argument name.
1116              
1117             =item *
1118              
1119             C<< lazy => 0|1 >>
1120              
1121             Validates that the attribute is/isn't lazy.
1122              
1123             =item *
1124              
1125             C<< required => 0|1 >>
1126              
1127             Validates that setting the attribute's value is/isn't required.
1128              
1129             =back
1130              
1131             =for Pod::Coverage is_anon is_class is_not_anon is_role
1132              
1133             =head1 VALIDATION METHODS
1134              
1135             =head2 validate_thing
1136              
1137             Runs a bunch of tests against the given C<$thing>, as defined:
1138              
1139             validate_thing $thing => (
1140              
1141             attributes => [ ... ],
1142             methods => [ ... ],
1143             isa => [ ... ],
1144              
1145             # ensures sugar is/is-not present
1146             sugar => 0,
1147              
1148             # ensures $thing does these roles
1149             does => [ ... ],
1150              
1151             # ensures $thing does not do these roles
1152             does_not => [ ... ],
1153             );
1154              
1155             C<$thing> can be the name of a role or class, an object instance, or a
1156             metaclass.
1157              
1158             =over 4
1159              
1160             =item *
1161              
1162             C<< -subtest => 'subtest name...' >>
1163              
1164             If set, all tests run will be wrapped in a subtest, the name of which will be
1165             whatever C<-subtest> is set to.
1166              
1167             =item *
1168              
1169             C<< isa => [ ... ] >>
1170              
1171             A list of superclasses thing should have.
1172              
1173             =item *
1174              
1175             C<< anonymous => 0|1 >>
1176              
1177             Check to see if the class is/isn't anonymous.
1178              
1179             =item *
1180              
1181             C<< does => [ ... ] >>
1182              
1183             A list of roles the thing should do.
1184              
1185             =item *
1186              
1187             C<< does_not => [ ... ] >>
1188              
1189             A list of roles the thing should not do.
1190              
1191             =item *
1192              
1193             C<< attributes => [ ... ] >>
1194              
1195             The attributes list specified here is in the form of a list of names, each
1196             optionally followed by a hashref of options to test the attribute for; this
1197             hashref takes the same arguments L</validate_attribute> does. e.g.:
1198              
1199             validate_thing $thing => (
1200              
1201             attributes => [
1202             'foo',
1203             'bar',
1204             baz => { is => 'ro', ... },
1205             'bip',
1206             ],
1207             );
1208              
1209             =item *
1210              
1211             C<< methods => [ ... ] >>
1212              
1213             A list of methods the thing should have; see L</has_method_ok>.
1214              
1215             =item *
1216              
1217             C<< no_methods => [ ... ] >>
1218              
1219             A list of methods the thing should not have; see L</has_no_method_ok>.
1220              
1221             =item *
1222              
1223             C<< sugar => 0|1 >>
1224              
1225             Ensure that thing can/cannot do the standard Moose sugar.
1226              
1227             =item *
1228              
1229             C<< metaclasses => { $mop => { ... }, ... } >>
1230              
1231             Validates this thing's metaclasses: that is, given a MOP type (e.g. class,
1232             attribute, method, ...) and a hashref, find the associated metaclass of the
1233             given type and invoke L</validate_thing> on it, using the hashref as options
1234             for C<validate_thing()>.
1235              
1236             e.g.
1237              
1238             validate_thing 'TestClass' => (
1239             metaclasses => {
1240             attribute => {
1241             isa => [ 'Moose::Meta::Attribute' ],
1242             does => [ 'MetaRole::attribute' ],
1243             },
1244             },
1245             );
1246              
1247             ...yields:
1248              
1249             # Subtest: Checking the attribute metaclass, Moose::Meta::Class::__ANON__::SERIAL::1
1250             ok 1 - TestClass's attribute metaclass has a metaclass
1251             ok 2 - TestClass's attribute metaclass is a Moose class
1252             ok 3 - TestClass's attribute metaclass isa Moose::Meta::Attribute
1253             ok 4 - TestClass's attribute metaclass does MetaRole::attribute
1254             1..4
1255             ok 1 - Checking the attribute metaclass, Moose::Meta::Class::__ANON__::SERIAL::1
1256              
1257             Note that C<validate_class()> and C<validate_role()> implement this using
1258             C<class_metaclasses> and C<role_metaclasses>, respectively.
1259              
1260             =back
1261              
1262             =head2 validate_role
1263              
1264             The same as C<validate_thing()>, but ensures C<$thing> is a role, and allows
1265             for additional role-specific tests.
1266              
1267             validate_role $thing => (
1268              
1269             required_methods => [ ... ],
1270              
1271             # ...and all other options from validate_thing()
1272             );
1273              
1274             =over 4
1275              
1276             =item *
1277              
1278             C<< -compose => 0|1 >>
1279              
1280             When true, attempt to compose the role into an anonymous class, then use it to
1281             run L</validate_class>. The options we're given are passed to C<validate_class()>
1282             directly, except that any C<required_methods> entry is removed and its contents
1283             pushed onto C<methods>. (A stub method for each entry in C<required_methods>
1284             will also be created in the new class.)
1285              
1286             e.g.:
1287              
1288             ok 1 - TestRole has a metaclass
1289             ok 2 - TestRole is a Moose role
1290             ok 3 - TestRole requires method blargh
1291             ok 4 - TestRole does TestRole
1292             ok 5 - TestRole does not do TestRole::Two
1293             ok 6 - TestRole has method method1
1294             ok 7 - TestRole has an attribute named bar
1295             # Subtest: role composed into Moose::Meta::Class::__ANON__::SERIAL::1
1296             ok 1 - TestRole's composed class has a metaclass
1297             ok 2 - TestRole's composed class is a Moose class
1298             ok 3 - TestRole's composed class does TestRole
1299             ok 4 - TestRole's composed class does not do TestRole::Two
1300             ok 5 - TestRole's composed class has method method1
1301             ok 6 - TestRole's composed class has method blargh
1302             ok 7 - TestRole's composed class has an attribute named bar
1303             1..7
1304             ok 8 - role composed into Moose::Meta::Class::__ANON__::SERIAL::1
1305             1..8
1306              
1307             =item *
1308              
1309             C<< -subtest => 'subtest name...' >>
1310              
1311             If set, all tests run will be wrapped in a subtest, the name of which will be
1312             whatever C<-subtest> is set to.
1313              
1314             =item *
1315              
1316             C<< required_methods => [ ... ] >>
1317              
1318             A list of methods the role requires a consuming class to supply.
1319              
1320             =item *
1321              
1322             C<< before => [ ... ] >>
1323              
1324             A list of methods the role expects to wrap before, on application to a class.
1325              
1326             See L<Moose/before> for information on before method modifiers.
1327              
1328             =item *
1329              
1330             C<< around => [ ... ] >>
1331              
1332             A list of methods the role expects to wrap around, on application to a class.
1333              
1334             See L<Moose/around> for information on around method modifiers.
1335              
1336             =item *
1337              
1338             C<< after => [ ... ] >>
1339              
1340             A list of methods the role expects to wrap after, on application to a class.
1341              
1342             See L<Moose/after> for information on after method modifiers.
1343              
1344             =item *
1345              
1346             C<< role_metaroles => { $mop => [ $role, ... ], ... } >>
1347              
1348             Checks metaclasses to ensure the given metaroles are applied. See
1349             L</does_metaroles_ok>.
1350              
1351             =item *
1352              
1353             C<< no_role_metaroles => { $mop => [ $role, ... ], ... } >>
1354              
1355             Checks metaclasses to ensure the given metaroles are applied. See
1356             L</does_not_metaroles_ok>.
1357              
1358             =item *
1359              
1360             C<< role_metaclasses => { $mop => { ... }, ... } >>
1361              
1362             Validates this role's metaclasses: that is, given a MOP type (e.g. role,
1363             attribute, method, ...) and a hashref, find the associated metaclass of the
1364             given type and invoke L</validate_thing> on it, using the hashref as options
1365             for C<validate_thing()>.
1366              
1367             e.g.
1368              
1369             validate_role 'TestRole' => (
1370             metaclasses => {
1371             attribute => {
1372             isa => [ 'Moose::Meta::Attribute' ],
1373             does => [ 'MetaRole::attribute' ],
1374             },
1375             },
1376             );
1377              
1378             ...yields:
1379              
1380             # Subtest: Checking the attribute metaclass, Moose::Meta::Class::__ANON__::SERIAL::1
1381             ok 1 - TestRole's attribute metaclass has a metaclass
1382             ok 2 - TestRole's attribute metaclass is a Moose class
1383             ok 3 - TestRole's attribute metaclass isa Moose::Meta::Attribute
1384             ok 4 - TestRole's attribute metaclass does MetaRole::attribute
1385             1..4
1386             ok 1 - Checking the attribute metaclass, Moose::Meta::Class::__ANON__::SERIAL::1
1387              
1388             Note that C<validate_class()> and C<validate_role()> implement this using
1389             C<class_metaclasses> and C<role_metaclasses>, respectively.
1390              
1391             =item *
1392              
1393             C<< class_metaclasses => { $mop => { ... }, ... } >>
1394              
1395             As with role_metaclasses, above, except that this option is only used
1396             if C<-compose> is also specified.
1397              
1398             =back
1399              
1400             =head2 validate_class
1401              
1402             The same as C<validate_thing()>, but ensures C<$thing> is a class, and allows
1403             for additional class-specific tests.
1404              
1405             validate_class $thing => (
1406              
1407             isa => [ ... ],
1408              
1409             attributes => [ ... ],
1410             methods => [ ... ],
1411              
1412             # ensures sugar is/is-not present
1413             sugar => 0,
1414              
1415             # ensures $thing does these roles
1416             does => [ ... ],
1417              
1418             # ensures $thing does not do these roles
1419             does_not => [ ... ],
1420              
1421             # ...and all other options from validate_thing()
1422             );
1423              
1424             =over 4
1425              
1426             =item *
1427              
1428             C<< -subtest => 'subtest name...' >>
1429              
1430             If set, all tests run will be wrapped in a subtest, the name of which will be
1431             whatever C<-subtest> is set to.
1432              
1433             =item *
1434              
1435             C<< immutable => 0|1 >>
1436              
1437             Checks the class to see if it is/isn't immutable.
1438              
1439             =item *
1440              
1441             C<< class_metaroles => { $mop => [ $role, ... ], ... } >>
1442              
1443             Checks metaclasses to ensure the given metaroles are applied. See
1444             L</does_metaroles_ok>.
1445              
1446             =item *
1447              
1448             C<< no_class_metaroles => { $mop => [ $role, ... ], ... } >>
1449              
1450             Checks metaclasses to ensure the given metaroles are applied. See
1451             L</does_not_metaroles_ok>.
1452              
1453             =item *
1454              
1455             C<< class_metaclasses => { $mop => { ... }, ... } >>
1456              
1457             Validates this class' metaclasses: that is, given a MOP type (e.g. role,
1458             attribute, method, ...) and a hashref, find the associated metaclass of the
1459             given type and invoke L</validate_thing> on it, using the hashref as options
1460             for C<validate_thing()>.
1461              
1462             e.g.
1463              
1464             validate_class 'TestClass' => (
1465             metaclasses => {
1466             attribute => {
1467             isa => [ 'Moose::Meta::Attribute' ],
1468             does => [ 'MetaRole::attribute' ],
1469             },
1470             },
1471             );
1472              
1473             ...yields:
1474              
1475             ok 1 - TestClass has a metaclass
1476             ok 2 - TestClass is a Moose class
1477             # Subtest: Checking the attribute metaclass, Moose::Meta::Class::__ANON__::SERIAL::1
1478             ok 1 - TestClass's attribute metaclass has a metaclass
1479             ok 2 - TestClass's attribute metaclass is a Moose class
1480             ok 3 - TestClass's attribute metaclass isa Moose::Meta::Attribute
1481             ok 4 - TestClass's attribute metaclass does MetaRole::attribute
1482             1..4
1483             ok 3 - Checking the attribute metaclass, Moose::Meta::Class::__ANON__::SERIAL::1
1484              
1485             =back
1486              
1487             =head2 validate_attribute
1488              
1489             C<validate_attribute()> allows you to test how an attribute looks once built
1490             and attached to a class.
1491              
1492             Let's say you have an attribute defined like this:
1493              
1494             has foo => (
1495             traits => [ 'TestRole' ],
1496             is => 'ro',
1497             isa => 'Int',
1498             builder => '_build_foo',
1499             lazy => 1,
1500             );
1501              
1502             You can use C<validate_attribute()> to ensure that it's built out in the way
1503             you expect:
1504              
1505             validate_attribute TestClass => foo => (
1506              
1507             # tests the attribute metaclass instance to ensure it does the roles
1508             -does => [ 'TestRole' ],
1509             # tests the attribute metaclass instance's inheritance
1510             -isa => [ 'Moose::Meta::Attribute' ], # for demonstration's sake
1511              
1512             traits => [ 'TestRole' ],
1513             isa => 'Int',
1514             does => 'Bar',
1515             handles => { },
1516             reader => 'foo',
1517             builder => '_build_foo',
1518             default => undef,
1519             init_arg => 'foo',
1520             lazy => 1,
1521             required => undef,
1522             );
1523              
1524             Options passed to C<validate_attribute()> prefixed with C<-> test the
1525             attribute's metaclass instance rather than a setting on the attribute; that
1526             is, C<-does> ensures that the metaclass does a particular role (e.g.
1527             L<MooseX::AttributeShortcuts>), while C<does> tests the setting of the
1528             attribute to require the value do a given role.
1529              
1530             This function takes all the options L</attribute_options_ok> takes, as well as
1531             the following:
1532              
1533             =over 4
1534              
1535             =item *
1536              
1537             C<< -subtest => 'subtest name...' >>
1538              
1539             If set, all tests run will be wrapped in a subtest, the name of which will be
1540             whatever C<-subtest> is set to.
1541              
1542             =back
1543              
1544             =head1 SEE ALSO
1545              
1546             Please see those modules/websites for more information related to this module.
1547              
1548             =over 4
1549              
1550             =item *
1551              
1552             L<Test::Moose>
1553              
1554             =back
1555              
1556             =head1 BUGS
1557              
1558             Please report any bugs or feature requests on the bugtracker website
1559             L<https://github.com/RsrchBoy/Test-Moose-More/issues>
1560              
1561             When submitting a bug or request, please include a test-file or a
1562             patch to an existing test-file that illustrates the bug or desired
1563             feature.
1564              
1565             =head1 AUTHOR
1566              
1567             Chris Weyl <cweyl@alumni.drew.edu>
1568              
1569             =head1 CONTRIBUTORS
1570              
1571             =for stopwords Chad Granum Karen Etheridge
1572              
1573             =over 4
1574              
1575             =item *
1576              
1577             Chad Granum <chad.granum@dreamhost.com>
1578              
1579             =item *
1580              
1581             Karen Etheridge <ether@cpan.org>
1582              
1583             =back
1584              
1585             =head1 COPYRIGHT AND LICENSE
1586              
1587             This software is Copyright (c) 2017, 2016, 2015, 2014, 2013, 2012 by Chris Weyl.
1588              
1589             This is free software, licensed under:
1590              
1591             The GNU Lesser General Public License, Version 2.1, February 1999
1592              
1593             =cut