Test2:...::Base(3perl)
π Test2::Compare::Base β the glow-up base class π
okay so bestie, welcome to Test2::Compare::Base from the Perl Programmers Reference Guide, and honestly? this one is giving "quiet main character" because literally every other comparison class in the Test2::Compare family is supposed to inherit from it. no cap. it's the blueprint, the origin story, the mom of the whole squad. π
β¨ NAME β¨
Test2::Compare::Base β the base class for comparison classes. periodt.
πΈ DESCRIPTION πΈ
listen up: all comparison classes for Test2::Compare should inherit from this base class. that's the whole vibe check. if you're writing a comparison class and not inheriting from this, bestie, we need to talk. π
π¦ SYNOPSIS π¦
here's the whole template, copy it exactly, don't freestyle the syntax:
package Test2::Compare::MyCheck;
use strict;
use warnings;
use base 'Test2::Compare::Base';
use Test2::Util::HashBase qw/stuff/;
sub name { 'STUFF' }
sub operator {
my $self = shift;
my ($got) = @_;
return 'eq';
}
sub verify {
my $self = shift;
my $params = @_;
# Always check if $got exists! This method must return false if no
# value at all was received.
return 0 unless $params{exists};
my $got = $params{got};
# Returns true if both values match. This includes undef, 0, and other
# false-y values!
return $got eq $self->stuff;
}
see how verify checks $params{exists} first? that's not optional bestie, that's the rule. π₯
π METHODS π
so some of these you must override, and others you can. it's giving "choose your own adventure but the scary ones are mandatory".
$dclass = $check->delta_classβ hands you back the delta subclass that should be used. if you don't say otherwise,Test2::Compare::Deltais the default. lowkey iconic default tbh.@deltas = $check->deltas(id => $id, exists => $bool, got => $got, convert => \&convert, seen => \%seen)β should return child deltas. that's it, that's the job.@lines = $check->got_lines($got)β this is your moment, your chance to provide line numbers for errors inside the$gotstructure. bestie is giving helpful error messages energy. π$op = $check->operator()$op = $check->operator($got)β returns the operator that was used to compare the check with the received data in$got. and here's the β¨ nuance β¨: if there was no value for got, then there will be no arguments at all.undefwill only be an argument ifundefwas actually seen in$got. this is literally how you tell the difference between a missing value and an undefined one. not me being obsessed with this distinction. π$bool = $check->verify(id => $id, exists => $bool, got => $got, convert => \&convert, seen => \%seen)β return true if there's a shallow match, like both items are arrayrefs, both items are the same string or same number, etc. and do not recurse in here β deep checks get handled over in"$check->deltas()". stay in your lane, it's slay.
β οΈ quick warning bestie: this one is a shallow check. no going deeper. the recursion lives elsewhere, iykyk.
$name = $check->nameβ get the name of the check. simple. clean. iconic.$display = $check->renderβ what should be displayed in a table for this check, usually the name or value.$delta = $check->run(id => $id, exists => $bool, got => $got, convert => \&convert, seen => \%seen)β this is where the checking actually goes down. first a shallow check using"$check->verify", then checking"$check->deltas()". and"\%seen"is used to prevent cycles, because we are NOT about to loop forever in this house. π
π SOURCE π
the source code repository for Test2-Suite can be found at:
https://github.com/Test-More/test-more/
go star it, go read it, go lurk, whatever β it's right there.
π MAINTAINERS π
Chad Granum <exodist@cpan.org>
β AUTHORS β
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
and that's the whole page, bestie β written for perl v5.44.0, dated 2026-10-17, filed under Test2:...::Base(3perl). π«Ά go forth and make your comparison classes inherit properly, mwah π
β¨
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND 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