Skip to content

Instantly share code, notes, and snippets.

@willsheppard
Last active September 20, 2023 11:28
Show Gist options
  • Save willsheppard/e78fad4cb1156b1be45c2ab97d6601e0 to your computer and use it in GitHub Desktop.
Save willsheppard/e78fad4cb1156b1be45c2ab97d6601e0 to your computer and use it in GitHub Desktop.
Test2 cheat sheet for Perl

Links

Docs entry point

  • Test2::Tools::Compare
    • is like isnt unlike
    • match mismatch validator
    • hash array bag object meta number float rounded within string subset bool
    • in_set not_in_set check_set
    • item field call call_list call_hash prop check all_items all_keys all_vals all_values
    • etc end filter_items
    • T F D DF E DNE FDNE U L
    • event fail_events
    • exact_ref

Summary

=head2 SYNOPSIS

use Test2::V0;

# Match regex in a hash
like( $some_hash, hash {                        # <-- Must use `like` keyword to make regex below work
    field 'message' => qr/Caught exception/;    # <-- Note `field` keyword and trailing semicolon. Quotes around key optional
    end();                                      # <-- Enforce that no other keys are present, optional
}, 'Logged error correctly');                   # <-- Test description and parentheses () are optional

Comparisons

is(
    {
        a => 1,
        b => 'foo',
    },
    {
        a => D(),   # value is Defined
        b => E(),   # value Exists
        c => DNE(), # key/value Does Not Exist
    },
    'I can haz Test2?'
);

Todo - legacy

use Test::More;

TODO: {
    local $TODO = 'Still working on this';
    ok(0, "work in progress");
}

Todo2

use Test2::Tools::Tiny qw/todo/;

todo 'Still working on this' => sub {
    ok(0, "failing test gonna fail");
}; # <--- remember the semicolon!

See t/regression/todo_and_facets.t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment