xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/lib/Test/Tester.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1b8851fccSafresh1use strict;
2b8851fccSafresh1
3b8851fccSafresh1package Test::Tester;
4b8851fccSafresh1
5b8851fccSafresh1BEGIN
6b8851fccSafresh1{
7b8851fccSafresh1	if (*Test::Builder::new{CODE})
8b8851fccSafresh1	{
9b8851fccSafresh1		warn "You should load Test::Tester before Test::Builder (or anything that loads Test::Builder)"
10b8851fccSafresh1	}
11b8851fccSafresh1}
12b8851fccSafresh1
13b8851fccSafresh1use Test::Builder;
14b8851fccSafresh1use Test::Tester::CaptureRunner;
15b8851fccSafresh1use Test::Tester::Delegate;
16b8851fccSafresh1
17b8851fccSafresh1require Exporter;
18b8851fccSafresh1
195759b3d2Safresh1use vars qw( @ISA @EXPORT );
20b8851fccSafresh1
21*3d61058aSafresh1our $VERSION = '1.302199';
225759b3d2Safresh1
23b8851fccSafresh1@EXPORT = qw( run_tests check_tests check_test cmp_results show_space );
24b8851fccSafresh1@ISA = qw( Exporter );
25b8851fccSafresh1
26b8851fccSafresh1my $Test = Test::Builder->new;
27b8851fccSafresh1my $Capture = Test::Tester::Capture->new;
28b8851fccSafresh1my $Delegator = Test::Tester::Delegate->new;
29b8851fccSafresh1$Delegator->{Object} = $Test;
30b8851fccSafresh1
31b8851fccSafresh1my $runner = Test::Tester::CaptureRunner->new;
32b8851fccSafresh1
33b8851fccSafresh1my $want_space = $ENV{TESTTESTERSPACE};
34b8851fccSafresh1
35b8851fccSafresh1sub show_space
36b8851fccSafresh1{
37b8851fccSafresh1	$want_space = 1;
38b8851fccSafresh1}
39b8851fccSafresh1
40b8851fccSafresh1my $colour = '';
41b8851fccSafresh1my $reset = '';
42b8851fccSafresh1
435759b3d2Safresh1if (my $want_colour = $ENV{TESTTESTERCOLOUR} || $ENV{TESTTESTERCOLOR})
44b8851fccSafresh1{
455759b3d2Safresh1	if (eval { require Term::ANSIColor; 1 })
46b8851fccSafresh1	{
475759b3d2Safresh1		eval { require Win32::Console::ANSI } if 'MSWin32' eq $^O;  # support color on windows platforms
48b8851fccSafresh1		my ($f, $b) = split(",", $want_colour);
49b8851fccSafresh1		$colour = Term::ANSIColor::color($f).Term::ANSIColor::color("on_$b");
50b8851fccSafresh1		$reset = Term::ANSIColor::color("reset");
51b8851fccSafresh1	}
52b8851fccSafresh1
53b8851fccSafresh1}
54b8851fccSafresh1
55b8851fccSafresh1sub new_new
56b8851fccSafresh1{
57b8851fccSafresh1	return $Delegator;
58b8851fccSafresh1}
59b8851fccSafresh1
60b8851fccSafresh1sub capture
61b8851fccSafresh1{
62b8851fccSafresh1	return Test::Tester::Capture->new;
63b8851fccSafresh1}
64b8851fccSafresh1
65b8851fccSafresh1sub fh
66b8851fccSafresh1{
67b8851fccSafresh1	# experiment with capturing output, I don't like it
68b8851fccSafresh1	$runner = Test::Tester::FHRunner->new;
69b8851fccSafresh1
70b8851fccSafresh1	return $Test;
71b8851fccSafresh1}
72b8851fccSafresh1
73b8851fccSafresh1sub find_run_tests
74b8851fccSafresh1{
75b8851fccSafresh1	my $d = 1;
76b8851fccSafresh1	my $found = 0;
77b8851fccSafresh1	while ((not $found) and (my ($sub) = (caller($d))[3]) )
78b8851fccSafresh1	{
79b8851fccSafresh1#		print "$d: $sub\n";
80b8851fccSafresh1		$found = ($sub eq "Test::Tester::run_tests");
81b8851fccSafresh1		$d++;
82b8851fccSafresh1	}
83b8851fccSafresh1
84b8851fccSafresh1#	die "Didn't find 'run_tests' in caller stack" unless $found;
85b8851fccSafresh1	return $d;
86b8851fccSafresh1}
87b8851fccSafresh1
88b8851fccSafresh1sub run_tests
89b8851fccSafresh1{
90b8851fccSafresh1	local($Delegator->{Object}) = $Capture;
91b8851fccSafresh1
92b8851fccSafresh1	$runner->run_tests(@_);
93b8851fccSafresh1
94b8851fccSafresh1	return ($runner->get_premature, $runner->get_results);
95b8851fccSafresh1}
96b8851fccSafresh1
97b8851fccSafresh1sub check_test
98b8851fccSafresh1{
99b8851fccSafresh1	my $test = shift;
100b8851fccSafresh1	my $expect = shift;
101b8851fccSafresh1	my $name = shift;
102b8851fccSafresh1	$name = "" unless defined($name);
103b8851fccSafresh1
104b8851fccSafresh1	@_ = ($test, [$expect], $name);
105b8851fccSafresh1	goto &check_tests;
106b8851fccSafresh1}
107b8851fccSafresh1
108b8851fccSafresh1sub check_tests
109b8851fccSafresh1{
110b8851fccSafresh1	my $test = shift;
111b8851fccSafresh1	my $expects = shift;
112b8851fccSafresh1	my $name = shift;
113b8851fccSafresh1	$name = "" unless defined($name);
114b8851fccSafresh1
115b8851fccSafresh1	my ($prem, @results) = eval { run_tests($test, $name) };
116b8851fccSafresh1
117b8851fccSafresh1	$Test->ok(! $@, "Test '$name' completed") || $Test->diag($@);
118b8851fccSafresh1	$Test->ok(! length($prem), "Test '$name' no premature diagnostication") ||
119b8851fccSafresh1		$Test->diag("Before any testing anything, your tests said\n$prem");
120b8851fccSafresh1
121b8851fccSafresh1	local $Test::Builder::Level = $Test::Builder::Level + 1;
122b8851fccSafresh1	cmp_results(\@results, $expects, $name);
123b8851fccSafresh1	return ($prem, @results);
124b8851fccSafresh1}
125b8851fccSafresh1
126b8851fccSafresh1sub cmp_field
127b8851fccSafresh1{
128b8851fccSafresh1	my ($result, $expect, $field, $desc) = @_;
129b8851fccSafresh1
130b8851fccSafresh1	if (defined $expect->{$field})
131b8851fccSafresh1	{
132b8851fccSafresh1		$Test->is_eq($result->{$field}, $expect->{$field},
133b8851fccSafresh1			"$desc compare $field");
134b8851fccSafresh1	}
135b8851fccSafresh1}
136b8851fccSafresh1
137b8851fccSafresh1sub cmp_result
138b8851fccSafresh1{
139b8851fccSafresh1	my ($result, $expect, $name) = @_;
140b8851fccSafresh1
141b8851fccSafresh1	my $sub_name = $result->{name};
142b8851fccSafresh1	$sub_name = "" unless defined($name);
143b8851fccSafresh1
144b8851fccSafresh1	my $desc = "subtest '$sub_name' of '$name'";
145b8851fccSafresh1
146b8851fccSafresh1	{
147b8851fccSafresh1		local $Test::Builder::Level = $Test::Builder::Level + 1;
148b8851fccSafresh1
149b8851fccSafresh1		cmp_field($result, $expect, "ok", $desc);
150b8851fccSafresh1
151b8851fccSafresh1		cmp_field($result, $expect, "actual_ok", $desc);
152b8851fccSafresh1
153b8851fccSafresh1		cmp_field($result, $expect, "type", $desc);
154b8851fccSafresh1
155b8851fccSafresh1		cmp_field($result, $expect, "reason", $desc);
156b8851fccSafresh1
157b8851fccSafresh1		cmp_field($result, $expect, "name", $desc);
158b8851fccSafresh1	}
159b8851fccSafresh1
160b8851fccSafresh1	# if we got no depth then default to 1
161b8851fccSafresh1	my $depth = 1;
162b8851fccSafresh1	if (exists $expect->{depth})
163b8851fccSafresh1	{
164b8851fccSafresh1		$depth = $expect->{depth};
165b8851fccSafresh1	}
166b8851fccSafresh1
167b8851fccSafresh1	# if depth was explicitly undef then don't test it
168b8851fccSafresh1	if (defined $depth)
169b8851fccSafresh1	{
170b8851fccSafresh1		$Test->is_eq($result->{depth}, $depth, "checking depth") ||
171b8851fccSafresh1			$Test->diag('You need to change $Test::Builder::Level');
172b8851fccSafresh1	}
173b8851fccSafresh1
174b8851fccSafresh1	if (defined(my $exp = $expect->{diag}))
175b8851fccSafresh1	{
1765759b3d2Safresh1
1775759b3d2Safresh1        my $got = '';
1785759b3d2Safresh1        if (ref $exp eq 'Regexp') {
1795759b3d2Safresh1
1805759b3d2Safresh1            if (not $Test->like($result->{diag}, $exp,
1815759b3d2Safresh1                "subtest '$sub_name' of '$name' compare diag"))
1825759b3d2Safresh1            {
1835759b3d2Safresh1                $got = $result->{diag};
1845759b3d2Safresh1            }
1855759b3d2Safresh1
1865759b3d2Safresh1        } else {
1875759b3d2Safresh1
188b8851fccSafresh1            # if there actually is some diag then put a \n on the end if it's not
189b8851fccSafresh1            # there already
190b8851fccSafresh1            $exp .= "\n" if (length($exp) and $exp !~ /\n$/);
1915759b3d2Safresh1
192b8851fccSafresh1            if (not $Test->ok($result->{diag} eq $exp,
1935759b3d2Safresh1    			"subtest '$sub_name' of '$name' compare diag"))
194b8851fccSafresh1            {
1955759b3d2Safresh1                $got = $result->{diag};
1965759b3d2Safresh1            }
1975759b3d2Safresh1        }
1985759b3d2Safresh1
1995759b3d2Safresh1        if ($got) {
200b8851fccSafresh1    		my $glen = length($got);
201b8851fccSafresh1    		my $elen = length($exp);
202b8851fccSafresh1    		for ($got, $exp)
203b8851fccSafresh1    		{
204b8851fccSafresh1    			my @lines = split("\n", $_);
205b8851fccSafresh1     			$_ = join("\n", map {
206b8851fccSafresh1    				if ($want_space)
207b8851fccSafresh1    				{
208b8851fccSafresh1    					$_ = $colour.escape($_).$reset;
209b8851fccSafresh1    				}
210b8851fccSafresh1    				else
211b8851fccSafresh1    				{
212b8851fccSafresh1    					"'$colour$_$reset'"
213b8851fccSafresh1    				}
214b8851fccSafresh1    			} @lines);
215b8851fccSafresh1    		}
216b8851fccSafresh1
217b8851fccSafresh1        	$Test->diag(<<EOM);
218b8851fccSafresh1Got diag ($glen bytes):
219b8851fccSafresh1$got
220b8851fccSafresh1Expected diag ($elen bytes):
221b8851fccSafresh1$exp
222b8851fccSafresh1EOM
223b8851fccSafresh1        }
224b8851fccSafresh1	}
225b8851fccSafresh1}
226b8851fccSafresh1
227b8851fccSafresh1sub escape
228b8851fccSafresh1{
229b8851fccSafresh1	my $str = shift;
230b8851fccSafresh1	my $res = '';
231b8851fccSafresh1	for my $char (split("", $str))
232b8851fccSafresh1	{
233b8851fccSafresh1		my $c = ord($char);
234b8851fccSafresh1		if(($c>32 and $c<125) or $c == 10)
235b8851fccSafresh1		{
236b8851fccSafresh1			$res .= $char;
237b8851fccSafresh1		}
238b8851fccSafresh1		else
239b8851fccSafresh1		{
240b8851fccSafresh1			$res .= sprintf('\x{%x}', $c)
241b8851fccSafresh1		}
242b8851fccSafresh1	}
243b8851fccSafresh1	return $res;
244b8851fccSafresh1}
245b8851fccSafresh1
246b8851fccSafresh1sub cmp_results
247b8851fccSafresh1{
248b8851fccSafresh1	my ($results, $expects, $name) = @_;
249b8851fccSafresh1
250b8851fccSafresh1	$Test->is_num(scalar @$results, scalar @$expects, "Test '$name' result count");
251b8851fccSafresh1
252b8851fccSafresh1	for (my $i = 0; $i < @$expects; $i++)
253b8851fccSafresh1	{
254b8851fccSafresh1		my $expect = $expects->[$i];
255b8851fccSafresh1		my $result = $results->[$i];
256b8851fccSafresh1
257b8851fccSafresh1		local $Test::Builder::Level = $Test::Builder::Level + 1;
258b8851fccSafresh1		cmp_result($result, $expect, $name);
259b8851fccSafresh1	}
260b8851fccSafresh1}
261b8851fccSafresh1
262b8851fccSafresh1######## nicked from Test::More
263b8851fccSafresh1sub plan {
264b8851fccSafresh1	my(@plan) = @_;
265b8851fccSafresh1
266b8851fccSafresh1	my $caller = caller;
267b8851fccSafresh1
268b8851fccSafresh1	$Test->exported_to($caller);
269b8851fccSafresh1
270b8851fccSafresh1	my @imports = ();
271b8851fccSafresh1	foreach my $idx (0..$#plan) {
272b8851fccSafresh1		if( $plan[$idx] eq 'import' ) {
273b8851fccSafresh1			my($tag, $imports) = splice @plan, $idx, 2;
274b8851fccSafresh1			@imports = @$imports;
275b8851fccSafresh1			last;
276b8851fccSafresh1		}
277b8851fccSafresh1	}
278b8851fccSafresh1
279b8851fccSafresh1	$Test->plan(@plan);
280b8851fccSafresh1
281b8851fccSafresh1	__PACKAGE__->_export_to_level(1, __PACKAGE__, @imports);
282b8851fccSafresh1}
283b8851fccSafresh1
284b8851fccSafresh1sub import {
285b8851fccSafresh1	my($class) = shift;
286b8851fccSafresh1		{
287b8851fccSafresh1			no warnings 'redefine';
288b8851fccSafresh1			*Test::Builder::new = \&new_new;
289b8851fccSafresh1		}
290b8851fccSafresh1	goto &plan;
291b8851fccSafresh1}
292b8851fccSafresh1
293b8851fccSafresh1sub _export_to_level
294b8851fccSafresh1{
295b8851fccSafresh1        my $pkg = shift;
296b8851fccSafresh1	my $level = shift;
297b8851fccSafresh1	(undef) = shift;	# redundant arg
298b8851fccSafresh1	my $callpkg = caller($level);
299b8851fccSafresh1	$pkg->export($callpkg, @_);
300b8851fccSafresh1}
301b8851fccSafresh1
302b8851fccSafresh1
303b8851fccSafresh1############
304b8851fccSafresh1
305b8851fccSafresh11;
306b8851fccSafresh1
307b8851fccSafresh1__END__
308b8851fccSafresh1
309b8851fccSafresh1=head1 NAME
310b8851fccSafresh1
311b8851fccSafresh1Test::Tester - Ease testing test modules built with Test::Builder
312b8851fccSafresh1
313b8851fccSafresh1=head1 SYNOPSIS
314b8851fccSafresh1
315b8851fccSafresh1  use Test::Tester tests => 6;
316b8851fccSafresh1
317b8851fccSafresh1  use Test::MyStyle;
318b8851fccSafresh1
319b8851fccSafresh1  check_test(
320b8851fccSafresh1    sub {
321b8851fccSafresh1      is_mystyle_eq("this", "that", "not eq");
322b8851fccSafresh1    },
323b8851fccSafresh1    {
324b8851fccSafresh1      ok => 0, # expect this to fail
325b8851fccSafresh1      name => "not eq",
326b8851fccSafresh1      diag => "Expected: 'this'\nGot: 'that'",
327b8851fccSafresh1    }
328b8851fccSafresh1  );
329b8851fccSafresh1
330b8851fccSafresh1or
331b8851fccSafresh1
3325759b3d2Safresh1  use Test::Tester tests => 6;
3335759b3d2Safresh1
3345759b3d2Safresh1  use Test::MyStyle;
3355759b3d2Safresh1
3365759b3d2Safresh1  check_test(
3375759b3d2Safresh1    sub {
3385759b3d2Safresh1      is_mystyle_qr("this", "that", "not matching");
3395759b3d2Safresh1    },
3405759b3d2Safresh1    {
3415759b3d2Safresh1      ok => 0, # expect this to fail
3425759b3d2Safresh1      name => "not matching",
3435759b3d2Safresh1      diag => qr/Expected: 'this'\s+Got: 'that'/,
3445759b3d2Safresh1    }
3455759b3d2Safresh1  );
3465759b3d2Safresh1
3475759b3d2Safresh1or
3485759b3d2Safresh1
349b8851fccSafresh1  use Test::Tester;
350b8851fccSafresh1
351b8851fccSafresh1  use Test::More tests => 3;
352b8851fccSafresh1  use Test::MyStyle;
353b8851fccSafresh1
354b8851fccSafresh1  my ($premature, @results) = run_tests(
355b8851fccSafresh1    sub {
356b8851fccSafresh1      is_database_alive("dbname");
357b8851fccSafresh1    }
358b8851fccSafresh1  );
359b8851fccSafresh1
360b8851fccSafresh1  # now use Test::More::like to check the diagnostic output
361b8851fccSafresh1
362b8851fccSafresh1  like($results[0]->{diag}, "/^Database ping took \\d+ seconds$"/, "diag");
363b8851fccSafresh1
364b8851fccSafresh1=head1 DESCRIPTION
365b8851fccSafresh1
366b8851fccSafresh1If you have written a test module based on Test::Builder then Test::Tester
367b8851fccSafresh1allows you to test it with the minimum of effort.
368b8851fccSafresh1
369b8851fccSafresh1=head1 HOW TO USE (THE EASY WAY)
370b8851fccSafresh1
371b8851fccSafresh1From version 0.08 Test::Tester no longer requires you to included anything
372b8851fccSafresh1special in your test modules. All you need to do is
373b8851fccSafresh1
374b8851fccSafresh1  use Test::Tester;
375b8851fccSafresh1
376b8851fccSafresh1in your test script B<before> any other Test::Builder based modules and away
377b8851fccSafresh1you go.
378b8851fccSafresh1
379b8851fccSafresh1Other modules based on Test::Builder can be used to help with the
380b8851fccSafresh1testing.  In fact you can even use functions from your module to test
381b8851fccSafresh1other functions from the same module (while this is possible it is
382b8851fccSafresh1probably not a good idea, if your module has bugs, then
383b8851fccSafresh1using it to test itself may give the wrong answers).
384b8851fccSafresh1
385b8851fccSafresh1The easiest way to test is to do something like
386b8851fccSafresh1
387b8851fccSafresh1  check_test(
388b8851fccSafresh1    sub { is_mystyle_eq("this", "that", "not eq") },
389b8851fccSafresh1    {
390b8851fccSafresh1      ok => 0, # we expect the test to fail
391b8851fccSafresh1      name => "not eq",
392b8851fccSafresh1      diag => "Expected: 'this'\nGot: 'that'",
393b8851fccSafresh1    }
394b8851fccSafresh1  );
395b8851fccSafresh1
39656d68f1eSafresh1this will execute the is_mystyle_eq test, capturing its results and
397b8851fccSafresh1checking that they are what was expected.
398b8851fccSafresh1
399b8851fccSafresh1You may need to examine the test results in a more flexible way, for
400b8851fccSafresh1example, the diagnostic output may be quite long or complex or it may involve
401b8851fccSafresh1something that you cannot predict in advance like a timestamp. In this case
402b8851fccSafresh1you can get direct access to the test results:
403b8851fccSafresh1
404b8851fccSafresh1  my ($premature, @results) = run_tests(
405b8851fccSafresh1    sub {
406b8851fccSafresh1      is_database_alive("dbname");
407b8851fccSafresh1    }
408b8851fccSafresh1  );
409b8851fccSafresh1
410b8851fccSafresh1  like($result[0]->{diag}, "/^Database ping took \\d+ seconds$"/, "diag");
411b8851fccSafresh1
4125759b3d2Safresh1or
4135759b3d2Safresh1
4145759b3d2Safresh1  check_test(
4155759b3d2Safresh1    sub { is_mystyle_qr("this", "that", "not matching") },
4165759b3d2Safresh1    {
4175759b3d2Safresh1      ok => 0, # we expect the test to fail
4185759b3d2Safresh1      name => "not matching",
4195759b3d2Safresh1      diag => qr/Expected: 'this'\s+Got: 'that'/,
4205759b3d2Safresh1    }
4215759b3d2Safresh1  );
422b8851fccSafresh1
423b8851fccSafresh1We cannot predict how long the database ping will take so we use
424b8851fccSafresh1Test::More's like() test to check that the diagnostic string is of the right
425b8851fccSafresh1form.
426b8851fccSafresh1
427b8851fccSafresh1=head1 HOW TO USE (THE HARD WAY)
428b8851fccSafresh1
429b8851fccSafresh1I<This is here for backwards compatibility only>
430b8851fccSafresh1
431b8851fccSafresh1Make your module use the Test::Tester::Capture object instead of the
432b8851fccSafresh1Test::Builder one. How to do this depends on your module but assuming that
433b8851fccSafresh1your module holds the Test::Builder object in $Test and that all your test
434b8851fccSafresh1routines access it through $Test then providing a function something like this
435b8851fccSafresh1
436b8851fccSafresh1  sub set_builder
437b8851fccSafresh1  {
438b8851fccSafresh1    $Test = shift;
439b8851fccSafresh1  }
440b8851fccSafresh1
441b8851fccSafresh1should allow your test scripts to do
442b8851fccSafresh1
443b8851fccSafresh1  Test::YourModule::set_builder(Test::Tester->capture);
444b8851fccSafresh1
445b8851fccSafresh1and after that any tests inside your module will captured.
446b8851fccSafresh1
447b8851fccSafresh1=head1 TEST RESULTS
448b8851fccSafresh1
449b8851fccSafresh1The result of each test is captured in a hash. These hashes are the same as
450b8851fccSafresh1the hashes returned by Test::Builder->details but with a couple of extra
451b8851fccSafresh1fields.
452b8851fccSafresh1
453b8851fccSafresh1These fields are documented in L<Test::Builder> in the details() function
454b8851fccSafresh1
455b8851fccSafresh1=over 2
456b8851fccSafresh1
457b8851fccSafresh1=item ok
458b8851fccSafresh1
459b8851fccSafresh1Did the test pass?
460b8851fccSafresh1
461b8851fccSafresh1=item actual_ok
462b8851fccSafresh1
463b8851fccSafresh1Did the test really pass? That is, did the pass come from
464b8851fccSafresh1Test::Builder->ok() or did it pass because it was a TODO test?
465b8851fccSafresh1
466b8851fccSafresh1=item name
467b8851fccSafresh1
468b8851fccSafresh1The name supplied for the test.
469b8851fccSafresh1
470b8851fccSafresh1=item type
471b8851fccSafresh1
472b8851fccSafresh1What kind of test? Possibilities include, skip, todo etc. See
473b8851fccSafresh1L<Test::Builder> for more details.
474b8851fccSafresh1
475b8851fccSafresh1=item reason
476b8851fccSafresh1
477b8851fccSafresh1The reason for the skip, todo etc. See L<Test::Builder> for more details.
478b8851fccSafresh1
479b8851fccSafresh1=back
480b8851fccSafresh1
481b8851fccSafresh1These fields are exclusive to Test::Tester.
482b8851fccSafresh1
483b8851fccSafresh1=over 2
484b8851fccSafresh1
485b8851fccSafresh1=item diag
486b8851fccSafresh1
487b8851fccSafresh1Any diagnostics that were output for the test. This only includes
488b8851fccSafresh1diagnostics output B<after> the test result is declared.
489b8851fccSafresh1
490b8851fccSafresh1Note that Test::Builder ensures that any diagnostics end in a \n and
491b8851fccSafresh1it in earlier versions of Test::Tester it was essential that you have
4925759b3d2Safresh1the final \n in your expected diagnostics. From version 0.10 onward,
493b8851fccSafresh1Test::Tester will add the \n if you forgot it. It will not add a \n if
494b8851fccSafresh1you are expecting no diagnostics. See below for help tracking down
495b8851fccSafresh1hard to find space and tab related problems.
496b8851fccSafresh1
497b8851fccSafresh1=item depth
498b8851fccSafresh1
499b8851fccSafresh1This allows you to check that your test module is setting the correct value
500b8851fccSafresh1for $Test::Builder::Level and thus giving the correct file and line number
501b8851fccSafresh1when a test fails. It is calculated by looking at caller() and
502b8851fccSafresh1$Test::Builder::Level. It should count how many subroutines there are before
503b8851fccSafresh1jumping into the function you are testing. So for example in
504b8851fccSafresh1
505b8851fccSafresh1  run_tests( sub { my_test_function("a", "b") } );
506b8851fccSafresh1
507b8851fccSafresh1the depth should be 1 and in
508b8851fccSafresh1
509b8851fccSafresh1  sub deeper { my_test_function("a", "b") }
510b8851fccSafresh1
511b8851fccSafresh1  run_tests(sub { deeper() });
512b8851fccSafresh1
513b8851fccSafresh1depth should be 2, that is 1 for the sub {} and one for deeper(). This
514b8851fccSafresh1might seem a little complex but if your tests look like the simple
515b8851fccSafresh1examples in this doc then you don't need to worry as the depth will
516b8851fccSafresh1always be 1 and that's what Test::Tester expects by default.
517b8851fccSafresh1
518b8851fccSafresh1B<Note>: if you do not specify a value for depth in check_test() then it
519b8851fccSafresh1automatically compares it against 1, if you really want to skip the depth
520b8851fccSafresh1test then pass in undef.
521b8851fccSafresh1
522b8851fccSafresh1B<Note>: depth will not be correctly calculated for tests that run from a
523b8851fccSafresh1signal handler or an END block or anywhere else that hides the call stack.
524b8851fccSafresh1
525b8851fccSafresh1=back
526b8851fccSafresh1
527b8851fccSafresh1Some of Test::Tester's functions return arrays of these hashes, just
528b8851fccSafresh1like Test::Builder->details. That is, the hash for the first test will
529b8851fccSafresh1be array element 1 (not 0). Element 0 will not be a hash it will be a
530b8851fccSafresh1string which contains any diagnostic output that came before the first
531b8851fccSafresh1test. This should usually be empty, if it's not, it means something
532b8851fccSafresh1output diagnostics before any test results showed up.
533b8851fccSafresh1
534b8851fccSafresh1=head1 SPACES AND TABS
535b8851fccSafresh1
536b8851fccSafresh1Appearances can be deceptive, especially when it comes to emptiness. If you
537b8851fccSafresh1are scratching your head trying to work out why Test::Tester is saying that
538b8851fccSafresh1your diagnostics are wrong when they look perfectly right then the answer is
539b8851fccSafresh1probably whitespace. From version 0.10 on, Test::Tester surrounds the
540b8851fccSafresh1expected and got diag values with single quotes to make it easier to spot
5415759b3d2Safresh1trailing whitespace. So in this example
542b8851fccSafresh1
543b8851fccSafresh1  # Got diag (5 bytes):
544b8851fccSafresh1  # 'abcd '
545b8851fccSafresh1  # Expected diag (4 bytes):
546b8851fccSafresh1  # 'abcd'
547b8851fccSafresh1
548b8851fccSafresh1it is quite clear that there is a space at the end of the first string.
549b8851fccSafresh1Another way to solve this problem is to use colour and inverse video on an
550b8851fccSafresh1ANSI terminal, see below COLOUR below if you want this.
551b8851fccSafresh1
552b8851fccSafresh1Unfortunately this is sometimes not enough, neither colour nor quotes will
553b8851fccSafresh1help you with problems involving tabs, other non-printing characters and
554b8851fccSafresh1certain kinds of problems inherent in Unicode. To deal with this, you can
555b8851fccSafresh1switch Test::Tester into a mode whereby all "tricky" characters are shown as
556b8851fccSafresh1\{xx}. Tricky characters are those with ASCII code less than 33 or higher
557b8851fccSafresh1than 126. This makes the output more difficult to read but much easier to
558b8851fccSafresh1find subtle differences between strings. To turn on this mode either call
5595759b3d2Safresh1C<show_space()> in your test script or set the C<TESTTESTERSPACE> environment
560b8851fccSafresh1variable to be a true value. The example above would then look like
561b8851fccSafresh1
562b8851fccSafresh1  # Got diag (5 bytes):
563b8851fccSafresh1  # abcd\x{20}
564b8851fccSafresh1  # Expected diag (4 bytes):
565b8851fccSafresh1  # abcd
566b8851fccSafresh1
567b8851fccSafresh1=head1 COLOUR
568b8851fccSafresh1
569b8851fccSafresh1If you prefer to use colour as a means of finding tricky whitespace
5705759b3d2Safresh1characters then you can set the C<TESTTESTCOLOUR> environment variable to a
571b8851fccSafresh1comma separated pair of colours, the first for the foreground, the second
572b8851fccSafresh1for the background. For example "white,red" will print white text on a red
573b8851fccSafresh1background. This requires the Term::ANSIColor module. You can specify any
574b8851fccSafresh1colour that would be acceptable to the Term::ANSIColor::color function.
575b8851fccSafresh1
5765759b3d2Safresh1If you spell colour differently, that's no problem. The C<TESTTESTERCOLOR>
577b8851fccSafresh1variable also works (if both are set then the British spelling wins out).
578b8851fccSafresh1
579b8851fccSafresh1=head1 EXPORTED FUNCTIONS
580b8851fccSafresh1
581b8851fccSafresh1=head3 ($premature, @results) = run_tests(\&test_sub)
582b8851fccSafresh1
583b8851fccSafresh1\&test_sub is a reference to a subroutine.
584b8851fccSafresh1
585b8851fccSafresh1run_tests runs the subroutine in $test_sub and captures the results of any
586b8851fccSafresh1tests inside it. You can run more than 1 test inside this subroutine if you
587b8851fccSafresh1like.
588b8851fccSafresh1
589b8851fccSafresh1$premature is a string containing any diagnostic output from before
590b8851fccSafresh1the first test.
591b8851fccSafresh1
592b8851fccSafresh1@results is an array of test result hashes.
593b8851fccSafresh1
594b8851fccSafresh1=head3 cmp_result(\%result, \%expect, $name)
595b8851fccSafresh1
596b8851fccSafresh1\%result is a ref to a test result hash.
597b8851fccSafresh1
598b8851fccSafresh1\%expect is a ref to a hash of expected values for the test result.
599b8851fccSafresh1
600b8851fccSafresh1cmp_result compares the result with the expected values. If any differences
601b8851fccSafresh1are found it outputs diagnostics. You may leave out any field from the
602b8851fccSafresh1expected result and cmp_result will not do the comparison of that field.
603b8851fccSafresh1
604b8851fccSafresh1=head3 cmp_results(\@results, \@expects, $name)
605b8851fccSafresh1
606b8851fccSafresh1\@results is a ref to an array of test results.
607b8851fccSafresh1
608b8851fccSafresh1\@expects is a ref to an array of hash refs.
609b8851fccSafresh1
610b8851fccSafresh1cmp_results checks that the results match the expected results and if any
611b8851fccSafresh1differences are found it outputs diagnostics. It first checks that the
612b8851fccSafresh1number of elements in \@results and \@expects is the same. Then it goes
613b8851fccSafresh1through each result checking it against the expected result as in
614b8851fccSafresh1cmp_result() above.
615b8851fccSafresh1
616b8851fccSafresh1=head3 ($premature, @results) = check_tests(\&test_sub, \@expects, $name)
617b8851fccSafresh1
618b8851fccSafresh1\&test_sub is a reference to a subroutine.
619b8851fccSafresh1
620b8851fccSafresh1\@expect is a ref to an array of hash refs which are expected test results.
621b8851fccSafresh1
622b8851fccSafresh1check_tests combines run_tests and cmp_tests into a single call. It also
623b8851fccSafresh1checks if the tests died at any stage.
624b8851fccSafresh1
625b8851fccSafresh1It returns the same values as run_tests, so you can further examine the test
626b8851fccSafresh1results if you need to.
627b8851fccSafresh1
628b8851fccSafresh1=head3 ($premature, @results) = check_test(\&test_sub, \%expect, $name)
629b8851fccSafresh1
630b8851fccSafresh1\&test_sub is a reference to a subroutine.
631b8851fccSafresh1
632b8851fccSafresh1\%expect is a ref to an hash of expected values for the test result.
633b8851fccSafresh1
634b8851fccSafresh1check_test is a wrapper around check_tests. It combines run_tests and
635b8851fccSafresh1cmp_tests into a single call, checking if the test died. It assumes
636b8851fccSafresh1that only a single test is run inside \&test_sub and include a test to
637b8851fccSafresh1make sure this is true.
638b8851fccSafresh1
639b8851fccSafresh1It returns the same values as run_tests, so you can further examine the test
640b8851fccSafresh1results if you need to.
641b8851fccSafresh1
642b8851fccSafresh1=head3 show_space()
643b8851fccSafresh1
644b8851fccSafresh1Turn on the escaping of characters as described in the SPACES AND TABS
645b8851fccSafresh1section.
646b8851fccSafresh1
647b8851fccSafresh1=head1 HOW IT WORKS
648b8851fccSafresh1
649b8851fccSafresh1Normally, a test module (let's call it Test:MyStyle) calls
650b8851fccSafresh1Test::Builder->new to get the Test::Builder object. Test::MyStyle calls
651b8851fccSafresh1methods on this object to record information about test results. When
652b8851fccSafresh1Test::Tester is loaded, it replaces Test::Builder's new() method with one
653b8851fccSafresh1which returns a Test::Tester::Delegate object. Most of the time this object
654b8851fccSafresh1behaves as the real Test::Builder object. Any methods that are called are
655b8851fccSafresh1delegated to the real Test::Builder object so everything works perfectly.
656b8851fccSafresh1However once we go into test mode, the method calls are no longer passed to
657b8851fccSafresh1the real Test::Builder object, instead they go to the Test::Tester::Capture
658b8851fccSafresh1object. This object seems exactly like the real Test::Builder object,
659b8851fccSafresh1except, instead of outputting test results and diagnostics, it just records
660b8851fccSafresh1all the information for later analysis.
661b8851fccSafresh1
662b8851fccSafresh1=head1 CAVEATS
663b8851fccSafresh1
664b8851fccSafresh1Support for calling Test::Builder->note is minimal. It's implemented
665b8851fccSafresh1as an empty stub, so modules that use it will not crash but the calls
666b8851fccSafresh1are not recorded for testing purposes like the others. Patches
667b8851fccSafresh1welcome.
668b8851fccSafresh1
669b8851fccSafresh1=head1 SEE ALSO
670b8851fccSafresh1
671b8851fccSafresh1L<Test::Builder> the source of testing goodness. L<Test::Builder::Tester>
672b8851fccSafresh1for an alternative approach to the problem tackled by Test::Tester -
673b8851fccSafresh1captures the strings output by Test::Builder. This means you cannot get
674b8851fccSafresh1separate access to the individual pieces of information and you must predict
675b8851fccSafresh1B<exactly> what your test will output.
676b8851fccSafresh1
677b8851fccSafresh1=head1 AUTHOR
678b8851fccSafresh1
679b8851fccSafresh1This module is copyright 2005 Fergal Daly <fergal@esatclear.ie>, some parts
680b8851fccSafresh1are based on other people's work.
681b8851fccSafresh1
682b8851fccSafresh1Plan handling lifted from Test::More. written by Michael G Schwern
683b8851fccSafresh1<schwern@pobox.com>.
684b8851fccSafresh1
685b8851fccSafresh1Test::Tester::Capture is a cut down and hacked up version of Test::Builder.
686b8851fccSafresh1Test::Builder was written by chromatic <chromatic@wgz.org> and Michael G
687b8851fccSafresh1Schwern <schwern@pobox.com>.
688b8851fccSafresh1
689b8851fccSafresh1=head1 LICENSE
690b8851fccSafresh1
691b8851fccSafresh1Under the same license as Perl itself
692b8851fccSafresh1
693*3d61058aSafresh1See L<https://dev.perl.org/licenses/>
694b8851fccSafresh1
695b8851fccSafresh1=cut
696