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

JIP::Spy::Events - the most basic function spy ability

VERSION

This document describes JIP::Spy::Events version v0.0.5.

SYNOPSIS

Testing with Test::More:

    use Test::More;

    use_ok 'JIP::Spy::Events';

    my $spy_events = JIP::Spy::Events->new();

    is_deeply $spy_events->events(), [];
    is_deeply $spy_events->times(), {};

    is $spy_events->foo(),   $spy_events;
    is $spy_events->foo(42), $spy_events;

    is_deeply $spy_events->events(), [
        { method => 'foo', arguments => [] },
        { method => 'foo', arguments => [42] },
    ];

    is_deeply $spy_events->times(), { foo => 2 };

    is $spy_events->clear(), $spy_events;

    is_deeply $spy_events->events(), [];
    is_deeply $spy_events->times(), {};

    done_testing();

Testing with Test::More, and want_array is turning on:

    use Test::More;

    use_ok 'JIP::Spy::Events';

    my $spy_events = JIP::Spy::Events->new( want_array => 1 );

    $spy_events->foo();
    scalar $spy_events->foo();
    ( () = $spy_events->foo() );

    is_deeply $spy_events->events(), [
        { method => 'foo', arguments => [], want_array => undef },
        { method => 'foo', arguments => [], want_array => q{} },
        { method => 'foo', arguments => [], want_array => 1 },
    ];

    done_testing();

FizzBuzz example:

    use Test::More;

    use_ok 'JIP::Spy::Events';

    my $spy_events = JIP::Spy::Events->new();

    $spy_events->on_spy_event(
        just_do_it => sub {
            my ($spy, $event) = @_;

            my $attempt = $event->arguments()->[0];

            return (
                !($attempt % 3)  ? 'Fizz' :
                !($attempt % 5)  ? 'Buzz' :
                $attempt
            );
        },
    );

    my @results = map { $spy_events->just_do_it($_) } 1 .. 5;

    is_deeply \@results, [
        1,
        2,
        'Fizz',
        4,
        'Buzz',
    ];

    done_testing();

ATTRIBUTES

JIP::Spy::Events implements the following attributes.

events

    $arrayref = $spy_events->events();

Returns an array of all the calls to the spied module.

times

    $hashref = $spy_events->times();

Returns a hash where keys are method names, and values are the number of times the method has been called.

want_array

    $bool = $spy_events->want_array(); # undef/1/q{}

Track (or not track) invocation context. Disabled by default.

SUBROUTINES/METHODS

new

    my $spy_events = JIP::Spy::Events->new();

Build new JIP::Spy::Events object.

clear

    $spy_events->clear();

on_spy_event

    $spy_events->on_spy_event( name => sub {...} );

Declare one or more subroutines in the spied module.

called

    $bool = $control->called();

Returns true if times returns non-empty hash.

not_called

    $bool = $control->not_called();

Returns true if times returns an empty hash.

DIAGNOSTICS

None.

DEPENDENCIES

Perl 5.10.1 or later.

CONFIGURATION AND ENVIRONMENT

JIP::Spy::Events requires no configuration files or environment variables.

SEE ALSO

Sub:Spy, Module::Spy

AUTHOR

Volodymyr Zhavoronkov, <flyweight at yandex dot ru>

LICENSE AND COPYRIGHT

Copyright (c) 2019-2023 Volodymyr Zhavoronkov.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.