| 651 | | #line 1328 |
| | 650 | =head1 NAME |
| | 651 | |
| | 652 | Test::Base - A Data Driven Testing Framework |
| | 653 | |
| | 654 | =head1 SYNOPSIS |
| | 655 | |
| | 656 | A new test module: |
| | 657 | |
| | 658 | # lib/MyProject/Test.pm |
| | 659 | package MyProject::Test; |
| | 660 | use Test::Base -Base; |
| | 661 | |
| | 662 | use MyProject; |
| | 663 | |
| | 664 | package MyProject::Test::Filter; |
| | 665 | use Test::Base::Filter -base; |
| | 666 | |
| | 667 | sub my_filter { |
| | 668 | return MyProject->do_something(shift); |
| | 669 | } |
| | 670 | |
| | 671 | A sample test: |
| | 672 | |
| | 673 | # t/sample.t |
| | 674 | use MyProject::Test; |
| | 675 | |
| | 676 | plan tests => 1 * blocks; |
| | 677 | |
| | 678 | run_is input => 'expected'; |
| | 679 | |
| | 680 | sub local_filter { |
| | 681 | s/my/your/; |
| | 682 | } |
| | 683 | |
| | 684 | __END__ |
| | 685 | |
| | 686 | === Test one (the name of the test) |
| | 687 | --- input my_filter local_filter |
| | 688 | my |
| | 689 | input |
| | 690 | lines |
| | 691 | --- expected |
| | 692 | expected |
| | 693 | output |
| | 694 | |
| | 695 | === Test two |
| | 696 | This is an optional description |
| | 697 | of this particular test. |
| | 698 | --- input my_filter |
| | 699 | other |
| | 700 | input |
| | 701 | lines |
| | 702 | --- expected |
| | 703 | other expected |
| | 704 | output |
| | 705 | |
| | 706 | =head1 DESCRIPTION |
| | 707 | |
| | 708 | Testing is usually the ugly part of Perl module authoring. Perl gives |
| | 709 | you a standard way to run tests with Test::Harness, and basic testing |
| | 710 | primitives with Test::More. After that you are pretty much on your own |
| | 711 | to develop a testing framework and philosophy. Test::More encourages |
| | 712 | you to make your own framework by subclassing Test::Builder, but that is |
| | 713 | not trivial. |
| | 714 | |
| | 715 | Test::Base gives you a way to write your own test framework base |
| | 716 | class that I<is> trivial. In fact it is as simple as two lines: |
| | 717 | |
| | 718 | package MyTestFramework; |
| | 719 | use Test::Base -Base; |
| | 720 | |
| | 721 | A module called C<MyTestFramework.pm> containing those two lines, will |
| | 722 | give all the power of Test::More and all the power of Test::Base to |
| | 723 | every test file that uses it. As you build up the capabilities of |
| | 724 | C<MyTestFramework>, your tests will have all of that power as well. |
| | 725 | |
| | 726 | C<MyTestFramework> becomes a place for you to put all of your reusable |
| | 727 | testing bits. As you write tests, you will see patterns and duplication, |
| | 728 | and you can "upstream" them into C<MyTestFramework>. Of course, you |
| | 729 | don't have to subclass Test::Base at all. You can use it directly in |
| | 730 | many applications, including everywhere you would use Test::More. |
| | 731 | |
| | 732 | Test::Base concentrates on offering reusable data driven patterns, so |
| | 733 | that you can write tests with a minimum of code. At the heart of all |
| | 734 | testing you have inputs, processes and expected outputs. Test::Base |
| | 735 | provides some clean ways for you to express your input and expected |
| | 736 | output data, so you can spend your time focusing on that rather than |
| | 737 | your code scaffolding. |
| | 738 | |
| | 739 | =head1 EXPORTED FUNCTIONS |
| | 740 | |
| | 741 | Test::Base extends Test::More and exports all of its functions. So you |
| | 742 | can basically write your tests the same as Test::More. Test::Base |
| | 743 | also exports many functions of its own: |
| | 744 | |
| | 745 | =head2 is(actual, expected, [test-name]) |
| | 746 | |
| | 747 | This is the equivalent of Test::More's C<is> function with one |
| | 748 | interesting twist. If your actual and expected results differ and the |
| | 749 | output is multi-line, this function will show you a unified diff format |
| | 750 | of output. Consider the benefit when looking for the one character that |
| | 751 | is different in hundreds of lines of output! |
| | 752 | |
| | 753 | Diff output requires the optional C<Text::Diff> CPAN module. If you |
| | 754 | don't have this module, the C<is()> function will simply give you normal |
| | 755 | Test::More output. To disable diffing altogether, set the |
| | 756 | C<TEST_SHOW_NO_DIFFS> environment variable (or C<$ENV{TEST_SHOW_NO_DIFFS}>) |
| | 757 | to a true value. You can also call the C<no_diff> function as a shortcut. |
| | 758 | |
| | 759 | =head2 blocks( [data-section-name] ) |
| | 760 | |
| | 761 | The most important function is C<blocks>. In list context it returns a |
| | 762 | list of C<Test::Base::Block> objects that are generated from the test |
| | 763 | specification in the C<DATA> section of your test file. In scalar |
| | 764 | context it returns the number of objects. This is useful to calculate |
| | 765 | your Test::More plan. |
| | 766 | |
| | 767 | Each Test::Base::Block object has methods that correspond to the names |
| | 768 | of that object's data sections. There is also a C<name> and a |
| | 769 | C<description> method for accessing those parts of the block if they |
| | 770 | were specified. |
| | 771 | |
| | 772 | The C<blocks> function can take an optional single argument, that |
| | 773 | indicates to only return the blocks that contain a particular named data |
| | 774 | section. Otherwise C<blocks> returns all blocks. |
| | 775 | |
| | 776 | my @all_of_my_blocks = blocks; |
| | 777 | |
| | 778 | my @just_the_foo_blocks = blocks('foo'); |
| | 779 | |
| | 780 | =head2 next_block() |
| | 781 | |
| | 782 | You can use the next_block function to iterate over all the blocks. |
| | 783 | |
| | 784 | while (my $block = next_block) { |
| | 785 | ... |
| | 786 | } |
| | 787 | |
| | 788 | It returns undef after all blocks have been iterated over. It can then |
| | 789 | be called again to reiterate. |
| | 790 | |
| | 791 | =head2 first_block() |
| | 792 | |
| | 793 | Returns the first block or undef if there are none. It resets the iterator to |
| | 794 | the C<next_block> function. |
| | 795 | |
| | 796 | =head2 run(&subroutine) |
| | 797 | |
| | 798 | There are many ways to write your tests. You can reference each block |
| | 799 | individually or you can loop over all the blocks and perform a common |
| | 800 | operation. The C<run> function does the looping for you, so all you need |
| | 801 | to do is pass it a code block to execute for each block. |
| | 802 | |
| | 803 | The C<run> function takes a subroutine as an argument, and calls the sub |
| | 804 | one time for each block in the specification. It passes the current |
| | 805 | block object to the subroutine. |
| | 806 | |
| | 807 | run { |
| | 808 | my $block = shift; |
| | 809 | is(process($block->foo), $block->bar, $block->name); |
| | 810 | }; |
| | 811 | |
| | 812 | =head2 run_is([data_name1, data_name2]) |
| | 813 | |
| | 814 | Many times you simply want to see if two data sections are equivalent in |
| | 815 | every block, probably after having been run through one or more filters. |
| | 816 | With the C<run_is> function, you can just pass the names of any two data |
| | 817 | sections that exist in every block, and it will loop over every block |
| | 818 | comparing the two sections. |
| | 819 | |
| | 820 | run_is 'foo', 'bar'; |
| | 821 | |
| | 822 | If no data sections are given C<run_is> will try to detect them |
| | 823 | automatically. |
| | 824 | |
| | 825 | NOTE: Test::Base will silently ignore any blocks that don't contain |
| | 826 | both sections. |
| | 827 | |
| | 828 | =head2 run_is_deeply([data_name1, data_name2]) |
| | 829 | |
| | 830 | Like C<run_is> but uses C<is_deeply> for complex data structure comparison. |
| | 831 | |
| | 832 | =head2 run_like([data_name, regexp | data_name]); |
| | 833 | |
| | 834 | The C<run_like> function is similar to C<run_is> except the second |
| | 835 | argument is a regular expression. The regexp can either be a C<qr{}> |
| | 836 | object or a data section that has been filtered into a regular |
| | 837 | expression. |
| | 838 | |
| | 839 | run_like 'foo', qr{<html.*}; |
| | 840 | run_like 'foo', 'match'; |
| | 841 | |
| | 842 | =head2 run_unlike([data_name, regexp | data_name]); |
| | 843 | |
| | 844 | The C<run_unlike> function is similar to C<run_like>, except the opposite. |
| | 845 | |
| | 846 | run_unlike 'foo', qr{<html.*}; |
| | 847 | run_unlike 'foo', 'no_match'; |
| | 848 | |
| | 849 | =head2 run_compare(data_name1, data_name2) |
| | 850 | |
| | 851 | The C<run_compare> function is like the C<run_is>, C<run_is_deeply> and |
| | 852 | the C<run_like> functions all rolled into one. It loops over each |
| | 853 | relevant block and determines what type of comparison to do. |
| | 854 | |
| | 855 | NOTE: If you do not specify either a plan, or run any tests, the |
| | 856 | C<run_compare> function will automatically be run. |
| | 857 | |
| | 858 | =head2 delimiters($block_delimiter, $data_delimiter) |
| | 859 | |
| | 860 | Override the default delimiters of C<===> and C<--->. |
| | 861 | |
| | 862 | =head2 spec_file($file_name) |
| | 863 | |
| | 864 | By default, Test::Base reads its input from the DATA section. This |
| | 865 | function tells it to get the spec from a file instead. |
| | 866 | |
| | 867 | =head2 spec_string($test_data) |
| | 868 | |
| | 869 | By default, Test::Base reads its input from the DATA section. This |
| | 870 | function tells it to get the spec from a string that has been |
| | 871 | prepared somehow. |
| | 872 | |
| | 873 | =head2 filters( @filters_list or $filters_hashref ) |
| | 874 | |
| | 875 | Specify a list of additional filters to be applied to all blocks. See |
| | 876 | L<FILTERS> below. |
| | 877 | |
| | 878 | You can also specify a hash ref that maps data section names to an array |
| | 879 | ref of filters for that data type. |
| | 880 | |
| | 881 | filters { |
| | 882 | xxx => [qw(chomp lines)], |
| | 883 | yyy => ['yaml'], |
| | 884 | zzz => 'eval', |
| | 885 | }; |
| | 886 | |
| | 887 | If a filters list has only one element, the array ref is optional. |
| | 888 | |
| | 889 | =head2 filters_delay( [1 | 0] ); |
| | 890 | |
| | 891 | By default Test::Base::Block objects are have all their filters run |
| | 892 | ahead of time. There are testing situations in which it is advantageous |
| | 893 | to delay the filtering. Calling this function with no arguments or a |
| | 894 | true value, causes the filtering to be delayed. |
| | 895 | |
| | 896 | use Test::Base; |
| | 897 | filters_delay; |
| | 898 | plan tests => 1 * blocks; |
| | 899 | for my $block (blocks) { |
| | 900 | ... |
| | 901 | $block->run_filters; |
| | 902 | ok($block->is_filtered); |
| | 903 | ... |
| | 904 | } |
| | 905 | |
| | 906 | In the code above, the filters are called manually, using the |
| | 907 | C<run_filters> method of Test::Base::Block. In functions like |
| | 908 | C<run_is>, where the tests are run automatically, filtering is delayed |
| | 909 | until right before the test. |
| | 910 | |
| | 911 | =head2 filter_arguments() |
| | 912 | |
| | 913 | Return the arguments after the equals sign on a filter. |
| | 914 | |
| | 915 | sub my_filter { |
| | 916 | my $args = filter_arguments; |
| | 917 | # is($args, 'whazzup'); |
| | 918 | ... |
| | 919 | } |
| | 920 | |
| | 921 | __DATA__ |
| | 922 | === A test |
| | 923 | --- data my_filter=whazzup |
| | 924 | |
| | 925 | =head2 tie_output() |
| | 926 | |
| | 927 | You can capture STDOUT and STDERR for operations with this function: |
| | 928 | |
| | 929 | my $out = ''; |
| | 930 | tie_output(*STDOUT, $buffer); |
| | 931 | print "Hey!\n"; |
| | 932 | print "Che!\n"; |
| | 933 | untie *STDOUT; |
| | 934 | is($out, "Hey!\nChe!\n"); |
| | 935 | |
| | 936 | =head2 no_diff() |
| | 937 | |
| | 938 | Turn off diff support for is() in a test file. |
| | 939 | |
| | 940 | =head2 default_object() |
| | 941 | |
| | 942 | Returns the default Test::Base object. This is useful if you feel |
| | 943 | the need to do an OO operation in otherwise functional test code. See |
| | 944 | L<OO> below. |
| | 945 | |
| | 946 | =head2 WWW() XXX() YYY() ZZZ() |
| | 947 | |
| | 948 | These debugging functions are exported from the Spiffy.pm module. See |
| | 949 | L<Spiffy> for more info. |
| | 950 | |
| | 951 | =head2 croak() carp() cluck() confess() |
| | 952 | |
| | 953 | You can use the functions from the Carp module without needing to import |
| | 954 | them. Test::Base does it for you by default. |
| | 955 | |
| | 956 | =head1 TEST SPECIFICATION |
| | 957 | |
| | 958 | Test::Base allows you to specify your test data in an external file, |
| | 959 | the DATA section of your program or from a scalar variable containing |
| | 960 | all the text input. |
| | 961 | |
| | 962 | A I<test specification> is a series of text lines. Each test (or block) |
| | 963 | is separated by a line containing the block delimiter and an optional |
| | 964 | test C<name>. Each block is further subdivided into named sections with |
| | 965 | a line containing the data delimiter and the data section name. A |
| | 966 | C<description> of the test can go on lines after the block delimiter but |
| | 967 | before the first data section. |
| | 968 | |
| | 969 | Here is the basic layout of a specification: |
| | 970 | |
| | 971 | === <block name 1> |
| | 972 | <optional block description lines> |
| | 973 | --- <data section name 1> <filter-1> <filter-2> <filter-n> |
| | 974 | <test data lines> |
| | 975 | --- <data section name 2> <filter-1> <filter-2> <filter-n> |
| | 976 | <test data lines> |
| | 977 | --- <data section name n> <filter-1> <filter-2> <filter-n> |
| | 978 | <test data lines> |
| | 979 | |
| | 980 | === <block name 2> |
| | 981 | <optional block description lines> |
| | 982 | --- <data section name 1> <filter-1> <filter-2> <filter-n> |
| | 983 | <test data lines> |
| | 984 | --- <data section name 2> <filter-1> <filter-2> <filter-n> |
| | 985 | <test data lines> |
| | 986 | --- <data section name n> <filter-1> <filter-2> <filter-n> |
| | 987 | <test data lines> |
| | 988 | |
| | 989 | Here is a code example: |
| | 990 | |
| | 991 | use Test::Base; |
| | 992 | |
| | 993 | delimiters qw(### :::); |
| | 994 | |
| | 995 | # test code here |
| | 996 | |
| | 997 | __END__ |
| | 998 | |
| | 999 | ### Test One |
| | 1000 | We want to see if foo and bar |
| | 1001 | are really the same... |
| | 1002 | ::: foo |
| | 1003 | a foo line |
| | 1004 | another foo line |
| | 1005 | |
| | 1006 | ::: bar |
| | 1007 | a bar line |
| | 1008 | another bar line |
| | 1009 | |
| | 1010 | ### Test Two |
| | 1011 | |
| | 1012 | ::: foo |
| | 1013 | some foo line |
| | 1014 | some other foo line |
| | 1015 | |
| | 1016 | ::: bar |
| | 1017 | some bar line |
| | 1018 | some other bar line |
| | 1019 | |
| | 1020 | ::: baz |
| | 1021 | some baz line |
| | 1022 | some other baz line |
| | 1023 | |
| | 1024 | This example specifies two blocks. They both have foo and bar data |
| | 1025 | sections. The second block has a baz component. The block delimiter is |
| | 1026 | C<###> and the data delimiter is C<:::>. |
| | 1027 | |
| | 1028 | The default block delimiter is C<===> and the default data delimiter |
| | 1029 | is C<--->. |
| | 1030 | |
| | 1031 | There are some special data section names used for control purposes: |
| | 1032 | |
| | 1033 | --- SKIP |
| | 1034 | --- ONLY |
| | 1035 | --- LAST |
| | 1036 | |
| | 1037 | A block with a SKIP section causes that test to be ignored. This is |
| | 1038 | useful to disable a test temporarily. |
| | 1039 | |
| | 1040 | A block with an ONLY section causes only that block to be used. This is |
| | 1041 | useful when you are concentrating on getting a single test to pass. If |
| | 1042 | there is more than one block with ONLY, the first one will be chosen. |
| | 1043 | |
| | 1044 | Because ONLY is very useful for debugging and sometimes you forgot to |
| | 1045 | remove the ONLY flag before commiting to the VCS or uploading to CPAN, |
| | 1046 | Test::Base by default gives you a diag message saying I<I found ONLY |
| | 1047 | ... maybe you're debugging?>. If you don't like it, use |
| | 1048 | C<no_diag_on_only>. |
| | 1049 | |
| | 1050 | A block with a LAST section makes that block the last one in the |
| | 1051 | specification. All following blocks will be ignored. |
| | 1052 | |
| | 1053 | =head1 FILTERS |
| | 1054 | |
| | 1055 | The real power in writing tests with Test::Base comes from its |
| | 1056 | filtering capabilities. Test::Base comes with an ever growing set |
| | 1057 | of useful generic filters than you can sequence and apply to various |
| | 1058 | test blocks. That means you can specify the block serialization in |
| | 1059 | the most readable format you can find, and let the filters translate |
| | 1060 | it into what you really need for a test. It is easy to write your own |
| | 1061 | filters as well. |
| | 1062 | |
| | 1063 | Test::Base allows you to specify a list of filters to each data |
| | 1064 | section of each block. The default filters are C<norm> and C<trim>. |
| | 1065 | These filters will be applied (in order) to the data after it has been |
| | 1066 | parsed from the specification and before it is set into its |
| | 1067 | Test::Base::Block object. |
| | 1068 | |
| | 1069 | You can add to the default filter list with the C<filters> function. You |
| | 1070 | can specify additional filters to a specific block by listing them after |
| | 1071 | the section name on a data section delimiter line. |
| | 1072 | |
| | 1073 | Example: |
| | 1074 | |
| | 1075 | use Test::Base; |
| | 1076 | |
| | 1077 | filters qw(foo bar); |
| | 1078 | filters { perl => 'strict' }; |
| | 1079 | |
| | 1080 | sub upper { uc(shift) } |
| | 1081 | |
| | 1082 | __END__ |
| | 1083 | |
| | 1084 | === Test one |
| | 1085 | --- foo trim chomp upper |
| | 1086 | ... |
| | 1087 | |
| | 1088 | --- bar -norm |
| | 1089 | ... |
| | 1090 | |
| | 1091 | --- perl eval dumper |
| | 1092 | my @foo = map { |
| | 1093 | - $_; |
| | 1094 | } 1..10; |
| | 1095 | \ @foo; |
| | 1096 | |
| | 1097 | Putting a C<-> before a filter on a delimiter line, disables that |
| | 1098 | filter. |
| | 1099 | |
| | 1100 | =head2 Scalar vs List |
| | 1101 | |
| | 1102 | Each filter can take either a scalar or a list as input, and will return |
| | 1103 | either a scalar or a list. Since filters are chained together, it is |
| | 1104 | important to learn which filters expect which kind of input and return |
| | 1105 | which kind of output. |
| | 1106 | |
| | 1107 | For example, consider the following filter list: |
| | 1108 | |
| | 1109 | norm trim lines chomp array dumper eval |
| | 1110 | |
| | 1111 | The data always starts out as a single scalar string. C<norm> takes a |
| | 1112 | scalar and returns a scalar. C<trim> takes a list and returns a list, |
| | 1113 | but a scalar is a valid list. C<lines> takes a scalar and returns a |
| | 1114 | list. C<chomp> takes a list and returns a list. C<array> takes a list |
| | 1115 | and returns a scalar (an anonymous array reference containing the list |
| | 1116 | elements). C<dumper> takes a list and returns a scalar. C<eval> takes a |
| | 1117 | scalar and creates a list. |
| | 1118 | |
| | 1119 | A list of exactly one element works fine as input to a filter requiring |
| | 1120 | a scalar, but any other list will cause an exception. A scalar in list |
| | 1121 | context is considered a list of one element. |
| | 1122 | |
| | 1123 | Data accessor methods for blocks will return a list of values when used |
| | 1124 | in list context, and the first element of the list in scalar context. |
| | 1125 | This is usually "the right thing", but be aware. |
| | 1126 | |
| | 1127 | =head2 The Stock Filters |
| | 1128 | |
| | 1129 | Test::Base comes with large set of stock filters. They are in the |
| | 1130 | C<Test::Base::Filter> module. See L<Test::Base::Filter> for a listing and |
| | 1131 | description of these filters. |
| | 1132 | |
| | 1133 | =head2 Rolling Your Own Filters |
| | 1134 | |
| | 1135 | Creating filter extensions is very simple. You can either write a |
| | 1136 | I<function> in the C<main> namespace, or a I<method> in the |
| | 1137 | C<Test::Base::Filter> namespace or a subclass of it. In either case the |
| | 1138 | text and any extra arguments are passed in and you return whatever you |
| | 1139 | want the new value to be. |
| | 1140 | |
| | 1141 | Here is a self explanatory example: |
| | 1142 | |
| | 1143 | use Test::Base; |
| | 1144 | |
| | 1145 | filters 'foo', 'bar=xyz'; |
| | 1146 | |
| | 1147 | sub foo { |
| | 1148 | transform(shift); |
| | 1149 | } |
| | 1150 | |
| | 1151 | sub Test::Base::Filter::bar { |
| | 1152 | my $self = shift; # The Test::Base::Filter object |
| | 1153 | my $data = shift; |
| | 1154 | my $args = $self->current_arguments; |
| | 1155 | my $current_block_object = $self->block; |
| | 1156 | # transform $data in a barish manner |
| | 1157 | return $data; |
| | 1158 | } |
| | 1159 | |
| | 1160 | If you use the method interface for a filter, you can access the block |
| | 1161 | internals by calling the C<block> method on the filter object. |
| | 1162 | |
| | 1163 | Normally you'll probably just use the functional interface, although all |
| | 1164 | the builtin filters are methods. |
| | 1165 | |
| | 1166 | Note that filters defined in the C<main> namespace can look like: |
| | 1167 | |
| | 1168 | sub filter9 { |
| | 1169 | s/foo/bar/; |
| | 1170 | } |
| | 1171 | |
| | 1172 | since Test::Base automatically munges the input string into $_ |
| | 1173 | variable and checks the return value of the function to see if it |
| | 1174 | looks like a number. If you must define a filter that returns just a |
| | 1175 | single number, do it in a different namespace as a method. These |
| | 1176 | filters don't allow the simplistic $_ munging. |
| | 1177 | |
| | 1178 | =head1 OO |
| | 1179 | |
| | 1180 | Test::Base has a nice functional interface for simple usage. Under the |
| | 1181 | hood everything is object oriented. A default Test::Base object is |
| | 1182 | created and all the functions are really just method calls on it. |
| | 1183 | |
| | 1184 | This means if you need to get fancy, you can use all the object |
| | 1185 | oriented stuff too. Just create new Test::Base objects and use the |
| | 1186 | functions as methods. |
| | 1187 | |
| | 1188 | use Test::Base; |
| | 1189 | my $blocks1 = Test::Base->new; |
| | 1190 | my $blocks2 = Test::Base->new; |
| | 1191 | |
| | 1192 | $blocks1->delimiters(qw(!!! @@@))->spec_file('test1.txt'); |
| | 1193 | $blocks2->delimiters(qw(### $$$))->spec_string($test_data); |
| | 1194 | |
| | 1195 | plan tests => $blocks1->blocks + $blocks2->blocks; |
| | 1196 | |
| | 1197 | # ... etc |
| | 1198 | |
| | 1199 | =head1 THE C<Test::Base::Block> CLASS |
| | 1200 | |
| | 1201 | In Test::Base, blocks are exposed as Test::Base::Block objects. This |
| | 1202 | section lists the methods that can be called on a Test::Base::Block |
| | 1203 | object. Of course, each data section name is also available as a method. |
| | 1204 | |
| | 1205 | =head2 name() |
| | 1206 | |
| | 1207 | This is the optional short description of a block, that is specified on the |
| | 1208 | block separator line. |
| | 1209 | |
| | 1210 | =head2 description() |
| | 1211 | |
| | 1212 | This is an optional long description of the block. It is the text taken from |
| | 1213 | between the block separator and the first data section. |
| | 1214 | |
| | 1215 | =head2 seq_num() |
| | 1216 | |
| | 1217 | Returns a sequence number for this block. Sequence numbers begin with 1. |
| | 1218 | |
| | 1219 | =head2 blocks_object() |
| | 1220 | |
| | 1221 | Returns the Test::Base object that owns this block. |
| | 1222 | |
| | 1223 | =head2 run_filters() |
| | 1224 | |
| | 1225 | Run the filters on the data sections of the blocks. You don't need to |
| | 1226 | use this method unless you also used the C<filters_delay> function. |
| | 1227 | |
| | 1228 | =head2 is_filtered() |
| | 1229 | |
| | 1230 | Returns true if filters have already been run for this block. |
| | 1231 | |
| | 1232 | =head2 original_values() |
| | 1233 | |
| | 1234 | Returns a hash of the original, unfiltered values of each data section. |
| | 1235 | |
| | 1236 | =head1 SUBCLASSING |
| | 1237 | |
| | 1238 | One of the nicest things about Test::Base is that it is easy to |
| | 1239 | subclass. This is very important, because in your personal project, you |
| | 1240 | will likely want to extend Test::Base with your own filters and other |
| | 1241 | reusable pieces of your test framework. |
| | 1242 | |
| | 1243 | Here is an example of a subclass: |
| | 1244 | |
| | 1245 | package MyTestStuff; |
| | 1246 | use Test::Base -Base; |
| | 1247 | |
| | 1248 | our @EXPORT = qw(some_func); |
| | 1249 | |
| | 1250 | sub some_func { |
| | 1251 | (my ($self), @_) = find_my_self(@_); |
| | 1252 | ... |
| | 1253 | } |
| | 1254 | |
| | 1255 | package MyTestStuff::Block; |
| | 1256 | use base 'Test::Base::Block'; |
| | 1257 | |
| | 1258 | sub desc { |
| | 1259 | $self->description(@_); |
| | 1260 | } |
| | 1261 | |
| | 1262 | package MyTestStuff::Filter; |
| | 1263 | use base 'Test::Base::Filter'; |
| | 1264 | |
| | 1265 | sub upper { |
| | 1266 | $self->assert_scalar(@_); |
| | 1267 | uc(shift); |
| | 1268 | } |
| | 1269 | |
| | 1270 | Note that you don't have to re-Export all the functions from |
| | 1271 | Test::Base. That happens automatically, due to the powers of Spiffy. |
| | 1272 | |
| | 1273 | The first line in C<some_func> allows it to be called as either a |
| | 1274 | function or a method in the test code. |
| | 1275 | |
| | 1276 | =head1 DISTRIBUTION SUPPORT |
| | 1277 | |
| | 1278 | You might be thinking that you do not want to use Test::Base in you |
| | 1279 | modules, because it adds an installation dependency. Fear not. |
| | 1280 | Module::Install takes care of that. |
| | 1281 | |
| | 1282 | Just write a Makefile.PL that looks something like this: |
| | 1283 | |
| | 1284 | use inc::Module::Install; |
| | 1285 | |
| | 1286 | name 'Foo'; |
| | 1287 | all_from 'lib/Foo.pm'; |
| | 1288 | |
| | 1289 | use_test_base; |
| | 1290 | |
| | 1291 | WriteAll; |
| | 1292 | |
| | 1293 | The line with C<use_test_base> will automatically bundle all the code |
| | 1294 | the user needs to run Test::Base based tests. |
| | 1295 | |
| | 1296 | =head1 OTHER COOL FEATURES |
| | 1297 | |
| | 1298 | Test::Base automatically adds: |
| | 1299 | |
| | 1300 | use strict; |
| | 1301 | use warnings; |
| | 1302 | |
| | 1303 | to all of your test scripts and Test::Base subclasses. A Spiffy |
| | 1304 | feature indeed. |
| | 1305 | |
| | 1306 | =head1 HISTORY |
| | 1307 | |
| | 1308 | This module started its life with the horrible and ridicule inducing |
| | 1309 | name C<Test::Chunks>. It was renamed to C<Test::Base> with the hope |
| | 1310 | that it would be seen for the very useful module that it has become. If |
| | 1311 | you are switching from C<Test::Chunks> to C<Test::Base>, simply |
| | 1312 | substitute the concept and usage of C<chunks> to C<blocks>. |
| | 1313 | |
| | 1314 | =head1 AUTHOR |
| | 1315 | |
| | 1316 | Ingy döt Net <ingy@cpan.org> |
| | 1317 | |
| | 1318 | =head1 COPYRIGHT |
| | 1319 | |
| | 1320 | Copyright (c) 2006. Ingy döt Net. All rights reserved. |
| | 1321 | Copyright (c) 2005. Brian Ingerson. All rights reserved. |
| | 1322 | |
| | 1323 | This program is free software; you can redistribute it and/or modify it |
| | 1324 | under the same terms as Perl itself. |
| | 1325 | |
| | 1326 | See http://www.perl.com/perl/misc/Artistic.html |
| | 1327 | |
| | 1328 | =cut |