Skip to content

Commit

Permalink
- Cleaned up internals
Browse files Browse the repository at this point in the history
-   Having a single method be simultaniously a setup and teardown
    method no longer supported
-   The undocumented add_method method, that you should not have
    been using anyway, disappears
  • Loading branch information
adrianh committed Feb 20, 2004
1 parent 18b67fd commit 190a012
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 50 deletions.
7 changes: 7 additions & 0 deletions Changes
@@ -1,5 +1,12 @@
Revision history for Perl extension Test::Class.

0.06_5
- Cleaned up internals
- Having a single method be simultaniously a setup and teardown
method no longer supported
- The undocumented add_method method, that you should not have
been using anyway, disappears

0.06_4
- Fixed bogus heading styles in POD
- Test names that default to $self->current_method now replace all
Expand Down
39 changes: 17 additions & 22 deletions lib/Test/Class.pm
Expand Up @@ -14,7 +14,7 @@ use Test::Builder;
use Test::Class::MethodInfo;


our $VERSION = '0.06_4';
our $VERSION = '0.06_5';


use constant NO_PLAN => "no_plan";
Expand Down Expand Up @@ -56,33 +56,21 @@ sub _methods_of_class {
return(values %{_test_info($self)->{$class}});
};

sub add_method {
my ($class, $name, $num_tests, $types) = @_;
$Tests->{$class}->{$name} = Test::Class::MethodInfo->new(
name => $name,
num_tests => $num_tests,
type => $types,
);
};

sub _new_method_info {
my ($class, $method_name, $args) = @_;
my $num_tests = 0;
my @types;
$args ||= "test => 1";
sub _parse_attribute_args {
my $args = shift || '';
my $num_tests;
my $type;
$args =~ s/\s+//sg;
foreach my $arg (split /=>/, $args) {
if (Test::Class::MethodInfo->is_num_tests($arg)) {
$num_tests = $arg;
} elsif (Test::Class::MethodInfo->is_method_type($arg)) {
push @types, $arg;
$type = $arg;
} else {
return(undef);
die 'bad attribute args';
};
};
push @types, TEST unless @types;
$class->add_method($method_name, $num_tests, [@types]);
return(1);
return( $type, $num_tests );
};

sub Test : ATTR(CODE,RAWDATA) {
Expand All @@ -92,8 +80,15 @@ sub Test : ATTR(CODE,RAWDATA) {
return;
};
my $name = *{$symbol}{NAME};
_new_method_info($class, $name, $args)
|| warn "bad test definition '$args' in $class->$name\n";

eval {
my ($type, $num_tests) = _parse_attribute_args($args);
$Tests->{$class}->{$name} = Test::Class::MethodInfo->new(
name => $name,
num_tests => $num_tests,
type => $type,
);
} || warn "bad test definition '$args' in $class->$name\n";
};

sub new {
Expand Down
2 changes: 0 additions & 2 deletions lib/Test/Class.pod
Expand Up @@ -663,8 +663,6 @@ If the environment variable C<TEST_VERBOSE> is set C<runtests> will display the

=item B<run_all_classes>

=item B<add_method>

Ignore these for the moment. Subject to change.

=back
Expand Down
25 changes: 11 additions & 14 deletions lib/Test/Class/MethodInfo.pm
Expand Up @@ -19,22 +19,19 @@ sub is_num_tests {
};

sub new {
my $class = shift;
my %param = @_;
my $self = bless {}, $class;
my ($name, $types, $num_tests) = map {
croak "need to set $_" unless exists $param{$_};
$param{$_};
} qw(name type num_tests);
foreach my $type (@$types) {
$self->{types}->{$type} = 1;
};
$self->num_tests($num_tests);
$self->{name} = $name;
my ($class, %param) = @_;
my $self = bless {
name => $param{name},
type => $param{type} || 'test',
}, $class;
unless ( defined( $param{num_tests} ) ) {
$param{num_tests} = $self->is_type('test') ? 1 : 0;
};
$self->num_tests( $param{num_tests} );
return($self);
};

sub name { shift->{name} };
sub name { shift->{name} };

sub num_tests {
my ($self, $n) = @_;
Expand All @@ -48,7 +45,7 @@ sub num_tests {

sub is_type {
my ($self, $type) = @_;
return( $self->{types}->{$type} );
$self->{type} eq $type;
};


Expand Down
9 changes: 7 additions & 2 deletions t/current_method.t
Expand Up @@ -12,9 +12,14 @@ sub test : Test {
is($self->current_method, "test", "current_method in method");
};

sub teardown : Test(setup => teardown => 1) {
sub setup : Test(setup => 1) {
my $self = shift;
is($self->current_method, "test", "current_method in setup/teardown");
is($self->current_method, "test", "current_method in setup");
};

sub teardown : Test(teardown => 1) {
my $self = shift;
is($self->current_method, "test", "current_method in teardown");
};

__PACKAGE__->runtests;
Expand Down
26 changes: 26 additions & 0 deletions t/methodinfo.t
@@ -0,0 +1,26 @@
#! /usr/bin/perl

use strict;
use warnings;
use Test::More 'no_plan';
use constant CLASS => 'Test::Class::MethodInfo';
BEGIN { use_ok( CLASS) };

{
isa_ok my $o = CLASS->new(name=> 'foo'), CLASS;
ok $o->is_type('test'), 'method type is test by default';
is $o->num_tests, 1, 'test methods default to 1 test';
};

{
isa_ok my $o = CLASS->new(name=> 'foo', num_tests => 0), CLASS;
is $o->num_tests, 0, 'test method can have zero tests';
};

{
foreach my $type qw(setup teardown startup shutdown) {
isa_ok my $o = CLASS->new(name=> 'foo', type=> $type), CLASS, $type;
ok $o->is_type($type), "method type is $type";
is $o->num_tests, 0, "$type methods default to 0 test";
};
};
6 changes: 1 addition & 5 deletions t/runtests.t
Expand Up @@ -7,10 +7,6 @@ package Foo;
use base qw(Test::Class);
use Test::More;

sub _first : Test(setup => teardown => 1) {
pass("_first");
};


sub initialise1 :Test(setup) {
my $self = shift;
Expand Down Expand Up @@ -69,6 +65,6 @@ sub teardown1 :Test(teardown => +3) {
};

package main;
use Test::More tests => 22;
use Test::More tests => 18;

Bar->new->runtests;
5 changes: 4 additions & 1 deletion t/todo.t
Expand Up @@ -17,9 +17,12 @@ sub todo_test : Test {
};

package main;
use Test::Builder::Tester tests => 1;
use Test::Builder::Tester tests => 2;
$ENV{TEST_VERBOSE}=0;
test_out("not ok 1 - object live # TODO unimplemented");
test_err("# Failed (TODO) test (t/todo.t at line 16)");
Foo->runtests;
test_test("todo tests work");

package Foo;
is( Foo->num_method_tests('todo_test'), 1, 'todo_test should run 1 test' );
4 changes: 0 additions & 4 deletions todo/todo.pod
Expand Up @@ -26,8 +26,6 @@ Solve http://use.perl.org/~ethan/journal/14815

Have the option of making test methods fail after the first failing test, for those who prefer that style.

Think about making it work without attributes for older perls.

Finish cleaning up up Test::Class documentation

Add an option to get a diag with the method name on a test method that contains failing tests
Expand All @@ -37,5 +35,3 @@ Need a simple way or running all the tests in a directory. Either by loading the
Need a way to avoid duplication of use-ing then running tests.

Talk about how to do aggregation (aka Suites)

Document the global var that holds the current test name. Much easier to inline into test name strings than calling the method.

0 comments on commit 190a012

Please sign in to comment.