📄 test.pm
字号:
(my $real_func = $func) =~ s/^have_/need_/; *$func = sub { # be nice to poor souls calling functions with $_ argument in # the foreach loop, etc.! local $_; local @SkipReasons; return $real_func->(@_); };}package Apache::TestToString;sub TIEHANDLE { my $string = ""; bless \$string;}sub PRINT { my $string = shift; $$string .= join '', @_;}sub start { tie *STDOUT, __PACKAGE__; Apache::Test::test_pm_refresh();}sub finish { my $s; { my $o = tied *STDOUT; $s = $$o; } untie *STDOUT; $s;}1;__END__=head1 NAMEApache::Test - Test.pm wrapper with helpers for testing Apache=head1 SYNOPSIS use Apache::Test;=head1 DESCRIPTIONB<Apache::Test> is a wrapper around the standard C<Test.pm> withhelpers for testing an Apache server.=head1 FUNCTIONS=over 4=item planThis function is a wrapper around C<Test::plan>: plan tests => 3;just like using Test.pm, plan 3 tests.If the first argument is an object, such as an C<Apache::RequestRec>object, C<STDOUT> will be tied to it. The C<Test.pm> global state willalso be refreshed by calling C<Apache::Test::test_pm_refresh>. Forexample: plan $r, tests => 7;ties STDOUT to the request object C<$r>.If there is a last argument that doesn't belong to C<Test::plan>(which expects a balanced hash), it's used to decide whether tocontinue with the test or to skip it all-together. This last argumentcan be:=over=item * a C<SCALAR>the test is skipped if the scalar has a false value. For example: plan tests => 5, 0;But this won't hint the reason for skipping therefore it's better touse need(): plan tests => 5, need 'LWP', { "not Win32" => sub { $^O eq 'MSWin32'} };see C<need()> for more info.=item * an C<ARRAY> referenceneed_module() is called for each value in this array. The test isskipped if need_module() returns false (which happens when at leastone C or Perl module from the list cannot be found).=item * a C<CODE> referencethe tests will be skipped if the function returns a false value. Forexample: plan tests => 5, need_lwp;the test will be skipped if LWP is not available=backAll other arguments are passed through to I<Test::plan> as is.=item okSame as I<Test::ok>, see I<Test.pm> documentation.=item sokAllows to skip a sub-test, controlled from the command line. Theargument to sok() is a CODE reference or a BLOCK whose return valuewill be passed to ok(). By default behaves like ok(). If all sub-testsof the same test are written using sok(), and a test is executed as: % ./t/TEST -v skip_subtest 1 3only sub-tests 1 and 3 will be run, the rest will be skipped.=item skipSame as I<Test::skip>, see I<Test.pm> documentation.=item test_pm_refreshNormally called by I<Apache::Test::plan>, this function will refreshthe global state maintained by I<Test.pm>, allowing C<plan> andfriends to be called more than once per-process. This function is notexported.=backFunctions that can be used as a last argument to the extended plan().Note that for each C<need_*> function there is a C<have_*> equivalentthat performs the exact same function except that it is designed tobe used outside of C<plan()>. C<need_*> functions have the side effectof generating skip messages, if the test is skipped. C<have_*> functionsdon't have this side effect. In other words, use C<need_apache()>with C<plan()> to decide whether a test will run, but C<have_apache()>within test logic to adjust expectations based on older or newerserver versions.=over =item need_http11 plan tests => 5, need_http11;Require HTTP/1.1 support.=item need_ssl plan tests => 5, need_ssl;Require SSL support.Not exported by default.=item need_lwp plan tests => 5, need_lwp;Require LWP support.=item need_cgi plan tests => 5, need_cgi;Requires mod_cgi or mod_cgid to be installed.=item need_php plan tests => 5, need_php;Requires a PHP module to be installed (version 4 or 5).=item need_php4 plan tests => 5, need_php4;Requires a PHP version 4 module to be installed.=item need_apache plan tests => 5, need_apache 2;Requires Apache 2nd generation httpd-2.x.xx plan tests => 5, need_apache 1;Requires Apache 1st generation (apache-1.3.xx)See also C<need_min_apache_version()>.=item need_min_apache_versionUsed to require a minimum version of Apache.For example: plan tests => 5, need_min_apache_version("2.0.40");requires Apache 2.0.40 or higher.=item need_apache_versionUsed to require a specific version of Apache.For example: plan tests => 5, need_apache_version("2.0.40");requires Apache 2.0.40.=item need_apache_mpmUsed to require a specific Apache Multi-Processing Module.For example: plan tests => 5, need_apache_mpm('prefork');requires the prefork MPM.=item need_perl plan tests => 5, need_perl 'iolayers'; plan tests => 5, need_perl 'ithreads';Requires a perl extension to be present, or perl compiled with certaincapabilities.The first example tests whether C<PerlIO> is available, the secondwhether: $Config{useithread} eq 'define';=item need_min_perl_versionUsed to require a minimum version of Perl.For example: plan tests => 5, need_min_perl_version("5.008001");requires Perl 5.8.1 or higher.=item need_module plan tests => 5, need_module 'CGI'; plan tests => 5, need_module qw(CGI Find::File); plan tests => 5, need_module ['CGI', 'Find::File', 'cgid'];Requires Apache C and Perl modules. The function accept a list ofarguments or a reference to a list.In case of C modules, depending on how the module name was passed itmay pass through the following completions:=over=item 1 need_module 'proxy_http.c'If there is the I<.c> extension, the module name will be looked up asis, i.e. I<'proxy_http.c'>.=item 2 need_module 'mod_cgi'The I<.c> extension will be appended before the lookup, turning it intoI<'mod_cgi.c'>.=item 3 need_module 'cgi'The I<.c> extension and I<mod_> prefix will be added before thelookup, turning it into I<'mod_cgi.c'>.=back=item need_min_module_versionUsed to require a minimum version of a moduleFor example: plan tests => 5, need_min_module_version(CGI => 2.81);requires C<CGI.pm> version 2.81 or higher.Currently works only for perl modules.=item need plan tests => 5, need 'LWP', { "perl >= 5.8.0 and w/ithreads is required" => ($Config{useperlio} && $] >= 5.008) }, { "not Win32" => sub { $^O eq 'MSWin32' }, "foo is disabled" => \&is_foo_enabled, }, 'cgid';need() is more generic function which can impose multiple requirementsat once. All requirements must be satisfied.need()'s argument is a list of things to test. The list can includescalars, which are passed to need_module(), and hash references. Ifhash references are used, the keys, are strings, containing a reasonfor a failure to satisfy this particular entry, the values are thecondition, which are satisfaction if they return true. If the value is0 or 1, it used to decide whether the requirements very satisfied, soyou can mix special C<need_*()> functions that return 0 or 1. Forexample: plan tests => 1, need 'Compress::Zlib', 'deflate', need_min_apache_version("2.0.49");If the scalar value is a string, different from 0 or 1, it's passed toI<need_module()>. If the value is a code reference, it gets executedat the time of check and its return value is used to check thecondition. If the condition check fails, the provided (in a key)reason is used to tell user why the test was skipped.In the presented example, we require the presence of the C<LWP> Perlmodule, C<mod_cgid>, that we run under perl E<gt>= 5.7.3 on Win32.It's possible to put more than one requirement into a single hashreference, but be careful that the keys will be different.It's also important to mention to avoid using: plan tests => 1, requirement1 && requirement2;technique. While test-wise that technique is equivalent to: plan tests => 1, need requirement1, requirement2;since the test will be skipped, unless all the rules are satisfied,it's not equivalent for the end users. The second technique, deployingC<need()> and a list of requirements, always runs all the requirementchecks and reports all the missing requirements. In the case of thefirst technique, if the first requirement fails, the second is notrun, and the missing requirement is not reported. So let's say all therequirements are missing Apache modules, and a user wants to satisfyall of these and run the test suite again. If all the unsatisfiedrequirements are reported at once, she will need to rebuild Apacheonce. If only one requirement is reported at a time, she will have torebuild Apache as many times as there are elements in the C<&&>statement.Also see plan().=item under_construction plan tests => 5, under_construction;skip all tests, noting that the tests are under construction=item skip_reason plan tests => 5, skip_reason('my custom reason');skip all tests. the reason you specify will be given at runtime.if no reason is given a default reason will be used.=back=head1 Additional Configuration Variables=item basic_config my $basic_cfg = Apache::Test::basic_config(); $basic_cfg->write_perlscript($file, $content);C<basic_config()> is similar to C<config()>, but doesn't contain anyhttpd-specific information and should be used for operations thatdon't require any httpd-specific knowledge.=item config my $cfg = Apache::Test::config(); my $server_rev = $cfg->{server}->{rev}; ...C<config()> gives an access to the configuration object.=item vars my $serverroot = Apache::Test::vars->{serverroot}; my $serverroot = Apache::Test::vars('serverroot'); my($top_dir, $t_dir) = Apache::Test::vars(qw(top_dir t_dir));C<vars()> gives an access to the configuration variables, otherwiseaccessible as: $vars = Apache::Test::config()->{vars};If no arguments are passed, the reference to the variables hash isreturned. If one or more arguments are passed the corresponding valuesare returned.=head1 Test::More IntegrationThere are a few caveats if you want to use I<Apache::Test> with I<Test::More> instead of the default I<Test> backend. The first isthat I<Test::More> requires you to use its own C<plan()> functionand not the one that ships with I<Apache::Test>. I<Test::More> alsodefines C<ok()> and C<skip()> functions that are different, and simply C<use>ing both modules in your test script will lead to redefinedwarnings for these subroutines.To assist I<Test::More> users we have created a special I<Apache::Test>import tag, C<:withtestmore>, which will export all of the standardI<Apache::Test> symbols into your namespace except the ones that collidewith I<Test::More>. use Apache::Test qw(:withtestmore); use Test::More; plan tests => 1; # Test::More::plan() ok ('yes', 'testing ok'); # Test::More::ok()Now, while this works fine for standard client-side tests (such as C<t/basic.t>), the more advanced features of I<Apache::Test>require using I<Test::More> as the sole driver behind the scenes.Should you choose to use I<Test::More> as the backend forserver-based tests (such as C<t/response/TestMe/basic.pm>) you willneed to use the C<-withtestmore> action tag: use Apache::Test qw(-withtestmore); sub handler { my $r = shift; plan $r, tests => 1; # Test::More::plan() with # Apache::Test features ok ('yes', 'testing ok'); # Test::More::ok() }C<-withtestmore> tells I<Apache::Test> to use I<Test::More>instead of I<Test.pm> behind the scenes. Note that you are notrequired to C<use Test::More> yourself with the C<-withtestmore>option and that the C<use Test::More tests =E<gt> 1> syntaxmay have unexpected results. Note that I<Test::More> version 0.49, available within theI<Test::Simple> 0.49 distribution on CPAN, or greater is requiredto use this feature.Because I<Apache:Test> was initially developed using I<Test> asthe framework driver, complete I<Test::More> integration isconsidered experimental at this time - it is supported as best aspossible but is not guaranteed to be as stable as the default I<Test>interface at this time.=head1 Apache::TestToString ClassThe I<Apache::TestToString> class is used to capture I<Test.pm> outputinto a string. Example: Apache::TestToString->start; plan tests => 4; ok $data eq 'foo'; ... # $tests will contain the Test.pm output: 1..4\nok 1\n... my $tests = Apache::TestToString->finish;=head1 SEE ALSOThe Apache-Test tutorial:L<http://perl.apache.org/docs/general/testing/testing.html>.L<Apache::TestRequest|Apache::TestRequest> subclasses LWP::UserAgent andexports a number of useful functions for sending request to the Apache testserver. You can then test the results of those requests.Use L<Apache::TestMM|Apache::TestMM> in your F<Makefile.PL> to set up yourdistribution for testing.=head1 AUTHORDoug MacEachern with contributions from Geoffrey Young, PhilippeM. Chiasson, Stas Bekman and others.Questions can be asked at the test-dev <at> httpd.apache.org listFor more information see: http://httpd.apache.org/test/.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -