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

You can create your own sugar with Test::Class::Sugar. Here's a variation on your theme:
use Test::Class::Sugar; testclass exercises Test::Class { my $test; startup >> 1 { ok $test->subject; } test autonaming { ok $test->subject; } test the naming of parts { ok $test->current_method, 'score'; } test multiple assertions >> 2 { ok ref($test), 'Test::Class::Bowling'; ok $test->current_method, 'test_multiple_assertions'; } sub score { my $game = Bowling->new; my @rolls = (2) x 20; my $expected_score = 40; } } Test::Class->runtests;
I still prefer saccharin:-).

Replies are listed 'Best First'.
Re^2: Test::Class with Test::Spec's sugar ?
by mascip (Pilgrim) on Jul 14, 2012 at 12:15 UTC

    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) ?

Re^2: Test::Class with Test::Spec's sugar ?
by mascip (Pilgrim) on Jul 14, 2012 at 08:28 UTC

    Cheers =)

    If i understood well, Test::Class::Sugar is one particular type of sugar. And i would need to use Test::Class::Sugar::CodeGenerator to create my own, which feels too complex for me now.
    I also checked the Test::Spec code, with the same result : too complex for me to build something equivalent.