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::TestCaseTest::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
- 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