GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Test2::Manual::Tooling::Plugin::TestExit(3) User Contributed Perl Documentation Test2::Manual::Tooling::Plugin::TestExit(3)

Test2::Manual::Tooling::Plugin::TestExit - How to safely add pre-exit behaviors.

This describes the correct/safe way to add pre-exit behaviors to tests via a custom plugin.

The naive way to attempt this would be to add an "END { ... }" block. That can work, and may not cause problems.... On the other hand there are a lot of ways that can bite you. Describing all the potential problems of an END block, and how it might conflict with Test2 (Which has its own END block) is beyond the scope of this document.

    package Test2::Plugin::MyPlugin;

    use Test2::API qw{test2_add_callback_exit};

    sub import {
        my $class = shift;

        test2_add_callback_exit(sub {
            my ($ctx, $orig_code, $new_exit_code_ref) = @_;

            return if $orig_code == 42;

            $$new_exit_code_ref = 42;
        });
    }

    1;

use Test2::API qw{test2_add_callback_exit};
This imports the "(test2_add_callback_exit)" callback.
test2_add_callback_exit(sub { ... });
This adds our callback to be called before exiting.
my ($ctx, $orig_code, $new_exit_code_ref) = @_
The callback gets 3 arguments. First is a context object you may use. The second is the original exit code of the "END" block Test2 is using. The third argument is a scalar reference which you may use to get the current exit code, or set a new one.
return if $orig_code == 42
This is a short-cut to do nothing if the original exit code was already 42.
$$new_exit_code_ref = 42
This changes the exit code to 42.

Test2::Manual - Primary index of the manual.

The source code repository for Test2-Manual can be found at https://github.com/Test-More/Test2-Suite/.

Chad Granum <exodist@cpan.org>

Chad Granum <exodist@cpan.org>

Copyright 2018 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/

2022-03-04 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.