in reply to Re: Test::Class with Test::Spec's sugar ?
in thread Test::Class with Test::Spec's sugar ?

The best idea i've come up with, right now, is using a similar technique to the one used by Damian Conway in the Perl6::Slurp tests with Test::More, and extend it to Test::Class. Here is what he does, to avoid duplicating the test name (Test::Class does this automatically, with the subroutine name) :

my $desc; sub TEST { $desc = $_[0] }; TEST "can't slurp in void context"; # your test here ok 1, $desc;

What i did to extend it to groups of tests in Test::Class, is to use Tests() groups, and having the TEST subroutine declare a test description for each test, and append the subroutine name at the beginning. Like this :

use strict; use warnings; #comment before releasing use Perl6::Say; use Time::TimeTick; # TEST : # my function for displaying information about each test, # in a Test::Class Test subroutine with several tests my $desc; sub TEST { my $test_name = $_[0]; my $caller_func = (caller(1))[3]; # the subroutine full name my $test_group = (split('::', $caller_func))[-1]; # only the subroutine name $test_group =~ s/_/ /g; # replace underscores by spaces $desc = "$test_group $test_name"; }; use base qw(Test::Class); use Test::More 0.98; __PACKAGE__->runtests unless caller; sub A_Fruit__On_a_branch : Tests(3) { TEST "Grows"; ok 1, $desc; TEST "Can rot"; ok 1, $desc; TEST "Can fall"; ok 1, $desc; }; sub A_Fruit__On_the_floor__Rots : Test { ok 1; } done_testing(); 1;
The output is
1..4 ok 1 - A Fruit On a branch Grows ok 2 - A Fruit On a branch Can rot ok 3 - A Fruit On a branch Can fall ok 4 - A Fruit On the floor Rots

Conclusion :
This does part of what i want : my (one level) nested structure enables me to avoid duplication in test names.
And it doesn't do some of the things that i want (and that Test::Spec does - just check the synopsis) :
- more than one level nested structure.
- different "before" setup functions in different test groups.
- nested "before" setup functions : "The setup work done in each before block cascades from one level to the next"

PS : i just realized, i have no idea to which extent this would work with the Test::Class inheritance process. I guess i'll have to try.

And same thing if i implemented a bit of Test::Spec into Test::Class (i thought of a possible not-too-hard way to implement "describe" and "before each"), i'm not sure that it would work with inheritance. You can inherit a subroutine, but how do you inherit it's context ("describe" and "before each" nested structure) ?