Code-Statistics

 view release on metacpan or  search on metacpan

t/lib/Test/BinRegression.pm  view on Meta::CPAN

package Test::BinRegression;

use warnings;
use strict;
use FileHandle;

=head1 NAME

Test::Regression - Test library that can be run in two modes; one to generate outputs and a second to compare against them

=head1 VERSION

Version 0.05

=cut

our $VERSION = '0.05';

=head1 SYNOPSIS

  use Test::Regression;

  # read and write the regression file while generating os-specific newlines
  ok_regression(sub {return "hello world"}, "t/out/hello_world.txt");

  # read and write the file without generating os-specific newlines
  ok_regression(sub {return "hello world"}, "t/out/hello_world.txt", 'binmode');

=head1 DESCRIPTION

Using the various Test:: modules you can compare the output of a function against what you expect.
However if the output is complex and changes from version to version, maintenance of the expected
output could be costly. This module allows one to use the test code to generate the expected output,
so that if the differences with model output are expected, one can easily refresh the model output.

=head1 EXPORT

ok_regression

=cut

use Test::Builder::Module;
use Test::Differences;
use base qw(Test::Builder::Module);
our @EXPORT = qw(ok_regression);
my $CLASS = __PACKAGE__;

=head1 FUNCTIONS

=head2 ok_regression

This function requires two arguments: a CODE ref and a file path.
The CODE ref is expected to return a SCALAR string which
can be compared against previous runs.
If the TEST_REGRESSION_GEN is set to a true value, then the CODE ref is run and the
output written to the file. Otherwise the output of the
file is compared against the contents of the file.
There is a third optional argument which is the test name.
There is a fourth optional argument which is a boolean which enables read/write
with bin mode if set to true.

=cut

sub ok_regression {
	my $code_ref = shift;
	my $file = shift;
	my $test_name = shift;
	my $bin_mode = shift;
	my $output = eval {&$code_ref();};
	my $tb = $CLASS->builder;
	if ($@) {
		$tb->diag($@);
		return $tb->ok(0, $test_name);
	}

	# generate the output files if required
	if ($ENV{TEST_REGRESSION_GEN}) {
		my $fh = FileHandle->new;
		$fh->open(">$file") ||  return $tb->ok(0, "$test_name: cannot open $file");
		$fh->binmode if $bin_mode;
		if (length $output) {
			$fh->print($output) || return $tb->ok(0, "actual write failed: $file");
		}
		return $tb->ok(1, $test_name);
	}

	# compare the files
	return $tb->ok(0, "$test_name: cannot read $file") unless -r $file;
	my $fh = FileHandle->new;
	$fh->open("<$file") ||  return $tb->ok(0, "$test_name: cannot open $file");
	$fh->binmode if $bin_mode;
	my $content = join '', (<$fh>);
	eq_or_diff($output, $content, $test_name);
	return $output eq $file;
}

=head1 ENVIRONMENT VARIABLES

=head2 TEST_REGRESSION_GEN

If the TEST_REGRESSION_GEN environment file is unset or false in a perl sense, then the named output files must exist and be readable and the
test will run normally comparing the outputs of the CODE refs against the contents of those files. If the environment variable is true in
a perl sense, then model output files will be overwritten with the output of the CODE ref.



( run in 0.789 second using v1.01-cache-2.11-cpan-e1769b4cff6 )