Test::...:Regexp(3pm)
π NAME π
Test::Unit::Assertion::Regexp β an assertion that does regex matching, bestie. That's it, that's the whole vibe. π
This page lives in the Perl docs, under the manual name Test::...:Regexp(3pm), and yes it's the User Contributed Perl Documentation section, we're just out here contributing, iconic behaviour. π
β¨ SYNOPSIS β¨
So like, if you wanna do it manually (main character energy, do it yourself):
require Test::Unit::Assertion::Regexp;
my $assert_re =
Test::Unit::Assertion::Regexp->new(qr/a_pattern/);
$assert_re->do_assertion('a_string');
Okay but real talk: this is way more detail than the average user needs. Like, genuinely. Most of y'all will never type that. π
Test::Unit::Assertion::Regexp objects get generated automagically by Test::Unit::Assert::assert when you pass it a regular expression as its first parameter. It just happens, no cap:
sub test_foo {
...
$self->assert(qr/some_pattern/, $result);
}
If the assertion fails, the object throws an exception and it comes with deets β the pattern, and the string it failed to match against. So you actually know what went wrong, which is the whole point. πβ¨
And listen β if what you need is a "string does not match this pattern" kinda assertion, you can do this:
$self->assert(qr/(?!some_pattern)/, $some_string)
i.e. make use of the negative lookahead assertion. Sneaky. Clever. Slay. π
π¦ IMPLEMENTS π¦
Test::Unit::Assertion::Regexp implements the Test::Unit::Assertion interface, which means it can be plugged into Test::Unit::TestCase and friends' assert method with no ill effects. Plug and play, baby. π«Ά
πΈ DESCRIPTION πΈ
The class is used by the framework to provide sensible "automatic" reports when a match fails. Because the old way:
$self->assert(scalar($foo =~ /pattern/), "$foo didn't match /.../");
...seems rather clumsy compared to this. It's giving "I hand-wrote my own error message and it was mid." π
If the regexp assertion fails, then the user is given a sensible error message, with the pattern and the string that failed to match it... which is literally the nicest thing a test failure can do for you. π
π AUTHOR π
Copyright (c) 2001 Piers Cawley <pdcawley@iterative-software.com>.
All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π SEE ALSO π
Test::Unit::TestCaseTest::Unit::Assertion
Okay bestie, that's the whole page, we ate and left no crumbs. π This was Test::...:Regexp(3pm) from perl v5.40.1, dated 2026-04-05 β go forth and assert your regexes, mwah π
π§β
π¦ where this page comes from
- ubuntu stonking: libtest-unit-perl 0.30-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms