Skip to content

Commit

Permalink
Switch to using modules for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
timbunce committed Jan 27, 2014
1 parent 6ba839b commit 1959dd1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
7 changes: 7 additions & 0 deletions sandbox/tim/in/ExampleCase.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ExampleCase;

use strict;
use Test::More;
use parent 'DBITestCaseBase';

1;
49 changes: 49 additions & 0 deletions sandbox/tim/lib/DBITestCaseBase.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package DBITestCaseBase;

# This is all very rough, experimental, and may change completely

use strict;

use Test::More;

use Try::Tiny;


# this method is kept very simple because some tests might want to
# override this and/or find_and_call_subtests to reuse the fixtures
# for multiple similar test runs, e.g. with different connection attributes
sub run_tests {
my ($class) = @_;

try {
$class->setup;
$class->find_and_call_subtests;
}
catch {
fail "Caught exception: $_";
}
finally {
$class->teardown;
};

return;
}


sub setup {
pass "base setup";
return;
}


sub teardown {
pass "base teardown";
done_testing(); # XXX here?
return;
}

sub find_and_call_subtests {
my ($class) = @_;
}

1;
7 changes: 7 additions & 0 deletions sandbox/tim/plug/DBM/ExampleExtraTests.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package DBM::ExampleExtraTests;

use strict;
use Test::More;
use parent 'DBITestCaseBase';

1;
25 changes: 18 additions & 7 deletions sandbox/tim/tumbler.pl
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ sub get_templates {
my %templates;

find(sub {
next unless m/\.t$/;
next unless m/\.pm$/;
my $name = $File::Find::name;
$name =~ s!\Q$template_dir\E/!!;
$templates{ $name } = { require => $File::Find::name };
$name =~ s!\.pm$!!;
$name =~ s!/!::!g;
$templates{ $name } = { lib => $template_dir, module => $name };
}, $template_dir);

return \%templates;
Expand All @@ -88,14 +90,23 @@ sub write_test_file {
for my $testname (sort keys %$leaf) {
my $testinfo = $leaf->{$testname};

$testname .= ".t" unless $testname =~ m/\.t$/;
mkfilepath("$dirpath/$testname");

warn "Write $dirpath/$testname\n";
open my $fh, ">", "$dirpath/$testname";
print $fh "#!perl\n";
print $fh qq{#!perl\n};
print $fh qq{use lib "lib";\n};
print $fh $pre;
print $fh "require '$testinfo->{require}';\n" if $testinfo->{require};
print $fh "$testinfo->{code}\n" if $testinfo->{code};
print $fh "require '$testinfo->{require}';\n"
if $testinfo->{require};
print $fh "$testinfo->{code}\n"
if $testinfo->{code};
if ($testinfo->{module}) {
print $fh "use lib '$testinfo->{lib}';\n" if $testinfo->{lib};
print $fh "require $testinfo->{module};\n";
print $fh "$testinfo->{module}->run_tests;\n";
}
print $fh $post;
close $fh;
}
Expand Down Expand Up @@ -283,8 +294,8 @@ sub dbd_dbm_settings_provider {

# Example of adding a test, in a subdir, for a single driver.
# Because $tests is cloned in the tumbler this extra item doesn't
# affect other contexts, but does affect all variants in this context.
$tests->{"deeper/path/example.t"} = { code => "use Test::More; pass(); done_testing;" };
# affect other contexts (but does affect all variants in this context).
$tests->{'plugin/ExampleExtraTests.t'} = { lib => 'plug', module => 'DBM::ExampleExtraTests' };

return \%settings;
}

0 comments on commit 1959dd1

Please sign in to comment.