Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

subtest_for / foreach function to combine subtest with for loop #294

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+82 −7
Split
View
@@ -230,8 +230,7 @@ See documentation of C<subtest> in Test::More.
=cut
sub subtest {
- my $self = shift;
- my($name, $subtests) = @_;
+ my($self, $name, $subtests) = (shift, shift, shift);
if ('CODE' ne ref $subtests) {
$self->croak("subtest()'s second argument must be a code ref");
@@ -252,12 +251,12 @@ sub subtest {
_copy($child, $self);
my $run_the_subtests = sub {
- $subtests->();
+ $subtests->(@_);
$self->done_testing unless $self->_plan_handled;
1;
};
- if( !eval { $run_the_subtests->() } ) {
+ if( !eval { $run_the_subtests->(@_) } ) {
$error = $@;
}
}
@@ -280,6 +279,31 @@ sub subtest {
return $finalize;
}
+=item B<subtest_for>
+
+=item B<subtest_foreach>
+
+ $builder->subtest_for([qw/a b c d/], \&subtests, $name_format);
+
+See documentation of C<subtest_for> in Test::More.
+
+=cut
+
+sub subtest_foreach {
+ my ($self, $list, $subtests, $name_format) = @_;
+
+ die "subtest_for/foreach()'s first argument must be an array ref"
+ if ('ARRAY' ne ref $list);
+ die "subtest_for/foreach()'s second argument must be a code ref"
+ if ('CODE' ne ref $subtests);
+
+ foreach my $var (@$list) {
+ my $name = $name_format||'' ? sprintf($name_format, $var) : $var;
+ $self->subtest($name, $subtests, $var);
+ }
+}
+*subtest_for{SUB} = *subtest_foreach{SUB}
+
=begin _private
=item B<_plan_handled>
View
@@ -33,7 +33,7 @@ our @EXPORT = qw(ok use_ok require_ok
done_testing
can_ok isa_ok new_ok
diag note explain
- subtest
+ subtest subtest_for subtest_foreach
BAIL_OUT
);
@@ -742,12 +742,63 @@ subtests are equivalent:
=cut
sub subtest {
- my ($name, $subtests) = @_;
-
my $tb = Test::More->builder;
return $tb->subtest(@_);
}
+=item B<subtest_for>
+
+=item B<subtest_foreach>
+
+ subtest_for ([qw/a b c d/] => sub {
+ my ($var) = @_;
+ plan tests => 2;
+
+ pass("This is a subtest using $var");
+ pass("So is this");
+ });
+
+This is a fancy way of combining a subtest with a for loop. It's the same
+as:
+
+ for my $var (qw/a b c d/) {
+ subtest $var => sub {
+ plan tests => 2;
+
+ pass("This is a subtest using $var");
+ pass("So is this");
+ }
+ }
+
+Except you don't have to deal with the double-nesting indentation. (Though,
+you do have to deal with a somewhat different syntax.)
+
+An optional name, in sprintf format, can be tacked on the end of the loop,
+like so:
+
+ subtest_for ([qw/a b c d/] => sub {
+ my ($var) = @_;
+ plan tests => 2;
+
+ pass("This is a subtest using $var");
+ pass("So is this");
+ } => "subtest for testing alphabet: %s");
+
+This would change the subtest line to:
+
+ subtest sprintf("subtest for testing alphabet: %s", $var) => sub { ... }
+
+Like Perl's counterpart, both subtest_for and subtest_foreach do the same
+thing.
+
+=cut
+
+sub subtest_foreach {
+ my $tb = Test::More->builder;
+ return $tb->subtest_foreach(@_);
+}
+*subtest_for{SUB} = *subtest_foreach{SUB}
+
=item B<pass>
=item B<fail>