The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Stream::Context - Object to represent a testing context.

DEPRECATED

This distribution is deprecated in favor of Test2, Test2::Suite, and Test2::Workflow.

See Test::Stream::Manual::ToTest2 for a conversion guide.

DESCRIPTION

The context object is the primary interface for authors of testing tools written with Test::Stream. The context object represents the context in which a test takes place (File and Line Number), and provides a quick way to generate events from that context. The context object also takes care of sending events to the correct Test::Stream::Hub instance.

SYNOPSIS

use Test::Stream::Context qw/context release/;

sub my_ok {
    my ($bool, $name) = @_;
    my $ctx = context();
    $ctx->ok($bool, $name);
    $ctx->release; # You MUST do this!
    return $bool;
}

Context objects make it easy to wrap other tools that also use context. Once you grab a context, any tool you call before releasing your context will inherit it:

sub wrapper {
    my ($bool, $name) = @_;
    my $ctx = context();
    $ctx->diag("wrapping my_ok");

    my $out = my_ok($bool, $name);
    $ctx->release; # You MUST do this!
    return $out;
}

Notice above that we are grabbing a return value, then releasing our context, then returning the value. We can combine these last 3 statements into a single statement using the release function:

sub wrapper {
    my ($bool, $name) = @_;
    my $ctx = context();
    $ctx->diag("wrapping my_ok");

    # You must always release the context.
    release $ctx, my_ok($bool, $name);
}

CRITICAL DETAILS

You MUST always release the context when done with it

Releasing the context tells the system you are done with it. This gives it a chance to run any necessary callbacks or cleanup tasks. If you forget to release the context it will be released for you using a destructor, and it will give you a warning.

In general the destructor is not preferred because it does not allow callbacks to run some types of code, for example you cannot throw an exception from a destructor.

You MUST NOT pass context objects around

When you obtain a context object it is made specifically for your tool and any tools nested within. If you pass a context around you run the risk of polluting other tools with incorrect context information.

If you are certain that you want a different tool to use the same context you may pass it a snapshot. $ctx->snapshot will give you a shallow clone of the context that is safe to pass around or store.

You MUST NOT store or cache a context for later

As long as a context exists for a given hub, all tools that try to get a context will get the existing instance. If you try to store the context you will pollute other tools with incorrect context information.

If you are certain that you want to save the context for later, you can use a snapshot. $ctx->snapshot will give you a shallow clone of the context that is safe to pass around or store.

context() has some mechanisms to protect you if you do cause a context to persist beyond the scope in which it was obtained. In practice you should not rely on these protections, and they are fairly noisy with warnings.

You SHOULD obtain your context as soon as possible in a given tool

You never know what tools you call from within your own tool will need a context. Obtaining the context early ensures that nested tools can find the context you want them to find.

EXPORTS

All exports are optional, you must specify subs to import. If you want to import all subs use '-all'.

use Test::Stream::Context '-all';

context()

Usage:

$ctx = context()
$ctx = context(%params)

The context() function will always return the current context to you. If there is already a context active it will be returned. If there is not an active context one will be generated. When a context is generated it will default to using the file and line number where the currently running sub was called from.

Please see the "CRITICAL DETAILS" section for important rools about what you can and acannot do with a context once it is obtained.

Note This function will throw an exception if you ignore the context object it returns.

OPTIONAL PARAMETERS

All parameters to context are optional.

level => $int

If you must obtain a context in a sub deper than your entry point you can use this to tell it how many EXTRA stack frames to look back. If this option is not provided the default of 0 is used.

sub third_party_tool {
    my $sub = shift;
    ... # Does not obtain a context
    $sub->();
    ...
}

third_party_tool(sub {
    my $ctx = context(level => 1);
    ...
    $ctx->release;
});
wrapped => $int

Use this if you need to write your own tool that wraps a call to context() with the intent that it should return a context object.

sub my_context {
    my %params = ( wrapped => 0, @_ );
    $params{wrapped}++;
    my $ctx = context(%params);
    ...
    return $ctx;
}

sub my_tool {
    my $ctx = my_context();
    ...
    $ctx->release;
}

If you do not do this than tools you call that also check for a context will notice that the context they grabbed was created at the same stack depth, which will trigger protective measures that warn you and destroy the existing context.

stack => $stack

Normally context() looks at the global hub stack initialized in Test::Stream::Sync. If you are maintaining your own Test::Stream::Stack instance you may pass it in to be used instead of the global one.

hub => $hub

Use this parameter if you want to onbtain the context for a specific hub instead of whatever one happens to be at the top of the stack.

on_init => sub { ... }

This lets you provide a callback sub that will be called ONLY if your call to c<context()> generated a new context. The callback WILL NOT be called if context() is returning an existing context. The only argument passed into the callback will be the context object itself.

sub foo {
    my $ctx = context(on_init => sub { 'will run' });

    my $inner = sub {
        # This callback is not run since we are getting the existing
        # context from our parent sub.
        my $ctx = context(on_init => sub { 'will NOT run' });
        $ctx->release;
    }
    $inner->();

    $ctx->release;
}
on_release => sub { ... }

This lets you provide a callback sub that will be called when the context instance is released. This callback will be added to the returned context even if an existing context is returned. If multiple calls to context add callbacks then all will be called in reverse order when the context is finally released.

sub foo {
    my $ctx = context(on_release => sub { 'will run second' });

    my $inner = sub {
        my $ctx = context(on_release => sub { 'will run first' });

        # Neither callback runs on this release
        $ctx->release;
    }
    $inner->();

    # Both callbacks run here.
    $ctx->release;
}

release()

Usage:

release $ctx;
release $ctx, ...;

This is intended as a shortcut that lets you release your context and return a value in one statement. This function will get your context, and any other arguments provided. It will release your context, then return everything else. If you only provide one argument it will return that one argument as a scalar. If you provide multiple arguments it will return them all as a list.

sub scalar_tool {
    my $ctx = context();
    ...

    return release $ctx, 1;
}

sub list_tool {
    my $ctx = context();
    ...

    return release $ctx, qw/a b c/;
}

This tool is most useful when you want to return the value you get from calling a function that needs to see the current context:

my $ctx = context();
my $out = some_tool(...);
$ctx->release;
return $out;

We can combine the last 3 lines of the above like so:

my $ctx = context();
release $ctx, some_tool(...);

METHODS

CLASS METHODS

Test::Stream::Context->ON_INIT(sub { ... }, ...)
Test::Stream::Context->ON_RELEASE(sub { ... }, ...)

These are GLOBAL hooks into the context tools. Every sub added via ON_INIT will be called every single time a new context is initialized. Every sub added via ON_RELEASE will be called every single time a context is released.

Subs will receive exactly 1 argument, that is the context itself. You should not call release on the context within your callback.

INSTANCE METHODS

$clone = $ctx->snapshot()

This will return a shallow clone of the context. The shallow clone is safe to store for later.

$ctx->release()

This will release the context. It will also set the $ctx variable to undef (it works regardless of what you name the variable).

$ctx->throw($message)

This will throw an exception reporting to the file and line number of the context. This will also release the context for you.

$ctx->alert($message)

This will issue a warning from the file and line number of the context.

$stack = $ctx->stack()

This will return the Test::Stream::Stack instance the context used to find the current hub.

$hub = $ctx->hub()

This will return the Test::Stream::Hub instance the context recognises as the current one to which all events should be sent.

$dbg = $ctx->debug()

This will return the Test::Stream::DebugInfo instance used by the context.

$ctx->do_in_context(\&code, @args);

Sometimes you have a context that is not current, and you want things to use it as the current one. In these cases you can call "$ctx->do_in_context(sub { ... })". The codeblock will be run, and anything inside of it that looks for a context will find the one on which the method was called.

This DOES NOT effect context on other hubs, only the hub used by the context will be effected.

my $ctx = ...;
$ctx->do_in_context(sub {
    my $ctx = context(); # returns the $ctx the sub is called on
});

EVENT PRODUCTION METHODS

$event = $ctx->ok($bool, $name)
$event = $ctx->ok($bool, $name, \@diag)

This will create an Test::Stream::Event::Ok object for you. The diagnostics array will be used on the object in the event of a failure, if the test passes the diagnostics will be ignored.

$event = $ctx->note($message)

Send an Test::Stream::Event::Note. This event prints a message to STDOUT.

$event = $ctx->diag($message)

Send an Test::Stream::Event::Diag. This event prints a message to STDERR.

$event = $ctx->plan($max)
$event = $ctx->plan(0, 'SKIP', $reason)

This can be used to send an Test::Stream::Event::Plan event. This event usually takes either a number of tests you expect to run. Optionally you can set the expected count to 0 and give the 'SKIP' directive with a reason to cause all tests to be skipped.

$event = $ctx->bail($reason)

This sends an Test::Stream::Event::Bail event. This event will completely terminate all testing.

$event = $ctx->send_event($Type, %parameters)

This lets you build and send an event of any type. The $Type argument should be the event package name with Test::Stream::Event:: left off, or a fully qualified package name prefixed with a '+'. The event is returned after it is sent.

my $event = $ctx->send_event('Ok', ...);

or

my $event = $ctx->send_event('+Test::Stream::Event::Ok', ...);
$event = $ctx->build_event($Type, %parameters)

This is the same as send_event(), except it builds and returns the event without sending it.

HOOKS

There are 2 types of hooks, init hooks, and release hooks. As the names suggest, these hooks are triggered when contexts are created or released.

INIT HOOKS

These are called whenever a context is initialized. That means when a new instance is created. These hooks are NOT called every time something requests a context, just when a new one is created.

GLOBAL

This is how you add a global init callback. Global callbacks happen for every context for any hub or stack.

Test::Stream::Context->ON_INIT(sub {
    my $ctx = shift;
    ...
});

PER HUB

This is how you add an init callback for all contexts created for a given hub. These callbacks will not run for other hubs.

$hub->add_context_init(sub {
    my $ctx = shift;
    ...
});

PER CONTEXT

This is how you specify an init hook that will only run if your call to context() generates a new context. The callback will be ignored if context() is returning an existing context.

my $ctx = context(on_init => sub {
    my $ctx = shift;
    ...
});

RELEASE HOOKS

These are called whenever a context is released. That means when the last reference to the instance is about to be destroyed. These hooks are NOT called every time $ctx->release is called.

GLOBAL

This is how you add a global release callback. Global callbacks happen for every context for any hub or stack.

Test::Stream::Context->ON_RELEASE(sub {
    my $ctx = shift;
    ...
});

PER HUB

This is how you add a release callback for all contexts created for a given hub. These callbacks will not run for other hubs.

$hub->add_context_release(sub {
    my $ctx = shift;
    ...
});

PER CONTEXT

This is how you add release callbacks directly to a context. The callback will ALWAYS be added to the context that gets returned, it does not matter if a new one is generated, or if an existing one is returned.

my $ctx = context(on_release => sub {
    my $ctx = shift;
    ...
});

SOURCE

The source code repository for Test::Stream can be found at http://github.com/Test-More/Test-Stream/.

MAINTAINERS

Chad Granum <exodist@cpan.org>

AUTHORS

Chad Granum <exodist@cpan.org>
Kent Fredric <kentnl@cpan.org>

COPYRIGHT

Copyright 2015 Chad Granum <exodist7@gmail.com>.

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/