|
NAMETest::Regexp - Test your regular expressionsSYNOPSISuse Test::Regexp 'no_plan'; match subject => "Foo", pattern => qr /\w+/; match subject => "Foo bar", keep_pattern => qr /(?<first_word>\w+)\s+(\w+)/, captures => [[first_word => 'Foo'], ['bar']]; no_match subject => "Baz", pattern => qr /Quux/; $checker = Test::Regexp -> new -> init ( keep_pattern => qr /(\w+)\s+\g{-1}/, name => "Double word matcher", ); $checker -> match ("foo foo", ["foo"]); $checker -> no_match ("foo bar"); DESCRIPTIONThis module is intended to test your regular expressions. Given a subject string and a regular expression (aka pattern), the module not only tests whether the regular expression complete matches the subject string, it performs a "utf8::upgrade" or "utf8::downgrade" on the subject string and performs the tests again, if necessary. Furthermore, given a pattern with capturing parenthesis, it checks whether all captures are present, and in the right order. Both named and unnamed captures are checked.By default, the module exports two subroutines, "match" and "no_match". The latter is actually a thin wrapper around "match", calling it with "match => 0". "Complete matching"A match is only considered to successfully match if the entire string is matched - that is, if $& matches the subject string. So:Subject Pattern "aaabb" qr /a+b+/ # Considered ok "aaabb" qr /a+/ # Not considered ok For efficiency reasons, when the matching is performed the pattern is actually anchored at the start. It's not anchored at the end as that would potentially influence the matching. UTF8 matchingCertain regular expression constructs match differently depending on whether UTF8 matching is in effect or not. This is only relevant if the subject string has characters with code points between 128 and 255, and no characters above 255 -- in such a case, matching may be different depending on whether the subject string has the UTF8 flag on or not. "Test::Regexp" detects such a case, and will then run the tests twice; once with the subject string "utf8::downgraded", and once with the subject string "utf8::upgraded".Number of testsThere's no fixed number of tests that is run. The number of tests depends on the number of captures, the number of different names of captures, and whether there is the need to up- or downgrade the subject string.It is therefore recommended to use "use Text::Regexp tests => 'no_plan';". In a later version, "Test::Regexp" will use a version of "Test::Builder" that allows for nested tests. Details The number of tests is as follows: If no match is expected ("no_match => 0", or "no_match" is used), only one test is performed. Otherwise (we are expecting a match), if "pattern" is used, there will be three tests. For "keep_pattern", there will be four tests, plus one tests for each capture, an additional test for each named capture, and a test for each name used in the set of named captures. So, if there are "N" captures, there will be at least "4 + N" tests, and at most "4 + 3 * N" tests. If both "pattern" and "keep_pattern" are used, the number of tests add up. If "Test::Regexp" decides to upgrade or downgrade, the number of tests double. "use" optionsWhen using "Test::Regexp", there are a few options you can give it.
"match"The subroutine "match" is the workhorse of the module. It takes a number of named arguments, most of them optional, and runs one or more tests. It returns 1 if all tests were run successfully, and 0 if one or more tests failed. The following options are available:
"no_match"Similar to "match", except that it tests whether a pattern does not match a string. Accepts the same arguments as "match", except for "match".OO interfaceSince one typically checks a pattern with multiple strings, and it can be tiresome to repeatedly call "match" or "no_match" with the same arguments, there's also an OO interface. Using a pattern, one constructs an object and can then repeatedly call the object to match a string.To construct and initialize the object, call the following: my $checker = Test::Regexp -> new -> init ( pattern => qr /PATTERN/, keep_pattern => qr /(PATTERN)/, ... ); "init" takes exactly the same arguments as "match", with the exception of "subject" and "captures". To perform a match, all "match" (or "no_match") on the object. The first argument should be the subject the pattern should match against (see the "subject" argument of "match" discussed above). If there is a match against a capturing pattern, the second argument is a reference to an array with the matches (see the "captures" argument of "match" discussed above). Both "match" and "no_match" can take additional (named) arguments, identical to the none-OO "match" and "no_match" routines. RATIONALEThe reason "Test::Regexp" was created is to aid testing for the rewrite of "Regexp::Common".DEVELOPMENTThe current sources of this module are found on github, <git://github.com/Abigail/Test-Regexp.git>.AUTHORAbigail <mailto:test-regexp@abigail.be>.COPYRIGHT and LICENSECopyright (C) 2009 by AbigailPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. INSTALLATIONTo install this module, run, after unpacking the tar-ball, the following commands:perl Makefile.PL make make test make install
Visit the GSP FreeBSD Man Page Interface. |