File Coverage

blib/lib/Test/Moose/More.pm
Criterion Covered Total %
statement 330 336 98.2
branch 131 150 87.3
condition 34 56 60.7
subroutine 73 78 93.5
pod 35 39 89.7
total 603 659 91.5


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