| 1 | package Test::Stream::Plugin::SRand; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Test::Stream::Plugin qw/import/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Test::Stream::Sync(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp qw/carp/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Test::Stream::Context qw/context/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | my $ADDED_HOOK = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | my $SEED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | my $FROM; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | sub seed { $SEED } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
seed: called CC=1 SCAR=0.0
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | sub from { $FROM } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from: called CC=1 SCAR=0.0
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | sub load_ts_plugin { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_ts_plugin: called CC=7 SCAR=19.7
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 6 | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | my $caller = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | carp "SRand loaded multiple times, re-seeding rand" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | if defined $SEED; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | ▶ | if (@_) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | ($SEED) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | $FROM = 'import arg' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | elsif(exists $ENV{TS_RAND_SEED}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | $SEED = $ENV{TS_RAND_SEED}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | $FROM = 'environment variable' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | else { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | my @ltime = localtime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | $SEED = sprintf('%04d%02d%02d', 1900 + $ltime[5], 1 + $ltime[4], $ltime[3]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | $FROM = 'local date'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | ▶ | $SEED = 0 unless $SEED; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 6 | srand($SEED); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 6 | ▶ | if ($ENV{HARNESS_IS_VERBOSE}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # If the harness is verbose then just display the message for all to | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # see. It is nice info and they already asked for noisy output. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | ▶ | Test::Stream::Sync->post_load(sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | my $ctx = context(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | $ctx->note("Seeded srand with seed '$SEED' from $FROM."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | $ctx->release; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | elsif (!$ADDED_HOOK++) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # The seed can be important for debugging, so if anything is wrong we | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # should output the seed message as a diagnostics message. This must be | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # done at the very end, even later than a hub hook. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | Test::Stream::Sync->add_hook( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=21.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | my ($ctx, $real, $new) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | $ctx->diag("Seeded srand with seed '$SEED' from $FROM.") | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$real or $new and $$new or not $ctx->hub->state->is_passing
$real or $new and $$new
$new and $$new
MC/DC0%0/4
$real or $new and $$new or not $ctx->hub->state->is_passing
$real
$new
$$new
not $ctx->hub->state->is_passing
Truth table16%1/6
A or B and C or not(D)
A= $realB= $newC= $$newD= not $ctx->hub->state->is_passing
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | if $real | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | || ($new && $$new) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | || !$ctx->hub->state->is_passing; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | =pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | =encoding UTF-8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | =head1 NAME | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | Test::Stream::Plugin::SRand - Control the random seed for more controlled test | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | environments. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | =head1 DEPRECATED | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | B<This distribution is deprecated> in favor of L<Test2>, L<Test2::Suite>, and | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | L<Test2::Workflow>. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | See L<Test::Stream::Manual::ToTest2> for a conversion guide. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | =head1 DESCRIPTION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | This module gives you control over the random seed used for your unit tests. In | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | some testing environments the random seed can play a major role in results. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | The default configuration for this module will seed srand with the local date. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | Using the date as the seed means that on any given day the random seed will | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | always be the same, this means behavior will not change from run to run on a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | given day. However the seed is different on different days allowing you to be | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | sure the code still works with actual randomness. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | The seed is printed for you on failure, or when the harness is verbose. You can | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | use the C<TS_RAND_SEED> environment variable to specify the seed. You can also | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | provide a specific seed as a load-time argument to the plugin. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | =head1 SYNOPSIS | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | Loading the plugin is easy, and the defaults are sane: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | use Test::Stream 'SRand'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | Custom seed: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | use Test::Stream SRand => ['42']; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | =head1 NOTE ON LOAD ORDER | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | If you use this plugin you probably want to use it as the first, or near-first | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | plugin. C<srand> is not called until the plugin is loaded, so other plugins | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | loaded first may already be making use of random numbers before your seed | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | takes effect. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | =head1 SOURCE | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | The source code repository for Test::Stream can be found at | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | F<http://github.com/Test-More/Test-Stream/>. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | =head1 MAINTAINERS | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | =over 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | =item Chad Granum E<lt>exodist@cpan.orgE<gt> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | =back | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | =head1 AUTHORS | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | =over 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | =item Chad Granum E<lt>exodist@cpan.orgE<gt> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | =back | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =head1 COPYRIGHT | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | Copyright 2015 Chad Granum E<lt>exodist7@gmail.comE<gt>. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | This program is free software; you can redistribute it and/or | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | modify it under the same terms as Perl itself. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | See F<http://dev.perl.org/licenses/> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||