Debian Codegraph @

lib/Test/Inter.pod

raw
1 # Copyright (c) 2010-2024 Sullivan Beck. All rights reserved.
2 # This program is free software; you can redistribute it and/or modify it
3 # under the same terms as Perl itself.
4
5 =pod
6
7 =head1 NAME
8
9 Test::Inter - framework for more readable interactive test scripts
10
11 =head1 DESCRIPTION
12
13 This is another framework for writing test scripts. Much of the syntax
14 is loosely inspired by L<Test::More>, and Test::Inter has most of it's
15 functionality, but it is not a drop-in replacement.
16
17 L<Test::More> (and other existing test frameworks) suffer from two
18 weaknesses, both of which have prevented me from ever using them:
19
20 None offer the ability to access specific tests in
21 a reasonably interactive fashion, primarily for
22 debugging purposes
23
24 None offer the ability to write the tests in
25 whatever format would make the tests the most
26 readable
27
28 The way I write and use test scripts, existing Test::* modules are not
29 nearly as useful as they could be.
30
31 Test scripts written using L<Test::More> work fine when running as part
32 of the test suite, but debugging an individual test requires extra
33 steps, and the tests themselves are not as readable as they should be.
34
35 =head1 INTERACTIVE EXECUTION
36
37 One requirement that I have of a test framework is the ability to
38 interact with it.
39
40 I do most of my debugging using test scripts. When I find a bug, I
41 write a test case for it (typically by adding it to an existing test
42 script) and then debug it using the test script. Then I leave the
43 test there to ensure that the bug won't come back (hopefully).
44
45 Since I use test scripts in a very interactive way (often in the
46 debugger), I want to be able to do the following trivially:
47
48 =over 4
49
50 =item B<Easy access to a specific test or tests>
51
52 I'd like to be able to run only a single test, or a subset
53 of tests.
54
55 =item B<Easily set breakpoints in the debugger>
56
57 Setting a breakpoint in the debugger to run up to the start
58 of the Nth test is one of the most common tasks I want to do
59 when I'm debugging a failed test.
60
61 =back
62
63 To illustrate the first point, in L<Test::More>, a series of tests might be
64 specified in a test script as shown in the following example (line numbers
65 added for convenience):
66
67 ...
68
69 100: # test 1
70 101: $result = func("apples","bushels");
71 102: is($result, "enough");
72 103:
73 104: # test 2
74 105: $result = func("grapefruit","tons");
75 106: is($result, "enough");
76 107:
77 108: # test 3
78 109: $result = func("oranges","boatloads");
79 110: is($result, "insufficient");
80 111:
81 112: # tests 4-6
82 113: foreach my $arg (qw(pears plums pineapple)) {
83 114: $result = func($arg,"boxes");
84 115: is($result, "enough");
85 116: }
86
87 ...
88
89 Say you ran the test suite, and test 3 failed. To debug it you have
90 to open up the test script, find the 3rd test, and set the appropriate
91 breakpoint. In this case, you'll want to break at line 109.
92
93 None of these steps are impossible of course, but it will take some time to
94 get it right. It becomes harder when there are lots of tests (imagine that you
95 want to test the 117th test instead of the 3rd test) or when tests are wrapped
96 up in loops, embedded in subroutines, or other similar situations.
97
98 As an example, what if it's the 5th test that fails in the example
99 above. Now the break point will be a conditional one, so you have to
100 figure out not only the line, but the condition the appropriate state
101 during that test. In this case, you need to stop at line 114 when
102 $arg is 'plums'.
103
104 Wouldn't it be far better to set a break point in func when the Nth test is
105 reached? With Test::Inter, you can.
106
107 So for the above script, the debugger commands that you would use to
108 debug the 3rd test are:
109
110 Test::More : b 109
111 Test::Inter: b func ($::TI_NUM==3)
112
113 and the 5th test are:
114
115 Test::More : b 114 ($arg eq 'plums')
116 Test::Inter: b func ($::TI_NUM==5)
117
118 It would also be nice to be able to skip the first two tests... perhaps
119 they take a long time to run, and I want to get right to work on test 3.
120 You can do this easily too by setting the $::TI_START variable.
121
122 There are some other variables that can be used to specify which test or
123 tests to run described in the L</"TEST::INTER VARIABLES"> section below.
124
125 The other thing I want to do when I run the test scripts interactively
126 is to see more information which will assist in debugging a failed
127 test.
128
129 This can be controlled with variables such as TI_QUIET, TI_MODE, and TI_WIDTH
130 described below in the L</"TEST::INTER VARIABLES"> section.
131
132 =head1 READABLE TESTS
133
134 The other feature that I wanted in a test suite is the ability to
135 define the tests in a format that is natural and readable FOR THE
136 TESTS. In almost every case, it is best to think of a test script
137 as consisting of two separate parts: a script part, and a test part.
138
139 The script part of a test script is the least important part! It's
140 usually fairly trivial, rarely needs to be changed, and is not the
141 focus of the test script.
142
143 The tests part of the script IS the important part, and these should
144 be expressed in a form that is natural to them, easy to maintain, easy
145 to read, and easy to modify, and none of these should involve
146 modifying the script portion of the test script in general. Because
147 the content of the tests is the important part of the script, the
148 emphasis should be in making them more readable, even at the expense
149 of the script portion. As a general rule, if the script portion of
150 the test script obscures the tests in any way, it's not written
151 correctly!
152
153 The solution to this is well understood, and is common to many other
154 systems where you are mixing two "languages". The task of correctly
155 specifying both the tests and the test script is virtually identical
156 to the task of creating a PHP script which consists of a mixture of
157 PHP and HTML, or the task of creating a template file using some
158 templating system where the file consists of a mixture of text to be
159 displayed and templating commands. It is well understood in each of
160 these cases that the more the two "languages" are interwoven, the less
161 readable both are, and the harder it is to maintain. The more you are
162 able to separate the two, the easier both are to read and maintain.
163
164 As often as possible, I want the tests to be written in some sort of
165 text format which can be easily viewed and modified (usually as a
166 simple table) with no perl commands interspersed. I want to the
167 freedom to define the tests in one section (a long string, the DATA
168 section, or even in a separate file) which is easily readable. This
169 may introduce the necessity of parsing it, but it makes it
170 significantly easier to maintain the tests.
171
172 This flexibility makes it much easier to read the tests (as opposed to
173 the script) which is the fundamental content of a test script.
174
175 Looking again at the example test script, you can see that there
176 is far too much perl interspersed with the tests.
177
178 It's difficult to read the tests individually in this script because
179 there is too much perl code among them, and virtually impossible to
180 look at them as a whole.
181
182 It is true that looking at this particular example, it is very
183 simple... but the script ISN'T the content you're interested in (and
184 bear in mind that many test scripts are nowhere near this simple). The
185 REAL content of this script are the tests, which consist of the
186 function arguments and the expected result. Although it's not
187 impossible to see each of these in the script above, it's not in a
188 format that is conducive to studying the tests, and especially not for
189 examining the list of tests as a whole.
190
191 Now, look at an alternate way of specifying the tests using this module:
192
193 $tests = "
194
195 apples bushels => enough
196
197 grapefruit tons => enough
198
199 oranges boatloads => insufficient
200
201 pears boxes => enough
202
203 plums boxes => enough
204
205 pineapple boxes => enough
206
207 ";
208
209 $o->tests(tests => $tests,
210 func => \&func);
211
212 Here, it's easy to see the list of tests, and adding additional
213 tests is a breeze.
214
215 =head1 CREATING A TEST
216
217 This module supports a number of methods for defining tests, so you
218 can use whichever one is most convenient (including methods that are
219 identical to L<Test::More> if that really is the best method).
220
221 Every test may have several pieces of information:
222
223 =over 4
224
225 =item B<A name>
226
227 Every test is automatically assigned a number, but it may be useful to
228 specify a name of a test (which is actually a short description of the
229 test). Whenever a test result is reported, the name will be given (if
230 one was specified).
231
232 The name may not have a '#' in it.
233
234 The name is completely optional, but can serve to make the results
235 more readable.
236
237 =item B<An expected result>
238
239 In order to test something, you need to know what result was
240 expected (or in some cases, what result was NOT expected).
241
242 =item B<A function and arguments OR a result>
243
244 You also need to know the results that you're comparing to
245 the expected results.
246
247 This can be obtained by simply working with a set of results,
248 or a function name and a set of arguments to pass to it.
249
250 =item B<Conditions>
251
252 It is useful to be able to specify state information at the start
253 of the test suite (for example, to see if certain features are
254 available), and some tests may only run if those conditions are
255 met.
256
257 If no conditions are set for a test, it will always run.
258
259 =item B<Todo tests>
260
261 Some tests may be marked as 'todo' tests. These are test which are
262 allowed to fail (meaning that they have been put in place for an
263 as-yet unimplemented feature). Since it is expected that the test
264 will fail, the test suite will still pass, even if these tests
265 fail.
266
267 The tests will still run and if they pass, a message is issued
268 saying that the feature is now implemented, and the tests should
269 be graduated to non-todo state.
270
271 =back
272
273 =head1 BASE METHODS
274
275 =over 4
276
277 =item B<new>
278
279 $o = new Test::Inter [$name] [%options];
280
281 This creates a new test framework. There are several options which may
282 be used to specify which tests are run, how they are run, and
283 what output is given.
284
285 The entire test script can be named by passing in $name.
286
287 All options can be set in four different ways.
288
289 First, you can pass in a hash of B<OPT => VAL> pairs in the new method.
290 So, to set the B<start> option, the C<%options>) hash would contain:
291
292 start => VALUE
293
294 Second, you can set an environment variable. This will override any
295 value passed in the first way. The environment variable is named
296 TI_XXX where XXX is the fully capitalized option. So:
297
298 $ENV{TI_START} = VALUE
299
300 The third method, which overrides the previous two, is to set a
301 global variable. It is also named TI_XXX in the main namespace, so
302 to set it this way, set:
303
304 $::TI_START = VALUE
305
306 The final method is to call one of the methods below and these override
307 all other methods.
308
309 Each of the allowed options are described below in the following base
310 methods:
311
312 start
313 end
314 testnum
315 plan
316 abort
317 quiet
318 mode
319 skip_all
320 width
321 use_lib
322
323 =item B<version>
324
325 $o->version();
326
327 Returns the version of the module.
328
329 =item B<encoding>
330
331 $o->encoding($encoding);
332
333 C<$encoding> is any value that can be passed as an encoding to perl's
334 Encode::decode function.
335
336 Use this if your test strings contain characters in other encodings.
337
338 =item B<start>
339
340 $o = new Test::Inter 'start' => $N;
341 $o->start($N)
342
343 To define which test you want to start with, set the B<start> option
344 as described in the B<new> method above.
345
346 When the start test is defined, most tests numbered less than N are
347 completely ignored. If the tests are being run quietly (see the B<quiet>
348 method below), nothing is printed out for these tests. Otherwise, a
349 skip message is printed out.
350
351 One class of tests IS still executed. Tests run using the B<require_ok>
352 or B<use_ok methods> (to test the loading of modules) are still run.
353
354 If no value (or a value of 0) is used, tests run from the first test.
355
356 =item B<end>
357
358 $o = new Test::Inter 'end' => $M;
359 $o->end($M);
360
361 To define which test you want to end with, set the B<end> option
362 as described in the B<new> method above.
363
364 When the end test is defined, all tests numbered more than M are
365 completely ignored. If the tests are being run quietly (see the quiet
366 method below), nothing is printed out for these tests. Otherwise, a
367 skip message is printed out.
368
369 If no value is given, it defaults to 0 (which means that all remaining
370 tests are run).
371
372 =item B<testnum>
373
374 $o = new Test::Inter 'testnum' => $N;
375 $o->testnum($N);
376
377 To run only a single test, set the B<testnum> option as described in
378 the B<new> method above.
379
380 It is equivalent to setting both the start and end tests to $N.
381
382 =item B<plan>
383
384 =item B<done_testing>
385
386 $o = new Test::Inter 'plan' => $N;
387 $o->plan($n);
388
389 $o->done_testing();
390 $o->done_testing($n);
391
392 The TAP API (the 'language' used to run a sequence of tests and see
393 which ones failed and which ones passed) requires a statement of the
394 number of tests that are expected to run.
395
396 This statement can appear at the start of the test suite, or at
397 the end.
398
399 If you know in advance how many tests should run in the test script,
400 you can set the B<plan> option as described in the B<new> method
401 above to the number of tests.
402
403 If you know how many tests should run at the end of the test script,
404 you can pass in a non-zero integer to the done_testing method.
405
406 Frequently, you don't really care how many tests are in the script
407 (especially if new tests are added on a regular basis). In this case,
408 you still need to include a statement that says that the number of
409 tests expected is however many were run. To do this, call the
410 done_testing method with no argument.
411
412 NOTE: if the plan method is used, it MUST be used before any tests are
413 run (including those that test the loading of modules). If the
414 done_testing method is used, it MUST be called after all tests are
415 run. You must specify a plan or use a done_testing statement, but you
416 cannot do both.
417
418 It is NOT strictly required to set a plan if the script is only run
419 interactively, so if for some reason this module is used for test
420 scripts which are not part of a standard perl test suite, the plan
421 and done_testing statements are optional. As a matter of fact, the
422 script will run just fine without them... but a perl installer will
423 report a failure in the test suite.
424
425 =item B<abort>
426
427 $o = new Test::Inter 'abort' => 0/1/2;
428 $o->abort(0/1/2);
429
430 To define how you want a failure to be treated, set the B<abort> option
431 as described in the B<new> method above. The B<abort> option can take
432 a value of 0, 1, or 2.
433
434 If this is set to 1, the test script will run unmodified until
435 a test fails. At that point, all remaining tests will be skipped.
436 If it is set to 2, the test script will run until a test fails
437 at which point it will exit with an error code of 1. With a value
438 of 0, failed tests will be reported, but the script will continue.
439
440 In both cases, todo tests will NOT trigger the abort behavior.
441
442 =item B<quiet>
443
444 $o = new Test::Inter 'quiet' => 0/1/2;
445 $o->quiet(0/1/2);
446
447 To define how you want failures to be reported, set the B<quiet> option
448 as described in the B<new> method above. The B<quiet> option can take
449 a value of 0, 1, or 2.
450
451 If this is set to 0 (the default), all information will be printed
452 out. If it is set to 1, some optional information will not be printed.
453 If it is set to 2, all optional information will not be printed.
454
455 =item B<mode>
456
457 $o = new Test::Inter 'mode' => MODE;
458 $o->mode(MODE);
459
460 Test::Inter scripts can be run in either an interactive mode, or as
461 part of a test suite with different behaviors. To select the mode,
462 set the B<mode> option as described in the B<new> method above. The
463 B<mode> option can take a value of 'inter' or 'test'.
464
465 When run in test mode, it prints out the results using the
466 TAP grammar (i.e. 'ok 1', 'not ok 3', etc.).
467
468 When run in interactive mode, it prints out results in a more
469 human readable format.
470
471 =item B<width>
472
473 $o = new Test::Inter 'width' => WIDTH;
474 $o->width(WIDTH);
475
476 The width option can be set as described in the B<new> method above.
477
478 WIDTH is the width of the terminal (for printing out failed test
479 information). It defaults to 80, but it can be set to any width (and
480 lines longer then this are truncated). If WIDTH is set to 0, no
481 truncation is done.
482
483 =item B<use_lib>
484
485 $o = new Test::Inter 'use_lib' => VALUE;
486 $o->use_lib(VALUE);
487 $o->use_lib();
488
489 By default, the library included in the module distribution will be added
490 to the search path for modules, so a 'use MODULE' line should find the
491 version stored in this module distribution.
492
493 If VALUE is set to 'off', the search path will not be modified automatically.
494
495 You may add the library path at a later time by calling:
496
497 $o->use_lib('on');
498 $o->use_lib();
499
500 Note: both calls must be used. The first sets the option, the second actually
501 modifies the search path.
502
503 =item B<skip_all>
504
505 $o = new Test::Inter 'skip_all' => REASON;
506 $o->skip_all(REASON);
507
508 The skip_all option can be set as described in the B<new> method above.
509
510 If this is set, the entire test script will be skipped for the reason
511 given. This must be done before any test is run, and before any plan
512 number is set.
513
514 The skip_all can also be called at any point during the script (i.e.
515 after tests have been run). In this case, all remaining scripts will
516 be skipped.
517
518 $o->skip_all(REASON,FEATURE,FEATURE,...);
519 $o->skip_all('',FEATURE,FEATURE,...);
520
521 This will skip all tests (or all remaining tests) unless all <FEATURE>s
522 are available. B<REASON> can be entered as an empty string and the
523 reason the tests are skipped will be a message about the missing
524 feature.
525
526 =item B<feature>
527
528 $o->feature($feature,$val);
529
530 This defines a feature. If C<$val> is non-zero, the feature is available.
531 Otherwise it is not.
532
533 =item B<diag>
534
535 =item B<note>
536
537 $o->diag($message);
538 $o->note($message);
539
540 Both of these print an optional message. Messages printed with the
541 C<note> method are always optional and will be omitted if the B<quiet>
542 option is set to 1 or 2. Messages printed with the C<diag> method are
543 optional and will not be printed if the B<quiet> option is set to 2,
544 but they will be printed if the B<quiet> method is set to 1.
545
546 =item B<testdir>
547
548 $o->testdir();
549 $o->testdir('mod');
550 $o->testdir('lib');
551
552 Occasionally, it may be necessary to know the directory where Test::Inter
553 gets some of it's information. By default, the directory containing the
554 tests will be returned, but if the optional argument 'mod' is included,
555 it will return the path to the module distribution (which should include
556 both a lib and t subdirerctory). If the argument 'lib' is included, it
557 will return the directory where the libraries are stored.
558
559 =back
560
561 =head1 METHODS FOR LOADING MODULES
562
563 Test scripts can load other modules (using either the perl C<use> or
564 C<require> commands). There are three different modes for doing this
565 which determine how this is done.
566
567 =over 4
568
569 =item B<required>
570
571 By default, this is used to test for a module that is required for
572 all tests in the test script.
573
574 Loading the module is treated as an actual test in the test suite. The
575 test is to determine whether the module is available and can be
576 loaded. If it can be loaded, it is, and it is reported as a successful
577 test. If it cannot be loaded, it is reported as a failed test.
578
579 In the result of a failed test, all remaining tests will be skipped
580 automatically (except for other tests which load modules).
581
582 =item B<feature>
583
584 In feature mode, loading the module is not treated as a test (i.e. it
585 will not print out an 'ok' or 'not ok' line. Instead, it will set a
586 feature (named the same as the module) which can be used to determine
587 whether other tests should run or not.
588
589 =item B<forbid>
590
591 In a few very rare cases, we may want to test for a module but expect
592 that it not be present. This is the exact opposite of the B<required>
593 mode.
594
595 Successfully loading the module is treated as a test failure. In the
596 event of a failure, all remaining tests will be skipped.
597
598 =back
599
600 The methods available are:
601
602 =over 4
603
604 =item B<require_ok>
605
606 $o->require_ok($module [,$mode]);
607
608 This is used to load a module using the perl C<require> function. If
609 C<$mode> is not passed in, the default mode (B<required>) is used to test
610 the existence of the module.
611
612 If C<$mode> is passed in, it must be either the string 'forbid' or
613 'feature'.
614
615 If C<$mode> is 'feature', a feature named C<$module> is set if the module
616 was able to be loaded.
617
618 =item B<use_ok>
619
620 $o->use_ok(@args [,$mode]);
621
622 This is used to load a module with C<use>, or check a perl version.
623
624 BEGIN { $o->use_ok('5.010'); }
625 BEGIN { $o->use_ok('Some::Module'); }
626 BEGIN { $o->use_ok('Some::Module',2.05); }
627 BEGIN { $o->use_ok('Some::Module','foo','bar'); }
628 BEGIN { $o->use_ok('Some::Module',2.05,'foo','bar'); }
629
630 are the same as:
631
632 use 5.010;
633 use Some::Module;
634 use Some::Module 2.05;
635 use Some::Module qw(foo bar);
636 use Some::Module 2.05 qw(foo bar);
637
638 Putting the B<use_ok> call in a BEGIN block allows the functions to
639 be imported at compile-time and prototypes are properly honored.
640 You'll also need to load the Test::Inter module, and create the
641 object in a BEGIN block.
642
643 C<$mode> acts the same as in the B<require_ok> method.
644
645 =back
646
647 =head1 METHODS FOR RUNNING TEST
648
649 There are several methods for running tests. The B<ok>, B<is>, and B<isnt>
650 methods are included for those already comfortable with L<Test::More>
651 and wishing to stick with the same format of test script. The
652 B<tests> method is the suggested method though since it makes use
653 of the full power of this module.
654
655 =over 4
656
657 =item B<ok>
658
659 $o->ok(TESTS);
660
661 A test run with ok looks at a result, and if it evaluates to 0 (or
662 false), it fails. If it evaluates to non-zero (or true), it
663 passes.
664
665 These tests do not require you to specify the expected results. If
666 expected results are given, they will be compared against the result
667 received, and if they differ, a diagnostic message will be printed,
668 but the test will still succeed or fail based only on the actual
669 result produced.
670
671 These tests require a single result and either zero or one expected
672 results.
673
674 To run a single test, use any of the following:
675
676 $o->ok(); # always succeeds
677
678 $o->ok($result);
679 $o->ok($result,$name);
680 $o->ok($result,$expected,$name);
681
682 $o->ok(\&func);
683 $o->ok(\&func,$name);
684 $o->ok(\&func,$expected,$name);
685
686 $o->ok(\&func,\@args);
687 $o->ok(\&func,\@args,$name);
688 $o->ok(\&func,\@args,$expected,$name);
689
690 If C<$result> is a scalar, the test passes if C<$result> is true. If
691 C<$result> is a list reference, the test succeeds if the list contains
692 any defined values. If C<$result> is a hash reference, the test
693 succeeds if the hash contains any key with a value that is not
694 C<undef>.
695
696 If C<\&func> and C<\@args> are passed in, then C<$result> is generated by
697 passing C<@args> to C<&func> and behaves identically to the calls where
698 C<$result> is passed in. If C<\&func> is passed in but no arguments, the
699 function takes no arguments, but still produces a result.
700
701 If an expected value is passed in and the result does not match it,
702 a diagnostic warning will be printed, even if the test passes.
703
704 =item B<is>
705
706 =item B<isnt>
707
708 $o->is(TESTS);
709 $o->isnt(TESTS);
710
711 A test run with B<is> looks at a result and tests to see if it is identical to
712 an expected result. If it is, the test passes. Otherwise it fails. In the
713 case of a failure, a diagnostic message will show what result was actually
714 obtained and what was expected.
715
716 A test run with B<isnt> looks at a result and tests to see if the result obtained
717 is different than an expected result. If it is different, the test passes.
718 Otherwise it fails.
719
720 The is method can be called in any of the following ways:
721
722 $o->is($result,$expected);
723 $o->is($result,$expected,$name);
724
725 $o->is(\&func,$expected);
726 $o->is(\&func,$expected,$name);
727
728 $o->is(\&func,\@args,$expected);
729 $o->is(\&func,\@args,$expected,$name);
730
731 The B<isnt> method can be called in exactly the same way.
732
733 As with the B<ok> method, the result can be a scalar, hashref, or listref. If it is
734 a hashref or listref, the entire structure must match the expected value.
735
736 =item B<tests>
737
738 $o->tests($opt=>$val, $opt=>$val, ...)
739
740 The options available are described in the following section.
741
742 =item B<file>
743
744 $o->file($func,$input,$outputdir,$expected,$name [,@args]);
745
746 Sometimes it may be easiest to store the input, output, and expected
747 output from a test in a text file. In this case, each line of
748 output will be treated as a single test, so the output and expected
749 output must match up exactly.
750
751 C<$func> is a reference to a function which will produce a temporary
752 output file.
753
754 If C<$input> is specified, it is the name of the input file. If it is
755 empty, no input file will be used. The input file can be fully specified,
756 or it can be relative to the test directory.
757
758 If C<$outputdir> is passed in, it is the directory where the output
759 file will be written. It can be fully specified, or relative to the
760 test directory. If C<$outputdir> is left blank, the temporary
761 file will be written to the test directory.
762
763 C<$expected> is the name of a file which contains the expected output.
764 It can be fully specified, or it will be checked for in the test
765 directory.
766
767 C<$name> is the name of this series of tests.
768
769 C<@args> are extra arguments to pass to the test function.
770
771 The function will be called with the arguments:
772
773 &$func( [$input,] $output,@args);
774
775 C<$input> is only passed in if it was passed in to this method. If no
776 input file is specified, nothing will be passed to the function.
777
778 C<$output> is the name of a temporary file where the output will
779 be written to.
780
781 =back
782
783 =head1 USING THE TESTS METHOD
784
785 It is expected that most tests (except for those that load a module)
786 will be run using the tests method called as:
787
788 $o->tests(%options);
789
790 The following options are available:
791
792 =over 4
793
794 =item B<name>
795
796 name => NAME
797
798 This sets the name of this set of tests. All tests will be given the
799 same name.
800
801 =item B<tests>
802
803 =item B<func>
804
805 =item B<expected>
806
807 In order to specify a series of tests, you have to specify either
808 a function and a list of arguments, or a list of results.
809
810 Specifying the function and list of arguments can be done using
811 the pair:
812
813 func => \&FUNCTION
814 tests => TESTS
815
816 If the B<func> option is not set, B<tests> contains a list of results.
817
818 A list of expected results may also be given. They can be included
819 in the
820
821 tests => TESTS
822
823 option or included separately as:
824
825 expected => RESULTS
826
827 The way to specify these are covered in the next section SPECIFYING THE TESTS.
828
829 =item B<feature>
830
831 =item B<disable>
832
833 feature => [FEATURE1, FEATURE2, ...]
834
835 disable => [FEATURE1, FEATURE2, ...]
836
837 The default set of tests to run is determined using the start, end, and skip_all
838 methods discussed above. Using those methods, a list of tests is obtained, and
839 it is expected that these will run.
840
841 The feature and disable options modify the list.
842
843 If the feature option is included, the tests given in this call will only run
844 if ALL of the features listed are available.
845
846 If the disable option is included, the tests will be run unless ANY of the features
847 listed are available.
848
849 =item B<skip>
850
851 skip => REASON
852
853 Skip these tests for the reason given.
854
855 =item B<todo>
856
857 todo => 0/1
858
859 Setting this to 1 says that these tests are allowed to fail. They represent
860 a feature that is not yet implemented.
861
862 If the tests succeed, a message will be printed notifying the developer that
863 the tests are now ready to promote to actual use.
864
865 =back
866
867 =head1 SPECIFYING THE TESTS
868
869 A series of tests can be specified in two different ways. The tests
870 can be written in a very simple string format, or stored as a list.
871
872 Demonstrating how this can be done is best done by example, so let's
873 say that there is a function (func) which takes two arguments, and
874 returns a single value. Let's say that the expected output (and the
875 actual output) from 3 different sets of arguments is:
876
877 Input Expected Output Actual Output
878 ----- --------------- -------------
879 1,2 a a
880 3,4 b x
881 5,6 c c
882
883 (so in this case, the first and third tests pass, but the 2nd
884 one will fail).
885
886 Specifying these tests as lists could be done as:
887
888 $o->tests(
889 func => &func,
890 tests => [ [1,2], [3,4], [5,6] ],
891 expected => [ [a], [b], [c] ],
892 );
893
894 Here, the tests are stored as a list, and each element in the list is a
895 listref containing the set of arguments.
896
897 If the B<func> option is not passed in, the tests option is set to a list
898 of results to compare with the expected results, so the following is
899 equivalent to the above:
900
901 $o->tests(
902 tests => [ [a], [x], [c] ],
903 expected => [ [a], [b], [c] ],
904 );
905
906 If an argument (or actual result) or an expected result is only a
907 single value, it can be entered as a scalar instead of a list ref, so
908 the following is also equivalent:
909
910 $o->tests(
911 func => &func,
912 tests => [ [1,2], [3,4], [5,6] ],
913 expected => [ a, b, [c] ],
914 );
915
916 The only exception to this is if the single value is itself a list
917 reference. In this case it MUST be included as a reference. In other
918 words, if you have a single test, and the expected value for this test
919 is a list reference, it must be passed in as:
920
921 expected => [ [ \@r ] ]
922
923 NOT as:
924
925 expected => [ \@r ]
926
927 Passing in a set of expected results is optional. If none are passed
928 in, the tests are treated as if they had been passed to the B<ok>
929 method (i.e. if they return something true, they pass, otherwise they
930 fail).
931
932 The second way to specify tests is as a string. The string is a
933 multi-line string with each tests being separate from the next test by
934 a blank line. Comments (lines which begin with '#') are allowed, and
935 are ignored. Whitespace at the start and end of the line is ignored.
936
937 The string may contain the results directly, or results may be passed
938 in separately. For example, the following all give the same sets of
939 tests as the example above:
940
941 $o->tests(
942 func => &func,
943 tests => "
944 # Test 1
945 1 2 => a
946
947 # Test 2
948 3 4 => b
949
950 5 6 => c
951 ",
952 );
953
954 $o->tests(
955 func => &func,
956 tests => "
957 1 2
958
959 3 4
960
961 5 6
962 ",
963 expected => [ [a], [b], [c] ]
964 );
965
966 $o->tests(
967 func => &func,
968 tests => [ [1,2], [3,4], [5,6] ],
969 expected => "
970 a
971
972 b
973
974 c
975 ",
976 );
977
978 $o->tests(
979 func => &func,
980 tests => "
981 1 2
982
983 3 4
984
985 5 6
986 ",
987 expected => "
988 a
989
990 b
991
992 c
993 ",
994 );
995
996 The expected results may also consist of only a single set of results (in this
997 case, it must be passed in as a listref). In this case, all of the tests are
998 expected to have the same results.
999
1000 So, the following are equivalent:
1001
1002 $o->tests(
1003 func => &func,
1004 tests => "
1005 1 2 => a b
1006
1007 3 4 => a b
1008
1009 5 6 => a b
1010 ",
1011 );
1012
1013 $o->tests(
1014 func => &func,
1015 tests => "
1016 1 2
1017
1018 3 4
1019
1020 5 6
1021 ",
1022 expected => [ [a, b] ],
1023 );
1024
1025 $o->tests(
1026 func => &func,
1027 tests => "
1028 1 2
1029
1030 3 4
1031
1032 5 6
1033 ",
1034 expected => "a b",
1035 );
1036
1037 The number of expected values must either be 1 (i.e. all of the tests
1038 are expected to produce the same value) or exactly the same number as
1039 the number of tests.
1040
1041 The parser is actually quite powerful, and can handle multi-line
1042 tests, quoted strings, and nested data structures.
1043
1044 The test may be split across any number of lines, provided there is
1045 not a completely blank line (which signals the end of the test), so
1046 the following are equivalent:
1047
1048 tests => "a b c",
1049 tests => "a b
1050 c",
1051
1052 Arguments (or expected results) may include data structures. For
1053 example, the following are equivalent:
1054
1055 tests => "[ a b ] { a 1 b 2 }"
1056 tests => [ [ [a,b], { a=>1, b=>2 } ] ]
1057
1058 Whitespace is mostly optional, but there is one exception. An item must
1059 end with some kind of delimiter, so the following will fail:
1060
1061 tests => "[a b][c d]"
1062
1063 The first element (the list ref [a b]) must be separated from the second
1064 element by the delimiter (which is whitespace in this case), so it must
1065 be written as:
1066
1067 tests => "[a b] [c d]"
1068
1069 As already demonstrated, hashrefs and listrefs may be included and
1070 nested. Elements may also be included inside parens, but this is optional
1071 since all arguments and expected results are already treated as lists,
1072 so the following are equivalent:
1073
1074 tests => "a b c"
1075 tests => "(a b) c"
1076
1077 Although parens are optional, they may make things more readable, and allow
1078 you to use something other than whitespace as the delimiter. Since parens are
1079 actually ignored, a string '()' is also ignored, so do not use empty
1080 parentheses.
1081
1082 If the character immediately following the opening paren, brace, or
1083 bracket is a punctuation mark, then it is used as the delimiter
1084 instead of whitespace. For example, the following are all equivalent:
1085
1086 [ a b c ]
1087 [a b c]
1088 [, a,b,c ]
1089 [, a, b, c ]
1090
1091 A delimiter is a single character, and the following may not be used
1092 as a delimiter:
1093
1094 any opening/closing characters () [] {}
1095 single or double quotes
1096 alphanumeric characters
1097 underscore
1098
1099 Whitespace (including newlines) around the delimiter is ignored, so
1100 the following is valid:
1101
1102 [, a,
1103 b,
1104 c ]
1105
1106 Two delimiters next to each other or a trailing delimiter produce an
1107 empty string.
1108
1109 "(,a,b,)" => (a, b, '')
1110 "(,a,,b)" => (a, '', b)
1111
1112 Hashrefs may be specified by braces and the following are equivalent:
1113
1114 { a 1 b 2 }
1115 {, a,1,b,2 }
1116 {, a,1,b,2, }
1117
1118 Note that a trailing delimiter is ignored if there are already an even
1119 number of elements, or an empty string otherwise.
1120
1121 Nested structures are allowed:
1122
1123 "[ [1 2] [3 4] ]"
1124
1125 For example,
1126
1127 $o->tests(
1128 func => &func,
1129 tests => "a [ b c ] { d 1 e 2 } => x y"
1130 );
1131
1132 is equivalent to:
1133
1134 $o->tests(
1135 func => &func,
1136 tests => [ [a, [b,c], {d=>1,e=>2}] ],
1137 results => [ [x,y] ],
1138 );
1139
1140 Any single value can be surrounded by single or double quotes in order
1141 to include the delimiter. So:
1142
1143 "(, a,'b,c',e )"
1144
1145 is equivalent to:
1146
1147 "( a b,c e )"
1148
1149 Any single value can be the string '__undef__' which will be turned
1150 into an actual undef. If the value is '__blank__' it is turned into an
1151 empty string (''), though it can also be specified as '' directly. Any
1152 value can have an embedded newline by including a __nl__ in the value,
1153 but the value must be written on a single line.
1154
1155 Expected results are separated from arguments by ' => '.
1156
1157 =head1 TEST::INTER VARIABLES
1158
1159 To summarize the information above, the following variables are used
1160 by Test::Inter. Each variable can be set in two different ways: as
1161 an environment variable and as a perl variable in the main namespace.
1162
1163 For example, the TI_END variable can be set as:
1164
1165 $::TI_END
1166 $ENV{TI_END}
1167
1168 The following variables can be used to define which tests are run:
1169
1170 =over 4
1171
1172 =item TI_START
1173
1174 Set this to define the test you want to start with.
1175
1176 Example: If you have a perl test script and you want to start running
1177 it at test 12, run the following shell commands:
1178
1179 TI_START=12
1180 ./my_test_script.t
1181
1182 =item TI_END
1183
1184 Set this to define the test you want to end with.
1185
1186 =item TI_TESTNUM
1187
1188 Set this to run only a single test
1189
1190 =back
1191
1192 There is also a variable TI_NUM (available only as $::TI_NUM) which is
1193 set automatically by Test::Inter to be the test currently being run.
1194
1195 The following variables control what is output from the tests, and how
1196 it is formatted:
1197
1198 =over 4
1199
1200 =item TI_QUIET
1201
1202 How verbose the test script is. Values are 0 (most verbose) to 2
1203 (least verbose).
1204
1205 =item TI_MODE
1206
1207 How the output is formatted. Values are 'inter' (interactive mode) or
1208 'test' (test suite mode). Interactive mode is easier to read. Test mode
1209 is for running as part of a test suite.
1210
1211 =item TI_WIDTH
1212
1213 The width of the terminal.
1214
1215 =back
1216
1217 The following variables control how some tests are run:
1218
1219 =over 4
1220
1221 =item TI_NOCLEAN
1222
1223 When running a file test, the temporary output file will not be removed
1224 if this is set.
1225
1226 =back
1227
1228 =head1 HISTORY
1229
1230 The history of this module dates back to 1996 when I needed to write a
1231 test suite for my Date::Manip module. At that time, none of the
1232 Test::* modules currently available in CPAN existed (the earliest ones
1233 didn't come along until 1998), so I was left completely on my own
1234 writing my test scripts.
1235
1236 I wrote a very basic version of my test framework which allowed me to
1237 write all of the tests as a string, it would parse the string, count
1238 the tests, and then run them.
1239
1240 Over the years, the functionality I wanted grew, and periodically, I'd
1241 go back and reexamine other Test frameworks (primarily Test::More) to
1242 see if I could replace my framework with an existing module... and I've
1243 always found them wanting, and chosen to extend my existing framework
1244 instead.
1245
1246 As I've written other modules, I've wanted to use the framework in
1247 them too, so I've always just copied it in, but this is obviously
1248 tedious and error prone. I'm not sure why it took me so long... but in
1249 2010, I finally decided it was time to rework the framework in a
1250 module form.
1251
1252 I loosely based my module on Test::More. I like the functionality of
1253 that module, and wanted most of it (and I plan on adding more in
1254 future versions). So this module uses some similar syntax to
1255 Test::More (though it allows a great deal more flexibility in how the
1256 tests are specified).
1257
1258 One thing to note is that I may have been able to write this module
1259 as an extension to Test::More, but after looking into that possibility,
1260 I decided that it would be faster to not do that. I did "borrow" a couple
1261 of routines from it (though they've been modified quite heavily) as a
1262 starting point for a few of the functions in this module, and I thank
1263 the authors of Test::More for their work.
1264
1265 =head1 KNOWN BUGS AND LIMITATIONS
1266
1267 None known.
1268
1269 =head1 SEE ALSO
1270
1271 L<Test::More> - the 'industry standard' of perl test frameworks
1272
1273 =head1 BUGS AND QUESTIONS
1274
1275 If you find a bug in Test::Inter, please let me know. As CPAN has removed
1276 email support, the only supported way to submit a bug is via ticket:
1277
1278 =over 4
1279
1280 =item GitHub
1281
1282 You can submit it as an issue on GitHub. This can be done at the following
1283 URL:
1284
1285 L<https://github.com/SBECK-github/Test-Inter>
1286
1287 =back
1288
1289 Please do not use other means to report bugs (such as forums for a specific
1290 OS or Linux distribution) as it is impossible for me to keep up with all of
1291 them.
1292
1293 When filing a bug report, please include the following information:
1294
1295 =over 4
1296
1297 =item B<Test::Inter version>
1298
1299 Please include the version of Test::Inter you are using. You can get
1300 this by using the script:
1301
1302 use Test::Inter;
1303 print $Test::Inter::VERSION,"\n";
1304
1305 =back
1306
1307 If you want to report missing or incorrect codes, you must be running the
1308 most recent version of Test::Inter.
1309
1310 If you find any problems with the documentation (errors, typos, or items
1311 that are not clear), please send them to me. I welcome any suggestions
1312 that will allow me to improve the documentation.
1313
1314 =head1 LICENSE
1315
1316 This script is free software; you can redistribute it and/or modify it under the same
1317 terms as Perl itself.
1318
1319 =head1 AUTHOR
1320
1321 Sullivan Beck (https://github.com/SBECK-github/)
1322
1323 =cut