Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
7.2 KiB

  1. use strict;
  2. package Test;
  3. use Test::Harness 1.1601 ();
  4. use Carp;
  5. use vars (qw($VERSION @ISA @EXPORT @EXPORT_OK $ntest $TestLevel), #public-ish
  6. qw($TESTOUT $ONFAIL %todo %history $planned @FAILDETAIL)); #private-ish
  7. $VERSION = '1.122';
  8. require Exporter;
  9. @ISA=('Exporter');
  10. @EXPORT=qw(&plan &ok &skip);
  11. @EXPORT_OK=qw($ntest $TESTOUT);
  12. $TestLevel = 0; # how many extra stack frames to skip
  13. $|=1;
  14. #$^W=1; ?
  15. $ntest=1;
  16. $TESTOUT = *STDOUT{IO};
  17. # Use of this variable is strongly discouraged. It is set mainly to
  18. # help test coverage analyzers know which test is running.
  19. $ENV{REGRESSION_TEST} = $0;
  20. sub plan {
  21. croak "Test::plan(%args): odd number of arguments" if @_ & 1;
  22. croak "Test::plan(): should not be called more than once" if $planned;
  23. my $max=0;
  24. for (my $x=0; $x < @_; $x+=2) {
  25. my ($k,$v) = @_[$x,$x+1];
  26. if ($k =~ /^test(s)?$/) { $max = $v; }
  27. elsif ($k eq 'todo' or
  28. $k eq 'failok') { for (@$v) { $todo{$_}=1; }; }
  29. elsif ($k eq 'onfail') {
  30. ref $v eq 'CODE' or croak "Test::plan(onfail => $v): must be CODE";
  31. $ONFAIL = $v;
  32. }
  33. else { carp "Test::plan(): skipping unrecognized directive '$k'" }
  34. }
  35. my @todo = sort { $a <=> $b } keys %todo;
  36. if (@todo) {
  37. print $TESTOUT "1..$max todo ".join(' ', @todo).";\n";
  38. } else {
  39. print $TESTOUT "1..$max\n";
  40. }
  41. ++$planned;
  42. }
  43. sub to_value {
  44. my ($v) = @_;
  45. (ref $v or '') eq 'CODE' ? $v->() : $v;
  46. }
  47. sub ok ($;$$) {
  48. croak "ok: plan before you test!" if !$planned;
  49. my ($pkg,$file,$line) = caller($TestLevel);
  50. my $repetition = ++$history{"$file:$line"};
  51. my $context = ("$file at line $line".
  52. ($repetition > 1 ? " fail \#$repetition" : ''));
  53. my $ok=0;
  54. my $result = to_value(shift);
  55. my ($expected,$diag);
  56. if (@_ == 0) {
  57. $ok = $result;
  58. } else {
  59. $expected = to_value(shift);
  60. my ($regex,$ignore);
  61. if ((ref($expected)||'') eq 'Regexp') {
  62. $ok = $result =~ /$expected/;
  63. } elsif (($regex) = ($expected =~ m,^ / (.+) / $,sx) or
  64. ($ignore, $regex) = ($expected =~ m,^ m([^\w\s]) (.+) \1 $,sx)) {
  65. $ok = $result =~ /$regex/;
  66. } else {
  67. $ok = $result eq $expected;
  68. }
  69. }
  70. my $todo = $todo{$ntest};
  71. if ($todo and $ok) {
  72. $context .= ' TODO?!' if $todo;
  73. print $TESTOUT "ok $ntest # ($context)\n";
  74. } else {
  75. print $TESTOUT "not " if !$ok;
  76. print $TESTOUT "ok $ntest\n";
  77. if (!$ok) {
  78. my $detail = { 'repetition' => $repetition, 'package' => $pkg,
  79. 'result' => $result, 'todo' => $todo };
  80. $$detail{expected} = $expected if defined $expected;
  81. $diag = $$detail{diagnostic} = to_value(shift) if @_;
  82. $context .= ' *TODO*' if $todo;
  83. if (!defined $expected) {
  84. if (!$diag) {
  85. print $TESTOUT "# Failed test $ntest in $context\n";
  86. } else {
  87. print $TESTOUT "# Failed test $ntest in $context: $diag\n";
  88. }
  89. } else {
  90. my $prefix = "Test $ntest";
  91. print $TESTOUT "# $prefix got: '$result' ($context)\n";
  92. $prefix = ' ' x (length($prefix) - 5);
  93. if ((ref($expected)||'') eq 'Regexp') {
  94. $expected = 'qr/'.$expected.'/'
  95. } else {
  96. $expected = "'$expected'";
  97. }
  98. if (!$diag) {
  99. print $TESTOUT "# $prefix Expected: $expected\n";
  100. } else {
  101. print $TESTOUT "# $prefix Expected: $expected ($diag)\n";
  102. }
  103. }
  104. push @FAILDETAIL, $detail;
  105. }
  106. }
  107. ++ $ntest;
  108. $ok;
  109. }
  110. sub skip ($$;$$) {
  111. my $whyskip = to_value(shift);
  112. if ($whyskip) {
  113. $whyskip = 'skip' if $whyskip =~ m/^\d+$/;
  114. print $TESTOUT "ok $ntest # $whyskip\n";
  115. ++ $ntest;
  116. 1;
  117. } else {
  118. local($TestLevel) = $TestLevel+1; #ignore this stack frame
  119. &ok;
  120. }
  121. }
  122. END {
  123. $ONFAIL->(\@FAILDETAIL) if @FAILDETAIL && $ONFAIL;
  124. }
  125. 1;
  126. __END__
  127. =head1 NAME
  128. Test - provides a simple framework for writing test scripts
  129. =head1 SYNOPSIS
  130. use strict;
  131. use Test;
  132. # use a BEGIN block so we print our plan before MyModule is loaded
  133. BEGIN { plan tests => 14, todo => [3,4] }
  134. # load your module...
  135. use MyModule;
  136. ok(0); # failure
  137. ok(1); # success
  138. ok(0); # ok, expected failure (see todo list, above)
  139. ok(1); # surprise success!
  140. ok(0,1); # failure: '0' ne '1'
  141. ok('broke','fixed'); # failure: 'broke' ne 'fixed'
  142. ok('fixed','fixed'); # success: 'fixed' eq 'fixed'
  143. ok('fixed',qr/x/); # success: 'fixed' =~ qr/x/
  144. ok(sub { 1+1 }, 2); # success: '2' eq '2'
  145. ok(sub { 1+1 }, 3); # failure: '2' ne '3'
  146. ok(0, int(rand(2)); # (just kidding :-)
  147. my @list = (0,0);
  148. ok @list, 3, "\@list=".join(',',@list); #extra diagnostics
  149. ok 'segmentation fault', '/(?i)success/'; #regex match
  150. skip($feature_is_missing, ...); #do platform specific test
  151. =head1 DESCRIPTION
  152. L<Test::Harness> expects to see particular output when it executes
  153. tests. This module aims to make writing proper test scripts just a
  154. little bit easier (and less error prone :-).
  155. =head1 TEST TYPES
  156. =over 4
  157. =item * NORMAL TESTS
  158. These tests are expected to succeed. If they don't something's
  159. screwed up!
  160. =item * SKIPPED TESTS
  161. Skip is for tests that might or might not be possible to run depending
  162. on the availability of platform specific features. The first argument
  163. should evaluate to true (think "yes, please skip") if the required
  164. feature is not available. After the first argument, skip works
  165. exactly the same way as do normal tests.
  166. =item * TODO TESTS
  167. TODO tests are designed for maintaining an B<executable TODO list>.
  168. These tests are expected NOT to succeed. If a TODO test does succeed,
  169. the feature in question should not be on the TODO list, now should it?
  170. Packages should NOT be released with succeeding TODO tests. As soon
  171. as a TODO test starts working, it should be promoted to a normal test
  172. and the newly working feature should be documented in the release
  173. notes or change log.
  174. =back
  175. =head1 RETURN VALUE
  176. Both C<ok> and C<skip> return true if their test succeeds and false
  177. otherwise in a scalar context.
  178. =head1 ONFAIL
  179. BEGIN { plan test => 4, onfail => sub { warn "CALL 911!" } }
  180. While test failures should be enough, extra diagnostics can be
  181. triggered at the end of a test run. C<onfail> is passed an array ref
  182. of hash refs that describe each test failure. Each hash will contain
  183. at least the following fields: C<package>, C<repetition>, and
  184. C<result>. (The file, line, and test number are not included because
  185. their correspondance to a particular test is tenuous.) If the test
  186. had an expected value or a diagnostic string, these will also be
  187. included.
  188. The B<optional> C<onfail> hook might be used simply to print out the
  189. version of your package and/or how to report problems. It might also
  190. be used to generate extremely sophisticated diagnostics for a
  191. particularly bizarre test failure. However it's not a panacea. Core
  192. dumps or other unrecoverable errors prevent the C<onfail> hook from
  193. running. (It is run inside an C<END> block.) Besides, C<onfail> is
  194. probably over-kill in most cases. (Your test code should be simpler
  195. than the code it is testing, yes?)
  196. =head1 SEE ALSO
  197. L<Test::Harness> and, perhaps, test coverage analysis tools.
  198. =head1 AUTHOR
  199. Copyright (c) 1998 Joshua Nathaniel Pritikin. All rights reserved.
  200. This package is free software and is provided "as is" without express
  201. or implied warranty. It may be used, redistributed and/or modified
  202. under the terms of the Perl Artistic License (see
  203. http://www.perl.com/perl/misc/Artistic.html)
  204. =cut