| 1 | package Test::Class::Moose::Role::AutoUse; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Automatically load the classes you're testing | ||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use 5.010000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | our $VERSION = '1.00'; | |||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use Carp 'confess'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 2 | has 'class_name' => ( | |||||||||
| 17 | is => 'ro', | ||||||||||
| 18 | isa => 'Maybe[Str]', | ||||||||||
| 19 | builder => '_build_class_name', | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 3 | ▶ | sub _build_class_name { | ||||||||
|
Subroutine
_build_class_name: called CC=3 SCAR=11.3
| |||||||||||
| 23 | 3 | my $test = shift; | |||||||||
| 24 | |||||||||||
| 25 | 3 | ▶ | my $name = $test->get_class_name_to_use or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | ## no critic (BuiltinFunctions::ProhibitStringyEval, ErrorHandling::RequireCheckingReturnValueOfEval) | ||||||||||
| 28 | 2 | ▶ | eval "use $name"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 3 | ▶ | if ( my $error = $@ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | confess("Could not use $name: $error"); | |||||||||
| 31 | } | ||||||||||
| 32 | 3 | return $name; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 3 | ▶ | sub get_class_name_to_use { | ||||||||
|
Subroutine
get_class_name_to_use: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 36 | 3 | my $test = shift; | |||||||||
| 37 | 3 | my $name = ref $test; | |||||||||
| 38 | 3 | $name =~ s/^[^:]+:://; | |||||||||
| 39 | 3 | return $name; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 2 | 1; | |||||||||
| 43 | |||||||||||
| 44 | =pod | ||||||||||
| 45 | |||||||||||
| 46 | =encoding UTF-8 | ||||||||||
| 47 | |||||||||||
| 48 | =head1 NAME | ||||||||||
| 49 | |||||||||||
| 50 | Test::Class::Moose::Role::AutoUse - Automatically load the classes you're testing | ||||||||||
| 51 | |||||||||||
| 52 | =head1 VERSION | ||||||||||
| 53 | |||||||||||
| 54 | version 1.00 | ||||||||||
| 55 | |||||||||||
| 56 | =head1 SYNOPSIS | ||||||||||
| 57 | |||||||||||
| 58 | package TestsFor::Some::Class; | ||||||||||
| 59 | use Test::Class::Moose; | ||||||||||
| 60 | with 'Test::Class::Moose::Role::AutoUse'; | ||||||||||
| 61 | |||||||||||
| 62 | sub test_constructor { | ||||||||||
| 63 | my $test = shift; | ||||||||||
| 64 | |||||||||||
| 65 | my $class = $test->class_name; # Some::Class | ||||||||||
| 66 | can_ok $class, 'new'; # Some::Class is already loaded | ||||||||||
| 67 | isa_ok my $object = $class->new, $class; # and can be used as normal | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | =head1 DESCRIPTION | ||||||||||
| 71 | |||||||||||
| 72 | This role allows you to automatically C<use> the classes your test class is | ||||||||||
| 73 | testing, providing the name of the class via the C<class_name> attribute. Thus, | ||||||||||
| 74 | you don't need to hardcode your class names. | ||||||||||
| 75 | |||||||||||
| 76 | =head1 PROVIDES | ||||||||||
| 77 | |||||||||||
| 78 | =head2 C<class_name> | ||||||||||
| 79 | |||||||||||
| 80 | Returns the name of the class you're testing. As a side-effect, the first time | ||||||||||
| 81 | it's called it will attempt to C<use> the class being tested. | ||||||||||
| 82 | |||||||||||
| 83 | =head2 C<get_class_name_to_use> | ||||||||||
| 84 | |||||||||||
| 85 | This method strips the leading section of the package name, up to and including | ||||||||||
| 86 | the first C<::>, and returns the rest of the name as the name of the class | ||||||||||
| 87 | being tested. For example, if your test class is named C<Tests::Some::Person>, | ||||||||||
| 88 | the name C<Some::Person> is returned as the name of the class to use and test. | ||||||||||
| 89 | If your test class is named C<IHateTestingThis::Person>, then C<Person> is the | ||||||||||
| 90 | name of the class to be used and tested. | ||||||||||
| 91 | |||||||||||
| 92 | If you don't like how the name is calculated, you can override this method in | ||||||||||
| 93 | your code. | ||||||||||
| 94 | |||||||||||
| 95 | Warning: Don't use L<Test::> as a prefix. There are already plenty of modules | ||||||||||
| 96 | in that namespace and you could accidentally cause a collision. | ||||||||||
| 97 | |||||||||||
| 98 | =head1 RATIONALE | ||||||||||
| 99 | |||||||||||
| 100 | The example from our synopsis looks like this: | ||||||||||
| 101 | |||||||||||
| 102 | package TestsFor::Some::Class; | ||||||||||
| 103 | use Test::Class::Moose; | ||||||||||
| 104 | with 'Test::Class::Moose::Role::AutoUse'; | ||||||||||
| 105 | |||||||||||
| 106 | sub test_constructor { | ||||||||||
| 107 | my $test = shift; | ||||||||||
| 108 | |||||||||||
| 109 | my $class = $test->class_name; # Some::Class | ||||||||||
| 110 | can_ok $class, 'new'; # Some::Class is already loaded | ||||||||||
| 111 | isa_ok my $object = $class->new, $class; # and can be used as normal | ||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | Without this role, it would often look like this: | ||||||||||
| 115 | |||||||||||
| 116 | package TestsFor::Some::Class; | ||||||||||
| 117 | use Test::Class::Moose; | ||||||||||
| 118 | use Some::Class; | ||||||||||
| 119 | |||||||||||
| 120 | sub test_constructor { | ||||||||||
| 121 | my $test = shift; | ||||||||||
| 122 | |||||||||||
| 123 | can_ok 'Some::Class', 'new'; | ||||||||||
| 124 | isa_ok my $object = 'Some::Class'->new, 'Some::Class'; | ||||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | That's OK, but there are a couple of issues here. | ||||||||||
| 128 | |||||||||||
| 129 | First, if you need to rename your class, you must change this name repeatedly. | ||||||||||
| 130 | With L<Test::Class::Moose::Role::AutoUse>, you only rename the test class name | ||||||||||
| 131 | to correspond to the new class name and you're done. | ||||||||||
| 132 | |||||||||||
| 133 | The first problem is not very serious, but the second problem is. Let's say you | ||||||||||
| 134 | have a C<Person> class and then you create a C<Person::Employee> subclass. Your | ||||||||||
| 135 | test subclass might look like this: | ||||||||||
| 136 | |||||||||||
| 137 | package TestsFor::Person::Employee; | ||||||||||
| 138 | |||||||||||
| 139 | use Test::Class::Moose extends => "TestsFor::Person"; | ||||||||||
| 140 | |||||||||||
| 141 | # insert tests here | ||||||||||
| 142 | |||||||||||
| 143 | Object-oriented tests I<inherit> their parent class tests. Thus, | ||||||||||
| 144 | C<TestsFor::Person::Employee> will inherit the C<< | ||||||||||
| 145 | TestsFor::Person->test_constructor() >> method. Except as you can see in our | ||||||||||
| 146 | example above, we've B<hardcoded> the class name, meaning that we won't be | ||||||||||
| 147 | testing our code appropriately. The code using the | ||||||||||
| 148 | L<Test::Class::Moose::Role::AutoUse> role doesn't hardcode the class name (at | ||||||||||
| 149 | least, it shouldn't), so when we call the inherited C<< | ||||||||||
| 150 | TestsFor::Person::Employee->test_constructor() >> method, it constructs a | ||||||||||
| 151 | C<TestsFor::Person::Employee> object, not a C<TestsFor::Person> object. | ||||||||||
| 152 | |||||||||||
| 153 | Some might argue that this is a strawman and we should have done this: | ||||||||||
| 154 | |||||||||||
| 155 | package TestsFor::Some::Class; | ||||||||||
| 156 | use Test::Class::Moose; | ||||||||||
| 157 | use Some::Class; | ||||||||||
| 158 | |||||||||||
| 159 | sub class_name { 'Some::Class' } | ||||||||||
| 160 | |||||||||||
| 161 | sub test_constructor { | ||||||||||
| 162 | my $test = shift; | ||||||||||
| 163 | |||||||||||
| 164 | my $class = $test->class_name; # Some::Class | ||||||||||
| 165 | can_ok $class, 'new'; # Some::Class is already loaded | ||||||||||
| 166 | isa_ok my $object = $class->new, $class; # and can be used as normal | ||||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | Yes, that's correct. We should have done this, except that now it's almost | ||||||||||
| 170 | identical to the AutoUse code, except that the first time you forget to C<use> | ||||||||||
| 171 | the class in question, you'll be unhappy. Why not automate this? | ||||||||||
| 172 | |||||||||||
| 173 | =head1 SUPPORT | ||||||||||
| 174 | |||||||||||
| 175 | Bugs may be submitted at L<https://github.com/Test-More/test-class-moose/issues>. | ||||||||||
| 176 | |||||||||||
| 177 | =head1 SOURCE | ||||||||||
| 178 | |||||||||||
| 179 | The source code repository for Test-Class-Moose can be found at L<https://github.com/Test-More/test-class-moose>. | ||||||||||
| 180 | |||||||||||
| 181 | =head1 AUTHORS | ||||||||||
| 182 | |||||||||||
| 183 | =over 4 | ||||||||||
| 184 | |||||||||||
| 185 | =item * | ||||||||||
| 186 | |||||||||||
| 187 | Curtis "Ovid" Poe <ovid@cpan.org> | ||||||||||
| 188 | |||||||||||
| 189 | =item * | ||||||||||
| 190 | |||||||||||
| 191 | Dave Rolsky <autarch@urth.org> | ||||||||||
| 192 | |||||||||||
| 193 | =item * | ||||||||||
| 194 | |||||||||||
| 195 | Chad Granum <exodist@cpan.org> | ||||||||||
| 196 | |||||||||||
| 197 | =back | ||||||||||
| 198 | |||||||||||
| 199 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 200 | |||||||||||
| 201 | This software is copyright (c) 2012 - 2025 by Curtis "Ovid" Poe. | ||||||||||
| 202 | |||||||||||
| 203 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 204 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 205 | |||||||||||
| 206 | The full text of the license can be found in the | ||||||||||
| 207 | F<LICENSE> file included with this distribution. | ||||||||||
| 208 | |||||||||||
| 209 | =cut | ||||||||||
| 210 | |||||||||||
| 211 | __END__ | ||||||||||