Skip to content

Commit

Permalink
Add 'attributes' so people don't rely on global variables.
Browse files Browse the repository at this point in the history
Also clean up the docs a bit.
  • Loading branch information
Curtis Poe committed Apr 10, 2010
1 parent 5a172dd commit 42598f3
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 25 deletions.
5 changes: 5 additions & 0 deletions Changes
@@ -1,5 +1,10 @@
Revision history for Test-Class-Most

0.05 April 10, 2010
- Extended and reorganized the documentation.
- Add 'attributes' to Test::Class::Most.
http://blogs.perl.org/users/ovid/2010/04/vienna-perl-qa-hackathon-day-1.html

0.04 February 6, 2010
- Removed all trace of "feature" and "mro" handling. This is because
it turns out to be fraught with error (what happens if your
Expand Down
6 changes: 3 additions & 3 deletions META.yml
Expand Up @@ -4,7 +4,7 @@ author:
- "Curtis \"Ovid\" Poe <ovid@cpan.org>"
configure_requires:
Module::Build: 0.36
generated_by: 'Module::Build version 0.3601'
generated_by: 'Module::Build version 0.3607'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
Expand All @@ -13,11 +13,11 @@ name: Test-Class-Most
provides:
Test::Class::Most:
file: lib/Test/Class/Most.pm
version: 0.04
version: 0.05
requires:
Test::Class: 0.33
Test::Most: 0.21
resources:
license: http://dev.perl.org/licenses/
repository: http://github.com/Ovid/Test-Class-Most/
version: 0.04
version: 0.05
2 changes: 1 addition & 1 deletion Makefile.PL
@@ -1,4 +1,4 @@
# Note: this file was auto-generated by Module::Build::Compat version 0.3601
# Note: this file was auto-generated by Module::Build::Compat version 0.3607
use ExtUtils::MakeMaker;
WriteMakefile
(
Expand Down
113 changes: 95 additions & 18 deletions lib/Test/Class/Most.pm
Expand Up @@ -15,7 +15,7 @@ Version 0.04
=cut

our $VERSION = '0.04';
our $VERSION = '0.05';
$VERSION = eval $VERSION;

=head1 SYNOPSIS
Expand All @@ -42,22 +42,6 @@ You type this:
=head1 DESCRIPTION
If you're not familiar with using L<Test::Class>, please see my tutorial at:
=over 4
=item * L<http://www.modernperlbooks.com/mt/2009/03/organizing-test-suites-with-testclass.html>
=item * L<http://www.modernperlbooks.com/mt/2009/03/reusing-test-code-with-testclass.html>
=item * L<http://www.modernperlbooks.com/mt/2009/03/making-your-testing-life-easier.html>
=item * L<http://www.modernperlbooks.com/mt/2009/03/using-test-control-methods-with-testclass.html>
=item * L<http://www.modernperlbooks.com/mt/2009/03/working-with-testclass-test-suites.html>
=back
When people write test classes with the excellent C<Test::Class>, you often
see the following at the top of the code:
Expand Down Expand Up @@ -112,6 +96,58 @@ figured I shouldn't force too many of my personal beliefs on you):
Some::Other::Class::For::Increased::Stupidity
/];
As a side note, it's recommended that even if you don't need test control
methods in your base class, put stubs in there:
package My::Test::Class;
use Test::Class::Most; # we now inherit from Test::Class
INIT { Test::Class->runtests }
sub startup : Tests(startup) {}
sub setup : Tests(setup) {}
sub teardown : Tests(teardown) {}
sub shutdown : Tests(shutdown) {}
1;
This allows developers to I<always> be able to safely call parent test control
methods rather than wonder if they are there:
package Tests::For::Customer;
use Test::Class::Most parent => 'My::Test::Class';
sub setup : Tests(setup) {
my $test = shift;
$test->next::method; # safe due to stub in base class
...
}
=head1 ATTRIBUTES
You can also specify "attributes" which are merely very simple getter/setters.
use Test::Class::Most
parent => 'My::Test::Class',
attributes => [qw/customer items/];
sub setup : Tests(setup) {
my $test = shift;
$test->SUPER::setup;
$test->customer( ... );
$test->items( ... );
}
sub some_tests : Tests {
my $test = shift;
my $customer = $test->customer;
...
}
If called with no arguments, returns the current value. If called with one
argument, sets that argument as the current value. If called with more than
one argument, it croaks.
=head1 EXPORT
All functions from L<Test::Most> are automatically exported into your
Expand Down Expand Up @@ -142,8 +178,48 @@ sub import {
no strict 'refs';
push @{"${caller}::ISA"} => 'Test::Class';
}
if ( my $attributes = delete $args{attributes} ) {
if ( ref $attributes && 'ARRAY' ne ref $attributes ) {
croak(
"Argument to 'attributes' must be a classname or array of classnames, not ($attributes)"
);
}
$attributes = [$attributes] unless ref $attributes;
foreach my $attr (@$attributes) {
my $method = "$caller\::$attr";
no strict 'refs';
*$method = sub {
my $test = shift;
return $test->{$method} unless @_;
if ( @_ > 1 ) {
croak("You may not pass more than one argument to '$method'");
}
$test->{$method} = shift;
return $test;
};
}
}
}

=head1 TUTORIAL
If you're not familiar with using L<Test::Class>, please see my tutorial at:
=over 4
=item * L<http://www.modernperlbooks.com/mt/2009/03/organizing-test-suites-with-testclass.html>
=item * L<http://www.modernperlbooks.com/mt/2009/03/reusing-test-code-with-testclass.html>
=item * L<http://www.modernperlbooks.com/mt/2009/03/making-your-testing-life-easier.html>
=item * L<http://www.modernperlbooks.com/mt/2009/03/using-test-control-methods-with-testclass.html>
=item * L<http://www.modernperlbooks.com/mt/2009/03/working-with-testclass-test-suites.html>
=back
=head1 AUTHOR
Curtis "Ovid" Poe, C<< <ovid at cpan.org> >>
Expand Down Expand Up @@ -218,4 +294,5 @@ under the same terms as Perl itself.
=cut

"Boilerplate it bad, m'kay";
no warnings 'void';
"Boilerplate is bad, m'kay";
5 changes: 5 additions & 0 deletions t/lib/My/Test/Class.pm
Expand Up @@ -6,6 +6,11 @@ INIT { Test::Class->runtests }

sub parent { ['Test::Class'] }

sub startup : Tests(startup) {}
sub setup : Tests(setup) {}
sub teardown : Tests(teardown) {}
sub shutdown : Tests(shutdown) {}

sub sanity : Tests(2) {
my $test = shift;

Expand Down
12 changes: 9 additions & 3 deletions t/lib/My/Test/Class/Child.pm
@@ -1,11 +1,17 @@
package My::Test::Class::Child;

use Test::Class::Most parent => 'My::Test::Class';
use Test::Class::Most
parent => 'My::Test::Class',
attributes => 'child1';

sub startup : Tests(startup) {
my $test = shift;
$test->SUPER::startup;
$test->child1('from child1');
}

sub parent {
['My::Test::Class'];
}

sub child1 { 'from child1' }

1;

0 comments on commit 42598f3

Please sign in to comment.