Test::Stream::Tester - Tools for validating testing tools.
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.
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.
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" );
This is actually relaxed_compare() from Test::Stream::DeepCheck, but this is an implementation detail you should not rely on.
relaxed_compare()
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.
events_are()
Both of these are re-exported from Test::Stream::Interceptor.
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, context and hub. The context passed in is a snapshot of the context for the intercept() tool itself, referencing the parent hub. The hub parameter is the new hub created for the intercept run.
context
hub
intercept()
intercept
my $events = intercept { my %params = @_; my $outer_ctx = $params{context}; my $our_hub = $params{hub}; ... };
By default the hub used has no_ending set 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:
no_ending
my %params = @_; $params{hub}->set_no_ending(0);
With no_ending turned 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.
$hub-
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 $grab object will automagically undef itself, but only for the reference used in the method call. If you have other references to the $grab object they will not be undef'd.
finish()
$grab
If the $grab object is destroyed without calling finish(), it will automatically clean up after itself and restore the parent hub.
{ my $grab = grab(); # Things are grabbed } # Things are back to normal
$grab->hub->set_no_ending(0);
With no_ending turned off, finish 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.
finish
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.
This is how you build an event check. The $TYPE should 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.
$TYPE
This lets you check the return from calling $event->$METHOD on your event object. $EXPECT should be the value you expect to be returned. You may provide scalars, hashrefs, arrayrefs, or Test::Stream::DeepCheck::Check instances as the $EXPECT value.
$event->$METHOD
$EXPECT
This lets you check the value of any key in the event hashref. $VALUE can be a scalar, arrayref, hashref or Test::Stream::DeepCheck::Check instance.
$VALUE
These all verify data in the Test::Stream::DebugInfo attached to the events.
Check the line number that any failures will be reported to.
Check the file name that any failures will be reported to.
Check the package name that any failures will be reported to.
Check the subname that any failures will be reported to.
Check the 'at FILE line LINE' string that will be used in the event of errors.
Check the TODO status. This will either be undef, or the todo string.
Check the SKIP status. This will either be undef, or the skip string.
Use this to say that there should be no remaining events in the array.
Use this to remove items from the event list. This can be used for example to strip out Diag and leave only Ok events.
The source code repository for Test::Stream can be found at http://github.com/Test-More/Test-Stream/.
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
To install Test::Stream, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Stream
CPAN shell
perl -MCPAN -e shell install Test::Stream
For more information on module installation, please visit the detailed CPAN module installation guide.