<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test2::Tools::Spec;
use strict;
use warnings;

our $VERSION = '0.000162';

use Carp qw/croak/;
use Test2::Workflow qw/parse_args build current_build root_build init_root build_stack/;

use Test2::API qw/test2_add_callback_testing_done/;

use Test2::Workflow::Runner();
use Test2::Workflow::Task::Action();
use Test2::Workflow::Task::Group();
use Test2::Tools::Mock();
use Test2::Util::Importer();

use vars qw/@EXPORT @EXPORT_OK/;
push @EXPORT =&gt; qw{describe cases};
push @EXPORT_OK =&gt; qw{include_workflow include_workflows spec_defaults};

my %HANDLED;
sub import {
    my $class = shift;
    my @caller = caller(0);

    my %root_args;
    my %runner_args;
    my @import;
    while (my $arg = shift @_) {
        if ($arg =~ s/^-//) {
            my $val = shift @_;

            if (Test2::Workflow::Runner-&gt;can($arg)) {
                $runner_args{$arg} = $val;
            }
            elsif (Test2::Workflow::Task::Group-&gt;can($arg)) {
                $root_args{$arg} = $val;
            }
            elsif ($arg eq 'root_args') {
                %root_args = (%root_args, %$val);
            }
            elsif ($arg eq 'runner_args') {
                %runner_args = (%runner_args, %$val);
            }
            else {
                croak "Unrecognized arg: $arg";
            }
        }
        else {
            push @import =&gt; $arg;
        }
    }

    if ($HANDLED{$caller[0]}++) {
        croak "Package $caller[0] has already been initialized"
            if keys(%root_args) || keys(%runner_args);
    }
    else {
        my $root = init_root(
            $caller[0],
            frame =&gt; \@caller,
            code =&gt; sub { 1 },
            %root_args,
        );

        my $runner = Test2::Workflow::Runner-&gt;new(%runner_args);

        Test2::Tools::Mock-&gt;add_handler(
            $caller[0],
            sub {
                my %params = @_;
                my ($class, $caller, $builder, $args) = @params{qw/class caller builder args/};

                my $do_it = eval "package $caller-&gt;[0];\n#line $caller-&gt;[2] \"$caller-&gt;[1]\"\nsub { \$runner\-&gt;add_mock(\$builder-&gt;()) }";

                # Running
                if (@{$runner-&gt;stack}) {
                    $do_it-&gt;();
                }
                else { # Not running
                    my $action = Test2::Workflow::Task::Action-&gt;new(
                        code     =&gt; $do_it,
                        name     =&gt; "mock $class",
                        frame    =&gt; $caller,
                        scaffold =&gt; 1,
                    );

                    my $build = current_build() || $root;

                    $build-&gt;add_primary_setup($action);
                    $build-&gt;add_stash($builder-&gt;()) unless $build-&gt;is_root;
                }

                return 1;
            }
        );

        test2_add_callback_testing_done(
            sub {
                return unless $root-&gt;populated;
                my $g = $root-&gt;compile;
                $runner-&gt;push_task($g);
                $runner-&gt;run;
            }
        );
    }

    Test2::Util::Importer-&gt;import_into($class, $caller[0], @import);
}

{
    no warnings 'once';
    *cases             = \&amp;describe;
    *include_workflows = \&amp;include_workflow;
}

sub describe {
    my @caller = caller(0);

    my $want = wantarray;

    my $build = build(args =&gt; \@_, caller =&gt; \@caller, stack_stop =&gt; defined $want ? 1 : 0);

    return $build if defined $want;

    my $current = current_build() || root_build($caller[0])
        or croak "No current workflow build!";

    $current-&gt;add_primary($build);
}

sub include_workflow {
    my @caller = caller(0);

    my $build = current_build() || root_build(\$caller[0])
        or croak "No current workflow build!";

    for my $task (@_) {
        croak "include_workflow only accepts Test2::Workflow::Task objects, got: $task"
            unless $task-&gt;isa('Test2::Workflow::Task');

        $build-&gt;add_primary($task);
    }
}

sub defaults {
    my %params = @_;

    my ($package, $tool) = @params{qw/package tool/};

    my @stack = (root_build($package), build_stack());
    return unless @stack;

    my %out;
    for my $build (@stack) {
        %out = () if $build-&gt;stack_stop;
        my $new = $build-&gt;defaults-&gt;{$tool} or next;
        %out = (%out, %$new);
    }

    return \%out;
}


# Generate a bunch of subs that only have minor differences between them.
BEGIN {
    @EXPORT = qw{
        tests it
        case
        before_all  around_all  after_all
        before_case around_case after_case
        before_each around_each after_each
    };

    @EXPORT_OK = qw{
        mini
        iso   miso
        async masync
    };

    my %stages = (
        case  =&gt; ['add_variant'],
        tests =&gt; ['add_primary'],
        it    =&gt; ['add_primary'],

        iso  =&gt; ['add_primary'],
        miso =&gt; ['add_primary'],

        async  =&gt; ['add_primary'],
        masync =&gt; ['add_primary'],

        mini =&gt; ['add_primary'],

        before_all =&gt; ['add_setup'],
        after_all  =&gt; ['add_teardown'],
        around_all =&gt; ['add_setup', 'add_teardown'],

        before_case =&gt; ['add_variant_setup'],
        after_case  =&gt; ['add_variant_teardown'],
        around_case =&gt; ['add_variant_setup', 'add_variant_teardown'],

        before_each =&gt; ['add_primary_setup'],
        after_each  =&gt; ['add_primary_teardown'],
        around_each =&gt; ['add_primary_setup', 'add_primary_teardown'],
    );

    my %props = (
        case  =&gt; [],
        tests =&gt; [],
        it    =&gt; [],

        iso  =&gt; [iso =&gt; 1],
        miso =&gt; [iso =&gt; 1, flat =&gt; 1],

        async  =&gt; [async =&gt; 1],
        masync =&gt; [async =&gt; 1, flat =&gt; 1],

        mini =&gt; [flat =&gt; 1],

        before_all =&gt; [scaffold =&gt; 1],
        after_all  =&gt; [scaffold =&gt; 1],
        around_all =&gt; [scaffold =&gt; 1, around =&gt; 1],

        before_case =&gt; [scaffold =&gt; 1],
        after_case  =&gt; [scaffold =&gt; 1],
        around_case =&gt; [scaffold =&gt; 1, around =&gt; 1],

        before_each =&gt; [scaffold =&gt; 1],
        after_each  =&gt; [scaffold =&gt; 1],
        around_each =&gt; [scaffold =&gt; 1, around =&gt; 1],
    );

    sub spec_defaults {
        my ($tool, %params) = @_;
        my @caller = caller(0);

        croak "'$tool' is not a spec tool"
            unless exists $props{$tool} || exists $stages{$tool};

        my $build = current_build() || root_build($caller[0])
            or croak "No current workflow build!";

        my $old = $build-&gt;defaults-&gt;{$tool} ||= {};
        $build-&gt;defaults-&gt;{$tool} = { %$old, %params };
    }

    my $run = "";
    for my $func (@EXPORT, @EXPORT_OK) {
        $run .= &lt;&lt;"        EOT";
#line ${ \(__LINE__ + 1) } "${ \__FILE__ }"
sub $func {
    my \@caller = caller(0);
    my \$args = parse_args(args =&gt; \\\@_, caller =&gt; \\\@caller);
    my \$action = Test2::Workflow::Task::Action-&gt;new(\@{\$props{$func}}, %\$args);

    return \$action if defined wantarray;

    my \$build = current_build() || root_build(\$caller[0])
        or croak "No current workflow build!";

    if (my \$defaults = defaults(package =&gt; \$caller[0], tool =&gt; '$func')) {
        for my \$attr (keys \%\$defaults) {
            next if defined \$action-&gt;\$attr;
            my \$sub = "set_\$attr";
            \$action-&gt;\$sub(\$defaults-&gt;{\$attr});
        }
    }

    \$build-&gt;\$_(\$action) for \@{\$stages{$func}};
}
        EOT
    }

    my ($ok, $err);
    {
        local $@;
        $ok = eval "$run\n1";
        $err = $@;
    }

    die $@ unless $ok;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Test2::Tools::Spec - RSPEC implementation on top of Test2::Workflow

=head1 DESCRIPTION

This uses L&lt;Test2::Workflow&gt; to implement an RSPEC variant. This variant
supports isolation and/or concurrency via forking or threads.

=head1 SYNOPSIS

    use Test2::Bundle::Extended;
    use Test2::Tools::Spec;

    describe foo =&gt; sub {
        before_all  once =&gt; sub { ... };
        before_each many =&gt; sub { ... };

        after_all  once =&gt; sub { ... };
        after_each many =&gt; sub { ... };

        case condition_a =&gt; sub { ... };
        case condition_b =&gt; sub { ... };

        tests foo =&gt; sub { ... };
        tests bar =&gt; sub { ... };
    };

    done_testing;

=head1 EXPORTS

All of these use the same argument pattern. The first argument must always be a
name for the block. The last argument must always be a code reference.
Optionally a configuration hash can be inserted between the name and the code
reference.

    FUNCTION "name" =&gt; sub { ... };

    FUNCTION "name" =&gt; {...}, sub { ... };

=over 4

=item NAME

The first argument to a Test2::Tools::Spec function MUST be a name. The name
does not need to be unique.

=item PARAMS

This argument is optional. If present this should be a hashref.

Here are the valid keys for the hashref:

=over 8

=item flat =&gt; $bool

If this is set to true then the block will not render as a subtest, instead the
events will be inline with the parent subtest (or main test).

=item async =&gt; $bool

Set this to true to mark a block as being capable of running concurrently with
other test blocks. This does not mean the block WILL be run concurrently, just
that it can be.

=item iso =&gt; $bool

Set this to true if the block MUST be run in isolation. If this is true then
the block will run in its own forked process.

These tests will be skipped on any platform that does not have true forking, or
working/enabled threads.

Threads will ONLY be used if the T2_WORKFLOW_USE_THREADS env var is set. Thread
tests are only run if the T2_DO_THREAD_TESTS env var is set.

=item todo =&gt; $reason

Use this to mark an entire block as TODO.

=item skip =&gt; $reason

Use this to prevent a block from running at all.

=back

=item CODEREF

This argument is required. This should be a code reference that will run some
assertions.

=back

=head2 ESSENTIALS

=over 4

=item tests NAME =&gt; sub { ... }

=item tests NAME =&gt; \%params, sub { ... }

=item tests($NAME, \%PARAMS, \&amp;CODE)

=item it NAME =&gt; sub { ... }

=item it NAME =&gt; \%params, sub { ... }

=item it($NAME, \%PARAMS, \&amp;CODE)

This defines a test block. Test blocks are essentially subtests. All test
blocks will be run, and are expected to produce events. Test blocks can run
multiple times if the C&lt;case()&gt; function is also used.

C&lt;it()&gt; is an alias to C&lt;tests()&gt;.

These ARE NOT inherited by nested describe blocks.

=item case NAME =&gt; sub { ... }

=item case NAME =&gt; \%params, sub { ... }

=item case($NAME, \%PARAMS, \&amp;CODE)

This lets you specify multiple conditions in which the test blocks should be
run. Every test block within the same group (C&lt;describe&gt;) will be run once per
case.

These ARE NOT inherited by nested describe blocks, but nested describe blocks
will be executed once per case.

=item before_each NAME =&gt; sub { ... }

=item before_each NAME =&gt; \%params, sub { ... }

=item before_each($NAME, \%PARAMS, \&amp;CODE)

Specify a codeblock that should be run multiple times, once before each
C&lt;tests()&gt; block is run. These will run AFTER C&lt;case()&gt; blocks but before
C&lt;tests()&gt; blocks.

These ARE inherited by nested describe blocks.

=item before_case NAME =&gt; sub { ... }

=item before_case NAME =&gt; \%params, sub { ... }

=item before_case($NAME, \%PARAMS, \&amp;CODE)

Same as C&lt;before_each()&gt;, except these blocks run BEFORE C&lt;case()&gt; blocks.

These ARE NOT inherited by nested describe blocks.

=item before_all NAME =&gt; sub { ... }

=item before_all NAME =&gt; \%params, sub { ... }

=item before_all($NAME, \%PARAMS, \&amp;CODE)

Specify a codeblock that should be run once, before all the test blocks run.

These ARE NOT inherited by nested describe blocks.

=item around_each NAME =&gt; sub { ... }

=item around_each NAME =&gt; \%params, sub { ... }

=item around_each($NAME, \%PARAMS, \&amp;CODE)

Specify a codeblock that should wrap around each test block. These blocks are
run AFTER case blocks, but before test blocks.

    around_each wrapit =&gt; sub {
        my $cont = shift;

        local %ENV = ( ... );

        $cont-&gt;();

        ...
    };

The first argument to the codeblock will be a callback that MUST be called
somewhere inside the sub in order for nested items to run.

These ARE inherited by nested describe blocks.

=item around_case NAME =&gt; sub { ... }

=item around_case NAME =&gt; \%params, sub { ... }

=item around_case($NAME, \%PARAMS, \&amp;CODE)

Same as C&lt;around_each&gt; except these run BEFORE case blocks.

These ARE NOT inherited by nested describe blocks.

=item around_all NAME =&gt; sub { ... }

=item around_all NAME =&gt; \%params, sub { ... }

=item around_all($NAME, \%PARAMS, \&amp;CODE)

Same as C&lt;around_each&gt; except that it only runs once to wrap ALL test blocks.

These ARE NOT inherited by nested describe blocks.

=item after_each NAME =&gt; sub { ... }

=item after_each NAME =&gt; \%params, sub { ... }

=item after_each($NAME, \%PARAMS, \&amp;CODE)

Same as C&lt;before_each&gt; except it runs right after each test block.

These ARE inherited by nested describe blocks.

=item after_case NAME =&gt; sub { ... }

=item after_case NAME =&gt; \%params, sub { ... }

=item after_case($NAME, \%PARAMS, \&amp;CODE)

Same as C&lt;after_each&gt; except it runs right after the case block, and before the
test block.

These ARE NOT inherited by nested describe blocks.

=item after_all NAME =&gt; sub { ... }

=item after_all NAME =&gt; \%params, sub { ... }

=item after_all($NAME, \%PARAMS, \&amp;CODE)

Same as C&lt;before_all&gt; except it runs after all test blocks have been run.

These ARE NOT inherited by nested describe blocks.

=back

=head2 SHORTCUTS

These are shortcuts. Each of these is the same as C&lt;tests()&gt; except some
parameters are added for you.

These are NOT exported by default/.

=over 4

=item mini NAME =&gt; sub { ... }

Same as:

    tests NAME =&gt; { flat =&gt; 1 }, sub { ... }

=item iso NAME =&gt; sub { ... }

Same as:

    tests NAME =&gt; { iso =&gt; 1 }, sub { ... }

=item miso NAME =&gt; sub { ... }

Same as:

    tests NAME =&gt; { mini =&gt; 1, iso =&gt; 1 }, sub { ... }

=item async NAME =&gt; sub { ... }

Same as:

    tests NAME =&gt; { async =&gt; 1 }, sub { ... }

B&lt;Note:&gt; This conflicts with the C&lt;async()&gt; exported from L&lt;threads&gt;. Don't
import both.

=item masync NAME =&gt; sub { ... }

Same as:

    tests NAME =&gt; { minit =&gt; 1, async =&gt; 1 }, sub { ... }

=back

=head2 CUSTOM ATTRIBUTE DEFAULTS

Sometimes you want to apply default attributes to all C&lt;tests()&gt; or C&lt;case()&gt;
blocks. This can be done, and is lexical to your describe or package root!

    use Test2::Bundle::Extended;
    use Test2::Tools::Spec ':ALL';

    # All 'tests' blocks after this declaration will have C&lt;&lt;iso =&gt; 1&gt;&gt; by default
    spec_defaults tests =&gt; (iso =&gt; 1);

    tests foo =&gt; sub { ... }; # isolated

    tests foo, {iso =&gt; 0}, sub { ... }; # Not isolated

    spec_defaults tests =&gt; (iso =&gt; 0); # Turn it off again

Defaults are inherited by nested describe blocks. You can also override the
defaults for the scope of the describe:

    spec_defaults tests =&gt; (iso =&gt; 1);

    describe foo =&gt; sub {
        spec_defaults tests =&gt; (async =&gt; 1); # Scoped to this describe and any child describes

        tests bar =&gt; sub { ... }; # both iso and async
    };

    tests baz =&gt; sub { ... }; # Just iso, no async.

You can apply defaults to any type of blocks:

    spec_defaults case =&gt; (iso =&gt; 1); # All cases are 'iso';

Defaults are not inherited when a builder's return is captured.

    spec_defaults tests =&gt; (iso =&gt; 1);

    # Note we are not calling this in void context, that is the key here.
    my $d = describe foo =&gt; {
        tests bar =&gt; sub { ... }; # Not iso
    };

=head1 EXECUTION ORDER

As each function is encountered it executes, just like any other function. The
C&lt;describe()&gt; function will immediately execute the codeblock it is given. All
other functions will stash their codeblocks to be run later. When
C&lt;done_testing()&gt; is run the workflow will be compiled, at which point all
other blocks will run.

Here is an overview of the order in which blocks get called once compiled (at
C&lt;done_testing()&gt;).

    before_all
        for-each-case {
            before_case
                case
            after_case

            # AND/OR nested describes
            before_each
                tests
            after_each
        }
    after_all

=head1 SOURCE

The source code repository for Test2-Workflow can be found at
F&lt;https://github.com/Test-More/Test2-Suite/&gt;.

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2018 Chad Granum E&lt;lt&gt;exodist7@gmail.comE&lt;gt&gt;.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F&lt;http://dev.perl.org/licenses/&gt;

=cut

</pre></body></html>