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::Delta is 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 $got structure. 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. undef will only be an argument if undef was 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 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