PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1-3-29/SWIG/Examples/test-suite/perl5/Test/Builder.pm

#
Perl | 1591 lines | 1277 code | 283 blank | 31 comment | 80 complexity | f76bc10c1aa69e373ab6f03ec7d69ceb MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. package Test::Builder;
  2. use 5.004;
  3. # $^C was only introduced in 5.005-ish. We do this to prevent
  4. # use of uninitialized value warnings in older perls.
  5. $^C ||= 0;
  6. use strict;
  7. use vars qw($VERSION);
  8. $VERSION = '0.22';
  9. $VERSION = eval $VERSION; # make the alpha version come out as a number
  10. # Make Test::Builder thread-safe for ithreads.
  11. BEGIN {
  12. use Config;
  13. # Load threads::shared when threads are turned on
  14. if( $] >= 5.008 && $Config{useithreads} && $INC{'threads.pm'}) {
  15. require threads::shared;
  16. # Hack around YET ANOTHER threads::shared bug. It would
  17. # occassionally forget the contents of the variable when sharing it.
  18. # So we first copy the data, then share, then put our copy back.
  19. *share = sub (\[$@%]) {
  20. my $type = ref $_[0];
  21. my $data;
  22. if( $type eq 'HASH' ) {
  23. %$data = %{$_[0]};
  24. }
  25. elsif( $type eq 'ARRAY' ) {
  26. @$data = @{$_[0]};
  27. }
  28. elsif( $type eq 'SCALAR' ) {
  29. $$data = ${$_[0]};
  30. }
  31. else {
  32. die "Unknown type: ".$type;
  33. }
  34. $_[0] = &threads::shared::share($_[0]);
  35. if( $type eq 'HASH' ) {
  36. %{$_[0]} = %$data;
  37. }
  38. elsif( $type eq 'ARRAY' ) {
  39. @{$_[0]} = @$data;
  40. }
  41. elsif( $type eq 'SCALAR' ) {
  42. ${$_[0]} = $$data;
  43. }
  44. else {
  45. die "Unknown type: ".$type;
  46. }
  47. return $_[0];
  48. };
  49. }
  50. # 5.8.0's threads::shared is busted when threads are off.
  51. # We emulate it here.
  52. else {
  53. *share = sub { return $_[0] };
  54. *lock = sub { 0 };
  55. }
  56. }
  57. =head1 NAME
  58. Test::Builder - Backend for building test libraries
  59. =head1 SYNOPSIS
  60. package My::Test::Module;
  61. use Test::Builder;
  62. require Exporter;
  63. @ISA = qw(Exporter);
  64. @EXPORT = qw(ok);
  65. my $Test = Test::Builder->new;
  66. $Test->output('my_logfile');
  67. sub import {
  68. my($self) = shift;
  69. my $pack = caller;
  70. $Test->exported_to($pack);
  71. $Test->plan(@_);
  72. $self->export_to_level(1, $self, 'ok');
  73. }
  74. sub ok {
  75. my($test, $name) = @_;
  76. $Test->ok($test, $name);
  77. }
  78. =head1 DESCRIPTION
  79. Test::Simple and Test::More have proven to be popular testing modules,
  80. but they're not always flexible enough. Test::Builder provides the a
  81. building block upon which to write your own test libraries I<which can
  82. work together>.
  83. =head2 Construction
  84. =over 4
  85. =item B<new>
  86. my $Test = Test::Builder->new;
  87. Returns a Test::Builder object representing the current state of the
  88. test.
  89. Since you only run one test per program, there is B<one and only one>
  90. Test::Builder object. No matter how many times you call new(), you're
  91. getting the same object. (This is called a singleton).
  92. =cut
  93. my $Test = Test::Builder->new;
  94. sub new {
  95. my($class) = shift;
  96. $Test ||= bless ['Move along, nothing to see here'], $class;
  97. return $Test;
  98. }
  99. =item B<reset>
  100. $Test->reset;
  101. Reinitializes the Test::Builder singleton to its original state.
  102. Mostly useful for tests run in persistent environments where the same
  103. test might be run multiple times in the same process.
  104. =cut
  105. my $Test_Died;
  106. my $Have_Plan;
  107. my $No_Plan;
  108. my $Curr_Test; share($Curr_Test);
  109. use vars qw($Level);
  110. my $Original_Pid;
  111. my @Test_Results; share(@Test_Results);
  112. my $Exported_To;
  113. my $Expected_Tests;
  114. my $Skip_All;
  115. my $Use_Nums;
  116. my($No_Header, $No_Ending);
  117. $Test->reset;
  118. sub reset {
  119. my ($self) = @_;
  120. $Test_Died = 0;
  121. $Have_Plan = 0;
  122. $No_Plan = 0;
  123. $Curr_Test = 0;
  124. $Level = 1;
  125. $Original_Pid = $$;
  126. @Test_Results = ();
  127. $Exported_To = undef;
  128. $Expected_Tests = 0;
  129. $Skip_All = 0;
  130. $Use_Nums = 1;
  131. ($No_Header, $No_Ending) = (0,0);
  132. $self->_dup_stdhandles unless $^C;
  133. return undef;
  134. }
  135. =back
  136. =head2 Setting up tests
  137. These methods are for setting up tests and declaring how many there
  138. are. You usually only want to call one of these methods.
  139. =over 4
  140. =item B<exported_to>
  141. my $pack = $Test->exported_to;
  142. $Test->exported_to($pack);
  143. Tells Test::Builder what package you exported your functions to.
  144. This is important for getting TODO tests right.
  145. =cut
  146. sub exported_to {
  147. my($self, $pack) = @_;
  148. if( defined $pack ) {
  149. $Exported_To = $pack;
  150. }
  151. return $Exported_To;
  152. }
  153. =item B<plan>
  154. $Test->plan('no_plan');
  155. $Test->plan( skip_all => $reason );
  156. $Test->plan( tests => $num_tests );
  157. A convenient way to set up your tests. Call this and Test::Builder
  158. will print the appropriate headers and take the appropriate actions.
  159. If you call plan(), don't call any of the other methods below.
  160. =cut
  161. sub plan {
  162. my($self, $cmd, $arg) = @_;
  163. return unless $cmd;
  164. if( $Have_Plan ) {
  165. die sprintf "You tried to plan twice! Second plan at %s line %d\n",
  166. ($self->caller)[1,2];
  167. }
  168. if( $cmd eq 'no_plan' ) {
  169. $self->no_plan;
  170. }
  171. elsif( $cmd eq 'skip_all' ) {
  172. return $self->skip_all($arg);
  173. }
  174. elsif( $cmd eq 'tests' ) {
  175. if( $arg ) {
  176. return $self->expected_tests($arg);
  177. }
  178. elsif( !defined $arg ) {
  179. die "Got an undefined number of tests. Looks like you tried to ".
  180. "say how many tests you plan to run but made a mistake.\n";
  181. }
  182. elsif( !$arg ) {
  183. die "You said to run 0 tests! You've got to run something.\n";
  184. }
  185. }
  186. else {
  187. require Carp;
  188. my @args = grep { defined } ($cmd, $arg);
  189. Carp::croak("plan() doesn't understand @args");
  190. }
  191. return 1;
  192. }
  193. =item B<expected_tests>
  194. my $max = $Test->expected_tests;
  195. $Test->expected_tests($max);
  196. Gets/sets the # of tests we expect this test to run and prints out
  197. the appropriate headers.
  198. =cut
  199. sub expected_tests {
  200. my $self = shift;
  201. my($max) = @_;
  202. if( @_ ) {
  203. die "Number of tests must be a postive integer. You gave it '$max'.\n"
  204. unless $max =~ /^\+?\d+$/ and $max > 0;
  205. $Expected_Tests = $max;
  206. $Have_Plan = 1;
  207. $self->_print("1..$max\n") unless $self->no_header;
  208. }
  209. return $Expected_Tests;
  210. }
  211. =item B<no_plan>
  212. $Test->no_plan;
  213. Declares that this test will run an indeterminate # of tests.
  214. =cut
  215. sub no_plan {
  216. $No_Plan = 1;
  217. $Have_Plan = 1;
  218. }
  219. =item B<has_plan>
  220. $plan = $Test->has_plan
  221. Find out whether a plan has been defined. $plan is either C<undef> (no plan has been set), C<no_plan> (indeterminate # of tests) or an integer (the number of expected tests).
  222. =cut
  223. sub has_plan {
  224. return($Expected_Tests) if $Expected_Tests;
  225. return('no_plan') if $No_Plan;
  226. return(undef);
  227. };
  228. =item B<skip_all>
  229. $Test->skip_all;
  230. $Test->skip_all($reason);
  231. Skips all the tests, using the given $reason. Exits immediately with 0.
  232. =cut
  233. sub skip_all {
  234. my($self, $reason) = @_;
  235. my $out = "1..0";
  236. $out .= " # Skip $reason" if $reason;
  237. $out .= "\n";
  238. $Skip_All = 1;
  239. $self->_print($out) unless $self->no_header;
  240. exit(0);
  241. }
  242. =back
  243. =head2 Running tests
  244. These actually run the tests, analogous to the functions in
  245. Test::More.
  246. $name is always optional.
  247. =over 4
  248. =item B<ok>
  249. $Test->ok($test, $name);
  250. Your basic test. Pass if $test is true, fail if $test is false. Just
  251. like Test::Simple's ok().
  252. =cut
  253. sub ok {
  254. my($self, $test, $name) = @_;
  255. # $test might contain an object which we don't want to accidentally
  256. # store, so we turn it into a boolean.
  257. $test = $test ? 1 : 0;
  258. unless( $Have_Plan ) {
  259. require Carp;
  260. Carp::croak("You tried to run a test without a plan! Gotta have a plan.");
  261. }
  262. lock $Curr_Test;
  263. $Curr_Test++;
  264. # In case $name is a string overloaded object, force it to stringify.
  265. $self->_unoverload(\$name);
  266. $self->diag(<<ERR) if defined $name and $name =~ /^[\d\s]+$/;
  267. You named your test '$name'. You shouldn't use numbers for your test names.
  268. Very confusing.
  269. ERR
  270. my($pack, $file, $line) = $self->caller;
  271. my $todo = $self->todo($pack);
  272. $self->_unoverload(\$todo);
  273. my $out;
  274. my $result = &share({});
  275. unless( $test ) {
  276. $out .= "not ";
  277. @$result{ 'ok', 'actual_ok' } = ( ( $todo ? 1 : 0 ), 0 );
  278. }
  279. else {
  280. @$result{ 'ok', 'actual_ok' } = ( 1, $test );
  281. }
  282. $out .= "ok";
  283. $out .= " $Curr_Test" if $self->use_numbers;
  284. if( defined $name ) {
  285. $name =~ s|#|\\#|g; # # in a name can confuse Test::Harness.
  286. $out .= " - $name";
  287. $result->{name} = $name;
  288. }
  289. else {
  290. $result->{name} = '';
  291. }
  292. if( $todo ) {
  293. $out .= " # TODO $todo";
  294. $result->{reason} = $todo;
  295. $result->{type} = 'todo';
  296. }
  297. else {
  298. $result->{reason} = '';
  299. $result->{type} = '';
  300. }
  301. $Test_Results[$Curr_Test-1] = $result;
  302. $out .= "\n";
  303. $self->_print($out);
  304. unless( $test ) {
  305. my $msg = $todo ? "Failed (TODO)" : "Failed";
  306. $self->_print_diag("\n") if $ENV{HARNESS_ACTIVE};
  307. $self->diag(" $msg test ($file at line $line)\n");
  308. }
  309. return $test ? 1 : 0;
  310. }
  311. sub _unoverload {
  312. my $self = shift;
  313. local($@,$!);
  314. eval { require overload } || return;
  315. foreach my $thing (@_) {
  316. eval {
  317. if( defined $$thing ) {
  318. if( my $string_meth = overload::Method($$thing, '""') ) {
  319. $$thing = $$thing->$string_meth();
  320. }
  321. }
  322. };
  323. }
  324. }
  325. =item B<is_eq>
  326. $Test->is_eq($got, $expected, $name);
  327. Like Test::More's is(). Checks if $got eq $expected. This is the
  328. string version.
  329. =item B<is_num>
  330. $Test->is_num($got, $expected, $name);
  331. Like Test::More's is(). Checks if $got == $expected. This is the
  332. numeric version.
  333. =cut
  334. sub is_eq {
  335. my($self, $got, $expect, $name) = @_;
  336. local $Level = $Level + 1;
  337. if( !defined $got || !defined $expect ) {
  338. # undef only matches undef and nothing else
  339. my $test = !defined $got && !defined $expect;
  340. $self->ok($test, $name);
  341. $self->_is_diag($got, 'eq', $expect) unless $test;
  342. return $test;
  343. }
  344. return $self->cmp_ok($got, 'eq', $expect, $name);
  345. }
  346. sub is_num {
  347. my($self, $got, $expect, $name) = @_;
  348. local $Level = $Level + 1;
  349. if( !defined $got || !defined $expect ) {
  350. # undef only matches undef and nothing else
  351. my $test = !defined $got && !defined $expect;
  352. $self->ok($test, $name);
  353. $self->_is_diag($got, '==', $expect) unless $test;
  354. return $test;
  355. }
  356. return $self->cmp_ok($got, '==', $expect, $name);
  357. }
  358. sub _is_diag {
  359. my($self, $got, $type, $expect) = @_;
  360. foreach my $val (\$got, \$expect) {
  361. if( defined $$val ) {
  362. if( $type eq 'eq' ) {
  363. # quote and force string context
  364. $$val = "'$$val'"
  365. }
  366. else {
  367. # force numeric context
  368. $$val = $$val+0;
  369. }
  370. }
  371. else {
  372. $$val = 'undef';
  373. }
  374. }
  375. return $self->diag(sprintf <<DIAGNOSTIC, $got, $expect);
  376. got: %s
  377. expected: %s
  378. DIAGNOSTIC
  379. }
  380. =item B<isnt_eq>
  381. $Test->isnt_eq($got, $dont_expect, $name);
  382. Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
  383. the string version.
  384. =item B<isnt_num>
  385. $Test->is_num($got, $dont_expect, $name);
  386. Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
  387. the numeric version.
  388. =cut
  389. sub isnt_eq {
  390. my($self, $got, $dont_expect, $name) = @_;
  391. local $Level = $Level + 1;
  392. if( !defined $got || !defined $dont_expect ) {
  393. # undef only matches undef and nothing else
  394. my $test = defined $got || defined $dont_expect;
  395. $self->ok($test, $name);
  396. $self->_cmp_diag($got, 'ne', $dont_expect) unless $test;
  397. return $test;
  398. }
  399. return $self->cmp_ok($got, 'ne', $dont_expect, $name);
  400. }
  401. sub isnt_num {
  402. my($self, $got, $dont_expect, $name) = @_;
  403. local $Level = $Level + 1;
  404. if( !defined $got || !defined $dont_expect ) {
  405. # undef only matches undef and nothing else
  406. my $test = defined $got || defined $dont_expect;
  407. $self->ok($test, $name);
  408. $self->_cmp_diag($got, '!=', $dont_expect) unless $test;
  409. return $test;
  410. }
  411. return $self->cmp_ok($got, '!=', $dont_expect, $name);
  412. }
  413. =item B<like>
  414. $Test->like($this, qr/$regex/, $name);
  415. $Test->like($this, '/$regex/', $name);
  416. Like Test::More's like(). Checks if $this matches the given $regex.
  417. You'll want to avoid qr// if you want your tests to work before 5.005.
  418. =item B<unlike>
  419. $Test->unlike($this, qr/$regex/, $name);
  420. $Test->unlike($this, '/$regex/', $name);
  421. Like Test::More's unlike(). Checks if $this B<does not match> the
  422. given $regex.
  423. =cut
  424. sub like {
  425. my($self, $this, $regex, $name) = @_;
  426. local $Level = $Level + 1;
  427. $self->_regex_ok($this, $regex, '=~', $name);
  428. }
  429. sub unlike {
  430. my($self, $this, $regex, $name) = @_;
  431. local $Level = $Level + 1;
  432. $self->_regex_ok($this, $regex, '!~', $name);
  433. }
  434. =item B<maybe_regex>
  435. $Test->maybe_regex(qr/$regex/);
  436. $Test->maybe_regex('/$regex/');
  437. Convenience method for building testing functions that take regular
  438. expressions as arguments, but need to work before perl 5.005.
  439. Takes a quoted regular expression produced by qr//, or a string
  440. representing a regular expression.
  441. Returns a Perl value which may be used instead of the corresponding
  442. regular expression, or undef if it's argument is not recognised.
  443. For example, a version of like(), sans the useful diagnostic messages,
  444. could be written as:
  445. sub laconic_like {
  446. my ($self, $this, $regex, $name) = @_;
  447. my $usable_regex = $self->maybe_regex($regex);
  448. die "expecting regex, found '$regex'\n"
  449. unless $usable_regex;
  450. $self->ok($this =~ m/$usable_regex/, $name);
  451. }
  452. =cut
  453. sub maybe_regex {
  454. my ($self, $regex) = @_;
  455. my $usable_regex = undef;
  456. return $usable_regex unless defined $regex;
  457. my($re, $opts);
  458. # Check for qr/foo/
  459. if( ref $regex eq 'Regexp' ) {
  460. $usable_regex = $regex;
  461. }
  462. # Check for '/foo/' or 'm,foo,'
  463. elsif( ($re, $opts) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or
  464. (undef, $re, $opts) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx
  465. )
  466. {
  467. $usable_regex = length $opts ? "(?$opts)$re" : $re;
  468. }
  469. return $usable_regex;
  470. };
  471. sub _regex_ok {
  472. my($self, $this, $regex, $cmp, $name) = @_;
  473. local $Level = $Level + 1;
  474. my $ok = 0;
  475. my $usable_regex = $self->maybe_regex($regex);
  476. unless (defined $usable_regex) {
  477. $ok = $self->ok( 0, $name );
  478. $self->diag(" '$regex' doesn't look much like a regex to me.");
  479. return $ok;
  480. }
  481. {
  482. local $^W = 0;
  483. my $test = $this =~ /$usable_regex/ ? 1 : 0;
  484. $test = !$test if $cmp eq '!~';
  485. $ok = $self->ok( $test, $name );
  486. }
  487. unless( $ok ) {
  488. $this = defined $this ? "'$this'" : 'undef';
  489. my $match = $cmp eq '=~' ? "doesn't match" : "matches";
  490. $self->diag(sprintf <<DIAGNOSTIC, $this, $match, $regex);
  491. %s
  492. %13s '%s'
  493. DIAGNOSTIC
  494. }
  495. return $ok;
  496. }
  497. =item B<cmp_ok>
  498. $Test->cmp_ok($this, $type, $that, $name);
  499. Works just like Test::More's cmp_ok().
  500. $Test->cmp_ok($big_num, '!=', $other_big_num);
  501. =cut
  502. sub cmp_ok {
  503. my($self, $got, $type, $expect, $name) = @_;
  504. my $test;
  505. {
  506. local $^W = 0;
  507. local($@,$!); # don't interfere with $@
  508. # eval() sometimes resets $!
  509. $test = eval "\$got $type \$expect";
  510. }
  511. local $Level = $Level + 1;
  512. my $ok = $self->ok($test, $name);
  513. unless( $ok ) {
  514. if( $type =~ /^(eq|==)$/ ) {
  515. $self->_is_diag($got, $type, $expect);
  516. }
  517. else {
  518. $self->_cmp_diag($got, $type, $expect);
  519. }
  520. }
  521. return $ok;
  522. }
  523. sub _cmp_diag {
  524. my($self, $got, $type, $expect) = @_;
  525. $got = defined $got ? "'$got'" : 'undef';
  526. $expect = defined $expect ? "'$expect'" : 'undef';
  527. return $self->diag(sprintf <<DIAGNOSTIC, $got, $type, $expect);
  528. %s
  529. %s
  530. %s
  531. DIAGNOSTIC
  532. }
  533. =item B<BAILOUT>
  534. $Test->BAILOUT($reason);
  535. Indicates to the Test::Harness that things are going so badly all
  536. testing should terminate. This includes running any additional test
  537. scripts.
  538. It will exit with 255.
  539. =cut
  540. sub BAILOUT {
  541. my($self, $reason) = @_;
  542. $self->_print("Bail out! $reason");
  543. exit 255;
  544. }
  545. =item B<skip>
  546. $Test->skip;
  547. $Test->skip($why);
  548. Skips the current test, reporting $why.
  549. =cut
  550. sub skip {
  551. my($self, $why) = @_;
  552. $why ||= '';
  553. $self->_unoverload(\$why);
  554. unless( $Have_Plan ) {
  555. require Carp;
  556. Carp::croak("You tried to run tests without a plan! Gotta have a plan.");
  557. }
  558. lock($Curr_Test);
  559. $Curr_Test++;
  560. $Test_Results[$Curr_Test-1] = &share({
  561. 'ok' => 1,
  562. actual_ok => 1,
  563. name => '',
  564. type => 'skip',
  565. reason => $why,
  566. });
  567. my $out = "ok";
  568. $out .= " $Curr_Test" if $self->use_numbers;
  569. $out .= " # skip";
  570. $out .= " $why" if length $why;
  571. $out .= "\n";
  572. $Test->_print($out);
  573. return 1;
  574. }
  575. =item B<todo_skip>
  576. $Test->todo_skip;
  577. $Test->todo_skip($why);
  578. Like skip(), only it will declare the test as failing and TODO. Similar
  579. to
  580. print "not ok $tnum # TODO $why\n";
  581. =cut
  582. sub todo_skip {
  583. my($self, $why) = @_;
  584. $why ||= '';
  585. unless( $Have_Plan ) {
  586. require Carp;
  587. Carp::croak("You tried to run tests without a plan! Gotta have a plan.");
  588. }
  589. lock($Curr_Test);
  590. $Curr_Test++;
  591. $Test_Results[$Curr_Test-1] = &share({
  592. 'ok' => 1,
  593. actual_ok => 0,
  594. name => '',
  595. type => 'todo_skip',
  596. reason => $why,
  597. });
  598. my $out = "not ok";
  599. $out .= " $Curr_Test" if $self->use_numbers;
  600. $out .= " # TODO & SKIP $why\n";
  601. $Test->_print($out);
  602. return 1;
  603. }
  604. =begin _unimplemented
  605. =item B<skip_rest>
  606. $Test->skip_rest;
  607. $Test->skip_rest($reason);
  608. Like skip(), only it skips all the rest of the tests you plan to run
  609. and terminates the test.
  610. If you're running under no_plan, it skips once and terminates the
  611. test.
  612. =end _unimplemented
  613. =back
  614. =head2 Test style
  615. =over 4
  616. =item B<level>
  617. $Test->level($how_high);
  618. How far up the call stack should $Test look when reporting where the
  619. test failed.
  620. Defaults to 1.
  621. Setting $Test::Builder::Level overrides. This is typically useful
  622. localized:
  623. {
  624. local $Test::Builder::Level = 2;
  625. $Test->ok($test);
  626. }
  627. =cut
  628. sub level {
  629. my($self, $level) = @_;
  630. if( defined $level ) {
  631. $Level = $level;
  632. }
  633. return $Level;
  634. }
  635. =item B<use_numbers>
  636. $Test->use_numbers($on_or_off);
  637. Whether or not the test should output numbers. That is, this if true:
  638. ok 1
  639. ok 2
  640. ok 3
  641. or this if false
  642. ok
  643. ok
  644. ok
  645. Most useful when you can't depend on the test output order, such as
  646. when threads or forking is involved.
  647. Test::Harness will accept either, but avoid mixing the two styles.
  648. Defaults to on.
  649. =cut
  650. sub use_numbers {
  651. my($self, $use_nums) = @_;
  652. if( defined $use_nums ) {
  653. $Use_Nums = $use_nums;
  654. }
  655. return $Use_Nums;
  656. }
  657. =item B<no_header>
  658. $Test->no_header($no_header);
  659. If set to true, no "1..N" header will be printed.
  660. =item B<no_ending>
  661. $Test->no_ending($no_ending);
  662. Normally, Test::Builder does some extra diagnostics when the test
  663. ends. It also changes the exit code as described below.
  664. If this is true, none of that will be done.
  665. =cut
  666. sub no_header {
  667. my($self, $no_header) = @_;
  668. if( defined $no_header ) {
  669. $No_Header = $no_header;
  670. }
  671. return $No_Header;
  672. }
  673. sub no_ending {
  674. my($self, $no_ending) = @_;
  675. if( defined $no_ending ) {
  676. $No_Ending = $no_ending;
  677. }
  678. return $No_Ending;
  679. }
  680. =back
  681. =head2 Output
  682. Controlling where the test output goes.
  683. It's ok for your test to change where STDOUT and STDERR point to,
  684. Test::Builder's default output settings will not be affected.
  685. =over 4
  686. =item B<diag>
  687. $Test->diag(@msgs);
  688. Prints out the given @msgs. Like C<print>, arguments are simply
  689. appended together.
  690. Normally, it uses the failure_output() handle, but if this is for a
  691. TODO test, the todo_output() handle is used.
  692. Output will be indented and marked with a # so as not to interfere
  693. with test output. A newline will be put on the end if there isn't one
  694. already.
  695. We encourage using this rather than calling print directly.
  696. Returns false. Why? Because diag() is often used in conjunction with
  697. a failing test (C<ok() || diag()>) it "passes through" the failure.
  698. return ok(...) || diag(...);
  699. =for blame transfer
  700. Mark Fowler <mark@twoshortplanks.com>
  701. =cut
  702. sub diag {
  703. my($self, @msgs) = @_;
  704. return unless @msgs;
  705. # Prevent printing headers when compiling (i.e. -c)
  706. return if $^C;
  707. # Smash args together like print does.
  708. # Convert undef to 'undef' so its readable.
  709. my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs;
  710. # Escape each line with a #.
  711. $msg =~ s/^/# /gm;
  712. # Stick a newline on the end if it needs it.
  713. $msg .= "\n" unless $msg =~ /\n\Z/;
  714. local $Level = $Level + 1;
  715. $self->_print_diag($msg);
  716. return 0;
  717. }
  718. =begin _private
  719. =item B<_print>
  720. $Test->_print(@msgs);
  721. Prints to the output() filehandle.
  722. =end _private
  723. =cut
  724. sub _print {
  725. my($self, @msgs) = @_;
  726. # Prevent printing headers when only compiling. Mostly for when
  727. # tests are deparsed with B::Deparse
  728. return if $^C;
  729. my $msg = join '', @msgs;
  730. local($\, $", $,) = (undef, ' ', '');
  731. my $fh = $self->output;
  732. # Escape each line after the first with a # so we don't
  733. # confuse Test::Harness.
  734. $msg =~ s/\n(.)/\n# $1/sg;
  735. # Stick a newline on the end if it needs it.
  736. $msg .= "\n" unless $msg =~ /\n\Z/;
  737. print $fh $msg;
  738. }
  739. =item B<_print_diag>
  740. $Test->_print_diag(@msg);
  741. Like _print, but prints to the current diagnostic filehandle.
  742. =cut
  743. sub _print_diag {
  744. my $self = shift;
  745. local($\, $", $,) = (undef, ' ', '');
  746. my $fh = $self->todo ? $self->todo_output : $self->failure_output;
  747. print $fh @_;
  748. }
  749. =item B<output>
  750. $Test->output($fh);
  751. $Test->output($file);
  752. Where normal "ok/not ok" test output should go.
  753. Defaults to STDOUT.
  754. =item B<failure_output>
  755. $Test->failure_output($fh);
  756. $Test->failure_output($file);
  757. Where diagnostic output on test failures and diag() should go.
  758. Defaults to STDERR.
  759. =item B<todo_output>
  760. $Test->todo_output($fh);
  761. $Test->todo_output($file);
  762. Where diagnostics about todo test failures and diag() should go.
  763. Defaults to STDOUT.
  764. =cut
  765. my($Out_FH, $Fail_FH, $Todo_FH);
  766. sub output {
  767. my($self, $fh) = @_;
  768. if( defined $fh ) {
  769. $Out_FH = _new_fh($fh);
  770. }
  771. return $Out_FH;
  772. }
  773. sub failure_output {
  774. my($self, $fh) = @_;
  775. if( defined $fh ) {
  776. $Fail_FH = _new_fh($fh);
  777. }
  778. return $Fail_FH;
  779. }
  780. sub todo_output {
  781. my($self, $fh) = @_;
  782. if( defined $fh ) {
  783. $Todo_FH = _new_fh($fh);
  784. }
  785. return $Todo_FH;
  786. }
  787. sub _new_fh {
  788. my($file_or_fh) = shift;
  789. my $fh;
  790. if( _is_fh($file_or_fh) ) {
  791. $fh = $file_or_fh;
  792. }
  793. else {
  794. $fh = do { local *FH };
  795. open $fh, ">$file_or_fh" or
  796. die "Can't open test output log $file_or_fh: $!";
  797. }
  798. return $fh;
  799. }
  800. sub _is_fh {
  801. my $maybe_fh = shift;
  802. return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob
  803. return UNIVERSAL::isa($maybe_fh, 'GLOB') ||
  804. UNIVERSAL::isa($maybe_fh, 'IO::Handle') ||
  805. # 5.5.4's tied() and can() doesn't like getting undef
  806. UNIVERSAL::can((tied($maybe_fh) || ''), 'TIEHANDLE');
  807. }
  808. sub _autoflush {
  809. my($fh) = shift;
  810. my $old_fh = select $fh;
  811. $| = 1;
  812. select $old_fh;
  813. }
  814. my $Opened_Testhandles = 0;
  815. sub _dup_stdhandles {
  816. my $self = shift;
  817. $self->_open_testhandles unless $Opened_Testhandles;
  818. # Set everything to unbuffered else plain prints to STDOUT will
  819. # come out in the wrong order from our own prints.
  820. _autoflush(\*TESTOUT);
  821. _autoflush(\*STDOUT);
  822. _autoflush(\*TESTERR);
  823. _autoflush(\*STDERR);
  824. $Test->output(\*TESTOUT);
  825. $Test->failure_output(\*TESTERR);
  826. $Test->todo_output(\*TESTOUT);
  827. }
  828. sub _open_testhandles {
  829. # We dup STDOUT and STDERR so people can change them in their
  830. # test suites while still getting normal test output.
  831. open(TESTOUT, ">&STDOUT") or die "Can't dup STDOUT: $!";
  832. open(TESTERR, ">&STDERR") or die "Can't dup STDERR: $!";
  833. $Opened_Testhandles = 1;
  834. }
  835. =back
  836. =head2 Test Status and Info
  837. =over 4
  838. =item B<current_test>
  839. my $curr_test = $Test->current_test;
  840. $Test->current_test($num);
  841. Gets/sets the current test number we're on. You usually shouldn't
  842. have to set this.
  843. If set forward, the details of the missing tests are filled in as 'unknown'.
  844. if set backward, the details of the intervening tests are deleted. You
  845. can erase history if you really want to.
  846. =cut
  847. sub current_test {
  848. my($self, $num) = @_;
  849. lock($Curr_Test);
  850. if( defined $num ) {
  851. unless( $Have_Plan ) {
  852. require Carp;
  853. Carp::croak("Can't change the current test number without a plan!");
  854. }
  855. $Curr_Test = $num;
  856. # If the test counter is being pushed forward fill in the details.
  857. if( $num > @Test_Results ) {
  858. my $start = @Test_Results ? $#Test_Results + 1 : 0;
  859. for ($start..$num-1) {
  860. $Test_Results[$_] = &share({
  861. 'ok' => 1,
  862. actual_ok => undef,
  863. reason => 'incrementing test number',
  864. type => 'unknown',
  865. name => undef
  866. });
  867. }
  868. }
  869. # If backward, wipe history. Its their funeral.
  870. elsif( $num < @Test_Results ) {
  871. $#Test_Results = $num - 1;
  872. }
  873. }
  874. return $Curr_Test;
  875. }
  876. =item B<summary>
  877. my @tests = $Test->summary;
  878. A simple summary of the tests so far. True for pass, false for fail.
  879. This is a logical pass/fail, so todos are passes.
  880. Of course, test #1 is $tests[0], etc...
  881. =cut
  882. sub summary {
  883. my($self) = shift;
  884. return map { $_->{'ok'} } @Test_Results;
  885. }
  886. =item B<details>
  887. my @tests = $Test->details;
  888. Like summary(), but with a lot more detail.
  889. $tests[$test_num - 1] =
  890. { 'ok' => is the test considered a pass?
  891. actual_ok => did it literally say 'ok'?
  892. name => name of the test (if any)
  893. type => type of test (if any, see below).
  894. reason => reason for the above (if any)
  895. };
  896. 'ok' is true if Test::Harness will consider the test to be a pass.
  897. 'actual_ok' is a reflection of whether or not the test literally
  898. printed 'ok' or 'not ok'. This is for examining the result of 'todo'
  899. tests.
  900. 'name' is the name of the test.
  901. 'type' indicates if it was a special test. Normal tests have a type
  902. of ''. Type can be one of the following:
  903. skip see skip()
  904. todo see todo()
  905. todo_skip see todo_skip()
  906. unknown see below
  907. Sometimes the Test::Builder test counter is incremented without it
  908. printing any test output, for example, when current_test() is changed.
  909. In these cases, Test::Builder doesn't know the result of the test, so
  910. it's type is 'unkown'. These details for these tests are filled in.
  911. They are considered ok, but the name and actual_ok is left undef.
  912. For example "not ok 23 - hole count # TODO insufficient donuts" would
  913. result in this structure:
  914. $tests[22] = # 23 - 1, since arrays start from 0.
  915. { ok => 1, # logically, the test passed since it's todo
  916. actual_ok => 0, # in absolute terms, it failed
  917. name => 'hole count',
  918. type => 'todo',
  919. reason => 'insufficient donuts'
  920. };
  921. =cut
  922. sub details {
  923. return @Test_Results;
  924. }
  925. =item B<todo>
  926. my $todo_reason = $Test->todo;
  927. my $todo_reason = $Test->todo($pack);
  928. todo() looks for a $TODO variable in your tests. If set, all tests
  929. will be considered 'todo' (see Test::More and Test::Harness for
  930. details). Returns the reason (ie. the value of $TODO) if running as
  931. todo tests, false otherwise.
  932. todo() is pretty part about finding the right package to look for
  933. $TODO in. It uses the exported_to() package to find it. If that's
  934. not set, it's pretty good at guessing the right package to look at.
  935. Sometimes there is some confusion about where todo() should be looking
  936. for the $TODO variable. If you want to be sure, tell it explicitly
  937. what $pack to use.
  938. =cut
  939. sub todo {
  940. my($self, $pack) = @_;
  941. $pack = $pack || $self->exported_to || $self->caller(1);
  942. no strict 'refs';
  943. return defined ${$pack.'::TODO'} ? ${$pack.'::TODO'}
  944. : 0;
  945. }
  946. =item B<caller>
  947. my $package = $Test->caller;
  948. my($pack, $file, $line) = $Test->caller;
  949. my($pack, $file, $line) = $Test->caller($height);
  950. Like the normal caller(), except it reports according to your level().
  951. =cut
  952. sub caller {
  953. my($self, $height) = @_;
  954. $height ||= 0;
  955. my @caller = CORE::caller($self->level + $height + 1);
  956. return wantarray ? @caller : $caller[0];
  957. }
  958. =back
  959. =cut
  960. =begin _private
  961. =over 4
  962. =item B<_sanity_check>
  963. _sanity_check();
  964. Runs a bunch of end of test sanity checks to make sure reality came
  965. through ok. If anything is wrong it will die with a fairly friendly
  966. error message.
  967. =cut
  968. #'#
  969. sub _sanity_check {
  970. _whoa($Curr_Test < 0, 'Says here you ran a negative number of tests!');
  971. _whoa(!$Have_Plan and $Curr_Test,
  972. 'Somehow your tests ran without a plan!');
  973. _whoa($Curr_Test != @Test_Results,
  974. 'Somehow you got a different number of results than tests ran!');
  975. }
  976. =item B<_whoa>
  977. _whoa($check, $description);
  978. A sanity check, similar to assert(). If the $check is true, something
  979. has gone horribly wrong. It will die with the given $description and
  980. a note to contact the author.
  981. =cut
  982. sub _whoa {
  983. my($check, $desc) = @_;
  984. if( $check ) {
  985. die <<WHOA;
  986. WHOA! $desc
  987. This should never happen! Please contact the author immediately!
  988. WHOA
  989. }
  990. }
  991. =item B<_my_exit>
  992. _my_exit($exit_num);
  993. Perl seems to have some trouble with exiting inside an END block. 5.005_03
  994. and 5.6.1 both seem to do odd things. Instead, this function edits $?
  995. directly. It should ONLY be called from inside an END block. It
  996. doesn't actually exit, that's your job.
  997. =cut
  998. sub _my_exit {
  999. $? = $_[0];
  1000. return 1;
  1001. }
  1002. =back
  1003. =end _private
  1004. =cut
  1005. $SIG{__DIE__} = sub {
  1006. # We don't want to muck with death in an eval, but $^S isn't
  1007. # totally reliable. 5.005_03 and 5.6.1 both do the wrong thing
  1008. # with it. Instead, we use caller. This also means it runs under
  1009. # 5.004!
  1010. my $in_eval = 0;
  1011. for( my $stack = 1; my $sub = (CORE::caller($stack))[3]; $stack++ ) {
  1012. $in_eval = 1 if $sub =~ /^\(eval\)/;
  1013. }
  1014. $Test_Died = 1 unless $in_eval;
  1015. };
  1016. sub _ending {
  1017. my $self = shift;
  1018. _sanity_check();
  1019. # Don't bother with an ending if this is a forked copy. Only the parent
  1020. # should do the ending.
  1021. do{ _my_exit($?) && return } if $Original_Pid != $$;
  1022. # Bailout if plan() was never called. This is so
  1023. # "require Test::Simple" doesn't puke.
  1024. do{ _my_exit(0) && return } if !$Have_Plan && !$Test_Died;
  1025. # Figure out if we passed or failed and print helpful messages.
  1026. if( @Test_Results ) {
  1027. # The plan? We have no plan.
  1028. if( $No_Plan ) {
  1029. $self->_print("1..$Curr_Test\n") unless $self->no_header;
  1030. $Expected_Tests = $Curr_Test;
  1031. }
  1032. # Auto-extended arrays and elements which aren't explicitly
  1033. # filled in with a shared reference will puke under 5.8.0
  1034. # ithreads. So we have to fill them in by hand. :(
  1035. my $empty_result = &share({});
  1036. for my $idx ( 0..$Expected_Tests-1 ) {
  1037. $Test_Results[$idx] = $empty_result
  1038. unless defined $Test_Results[$idx];
  1039. }
  1040. my $num_failed = grep !$_->{'ok'}, @Test_Results[0..$Expected_Tests-1];
  1041. $num_failed += abs($Expected_Tests - @Test_Results);
  1042. if( $Curr_Test < $Expected_Tests ) {
  1043. my $s = $Expected_Tests == 1 ? '' : 's';
  1044. $self->diag(<<"FAIL");
  1045. Looks like you planned $Expected_Tests test$s but only ran $Curr_Test.
  1046. FAIL
  1047. }
  1048. elsif( $Curr_Test > $Expected_Tests ) {
  1049. my $num_extra = $Curr_Test - $Expected_Tests;
  1050. my $s = $Expected_Tests == 1 ? '' : 's';
  1051. $self->diag(<<"FAIL");
  1052. Looks like you planned $Expected_Tests test$s but ran $num_extra extra.
  1053. FAIL
  1054. }
  1055. elsif ( $num_failed ) {
  1056. my $s = $num_failed == 1 ? '' : 's';
  1057. $self->diag(<<"FAIL");
  1058. Looks like you failed $num_failed test$s of $Expected_Tests.
  1059. FAIL
  1060. }
  1061. if( $Test_Died ) {
  1062. $self->diag(<<"FAIL");
  1063. Looks like your test died just after $Curr_Test.
  1064. FAIL
  1065. _my_exit( 255 ) && return;
  1066. }
  1067. _my_exit( $num_failed <= 254 ? $num_failed : 254 ) && return;
  1068. }
  1069. elsif ( $Skip_All ) {
  1070. _my_exit( 0 ) && return;
  1071. }
  1072. elsif ( $Test_Died ) {
  1073. $self->diag(<<'FAIL');
  1074. Looks like your test died before it could output anything.
  1075. FAIL
  1076. _my_exit( 255 ) && return;
  1077. }
  1078. else {
  1079. $self->diag("No tests run!\n");
  1080. _my_exit( 255 ) && return;
  1081. }
  1082. }
  1083. END {
  1084. $Test->_ending if defined $Test and !$Test->no_ending;
  1085. }
  1086. =head1 EXIT CODES
  1087. If all your tests passed, Test::Builder will exit with zero (which is
  1088. normal). If anything failed it will exit with how many failed. If
  1089. you run less (or more) tests than you planned, the missing (or extras)
  1090. will be considered failures. If no tests were ever run Test::Builder
  1091. will throw a warning and exit with 255. If the test died, even after
  1092. having successfully completed all its tests, it will still be
  1093. considered a failure and will exit with 255.
  1094. So the exit codes are...
  1095. 0 all tests successful
  1096. 255 test died
  1097. any other number how many failed (including missing or extras)
  1098. If you fail more than 254 tests, it will be reported as 254.
  1099. =head1 THREADS
  1100. In perl 5.8.0 and later, Test::Builder is thread-safe. The test
  1101. number is shared amongst all threads. This means if one thread sets
  1102. the test number using current_test() they will all be effected.
  1103. Test::Builder is only thread-aware if threads.pm is loaded I<before>
  1104. Test::Builder.
  1105. =head1 EXAMPLES
  1106. CPAN can provide the best examples. Test::Simple, Test::More,
  1107. Test::Exception and Test::Differences all use Test::Builder.
  1108. =head1 SEE ALSO
  1109. Test::Simple, Test::More, Test::Harness
  1110. =head1 AUTHORS
  1111. Original code by chromatic, maintained by Michael G Schwern
  1112. E<lt>schwern@pobox.comE<gt>
  1113. =head1 COPYRIGHT
  1114. Copyright 2002, 2004 by chromatic E<lt>chromatic@wgz.orgE<gt> and
  1115. Michael G Schwern E<lt>schwern@pobox.comE<gt>.
  1116. This program is free software; you can redistribute it and/or
  1117. modify it under the same terms as Perl itself.
  1118. See F<http://www.perl.com/perl/misc/Artistic.html>
  1119. =cut
  1120. 1;