Test::...odeRef(3pm)

πŸ’– Test::Unit::Assertion::CodeRef β€” the delayed-evaluation assertion glow-up ✨

okay bestie, sit down, hydrate, because we're talking about Test::Unit::Assertion::CodeRef, a lil module from the Test::Unit family, straight out of the User Contributed Perl Documentation collection. it's giving "assertions but functional" and honestly? iconic. πŸŽ€


🌸 NAME 🌸

Test::Unit::Assertion::CodeRef β€” a delayed evaluation assertion that lives inside a Coderef. yes queen, your assertion doesn't run right now, it waits, patiently, like a good little bestie, and then it POUNCES. πŸ¦‹


✨ SYNOPSIS ✨

here's the vibe, straight up:

require Test::Unit::Assertion::CodeRef;

my $assert_eq =
  Test::Unit::Assertion::CodeRef->new(sub {
    $_[0] eq $_[1]
      or Test::Unit::Failure->throw(-text =>
                                      "Expected '$_[0]', got '$_[1]'\n");
  });

$assert_eq->do_assertion('foo', 'bar');

so you make a CodeRef object, you hand it a sub, and then you just… do_assertion('foo', 'bar') and it goes off. periodt. πŸ’…

now, lowkey important: even though that's how you'd use Test::Unit::Assertion::CodeRef directly, most of the time you're gonna touch it indirectly through Test::Unit::Test::assert, because that method instantiates a Test::Unit::Assertion::CodeRef for you whenever you pass it a Coderef as its first argument. it's the wingman of assertions. 🀝


πŸ”₯ IMPLEMENTS πŸ”₯

Test::Unit::Assertion::CodeRef implements the Test::Unit::Assertion interface, which means it can be plugged into the Test::Unit::TestCase and friends' "assert" method with no ill effects. zero drama, zero chaos, just vibes. it slots in like it was always meant to be there. πŸ‘‘


🫢 DESCRIPTION 🫢

so this class is what the framework uses to let us do assertions in a 'functional' manner. it's typically used generated automagically in code like:

$self->assert(sub {
                $_[0] == $_[1]
                  or $self->fail("Expected $_[0], got $_[1]");
              }, 1, 2);

like, the framework literally just whips one of these up for you. no cap.

🧚 and uhh, note that if Damian Conway's Perl6 RFC for currying ever comes to pass, then we'll be able to do it like THIS: perl $self->assert(^1 == ^2 || $self->fail("Expected ^1, got ^2"), 1, 2) which will be nice… 😭 manifesting fr fr

and okay one more little gem: if you have a working B::Deparse installed with your perl installation, then when an assertion fails you'll see a listing of the decompiled coderef. it will be sadly devoid of comments (crying, shaking, throwing up 😭) but it should still be useful. still ate, honestly. πŸ’€


πŸ’œ 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

and that's the whole page bestie, no skips, no cuts. 🫢 this ran on perl v5.40.1, dated 2026-04-05, and that's the tea. sipping, signing off, mwah πŸ’…βœ¨πŸŒˆ

πŸ“¦ where this page comes from