NAME
Test::Stream::Tester - Tools for validating testing tools.
EXPERIMENTAL CODE WARNING
This is an experimental release! Test-Stream, and all its components are still in an experimental phase. This dist has been released to cpan in order to allow testers and early adopters the chance to write experimental new tools with it, or to add experimental support for it into old tools.
PLEASE DO NOT COMPLETELY CONVERT OLD TOOLS YET. This experimental release is very likely to see a lot of code churn. API's may break at any time. Test-Stream should NOT be depended on by any toolchain level tools until the experimental phase is over.
DESCRIPTION
This library provides tools that make it easy to validate your testing tools. If you are writing a Test::Stream based testing tool, this is the library you should use to test it.
SYNOPSIS
use Test::Stream;
use Test::Stream::Tester;
events_are(
intercept {
ok(1, 'pass');
ok(0, 'fail');
diag "foo";
note "bar";
done_testing;
},
events {
event Ok => sub {
event_call pass => 1;
event_field effective_pass => 1;
event_line 42;
};
event Ok => sub {
event_call pass => 0;
event_field effective_pass => 0;
event_line 43;
event_call diag => [ qr/Failed test 'fail'/ ]
};
event Diag => { message => 'foo' };
event Note => { message => 'bar' };
event Plan => { max => 2 };
end_events;
},
"Basic check of events"
);
EXPORTS
ASSERTIONS
- events_are($events, $checks, $name);
-
This is actually
relaxed_compare()from Test::Stream::DeepCheck, but this is an implementation detail you should not rely on.events_are()compares the events provided in the first argument against the event checks in the second argument. The second argument may be a an arrayref with hashrefs to define the events, or it can use a check constructed to provide extra debugging details.
CAPTURING EVENTS
Both of these are re-exported from Test::Stream::Interceptor.
- $events = intercept { ... }
-
This lets you intercept all events inside the codeblock. All the events will be returned in an arrayref.
my $events = intercept { ok(1, 'foo'); ok(0, 'bar'); }; is(@$events, 2, "intercepted 2 events.");There are also 2 named parameters passed in,
contextandhub. Thecontextpassed in is a snapshot of the context for theintercept()tool itself, referencing the parent hub. Thehubparameter is the new hub created for theinterceptrun.my $events = intercept { my %params = @_; my $outer_ctx = $params{context}; my $our_hub = $params{hub}; ... };By default the hub used has
no_endingset to true. This will prevent the hub from enforcing that you issued a plan and ran at least 1 test. You can turn enforcement back one like this:my %params = @_; $params{hub}->set_no_ending(0);With
no_endingturned off,$hub-finalize()> will run the post-test checks to enforce the plan and that tests were run. In many cases this will result in additional events in your events array. - $grab = grab()
-
This lets you intercept all events for a section of code without adding anything to your call stack. This is useful for things that are sensitive to changes in the stack depth.
my $grab = grab(); ok(1, 'foo'); ok(0, 'bar'); # $grab is magically undef after this. my $events = $grab->finish; is(@$events, 2, "grabbed 2 events.");When you call
finish()the$grabobject will automagically undef itself, but only for the reference used in the method call. If you have other references to the$grabobject they will not be undef'd.If the
$grabobject is destroyed without callingfinish(), it will automatically clean up after itself and restore the parent hub.{ my $grab = grab(); # Things are grabbed } # Things are back to normalBy default the hub used has
no_endingset to true. This will prevent the hub from enforcing that you issued a plan and ran at least 1 test. You can turn enforcement back one like this:$grab->hub->set_no_ending(0);With
no_endingturned off,finishwill run the post-test checks to enforce the plan and that tests were run. In many cases this will result in additional events in your events array.
DEFINING EVENTS
- events { ... }
-
This runs the codeblock to build an arrayref of event checks. Within the codeblock you should call the other functions in this section to define each event.
- event $TYPE => \%SPEC
- event $TYPE => sub { ... }
-
This is how you build an event check. The
$TYPEshould be the final part of the Test::Stream::Event::HERE package name. You can define the event using either a hashref of fields, or a codeblock that calls other functions in this section to define checks. - event_call $METHOD => $EXPECT
-
This lets you check the return from calling
$event->$METHODon your event object.$EXPECTshould be the value you expect to be returned. You may provide scalars, hashrefs, arrayrefs, or Test::Stream::DeepCheck::Check instances as the$EXPECTvalue. - event_field $KEY => $VALUE
-
This lets you check the value of any key in the event hashref.
$VALUEcan be a scalar, arrayref, hashref or Test::Stream::DeepCheck::Check instance.
DEBUG CHECKS
These all verify data in the Test::Stream::DebugInfo attached to the events.
- event_line $LINE
-
Check the line number that any failures will be reported to.
- event_file $FILE
-
Check the file name that any failures will be reported to.
- event_package $PACKAGE
-
Check the package name that any failures will be reported to.
- event_sub $SUBNAME
-
Check the subname that any failures will be reported to.
- event_trace $DEBUG_TRACE
-
Check the 'at FILE line LINE' string that will be used in the event of errors.
- event_todo $TODO_REASON
-
Check the TODO status. This will either be undef, or the todo string.
- event_skip $SKIP_REASON
-
Check the SKIP status. This will either be undef, or the skip string.
MANIPULATING THE EVENT LIST
- end_events()
-
Use this to say that there should be no remaining events in the array.
- @events = filter_events { grep { ... } @_ }
-
Use this to remove items from the event list. This can be used for example to strip out Diag and leave only Ok events.
SOURCE
The source code repository for Test::Stream can be found at http://github.com/Test-More/Test-Stream/.
MAINTAINERS
AUTHORS
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://www.perl.com/perl/misc/Artistic.html