Leaked source code of windows server 2003
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.

262 lines
7.9 KiB

  1. use strict;
  2. package Test;
  3. use Test::Harness 1.1601 ();
  4. use Carp;
  5. our($VERSION, @ISA, @EXPORT, @EXPORT_OK, $ntest, $TestLevel); #public-ish
  6. our($TESTOUT, $ONFAIL, %todo, %history, $planned, @FAILDETAIL); #private-ish
  7. $VERSION = '1.15';
  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 (!defined $expected) {
  62. $ok = !defined $result;
  63. } elsif (!defined $result) {
  64. $ok = 0;
  65. } elsif ((ref($expected)||'') eq 'Regexp') {
  66. $ok = $result =~ /$expected/;
  67. } elsif (($regex) = ($expected =~ m,^ / (.+) / $,sx) or
  68. ($ignore, $regex) = ($expected =~ m,^ m([^\w\s]) (.+) \1 $,sx)) {
  69. $ok = $result =~ /$regex/;
  70. } else {
  71. $ok = $result eq $expected;
  72. }
  73. }
  74. my $todo = $todo{$ntest};
  75. if ($todo and $ok) {
  76. $context .= ' TODO?!' if $todo;
  77. print $TESTOUT "ok $ntest # ($context)\n";
  78. } else {
  79. # Issuing two separate print()s causes severe trouble with
  80. # Test::Harness on VMS. The "not "'s for failed tests occur
  81. # on a separate line and would not get counted as failures.
  82. #print $TESTOUT "not " if !$ok;
  83. #print $TESTOUT "ok $ntest\n";
  84. # Replace with a single print() as a workaround:
  85. my $okline = '';
  86. $okline = "not " if !$ok;
  87. $okline .= "ok $ntest\n";
  88. print $TESTOUT $okline;
  89. if (!$ok) {
  90. my $detail = { 'repetition' => $repetition, 'package' => $pkg,
  91. 'result' => $result, 'todo' => $todo };
  92. $$detail{expected} = $expected if defined $expected;
  93. $diag = $$detail{diagnostic} = to_value(shift) if @_;
  94. $context .= ' *TODO*' if $todo;
  95. if (!defined $expected) {
  96. if (!$diag) {
  97. print $TESTOUT "# Failed test $ntest in $context\n";
  98. } else {
  99. print $TESTOUT "# Failed test $ntest in $context: $diag\n";
  100. }
  101. } else {
  102. my $prefix = "Test $ntest";
  103. print $TESTOUT "# $prefix got: ".
  104. (defined $result? "'$result'":'<UNDEF>')." ($context)\n";
  105. $prefix = ' ' x (length($prefix) - 5);
  106. if ((ref($expected)||'') eq 'Regexp') {
  107. $expected = 'qr/'.$expected.'/'
  108. } else {
  109. $expected = "'$expected'";
  110. }
  111. if (!$diag) {
  112. print $TESTOUT "# $prefix Expected: $expected\n";
  113. } else {
  114. print $TESTOUT "# $prefix Expected: $expected ($diag)\n";
  115. }
  116. }
  117. push @FAILDETAIL, $detail;
  118. }
  119. }
  120. ++ $ntest;
  121. $ok;
  122. }
  123. sub skip ($$;$$) {
  124. my $whyskip = to_value(shift);
  125. if ($whyskip) {
  126. $whyskip = 'skip' if $whyskip =~ m/^\d+$/;
  127. print $TESTOUT "ok $ntest # $whyskip\n";
  128. ++ $ntest;
  129. 1;
  130. } else {
  131. local($TestLevel) = $TestLevel+1; #ignore this stack frame
  132. &ok;
  133. }
  134. }
  135. END {
  136. $ONFAIL->(\@FAILDETAIL) if @FAILDETAIL && $ONFAIL;
  137. }
  138. 1;
  139. __END__
  140. =head1 NAME
  141. Test - provides a simple framework for writing test scripts
  142. =head1 SYNOPSIS
  143. use strict;
  144. use Test;
  145. # use a BEGIN block so we print our plan before MyModule is loaded
  146. BEGIN { plan tests => 14, todo => [3,4] }
  147. # load your module...
  148. use MyModule;
  149. ok(0); # failure
  150. ok(1); # success
  151. ok(0); # ok, expected failure (see todo list, above)
  152. ok(1); # surprise success!
  153. ok(0,1); # failure: '0' ne '1'
  154. ok('broke','fixed'); # failure: 'broke' ne 'fixed'
  155. ok('fixed','fixed'); # success: 'fixed' eq 'fixed'
  156. ok('fixed',qr/x/); # success: 'fixed' =~ qr/x/
  157. ok(sub { 1+1 }, 2); # success: '2' eq '2'
  158. ok(sub { 1+1 }, 3); # failure: '2' ne '3'
  159. ok(0, int(rand(2)); # (just kidding :-)
  160. my @list = (0,0);
  161. ok @list, 3, "\@list=".join(',',@list); #extra diagnostics
  162. ok 'segmentation fault', '/(?i)success/'; #regex match
  163. skip($feature_is_missing, ...); #do platform specific test
  164. =head1 DESCRIPTION
  165. L<Test::Harness|Test::Harness> expects to see particular output when it
  166. executes tests. This module aims to make writing proper test scripts just
  167. a little bit easier (and less error prone :-).
  168. =head1 TEST TYPES
  169. =over 4
  170. =item * NORMAL TESTS
  171. These tests are expected to succeed. If they don't something's
  172. screwed up!
  173. =item * SKIPPED TESTS
  174. Skip is for tests that might or might not be possible to run depending
  175. on the availability of platform specific features. The first argument
  176. should evaluate to true (think "yes, please skip") if the required
  177. feature is not available. After the first argument, skip works
  178. exactly the same way as do normal tests.
  179. =item * TODO TESTS
  180. TODO tests are designed for maintaining an B<executable TODO list>.
  181. These tests are expected NOT to succeed. If a TODO test does succeed,
  182. the feature in question should not be on the TODO list, now should it?
  183. Packages should NOT be released with succeeding TODO tests. As soon
  184. as a TODO test starts working, it should be promoted to a normal test
  185. and the newly working feature should be documented in the release
  186. notes or change log.
  187. =back
  188. =head1 RETURN VALUE
  189. Both C<ok> and C<skip> return true if their test succeeds and false
  190. otherwise in a scalar context.
  191. =head1 ONFAIL
  192. BEGIN { plan test => 4, onfail => sub { warn "CALL 911!" } }
  193. While test failures should be enough, extra diagnostics can be
  194. triggered at the end of a test run. C<onfail> is passed an array ref
  195. of hash refs that describe each test failure. Each hash will contain
  196. at least the following fields: C<package>, C<repetition>, and
  197. C<result>. (The file, line, and test number are not included because
  198. their correspondence to a particular test is tenuous.) If the test
  199. had an expected value or a diagnostic string, these will also be
  200. included.
  201. The B<optional> C<onfail> hook might be used simply to print out the
  202. version of your package and/or how to report problems. It might also
  203. be used to generate extremely sophisticated diagnostics for a
  204. particularly bizarre test failure. However it's not a panacea. Core
  205. dumps or other unrecoverable errors prevent the C<onfail> hook from
  206. running. (It is run inside an C<END> block.) Besides, C<onfail> is
  207. probably over-kill in most cases. (Your test code should be simpler
  208. than the code it is testing, yes?)
  209. =head1 SEE ALSO
  210. L<Test::Harness> and, perhaps, test coverage analysis tools.
  211. =head1 AUTHOR
  212. Copyright (c) 1998-1999 Joshua Nathaniel Pritikin. All rights reserved.
  213. This package is free software and is provided "as is" without express
  214. or implied warranty. It may be used, redistributed and/or modified
  215. under the terms of the Perl Artistic License (see
  216. http://www.perl.com/perl/misc/Artistic.html)
  217. =cut