File: Regression.pm

package info (click to toggle)
libtest-regression-perl 0.05-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 156 kB
  • sloc: perl: 274; makefile: 2
file content (164 lines) | stat: -rw-r--r-- 4,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package Test::Regression;

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;
  ok_regression(sub {return "hello world"}, "t/out/hello_world.txt");

=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.

=cut

sub ok_regression {
	my $code_ref = shift;
	my $file = shift;
	my $test_name = 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");
		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");
	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.

=head1 AUTHOR

Nicholas Bamber, C<< <nicholas at periapt.co.uk> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-test-regression at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Regression>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head2 testing of STDERR

The testing of stderr from this module is not as thorough as I would like. L<Test::Builder::Tester> allows turning
off of stderr checking but not matching by regular expression. Handcrafted efforts currently fall foul of L<Test::Harness>.
Still it is I believe adequately tested in terms of coverage.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Test::Regression


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Regression>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Test-Regression>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Test-Regression>

=item * Search CPAN

L<http://search.cpan.org/dist/Test-Regression/>

=back


=head1 ACKNOWLEDGEMENTS

=over

=item Some documentation improvements have been suggested by toolic (http://perlmonks.org/?node_id=622051).

=item Thanks to Filip GraliE<0x144>ski for pointing out I need to test against output of zero length and providing a patch.

=back 

=head1 COPYRIGHT & LICENSE

Copyright 2009-10 Nicholas Bamber.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


=cut

1; # End of Test::Regression