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::TestCase
  • Test::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