perl
view release on metacpan or search on metacpan
ext/B/t/OptreeCheck.pm view on Meta::CPAN
chomp $code;
return unless $code =~ /\S/;
my $comment = $1;
my $testcode = qq{
checkOptree(note => q{$comment},
bcopts => q{-exec},
code => q{$code},
expect => <<EOT_EOT, expect_nt => <<EONT_EONT);
ThreadedRef
paste your 'golden-example' here, then retest
EOT_EOT
NonThreadedRef
paste your 'golden-example' here, then retest
EONT_EONT
};
return $testcode;
}
sub OptreeCheck::gentest {
my ($code,$opts) = @_;
my $rendering = getRendering({code => $code});
my $testcode = OptreeCheck::wrap($code);
return unless $testcode;
# run the prog, capture 'reference' concise output
my $preamble = preamble(1);
my $got = runperl( prog => "$preamble $testcode", stderr => 1,
#switches => ["-I../ext/B/t", "-MOptreeCheck"],
); #verbose => 1);
# extract the 'reftext' ie the got 'block'
if ($got =~ m/got \'.*?\n(.*)\n\# \'\n\# expected/s) {
my $goldentxt = $1;
#and plug it into the test-src
if ($threaded) {
$testcode =~ s/ThreadedRef/$goldentxt/;
} else {
$testcode =~ s/NonThreadRef/$goldentxt/;
}
my $b4 = q{expect => <<EOT_EOT, expect_nt => <<EONT_EONT};
my $af = q{expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT'};
$testcode =~ s/$b4/$af/;
return $testcode;
}
return '';
}
sub OptreeCheck::processExamples {
my @files = @_;
# gets array of paragraphs, which should be code-samples. They're
# turned into optreeCheck tests,
foreach my $file (@files) {
open (my $fh, '<', $file) or die "cant open $file: $!\n";
$/ = "";
my @chunks = <$fh>;
print preamble (scalar @chunks);
foreach my $t (@chunks) {
print "\n\n=for gentest\n\n# chunk: $t=cut\n\n";
print OptreeCheck::gentest ($t);
}
}
}
# OK - now for the final insult to your good taste...
if ($0 =~ /OptreeCheck\.pm/) {
#use lib 't';
require './t/test.pl';
# invoked as program. Work like former gentest.pl,
# ie read files given as cmdline args,
# convert them to usable test files.
require Getopt::Std;
Getopt::Std::getopts('') or
die qq{ $0 sample-files* # no options
expecting filenames as args. Each should have paragraphs,
these are converted to checkOptree() tests, and printed to
stdout. Redirect to file then edit for test. \n};
OptreeCheck::processExamples(@ARGV);
}
1;
__END__
=head1 TEST DEVELOPMENT SUPPORT
This optree regression testing framework needs tests in order to find
bugs. To that end, OptreeCheck has support for developing new tests,
according to the following model:
1. write a set of sample code into a single file, one per
paragraph. Add <=for gentest> blocks if you care to, or just look at
f_map and f_sort in ext/B/t/ for examples.
2. run OptreeCheck as a program on the file
./perl -Ilib ext/B/t/OptreeCheck.pm -w ext/B/t/f_map
./perl -Ilib ext/B/t/OptreeCheck.pm -w ext/B/t/f_sort
gentest reads the sample code, runs each to generate a reference
rendering, folds this rendering into an optreeCheck() statement,
and prints it to stdout.
3. run the output file as above, redirect to files, then rerun on
same build (for sanity check), and on thread-opposite build. With
editor in 1 window, and cmd in other, it's fairly easy to cut-paste
the gots into the expects, easier than running step 2 on both
builds then trying to sdiff them together.
( run in 1.850 second using v1.01-cache-2.11-cpan-800906f7e73 )