Test-MockRandom-1.01/000755 000765 000024 00000000000 12275406314 014640 5ustar00davidstaff000000 000000 Test-MockRandom-1.01/Changes000644 000765 000024 00000004210 12275406314 016130 0ustar00davidstaff000000 000000 Revision history for Perl module Test::MockRandom 1.01 2014-02-08 05:53:59-05:00 America/New_York [CHANGED] - Distribution converted to Dist::Zilla; updated repo, issue tracker, etc. metadata 1.00 Mon Dec 17 11:39:59 EST 2007 - added prototype to rand() function to match built-in (RT#30471; patch by INFINOID) - removed Exporter as superclass (unused) - changed to the Apache License, version 2.0; (it's clearer, relicensable, and is explicit about contributions) - updated Build/documentation system and associated packaging; author tests moved to xt directory; various perl critic and other cleanups 0.99 Tue Feb 7 07:09:45 EST 2006 - removed Test::Exception dependency - added optional pod/pod-coverage tests (skipped by default) - downgraded Test::More prereq version - removed 'warnings' pragma for backwards compatibility 0.98 Mon Aug 1 10:41:51 EDT 2005 - build_requires moved to requires to workaround CPANPLUS bug 0.97 Thu Jun 9 07:14:36 EDT 2005 - removed pod/podcoverage tests to minimize dependencies 0.96 Tue Jun 7 17:54:17 EDT 2005 - updated Build.PL with dependencies - changed Makefile compatibility to traditional 0.95 Tue Mar 8 22:46:51 EST 2005 - added pod and pod coverage tests 0.94 Mon Jan 31 21:11:50 EST 2005 - changed exports so that nothing is exported by default - API for "use" changed substantially -- see POD for new API - redefine warnings supressed 0.93 Wed Jan 5 14:44:00 EST 2005 - added export_srand_to function - cleaned up Todo 0.92 Tue Dec 21 00:21:50 EST 2004 - changed API for "use" to facilitate exporting rand to other packages (the common case when used in test scripts) - improved documentation substantially, particularly around "use" - expanded the test suite 0.91 Thu Dec 16 13:17:46 EST 2004 - Added Test::MockObject and Test::MockModule to the "See Also" section - changed oneish to use 2**32 instead of 2**31 - added function "export_rand_to" 0.90 Tue Dec 14 17:03:45 EST 2004 - first public release Test-MockRandom-1.01/CONTRIBUTING000644 000765 000024 00000006512 12275406314 016476 0ustar00davidstaff000000 000000 ## HOW TO CONTRIBUTE Thank you for considering contributing to this distribution. This file contains instructions that will help you work with the source code. The distribution is managed with Dist::Zilla. This means than many of the usual files you might expect are not in the repository, but are generated at release time (e.g. Makefile.PL). Generally, **you do not need Dist::Zilla to contribute patches**. You do need Dist::Zilla to create a tarball and/or install from the repository. See below for guidance. ### Getting dependencies See the included `cpanfile` file for a list of dependencies. If you have App::cpanminus 1.6 or later installed, you can use `cpanm` to satisfy dependencies like this: $ cpanm --installdeps . Otherwise, you can install Module::CPANfile 1.0002 or later and then satisfy dependencies with the regular `cpan` client and `cpanfile-dump`: $ cpan `cpanfile-dump` ### Running tests You can run tests directly using the `prove` tool: $ prove -l $ prove -lv t/some_test_file.t For most of my distributions, `prove` is entirely sufficient for you to test any patches you have. I use `prove` for 99% of my testing during development. ### Code style and tidying Please try to match any existing coding style. If there is a `.perltidyrc` file, please install Perl::Tidy and use perltidy before submitting patches. If there is a `tidyall.ini` file, you can also install Code::TidyAll and run `tidyall` on a file or `tidyall -a` to tidy all files. ### Patching documentation Much of the documentation Pod is generated at release time. Depending on the distribution, some of my documentation may be written in a Pod dialect called WikiDoc. (See Pod::WikiDoc on CPAN.) If you would like to submit a documentation edit, please limit yourself to the documentation you see. If you see typos or documentation issues in the generated docs, please email or open a bug ticket instead of patching. ### Installing from the repository If you want to install directly from the repository, you need to have Dist::Zilla installed (see below). If this is a burden to you, I welcome patches against a CPAN tarball instead of the repository. ### Installing and using Dist::Zilla Dist::Zilla is a very powerful authoring tool, optimized for maintaining a large number of distributions with a high degree of automation, but it has a large dependency chain, a bit of a learning curve and requires a number of author-specific plugins. To install it from CPAN, I recommend one of the following approaches for the quickest installation: # using CPAN.pm, but bypassing non-functional pod tests $ cpan TAP::Harness::Restricted $ PERL_MM_USE_DEFAULT=1 HARNESS_CLASS=TAP::Harness::Restricted cpan Dist::Zilla # using cpanm, bypassing *all* tests $ cpanm -n Dist::Zilla In either case, it's probably going to take about 10 minutes. Go for a walk, go get a cup of your favorite beverage, take a bathroom break, or whatever. When you get back, Dist::Zilla should be ready for you. Then you need to install any plugins specific to this distribution: $ cpan `dzil authordeps` $ dzil authordeps | cpanm Once installed, here are some dzil commands you might try: $ dzil build $ dzil test $ dzil xtest To install from the repository, use: $ dzil install You can learn more about Dist::Zilla at http://dzil.org/ Test-MockRandom-1.01/cpanfile000644 000765 000024 00000001700 12275406314 016342 0ustar00davidstaff000000 000000 requires "Carp" => "0"; requires "perl" => "5.006"; requires "strict" => "0"; requires "warnings" => "0"; on 'test' => sub { requires "ExtUtils::MakeMaker" => "0"; requires "File::Spec::Functions" => "0"; requires "List::Util" => "0"; requires "Test::More" => "0"; requires "lib" => "0"; requires "version" => "0"; }; on 'test' => sub { recommends "CPAN::Meta" => "0"; recommends "CPAN::Meta::Requirements" => "2.120900"; }; on 'configure' => sub { requires "ExtUtils::MakeMaker" => "6.17"; }; on 'develop' => sub { requires "Dist::Zilla" => "5.012"; requires "Dist::Zilla::PluginBundle::DAGOLDEN" => "0.060"; requires "File::Spec" => "0"; requires "File::Temp" => "0"; requires "IO::Handle" => "0"; requires "IPC::Open3" => "0"; requires "Pod::Coverage::TrustPod" => "0"; requires "Test::CPAN::Meta" => "0"; requires "Test::More" => "0"; requires "Test::Pod" => "1.41"; requires "Test::Pod::Coverage" => "1.08"; }; Test-MockRandom-1.01/dist.ini000644 000765 000024 00000000224 12275406314 016302 0ustar00davidstaff000000 000000 name = Test-MockRandom author = David Golden license = Apache_2_0 copyright_holder = David Golden [@DAGOLDEN] :version = 0.060 Test-MockRandom-1.01/examples/000755 000765 000024 00000000000 12275406314 016456 5ustar00davidstaff000000 000000 Test-MockRandom-1.01/lib/000755 000765 000024 00000000000 12275406314 015406 5ustar00davidstaff000000 000000 Test-MockRandom-1.01/LICENSE000644 000765 000024 00000026354 12275406314 015657 0ustar00davidstaff000000 000000 This software is Copyright (c) 2014 by David Golden. This is free software, licensed under: The Apache License, Version 2.0, January 2004 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Test-MockRandom-1.01/Makefile.PL000644 000765 000024 00000002602 12275406314 016612 0ustar00davidstaff000000 000000 # This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.012. use strict; use warnings; use 5.006; use ExtUtils::MakeMaker 6.17; my %WriteMakefileArgs = ( "ABSTRACT" => "Replaces random number generation with non-random number generation", "AUTHOR" => "David Golden ", "BUILD_REQUIRES" => {}, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.17" }, "DISTNAME" => "Test-MockRandom", "EXE_FILES" => [], "LICENSE" => "apache", "NAME" => "Test::MockRandom", "PREREQ_PM" => { "Carp" => 0, "strict" => 0, "warnings" => 0 }, "TEST_REQUIRES" => { "ExtUtils::MakeMaker" => 0, "File::Spec::Functions" => 0, "List::Util" => 0, "Test::More" => 0, "lib" => 0, "version" => 0 }, "VERSION" => "1.01", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "Carp" => 0, "ExtUtils::MakeMaker" => 0, "File::Spec::Functions" => 0, "List::Util" => 0, "Test::More" => 0, "lib" => 0, "strict" => 0, "version" => 0, "warnings" => 0 ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); Test-MockRandom-1.01/MANIFEST000644 000765 000024 00000001542 12275406314 015773 0ustar00davidstaff000000 000000 # This file was automatically generated by Dist::Zilla::Plugin::Manifest v5.012. CONTRIBUTING Changes LICENSE MANIFEST META.json META.yml Makefile.PL README cpanfile dist.ini examples/mock-rand.t lib/Test/MockRandom.pm perlcritic.rc t/00-report-prereqs.t t/01-OO-syntax.t t/02-override-PACKAGE.t t/03-override-PACKAGE-dwim-hack.t t/04-override-module.t t/05-override-list.t t/06-override-global.t t/07-empty-string-import.t t/09-import-override-reoverride.t t/10-export_fcn_to.t t/11-export_aliasing.t t/15-custom-export-spec.t t/16-custom-export-error-handling.t t/17-list-context.t t/RandomList.pm t/SomePackage.pm t/SomeRandPackage.pm tidyall.ini xt/author/00-compile.t xt/author/critic.t xt/author/pod-spell.t xt/release/distmeta.t xt/release/minimum-version.t xt/release/pod-coverage.t xt/release/pod-syntax.t xt/release/portability.t xt/release/test-version.t Test-MockRandom-1.01/META.json000644 000765 000024 00000004534 12275406314 016267 0ustar00davidstaff000000 000000 { "abstract" : "Replaces random number generation with non-random number generation", "author" : [ "David Golden " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 5.012, CPAN::Meta::Converter version 2.133380", "license" : [ "apache_2_0" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Test-MockRandom", "no_index" : { "directory" : [ "t", "xt", "examples", "corpus" ], "package" : [ "DB" ] }, "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.17" } }, "develop" : { "requires" : { "Dist::Zilla" : "5.012", "Dist::Zilla::PluginBundle::DAGOLDEN" : "0.060", "File::Spec" : "0", "File::Temp" : "0", "IO::Handle" : "0", "IPC::Open3" : "0", "Pod::Coverage::TrustPod" : "0", "Test::CPAN::Meta" : "0", "Test::More" : "0", "Test::Pod" : "1.41", "Test::Pod::Coverage" : "1.08" } }, "runtime" : { "requires" : { "Carp" : "0", "perl" : "5.006", "strict" : "0", "warnings" : "0" } }, "test" : { "recommends" : { "CPAN::Meta" : "0", "CPAN::Meta::Requirements" : "2.120900" }, "requires" : { "ExtUtils::MakeMaker" : "0", "File::Spec::Functions" : "0", "List::Util" : "0", "Test::More" : "0", "lib" : "0", "version" : "0" } } }, "provides" : { "Test::MockRandom" : { "file" : "lib/Test/MockRandom.pm", "version" : "1.01" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/dagolden/Test-MockRandom/issues" }, "homepage" : "https://github.com/dagolden/Test-MockRandom", "repository" : { "type" : "git", "url" : "https://github.com/dagolden/Test-MockRandom.git", "web" : "https://github.com/dagolden/Test-MockRandom" } }, "version" : "1.01", "x_authority" : "cpan:DAGOLDEN" } Test-MockRandom-1.01/META.yml000644 000765 000024 00000001774 12275406314 016122 0ustar00davidstaff000000 000000 --- abstract: 'Replaces random number generation with non-random number generation' author: - 'David Golden ' build_requires: ExtUtils::MakeMaker: '0' File::Spec::Functions: '0' List::Util: '0' Test::More: '0' lib: '0' version: '0' configure_requires: ExtUtils::MakeMaker: '6.17' dynamic_config: 0 generated_by: 'Dist::Zilla version 5.012, CPAN::Meta::Converter version 2.133380' license: apache meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Test-MockRandom no_index: directory: - t - xt - examples - corpus package: - DB provides: Test::MockRandom: file: lib/Test/MockRandom.pm version: '1.01' requires: Carp: '0' perl: '5.006' strict: '0' warnings: '0' resources: bugtracker: https://github.com/dagolden/Test-MockRandom/issues homepage: https://github.com/dagolden/Test-MockRandom repository: https://github.com/dagolden/Test-MockRandom.git version: '1.01' x_authority: cpan:DAGOLDEN Test-MockRandom-1.01/perlcritic.rc000644 000765 000024 00000001166 12275406314 017332 0ustar00davidstaff000000 000000 severity = 5 verbose = 8 [Variables::ProhibitPunctuationVars] allow = $@ $! [TestingAndDebugging::ProhibitNoStrict] allow = refs [Variables::ProhibitEvilVariables] variables = $DB::single # Turn these off [-BuiltinFunctions::ProhibitStringyEval] [-ControlStructures::ProhibitPostfixControls] [-ControlStructures::ProhibitUnlessBlocks] [-Documentation::RequirePodSections] [-InputOutput::ProhibitInteractiveTest] [-References::ProhibitDoubleSigils] [-RegularExpressions::RequireExtendedFormatting] [-InputOutput::ProhibitTwoArgOpen] [-Modules::ProhibitEvilModules] # Turn this on [Lax::ProhibitStringyEval::ExceptForRequire] Test-MockRandom-1.01/README000644 000765 000024 00000032242 12275406314 015523 0ustar00davidstaff000000 000000 NAME Test::MockRandom - Replaces random number generation with non-random number generation VERSION version 1.01 SYNOPSIS # intercept rand in another package use Test::MockRandom 'Some::Other::Package'; use Some::Other::Package; # exports sub foo { return rand } srand(0.13); foo(); # returns 0.13 # using a seed list and "oneish" srand(0.23, 0.34, oneish() ); foo(); # returns 0.23 foo(); # returns 0.34 foo(); # returns a number just barely less than one foo(); # returns 0, as the seed array is empty # object-oriented, for use in the current package use Test::MockRandom (); my $nrng = Test::MockRandom->new(0.42, 0.23); $nrng->rand(); # returns 0.42 DESCRIPTION This perhaps ridiculous-seeming module was created to test routines that manipulate random numbers by providing a known output from "rand". Given a list of seeds with "srand", it will return each in turn. After seeded random numbers are exhausted, it will always return 0. Seed numbers must be of a form that meets the expected output from "rand" as called with no arguments -- i.e. they must be between 0 (inclusive) and 1 (exclusive). In order to facilitate generating and testing a nearly-one number, this module exports the function "oneish", which returns a number just fractionally less than one. Depending on how this module is called with "use", it will export "rand" to a specified package (e.g. a class being tested) effectively overriding and intercepting calls in that package to the built-in "rand". It can also override "rand" in the current package or even globally. In all of these cases, it also exports "srand" and "oneish" to the current package in order to control the output of "rand". See "USAGE" for details. Alternatively, this module can be used to generate objects, with each object maintaining its own distinct seed array. USAGE By default, Test::MockRandom does not export any functions. This still allows object-oriented use by calling "Test::MockRandom->new(@seeds)". In order for Test::MockRandom to be more useful, arguments must be provided during the call to "use". use Test::MockRandom 'Target::Package' The simplest way to intercept "rand" in another package is to provide the name(s) of the package(s) for interception as arguments in the "use" statement. This will export "rand" to the listed packages and will export "srand" and "oneish" to the current package to control the behavior of "rand". You must "use" Test::MockRandom before you "use" the target package. This is a typical case for testing a module that uses random numbers: use Test::More 'no_plan'; use Test::MockRandom 'Some::Package'; BEGIN { use_ok( Some::Package ) } # assume sub foo { return rand } was imported from Some::Package srand(0.5) is( foo(), 0.5, "is foo() 0.5?") # test gives "ok" If multiple package names are specified, "rand" will be exported to all of them. If you wish to export "rand" to the current package, simply provide "__PACKAGE__" as the parameter for "use", or "main" if importing to a script without a specified package. This can be part of a list provided to "use". All of the following idioms work: use Test::MockRandom qw( main Some::Package ); # Assumes a script use Test::MockRandom __PACKAGE__, 'Some::Package'; # The following doesn't interpolate __PACKAGE__ as above, but # Test::MockRandom will still DWIM and handle it correctly use Test::MockRandom qw( __PACKAGE__ Some::Package ); use Test::MockRandom %customized As an alternative to a package name as an argument to "use", Test::MockRandom will also accept a hash reference with a custom set of instructions for how to export functions: use Test::MockRandom { rand => [ Some::Package, {Another::Package => 'random'} ], srand => { Another::Package => 'seed' }, oneish => __PACKAGE__ }; The keys of the hash may be any of "rand", "srand", and "oneish". The values of the hash give instructions for where to export the symbol corresponding to the key. These are interpreted as follows, depending on their type: * String: a package to which Test::MockRandom will export the symbol * Hash Reference: the key is the package to which Test::MockRandom will export the symbol and the value is the name under which it will be exported * Array Reference: a list of strings or hash references which will be handled as above Test::MockRandom->export_rand_to() In order to intercept the built-in "rand" in another package, Test::MockRandom must export its own "rand" function to the target package before the target package is compiled, thus overriding calls to the built-in. The simple approach (described above) of providing the target package name in the "use Test::MockRandom" statement accomplishes this because "use" is equivalent to a "require" and "import" within a "BEGIN" block. To explicitly intercept "rand" in another package, you can also call "export_rand_to", but it must be enclosed in a "BEGIN" block of its own. The explicit form also support function aliasing just as with the custom approach with "use", described above: use Test::MockRandom; BEGIN {Test::MockRandom->export_rand_to('AnotherPackage'=>'random')} use AnotherPackage; This "BEGIN" block must not include a "use" statement for the package to be intercepted, or perl will compile the package to be intercepted before the "export_rand_to" function has a chance to execute and intercept calls to the built-in "rand". This is very important in testing. The "export_rand_to" call must be in a separate "BEGIN" block from a "use" or "use_ok" test, which should be enclosed in a "BEGIN" block of its own: use Test::More tests => 1; use Test::MockRandom; BEGIN { Test::MockRandom->export_rand_to( 'AnotherPackage' ); } BEGIN { use_ok( 'AnotherPackage' ); } Given these cautions, it's probably best to use either the simple or custom approach with "use", which does the right thing in most circumstances. Should additional explicit customization be necessary, Test::MockRandom also provides "export_srand_to" and "export_oneish_to". Overriding "rand" globally: use Test::MockRandom 'CORE::GLOBAL' This is just like intercepting "rand" in a package, except that you do it globally by overriding the built-in function in "CORE::GLOBAL". use Test::MockRandom 'CORE::GLOBAL'; # or BEGIN { Test::MockRandom->export_rand_to('CORE::GLOBAL') } You can always access the real, built-in "rand" by calling it explicitly as "CORE::rand". Intercepting "rand" in a package that also contains a "rand" function This is tricky as the order in which the symbol table is manipulated will lead to very different results. This can be done safely (maybe) if the module uses the same rand syntax/prototype as the system call but offers them up as method calls which resolve at run-time instead of compile time. In this case, you will need to do an explicit intercept (as above) but do it after importing the package. I.e.: use Test::MockRandom 'SomeRandPackage'; use SomeRandPackage; BEGIN { Test::MockRandom->export_rand_to('SomeRandPackage'); The first line is necessary to get "srand" and "oneish" exported to the current package. The second line will define a "sub rand" in "SomeRandPackage", overriding the results of the first line. The third line then re-overrides the "rand". You may see warnings about "rand" being redefined. Depending on how your "rand" is written and used, there is a good likelihood that this isn't going to do what you're expecting, no matter what. If your package that defines "rand" relies internally upon the system "CORE::GLOBAL::rand" function, then you may be best off overriding that instead. FUNCTIONS "new" $obj = new( LIST OF SEEDS ); Returns a new Test::MockRandom object with the specified list of seeds. "srand" srand( LIST OF SEEDS ); $obj->srand( LIST OF SEEDS); If called as a bare function call or package method, sets the seed list for bare/package calls to "rand". If called as an object method, sets the seed list for that object only. "rand" $rv = rand(); $rv = $obj->rand(); $rv = rand(3); If called as a bare or package function, returns the next value from the package seed list. If called as an object method, returns the next value from the object seed list. If "rand" is called with a numeric argument, it follows the same behavior as the built-in function -- it multiplies the argument with the next value from the seed array (resulting in a random fractional value between 0 and the argument, just like the built-in). If the argument is 0, undef, or non-numeric, it is treated as if the argument is 1. Using this with an argument in testing may be complicated, as limits in floating point precision mean that direct numeric comparisons are not reliable. E.g. srand(1/3); rand(3); # does this return 1.0 or .999999999 etc. "oneish" srand( oneish() ); if ( rand() == oneish() ) { print "It's almost one." }; A utility function to return a nearly-one value. Equal to ( 2^32 - 1 ) / 2^32. Useful in "srand" and test functions. "export_rand_to" Test::MockRandom->export_rand_to( 'Some::Class' ); Test::MockRandom->export_rand_to( 'Some::Class' => 'random' ); This function exports "rand" into the specified package namespace. It must be called as a class function. If a second argument is provided, it is taken as the symbol name used in the other package as the alias to "rand": use Test::MockRandom; BEGIN { Test::MockRandom->export_rand_to( 'Some::Class' => 'random' ); } use Some::Class; srand (0.5); print Some::Class::random(); # prints 0.5 It can also be used to explicitly intercept "rand" after Test::MockRandom has been loaded. The effect of this function is highly dependent on when it is called in the compile cycle and should usually called from within a BEGIN block. See "USAGE" for details. Most users will not need this function. "export_srand_to" Test::MockRandom->export_srand_to( 'Some::Class' ); Test::MockRandom->export_srand_to( 'Some::Class' => 'seed' ); This function exports "srand" into the specified package namespace. It must be called as a class function. If a second argument is provided, it is taken as the symbol name to use in the other package as the alias for "srand". This function may be useful if another package wraps "srand": # In Some/Class.pm package Some::Class; sub seed { srand(shift) } sub foo { rand } # In a script use Test::MockRandom 'Some::Class'; BEGIN { Test::MockRandom->export_srand_to( 'Some::Class' ); } use Some::Class; seed(0.5); print foo(); # prints "0.5" The effect of this function is highly dependent on when it is called in the compile cycle and should usually be called from within a BEGIN block. See "USAGE" for details. Most users will not need this function. "export_oneish_to" Test::MockRandom->export_oneish_to( 'Some::Class' ); Test::MockRandom->export_oneish_to( 'Some::Class' => 'nearly_one' ); This function exports "oneish" into the specified package namespace. It must be called as a class function. If a second argument is provided, it is taken as the symbol name to use in the other package as the alias for "oneish". Since "oneish" is usually only used in a test script, this function is likely only necessary to alias "oneish" to some other name in the current package: use Test::MockRandom 'Some::Class'; BEGIN { Test::MockRandom->export_oneish_to( __PACKAGE__, "one" ); } use Some::Class; seed( one() ); print foo(); # prints a value very close to one The effect of this function is highly dependent on when it is called in the compile cycle and should usually be called from within a BEGIN block. See "USAGE" for details. Most users will not need this function. SEE ALSO * Test::MockObject * Test::MockModule SUPPORT Bugs / Feature Requests Please report any bugs or feature requests through the issue tracker at . You will be notified automatically of any progress on your issue. Source Code This is open source software. The code repository is available for public review and contribution under the terms of the license. git clone https://github.com/dagolden/Test-MockRandom.git AUTHOR David Golden COPYRIGHT AND LICENSE This software is Copyright (c) 2014 by David Golden. This is free software, licensed under: The Apache License, Version 2.0, January 2004 Test-MockRandom-1.01/t/000755 000765 000024 00000000000 12275406314 015103 5ustar00davidstaff000000 000000 Test-MockRandom-1.01/tidyall.ini000644 000765 000024 00000000240 12275406314 016777 0ustar00davidstaff000000 000000 ; Install Code::TidyAll ; run "tidyall -a" to tidy all files ; run "tidyall -g" to tidy only files modified from git [PerlTidy] select = {lib,t}/**/*.{pl,pm,t} Test-MockRandom-1.01/xt/000755 000765 000024 00000000000 12275406314 015273 5ustar00davidstaff000000 000000 Test-MockRandom-1.01/xt/author/000755 000765 000024 00000000000 12275406314 016575 5ustar00davidstaff000000 000000 Test-MockRandom-1.01/xt/release/000755 000765 000024 00000000000 12275406314 016713 5ustar00davidstaff000000 000000 Test-MockRandom-1.01/xt/release/distmeta.t000644 000765 000024 00000000332 12275406314 020710 0ustar00davidstaff000000 000000 #!perl # This file was automatically generated by Dist::Zilla::Plugin::MetaTests. use Test::More; eval "use Test::CPAN::Meta"; plan skip_all => "Test::CPAN::Meta required for testing META.yml" if $@; meta_yaml_ok(); Test-MockRandom-1.01/xt/release/minimum-version.t000644 000765 000024 00000000266 12275406314 022242 0ustar00davidstaff000000 000000 #!perl use Test::More; eval "use Test::MinimumVersion"; plan skip_all => "Test::MinimumVersion required for testing minimum versions" if $@; all_minimum_version_ok( qq{5.010} ); Test-MockRandom-1.01/xt/release/pod-coverage.t000644 000765 000024 00000000651 12275406314 021455 0ustar00davidstaff000000 000000 #!perl # This file was automatically generated by Dist::Zilla::Plugin::PodCoverageTests. use Test::More; eval "use Test::Pod::Coverage 1.08"; plan skip_all => "Test::Pod::Coverage 1.08 required for testing POD coverage" if $@; eval "use Pod::Coverage::TrustPod"; plan skip_all => "Pod::Coverage::TrustPod required for testing POD coverage" if $@; all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' }); Test-MockRandom-1.01/xt/release/pod-syntax.t000644 000765 000024 00000000332 12275406314 021204 0ustar00davidstaff000000 000000 #!perl # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use Test::More; eval "use Test::Pod 1.41"; plan skip_all => "Test::Pod 1.41 required for testing POD" if $@; all_pod_files_ok(); Test-MockRandom-1.01/xt/release/portability.t000644 000765 000024 00000000332 12275406314 021440 0ustar00davidstaff000000 000000 #!perl use strict; use warnings; use Test::More; eval 'use Test::Portability::Files'; plan skip_all => 'Test::Portability::Files required for testing portability' if $@; options(test_one_dot => 0); run_tests(); Test-MockRandom-1.01/xt/release/test-version.t000644 000765 000024 00000000643 12275406314 021545 0ustar00davidstaff000000 000000 use strict; use warnings; use Test::More; # generated by Dist::Zilla::Plugin::Test::Version 0.002004 BEGIN { eval "use Test::Version; 1;" or die $@; } my @imports = ( 'version_all_ok' ); my $params = { is_strict => 0, has_version => 1, }; push @imports, $params if version->parse( $Test::Version::VERSION ) >= version->parse('1.002'); Test::Version->import(@imports); version_all_ok; done_testing; Test-MockRandom-1.01/xt/author/00-compile.t000644 000765 000024 00000002041 12275406314 020624 0ustar00davidstaff000000 000000 use 5.006; use strict; use warnings; # this test was generated with Dist::Zilla::Plugin::Test::Compile 2.039 use Test::More tests => 1 + ($ENV{AUTHOR_TESTING} ? 1 : 0); my @module_files = ( 'Test/MockRandom.pm' ); # fake home for cpan-testers use File::Temp; local $ENV{HOME} = File::Temp::tempdir( CLEANUP => 1 ); my $inc_switch = -d 'blib' ? '-Mblib' : '-Ilib'; use File::Spec; use IPC::Open3; use IO::Handle; open my $stdin, '<', File::Spec->devnull or die "can't open devnull: $!"; my @warnings; for my $lib (@module_files) { # see L my $stderr = IO::Handle->new; my $pid = open3($stdin, '>&STDERR', $stderr, $^X, $inc_switch, '-e', "require q[$lib]"); binmode $stderr, ':crlf' if $^O eq 'MSWin32'; my @_warnings = <$stderr>; waitpid($pid, 0); is($?, 0, "$lib loaded ok"); if (@_warnings) { warn @_warnings; push @warnings, @_warnings; } } is(scalar(@warnings), 0, 'no warnings found') if $ENV{AUTHOR_TESTING}; Test-MockRandom-1.01/xt/author/critic.t000644 000765 000024 00000000435 12275406314 020241 0ustar00davidstaff000000 000000 #!perl use strict; use warnings; use Test::More; use English qw(-no_match_vars); eval "use Test::Perl::Critic"; plan skip_all => 'Test::Perl::Critic required to criticise code' if $@; Test::Perl::Critic->import( -profile => "perlcritic.rc" ) if -e "perlcritic.rc"; all_critic_ok(); Test-MockRandom-1.01/xt/author/pod-spell.t000644 000765 000024 00000000417 12275406314 020663 0ustar00davidstaff000000 000000 use strict; use warnings; use Test::More; # generated by Dist::Zilla::Plugin::Test::PodSpelling 2.006002 use Test::Spelling 0.12; use Pod::Wordlist; add_stopwords(); all_pod_files_spelling_ok( qw( bin lib ) ); __DATA__ David Golden dagolden lib Test MockRandom Test-MockRandom-1.01/t/00-report-prereqs.t000644 000765 000024 00000013553 12275406314 020506 0ustar00davidstaff000000 000000 #!perl use strict; use warnings; # This test was generated by Dist::Zilla::Plugin::Test::ReportPrereqs 0.013 use Test::More tests => 1; use ExtUtils::MakeMaker; use File::Spec::Functions; use List::Util qw/max/; use version; # hide optional CPAN::Meta modules from prereq scanner # and check if they are available my $cpan_meta = "CPAN::Meta"; my $cpan_meta_req = "CPAN::Meta::Requirements"; my $HAS_CPAN_META = eval "require $cpan_meta"; ## no critic my $HAS_CPAN_META_REQ = eval "require $cpan_meta_req; $cpan_meta_req->VERSION('2.120900')"; # Verify requirements? my $DO_VERIFY_PREREQS = 1; sub _merge_requires { my ($collector, $prereqs) = @_; for my $phase ( qw/configure build test runtime develop/ ) { next unless exists $prereqs->{$phase}; if ( my $req = $prereqs->{$phase}{'requires'} ) { my $cmr = CPAN::Meta::Requirements->from_string_hash( $req ); $collector->add_requirements( $cmr ); } } } my %include = map {; $_ => 1 } qw( ); my %exclude = map {; $_ => 1 } qw( ); # Add static prereqs to the included modules list my $static_prereqs = do { my $x = { 'configure' => { 'requires' => { 'ExtUtils::MakeMaker' => '6.17' } }, 'develop' => { 'requires' => { 'Dist::Zilla' => '5.012', 'Dist::Zilla::PluginBundle::DAGOLDEN' => '0.060', 'File::Spec' => '0', 'File::Temp' => '0', 'IO::Handle' => '0', 'IPC::Open3' => '0', 'Pod::Coverage::TrustPod' => '0', 'Test::CPAN::Meta' => '0', 'Test::More' => '0', 'Test::Pod' => '1.41', 'Test::Pod::Coverage' => '1.08' } }, 'runtime' => { 'requires' => { 'Carp' => '0', 'perl' => '5.006', 'strict' => '0', 'warnings' => '0' } }, 'test' => { 'recommends' => { 'CPAN::Meta' => '0', 'CPAN::Meta::Requirements' => '2.120900' }, 'requires' => { 'ExtUtils::MakeMaker' => '0', 'File::Spec::Functions' => '0', 'List::Util' => '0', 'Test::More' => '0', 'lib' => '0', 'version' => '0' } } }; $x; }; delete $static_prereqs->{develop} if not $ENV{AUTHOR_TESTING}; $include{$_} = 1 for map { keys %$_ } map { values %$_ } values %$static_prereqs; # Merge requirements for major phases (if we can) my $all_requires; if ( $DO_VERIFY_PREREQS && $HAS_CPAN_META_REQ ) { $all_requires = $cpan_meta_req->new; _merge_requires($all_requires, $static_prereqs); } # Add dynamic prereqs to the included modules list (if we can) my ($source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; if ( $source && $HAS_CPAN_META ) { if ( my $meta = eval { CPAN::Meta->load_file($source) } ) { my $dynamic_prereqs = $meta->prereqs; delete $dynamic_prereqs->{develop} if not $ENV{AUTHOR_TESTING}; $include{$_} = 1 for map { keys %$_ } map { values %$_ } values %$dynamic_prereqs; if ( $DO_VERIFY_PREREQS && $HAS_CPAN_META_REQ ) { _merge_requires($all_requires, $dynamic_prereqs); } } } else { $source = 'static metadata'; } my @modules = sort grep { ! $exclude{$_} } keys %include; my @reports = [qw/Version Module/]; my @dep_errors; my $req_hash = defined($all_requires) ? $all_requires->as_string_hash : {}; for my $mod ( @modules ) { next if $mod eq 'perl'; my $file = $mod; $file =~ s{::}{/}g; $file .= ".pm"; my ($prefix) = grep { -e catfile($_, $file) } @INC; if ( $prefix ) { my $ver = MM->parse_version( catfile($prefix, $file) ); $ver = "undef" unless defined $ver; # Newer MM should do this anyway push @reports, [$ver, $mod]; if ( $DO_VERIFY_PREREQS && $all_requires ) { my $req = $req_hash->{$mod}; if ( defined $req && length $req ) { if ( ! defined eval { version->parse($ver) } ) { push @dep_errors, "$mod version '$ver' cannot be parsed (version '$req' required)"; } elsif ( ! $all_requires->accepts_module( $mod => $ver ) ) { push @dep_errors, "$mod version '$ver' is not in required range '$req'"; } } } } else { push @reports, ["missing", $mod]; if ( $DO_VERIFY_PREREQS && $all_requires ) { my $req = $req_hash->{$mod}; if ( defined $req && length $req ) { push @dep_errors, "$mod is not installed (version '$req' required)"; } } } } if ( @reports ) { my $vl = max map { length $_->[0] } @reports; my $ml = max map { length $_->[1] } @reports; splice @reports, 1, 0, ["-" x $vl, "-" x $ml]; diag "\nVersions for all modules listed in $source (including optional ones):\n", map {sprintf(" %*s %*s\n",$vl,$_->[0],-$ml,$_->[1])} @reports; } if ( @dep_errors ) { diag join("\n", "\n*** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING ***\n", "The following REQUIRED prerequisites were not satisfied:\n", @dep_errors, "\n" ); } pass; # vim: ts=4 sts=4 sw=4 et: Test-MockRandom-1.01/t/01-OO-syntax.t000644 000765 000024 00000004454 12275406314 017356 0ustar00davidstaff000000 000000 # Testing Test::MockRandom use strict; use Test::More tests => 23; #--------------------------------------------------------------------------# # Test object oriented functionality #--------------------------------------------------------------------------# use Test::MockRandom; my $obj = Test::MockRandom->new(); isa_ok( $obj, 'Test::MockRandom', "Class constructor" ); isa_ok( $obj->new, 'Test::MockRandom', "Object constructor" ); is( $obj->rand(), 0, 'is uninitialized call to $obj->rand() equal to zero' ); eval { Test::MockRandom->new(1) }; ok( $@, 'does Test::MockRandom->new die if argument is equal to one' ); eval { Test::MockRandom->new(1.1) }; ok( $@, 'does Test::MockRandom->new die if argument is greater than one' ); eval { Test::MockRandom->new(-0.1) }; ok( $@, 'does Test::MockRandom->new die if argument is less than zero' ); eval { Test::MockRandom->new(0) }; is( $@, q{}, 'does Test::MockRandom->new(0) live' ); eval { Test::MockRandom->new(Test::MockRandom::oneish) }; is( $@, q{}, 'does Test::MockRandom->new(Test::MockRandom::oneish) live' ); eval { $obj->srand(1) }; ok( $@, 'does $obj->srand die if argument is equal to one' ); eval { $obj->srand(1.1) }; ok( $@, 'does $obj->srand die if argument is greater than one' ); eval { $obj->srand(-0.1) }; ok( $@, 'does $obj->srand die if argument is less than zero' ); eval { $obj->srand(0) }; is( $@, q{}, 'does $obj->srand(0) live' ); eval { $obj->srand( $obj->oneish ) }; is( $@, q{}, 'does $obj->srand($obj->oneish) live' ); $obj->srand(); is( $obj->rand(), 0, 'testing $obj->srand() gives $obj->rand() == 0' ); $obj->srand( $obj->oneish ); is( $obj->rand(), $obj->oneish, 'testing $obj->srand($obj->oneish) gives $obj->rand == $obj->oneish' ); $obj->srand(.5); is( $obj->rand(), .5, 'testing $obj->srand(.5) gives $obj->rand == .5' ); $obj->srand(0); is( $obj->rand(), 0, 'testing $obj->srand(0) gives $obj->rand == 0' ); $obj->srand( $obj->oneish, .3, .2, .1 ); ok( 1, 'setting $obj->srand(oneish,.3, .2, .1)' ); is( $obj->rand(), $obj->oneish, 'testing $obj->rand == oneish' ); is( $obj->rand(), .3, 'testing $obj->rand == .3' ); is( $obj->rand(), .2, 'testing $obj->rand == .2' ); is( $obj->rand(), .1, 'testing $obj->rand == .1' ); is( $obj->rand(), 0, 'testing $obj->rand == 0 (nothing left in $obj->srand array' ); Test-MockRandom-1.01/t/02-override-PACKAGE.t000644 000765 000024 00000003567 12275406314 020372 0ustar00davidstaff000000 000000 # Testing Test::MockRandom use strict; use Test::More tests => 25; #--------------------------------------------------------------------------# # Test non-object functionality #--------------------------------------------------------------------------# use Test::MockRandom __PACKAGE__; for (qw ( rand srand oneish )) { can_ok( __PACKAGE__, $_ ); } is( oneish(), ( 2**32 - 1 ) / ( 2**32 ), 'is oneish nearly one' ); is( rand(), 0, 'is uninitialized call to rand() equal to zero' ); eval { srand(1) }; ok( $@, 'does srand die if argument is equal to one' ); eval { srand(1.1) }; ok( $@, 'does srand die if argument is greater than one' ); eval { srand(-0.1) }; ok( $@, 'does srand die if argument is less than zero' ); eval { srand(0) }; is( $@, q{}, 'does srand(0) live' ); eval { srand(oneish) }; is( $@, q{}, 'does srand(oneish) live' ); srand(); is( rand(), 0, 'testing srand() gives rand() == 0' ); srand(oneish); is( rand(), oneish, 'testing srand(oneish) gives rand == oneish' ); srand(.5); is( rand(), .5, 'testing srand(.5) gives rand == .5' ); srand(0); is( rand(), 0, 'testing srand(0) gives rand == 0' ); srand( oneish, .3, .2, .1 ); ok( 1, 'setting srand(oneish,.3, .2, .1)' ); is( rand(), oneish, 'testing rand == oneish' ); is( rand(), .3, 'testing rand == .3' ); is( rand(), .2, 'testing rand == .2' ); is( rand(), .1, 'testing rand == .1' ); is( rand(), 0, 'testing rand == 0 (nothing left in srand array' ); #--------------------------------------------------------------------------# # Test rand(N) functionality #--------------------------------------------------------------------------# srand( 0.5, 0.25, .1, 0.6 ); ok( 1, 'setting srand( 0.5, 0.25 )' ); is( rand(2), 1, 'testing rand(2) == 1' ); is( rand(0), 0.25, 'testing rand(0) == 0.25' ); is( rand(-1), -0.1, 'testing rand(-1) == -0.1' ); is( rand('a'), 0.6, 'testing rand("a") == 0.6' ); Test-MockRandom-1.01/t/03-override-PACKAGE-dwim-hack.t000755 000765 000024 00000000703 12275406314 022225 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More tests => 3; #--------------------------------------------------------------------------# # Test package overriding via import # # In case __PACKAGE__ winds up in a qw() list, the import will still work #--------------------------------------------------------------------------# use Test::MockRandom qw( __PACKAGE__ ); # "__PACKAGE__" for (qw ( rand srand oneish )) { can_ok( __PACKAGE__, $_ ); } Test-MockRandom-1.01/t/04-override-module.t000755 000765 000024 00000001570 12275406314 020621 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More tests => 8; #--------------------------------------------------------------------------# # Test package overriding via import #--------------------------------------------------------------------------# use Test::MockRandom qw( SomePackage ); use lib qw( ./t ); use SomePackage; can_ok( 'SomePackage', 'rand' ); can_ok( __PACKAGE__, $_ ) for qw ( srand oneish ); ok( !UNIVERSAL::can( __PACKAGE__, 'rand' ), "confirming that rand() wasn't imported into " . __PACKAGE__ ); ok( !UNIVERSAL::can( 'SomePackage', 'srand' ), "confirming that srand wasn't imported into SomePackage" ); ok( !UNIVERSAL::can( 'SomePackage', 'oneish' ), "confirming that oneish wasn't imported into SomePackage" ); my $obj = SomePackage->new; isa_ok( $obj, 'SomePackage' ); srand(.5); is( $obj->next_random(), .5, 'testing $obj->next_random == .5' ); Test-MockRandom-1.01/t/05-override-list.t000755 000765 000024 00000001425 12275406314 020307 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More tests => 9; #--------------------------------------------------------------------------# # Test package overriding via import #--------------------------------------------------------------------------# use Test::MockRandom qw( SomePackage __PACKAGE__ ); use lib qw( ./t ); use SomePackage; can_ok( 'SomePackage', 'rand' ); for (qw(srand oneish)) { ok( !UNIVERSAL::can( 'SomePackage', $_ ), "confirming $_ wasn't exported to SomePackage" ); } can_ok( __PACKAGE__, $_ ) for qw( rand srand oneish ); my $obj = SomePackage->new; isa_ok( $obj, 'SomePackage' ); srand( .5, .6 ); is( $obj->next_random(), .5, 'testing $obj->next_random == .5' ); is( rand, .6, 'testing rand == .6 in current package' ); Test-MockRandom-1.01/t/06-override-global.t000755 000765 000024 00000001251 12275406314 020572 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More tests => 6; #--------------------------------------------------------------------------# # Test package overriding via import to global #--------------------------------------------------------------------------# use Test::MockRandom qw( CORE::GLOBAL ); use lib qw( . ./t ); use SomePackage; for ( __PACKAGE__, "SomePackage" ) { is( UNIVERSAL::can( $_, 'rand' ), undef, "rand should not have been imported into $_" ); } for (qw ( srand oneish )) { can_ok( __PACKAGE__, $_ ); } my $obj = SomePackage->new; isa_ok( $obj, 'SomePackage' ); srand(.5); is( $obj->next_random(), .5, 'testing $obj->next_random == .5' ); Test-MockRandom-1.01/t/07-empty-string-import.t000755 000765 000024 00000000547 12275406314 021477 0ustar00davidstaff000000 000000 # Testing Test::MockRandom use strict; use Test::More tests => 7; use Test::MockRandom ''; my @fcns = qw( rand srand oneish export_rand_to export_srand_to export_oneish_to ); can_ok( 'Test::MockRandom', @fcns ); for my $fcn (@fcns) { ok( !UNIVERSAL::can( __PACKAGE__, $fcn ), "confirming that $fcn wasn't imported by default" ); } Test-MockRandom-1.01/t/09-import-override-reoverride.t000755 000765 000024 00000001253 12275406314 023015 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More tests => 6; #--------------------------------------------------------------------------# # Test package overriding via import #--------------------------------------------------------------------------# use Test::MockRandom __PACKAGE__; use lib qw( . ./t ); use SomeRandPackage; # SomeRandPackage has its own rand(), so we have to re-override BEGIN { Test::MockRandom->export_rand_to('SomeRandPackage') } for (qw ( rand srand oneish )) { can_ok( __PACKAGE__, $_ ); } my $obj = SomeRandPackage->new; isa_ok( $obj, 'SomeRandPackage' ); can_ok( $obj, qw ( rand ) ); srand(.5); is( $obj->rand(), .5, 'testing $obj->rand == .5' ); Test-MockRandom-1.01/t/10-export_fcn_to.t000755 000765 000024 00000001647 12275406314 020372 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More tests => 5; #--------------------------------------------------------------------------# # Test package overriding #--------------------------------------------------------------------------# use Test::MockRandom; BEGIN { Test::MockRandom->export_rand_to('OverrideTest'); Test::MockRandom->export_srand_to('OverrideTest'); Test::MockRandom->export_oneish_to('OverrideTest'); } eval { Test::MockRandom::export_rand_to('bogus') }; ok( $@, "Dies when export_*_to not called as class function" ); eval { Test::MockRandom->export_rand_to() }; ok( $@, "Dies when export_*_to not given an argument" ); can_ok( 'OverrideTest', qw ( rand srand oneish ) ); OverrideTest::srand( .5, OverrideTest::oneish ); is( OverrideTest::rand(), .5, 'testing OverrideTest::srand(.5)' ); is( OverrideTest::rand(), OverrideTest::oneish, 'testing OverrideTest::srand(OverrideTest::oneish)' ); Test-MockRandom-1.01/t/11-export_aliasing.t000755 000765 000024 00000001412 12275406314 020700 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More tests => 3; #--------------------------------------------------------------------------# # Test package overriding #--------------------------------------------------------------------------# use Test::MockRandom; BEGIN { Test::MockRandom->export_rand_to( 'OverrideTest' => 'random' ); Test::MockRandom->export_srand_to( 'OverrideTest' => 'seed' ); Test::MockRandom->export_oneish_to( 'OverrideTest' => 'nearly_one' ); } can_ok( 'OverrideTest', qw ( random seed nearly_one ) ); OverrideTest::seed( .5, OverrideTest::nearly_one ); is( OverrideTest::random(), .5, 'testing OverrideTest::seed(.5)' ); is( OverrideTest::random(), OverrideTest::nearly_one, 'testing OverrideTest::seed(OverrideTest::nearly_one)' ); Test-MockRandom-1.01/t/15-custom-export-spec.t000755 000765 000024 00000001361 12275406314 021300 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More tests => 5; #--------------------------------------------------------------------------# # Test package overriding #--------------------------------------------------------------------------# use Test::MockRandom { 'rand' => [ { 'OverrideTest' => 'random' }, 'AnotherOverride' ], 'srand' => { 'OverrideTest' => 'seed' }, 'oneish' => __PACKAGE__, }; can_ok( 'OverrideTest', qw ( random seed ) ); can_ok( 'AnotherOverride', qw ( rand ) ); can_ok( __PACKAGE__, qw ( oneish ) ); OverrideTest::seed( .5, oneish() ); is( OverrideTest::random(), .5, 'testing OverrideTest::random()' ); is( AnotherOverride::rand(), oneish(), 'testing AnotherOverride::rand()' ); Test-MockRandom-1.01/t/16-custom-export-error-handling.t000755 000765 000024 00000000712 12275406314 023261 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More tests => 1; #--------------------------------------------------------------------------# # Test package overriding #--------------------------------------------------------------------------# eval { require Test::MockRandom; Test::MockRandom->import( { bogus => [ { 'OverrideTest' => 'random' }, 'AnotherOverride' ], } ); }; ok( $@, "Does custom import spec croak on unrecognized symbol?" ); Test-MockRandom-1.01/t/17-list-context.t000644 000765 000024 00000001620 12275406314 020151 0ustar00davidstaff000000 000000 # Test::MockRandom use strict; use Test::More; plan tests => 8; #--------------------------------------------------------------------------# # Test rand(@list) uses scalar context (in RandomList.pm) #--------------------------------------------------------------------------# use Test::MockRandom qw( CORE::GLOBAL ); use lib qw( ./t ); use RandomList; for ( __PACKAGE__, "RandomList" ) { is( UNIVERSAL::can( $_, 'rand' ), undef, "rand should not have been imported into $_" ); } for (qw ( srand oneish )) { can_ok( __PACKAGE__, $_ ); } my $list = RandomList->new( 0, 1, 2, 3, 4, 5 ); isa_ok( $list, 'RandomList' ); srand(0); is( $list->random(), 0, 'testing $list->random() -- return first element' ); srand( oneish() ); is( $list->random(), 5, 'testing $list->random() -- return last element' ); srand(.49); is( $list->random(), 2, 'testing $list->random() -- return third element' ); Test-MockRandom-1.01/t/RandomList.pm000644 000765 000024 00000000304 12275406314 017512 0ustar00davidstaff000000 000000 package RandomList; sub new { my ( $class, @items ) = @_; return bless( \@items, $class ); } sub random { my ($self) = @_; return @$self ? $self->[ rand(@$self) ] : undef; } 1; Test-MockRandom-1.01/t/SomePackage.pm000755 000765 000024 00000000246 12275406314 017625 0ustar00davidstaff000000 000000 package SomePackage; sub new { my $class = shift; return bless( {}, ref($class) || $class ); } sub next_random { my $rnd = rand; return $rnd; } 1; Test-MockRandom-1.01/t/SomeRandPackage.pm000755 000765 000024 00000000255 12275406314 020432 0ustar00davidstaff000000 000000 package SomeRandPackage; sub new { my $class = shift; return bless( {}, ref($class) || $class ); } sub rand(;$) { my $rnd = CORE::rand; return $rnd; } 1; Test-MockRandom-1.01/lib/Test/000755 000765 000024 00000000000 12275406314 016325 5ustar00davidstaff000000 000000 Test-MockRandom-1.01/lib/Test/MockRandom.pm000644 000765 000024 00000045566 12275406314 020735 0ustar00davidstaff000000 000000 use strict; use warnings; package Test::MockRandom; # ABSTRACT: Replaces random number generation with non-random number generation our $VERSION = '1.01'; # VERSION use Carp qw/croak/; #--------------------------------------------------------------------------# # Class data #--------------------------------------------------------------------------# my @data = (0); #--------------------------------------------------------------------------# # new() #--------------------------------------------------------------------------# sub new { my ( $class, @data ) = @_; my $self = bless( [], ref($class) || $class ); $self->srand(@data); return $self; } #--------------------------------------------------------------------------# # srand() #--------------------------------------------------------------------------# sub srand { ## no critic if ( ref( $_[0] ) eq __PACKAGE__ ) { my $self = shift; @$self = $self->_test_srand(@_); return; } else { @data = Test::MockRandom->_test_srand(@_); return; } } sub _test_srand { my ( $self, @data ) = @_; my $error = "Seeds for " . __PACKAGE__ . " must be between 0 (inclusive) and 1 (exclusive)"; croak $error if grep { $_ < 0 or $_ >= 1 } @data; return @data ? @data : (0); } #--------------------------------------------------------------------------# # rand() #--------------------------------------------------------------------------# sub rand(;$) { ## no critic my ( $mult, $val ); if ( ref( $_[0] ) eq __PACKAGE__ ) { # we're a MockRandom object $mult = $_[1]; $val = shift @{ $_[0] } || 0; } else { # we might be called as a method of some other class # so we need to ignore that and get the right multiplier $mult = $_[ ref( $_[0] ) ? 1 : 0 ]; $val = shift @data || 0; } # default to 1 for undef, 0, or strings that aren't numbers eval { no warnings; local $^W = 0; my $bogus = 1 / $mult }; $mult = 1 if $@; return $val * $mult; } #--------------------------------------------------------------------------# # oneish() #--------------------------------------------------------------------------# sub oneish { return ( 2**32 - 1 ) / ( 2**32 ); } #--------------------------------------------------------------------------# # import() #--------------------------------------------------------------------------# sub import { my ( $class, @import_list ) = @_; my $caller = caller(0); # Nothing exported by default or if empty string requested return unless @import_list; return if ( @import_list == 1 && $import_list[0] eq '' ); for my $tgt (@import_list) { # custom handling if it's a hashref if ( ref($tgt) eq "HASH" ) { for my $sym ( keys %$tgt ) { croak "Unrecognized symbol '$sym'" unless grep { $sym eq $_ } qw (rand srand oneish); my @custom = ref( $tgt->{$sym} ) eq 'ARRAY' ? @{ $tgt->{$sym} } : $tgt->{$sym}; _custom_export( $sym, $_ ) for (@custom); } } # otherwise, export rand to target and srand/oneish to caller else { my $pkg = ( $tgt =~ /^__PACKAGE__$/ ) ? $caller : $tgt; # DWIM _export_symbol( "rand", $pkg ); _export_symbol( $_, $caller ) for qw( srand oneish ); } } return; } #--------------------------------------------------------------------------# # export_oneish_to() #--------------------------------------------------------------------------# sub export_oneish_to { my ( $class, @args ) = @_; _export_fcn_to( $class, "oneish", @args ); return; } #--------------------------------------------------------------------------# # export_rand_to() #--------------------------------------------------------------------------# sub export_rand_to { my ( $class, @args ) = @_; _export_fcn_to( $class, "rand", @args ); return; } #--------------------------------------------------------------------------# # export_srand_to() #--------------------------------------------------------------------------# sub export_srand_to { my ( $class, @args ) = @_; _export_fcn_to( $class, "srand", @args ); return; } #--------------------------------------------------------------------------# # _custom_export #--------------------------------------------------------------------------# sub _custom_export { my ( $sym, $custom ) = @_; if ( ref($custom) eq 'HASH' ) { _export_symbol( $sym, %$custom ); # flatten { pkg => 'alias' } } else { _export_symbol( $sym, $custom ); } return; } #--------------------------------------------------------------------------# # _export_fcn_to #--------------------------------------------------------------------------# sub _export_fcn_to { my ( $self, $fcn, $pkg, $alias ) = @_; croak "Must call to export_${fcn}_to() as a class method" unless ( $self eq __PACKAGE__ ); croak("export_${fcn}_to() requires a package name") unless $pkg; _export_symbol( $fcn, $pkg, $alias ); return; } #--------------------------------------------------------------------------# # _export_symbol() #--------------------------------------------------------------------------# sub _export_symbol { my ( $sym, $pkg, $alias ) = @_; $alias ||= $sym; { no strict 'refs'; ## no critic no warnings 'redefine'; local $^W = 0; # no redefine warnings *{"${pkg}::${alias}"} = \&{"Test::MockRandom::${sym}"}; } return; } 1; __END__ =pod =encoding UTF-8 =head1 NAME Test::MockRandom - Replaces random number generation with non-random number generation =head1 VERSION version 1.01 =head1 SYNOPSIS # intercept rand in another package use Test::MockRandom 'Some::Other::Package'; use Some::Other::Package; # exports sub foo { return rand } srand(0.13); foo(); # returns 0.13 # using a seed list and "oneish" srand(0.23, 0.34, oneish() ); foo(); # returns 0.23 foo(); # returns 0.34 foo(); # returns a number just barely less than one foo(); # returns 0, as the seed array is empty # object-oriented, for use in the current package use Test::MockRandom (); my $nrng = Test::MockRandom->new(0.42, 0.23); $nrng->rand(); # returns 0.42 =head1 DESCRIPTION This perhaps ridiculous-seeming module was created to test routines that manipulate random numbers by providing a known output from C<<< rand >>>. Given a list of seeds with C<<< srand >>>, it will return each in turn. After seeded random numbers are exhausted, it will always return 0. Seed numbers must be of a form that meets the expected output from C<<< rand >>> as called with no arguments -- i.e. they must be between 0 (inclusive) and 1 (exclusive). In order to facilitate generating and testing a nearly-one number, this module exports the function C<<< oneish >>>, which returns a number just fractionally less than one. Depending on how this module is called with C<<< use >>>, it will export C<<< rand >>> to a specified package (e.g. a class being tested) effectively overriding and intercepting calls in that package to the built-in C<<< rand >>>. It can also override C<<< rand >>> in the current package or even globally. In all of these cases, it also exports C<<< srand >>> and C<<< oneish >>> to the current package in order to control the output of C<<< rand >>>. See L for details. Alternatively, this module can be used to generate objects, with each object maintaining its own distinct seed array. =head1 USAGE By default, Test::MockRandom does not export any functions. This still allows object-oriented use by calling C<<< Test::MockRandom->new(@seeds) >>>. In order for Test::MockRandom to be more useful, arguments must be provided during the call to C<<< use >>>. =head2 use Test::MockRandom 'Target::Package' The simplest way to intercept C<<< rand >>> in another package is to provide the name(s) of the package(s) for interception as arguments in the C<<< use >>> statement. This will export C<<< rand >>> to the listed packages and will export C<<< srand >>> and C<<< oneish >>> to the current package to control the behavior of C<<< rand >>>. You B C<<< use >>> Test::MockRandom before you C<<< use >>> the target package. This is a typical case for testing a module that uses random numbers: use Test::More 'no_plan'; use Test::MockRandom 'Some::Package'; BEGIN { use_ok( Some::Package ) } # assume sub foo { return rand } was imported from Some::Package srand(0.5) is( foo(), 0.5, "is foo() 0.5?") # test gives "ok" If multiple package names are specified, C<<< rand >>> will be exported to all of them. If you wish to export C<<< rand >>> to the current package, simply provide C<<< __PACKAGE__ >>> as the parameter for C<<< use >>>, or C<<< main >>> if importing to a script without a specified package. This can be part of a list provided to C<<< use >>>. All of the following idioms work: use Test::MockRandom qw( main Some::Package ); # Assumes a script use Test::MockRandom __PACKAGE__, 'Some::Package'; # The following doesn't interpolate __PACKAGE__ as above, but # Test::MockRandom will still DWIM and handle it correctly use Test::MockRandom qw( __PACKAGE__ Some::Package ); =head2 use Test::MockRandom %customized As an alternative to a package name as an argument to C<<< use >>>, Test::MockRandom will also accept a hash reference with a custom set of instructions for how to export functions: use Test::MockRandom { rand => [ Some::Package, {Another::Package => 'random'} ], srand => { Another::Package => 'seed' }, oneish => __PACKAGE__ }; The keys of the hash may be any of C<<< rand >>>, C<<< srand >>>, and C<<< oneish >>>. The values of the hash give instructions for where to export the symbol corresponding to the key. These are interpreted as follows, depending on their type: =over =item * String: a package to which Test::MockRandom will export the symbol =item * Hash Reference: the key is the package to which Test::MockRandom will export the symbol and the value is the name under which it will be exported =item * Array Reference: a list of strings or hash references which will be handled as above =back =head2 Test::MockRandom-Eexport_rand_to() In order to intercept the built-in C<<< rand >>> in another package, Test::MockRandom must export its own C<<< rand >>> function to the target package B the target package is compiled, thus overriding calls to the built-in. The simple approach (described above) of providing the target package name in the C<<< use Test::MockRandom >>> statement accomplishes this because C<<< use >>> is equivalent to a C<<< require >>> and C<<< import >>> within a C<<< BEGIN >>> block. To explicitly intercept C<<< rand >>> in another package, you can also call C<<< export_rand_to >>>, but it must be enclosed in a C<<< BEGIN >>> block of its own. The explicit form also support function aliasing just as with the custom approach with C<<< use >>>, described above: use Test::MockRandom; BEGIN {Test::MockRandom->export_rand_to('AnotherPackage'=>'random')} use AnotherPackage; This C<<< BEGIN >>> block must not include a C<<< use >>> statement for the package to be intercepted, or perl will compile the package to be intercepted before the C<<< export_rand_to >>> function has a chance to execute and intercept calls to the built-in C<<< rand >>>. This is very important in testing. The C<<< export_rand_to >>> call must be in a separate C<<< BEGIN >>> block from a C<<< use >>> or C<<< use_ok >>> test, which should be enclosed in a C<<< BEGIN >>> block of its own: use Test::More tests => 1; use Test::MockRandom; BEGIN { Test::MockRandom->export_rand_to( 'AnotherPackage' ); } BEGIN { use_ok( 'AnotherPackage' ); } Given these cautions, it's probably best to use either the simple or custom approach with C<<< use >>>, which does the right thing in most circumstances. Should additional explicit customization be necessary, Test::MockRandom also provides C<<< export_srand_to >>> and C<<< export_oneish_to >>>. =head2 Overriding C<<< rand >>> globally: use Test::MockRandom 'CORE::GLOBAL' This is just like intercepting C<<< rand >>> in a package, except that you do it globally by overriding the built-in function in C<<< CORE::GLOBAL >>>. use Test::MockRandom 'CORE::GLOBAL'; # or BEGIN { Test::MockRandom->export_rand_to('CORE::GLOBAL') } You can always access the real, built-in C<<< rand >>> by calling it explicitly as C<<< CORE::rand >>>. =head2 Intercepting C<<< rand >>> in a package that also contains a C<<< rand >>> function This is tricky as the order in which the symbol table is manipulated will lead to very different results. This can be done safely (maybe) if the module uses the same rand syntaxEprototype as the system call but offers them up as method calls which resolve at run-time instead of compile time. In this case, you will need to do an explicit intercept (as above) but do it B importing the package. I.e.: use Test::MockRandom 'SomeRandPackage'; use SomeRandPackage; BEGIN { Test::MockRandom->export_rand_to('SomeRandPackage'); The first line is necessary to get C<<< srand >>> and C<<< oneish >>> exported to the current package. The second line will define a C<<< sub rand >>> in C<<< SomeRandPackage >>>, overriding the results of the first line. The third line then re-overrides the C<<< rand >>>. You may see warnings about C<<< rand >>> being redefined. Depending on how your C<<< rand >>> is written and used, there is a good likelihood that this isn't going to do what you're expecting, no matter what. If your package that defines C<<< rand >>> relies internally upon the system C<<< CORE::GLOBAL::rand >>> function, then you may be best off overriding that instead. =head1 FUNCTIONS =head2 C<<< new >>> $obj = new( LIST OF SEEDS ); Returns a new Test::MockRandom object with the specified list of seeds. =head2 C<<< srand >>> srand( LIST OF SEEDS ); $obj->srand( LIST OF SEEDS); If called as a bare function call or package method, sets the seed list for bareEpackage calls to C<<< rand >>>. If called as an object method, sets the seed list for that object only. =head2 C<<< rand >>> $rv = rand(); $rv = $obj->rand(); $rv = rand(3); If called as a bare or package function, returns the next value from the package seed list. If called as an object method, returns the next value from the object seed list. If C<<< rand >>> is called with a numeric argument, it follows the same behavior as the built-in function -- it multiplies the argument with the next value from the seed array (resulting in a random fractional value between 0 and the argument, just like the built-in). If the argument is 0, undef, or non-numeric, it is treated as if the argument is 1. Using this with an argument in testing may be complicated, as limits in floating point precision mean that direct numeric comparisons are not reliable. E.g. srand(1/3); rand(3); # does this return 1.0 or .999999999 etc. =head2 C<<< oneish >>> srand( oneish() ); if ( rand() == oneish() ) { print "It's almost one." }; A utility function to return a nearly-one value. Equal to ( 2^32 - 1 ) E 2^32. Useful in C<<< srand >>> and test functions. =head2 C<<< export_rand_to >>> Test::MockRandom->export_rand_to( 'Some::Class' ); Test::MockRandom->export_rand_to( 'Some::Class' => 'random' ); This function exports C<<< rand >>> into the specified package namespace. It must be called as a class function. If a second argument is provided, it is taken as the symbol name used in the other package as the alias to C<<< rand >>>: use Test::MockRandom; BEGIN { Test::MockRandom->export_rand_to( 'Some::Class' => 'random' ); } use Some::Class; srand (0.5); print Some::Class::random(); # prints 0.5 It can also be used to explicitly intercept C<<< rand >>> after Test::MockRandom has been loaded. The effect of this function is highly dependent on when it is called in the compile cycle and should usually called from within a BEGIN block. See L for details. Most users will not need this function. =head2 C<<< export_srand_to >>> Test::MockRandom->export_srand_to( 'Some::Class' ); Test::MockRandom->export_srand_to( 'Some::Class' => 'seed' ); This function exports C<<< srand >>> into the specified package namespace. It must be called as a class function. If a second argument is provided, it is taken as the symbol name to use in the other package as the alias for C<<< srand >>>. This function may be useful if another package wraps C<<< srand >>>: # In Some/Class.pm package Some::Class; sub seed { srand(shift) } sub foo { rand } # In a script use Test::MockRandom 'Some::Class'; BEGIN { Test::MockRandom->export_srand_to( 'Some::Class' ); } use Some::Class; seed(0.5); print foo(); # prints "0.5" The effect of this function is highly dependent on when it is called in the compile cycle and should usually be called from within a BEGIN block. See L for details. Most users will not need this function. =head2 C<<< export_oneish_to >>> Test::MockRandom->export_oneish_to( 'Some::Class' ); Test::MockRandom->export_oneish_to( 'Some::Class' => 'nearly_one' ); This function exports C<<< oneish >>> into the specified package namespace. It must be called as a class function. If a second argument is provided, it is taken as the symbol name to use in the other package as the alias for C<<< oneish >>>. Since C<<< oneish >>> is usually only used in a test script, this function is likely only necessary to alias C<<< oneish >>> to some other name in the current package: use Test::MockRandom 'Some::Class'; BEGIN { Test::MockRandom->export_oneish_to( __PACKAGE__, "one" ); } use Some::Class; seed( one() ); print foo(); # prints a value very close to one The effect of this function is highly dependent on when it is called in the compile cycle and should usually be called from within a BEGIN block. See L for details. Most users will not need this function. =head1 SEE ALSO =over =item * L =item * L =back =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan =head1 SUPPORT =head2 Bugs / Feature Requests Please report any bugs or feature requests through the issue tracker at L. You will be notified automatically of any progress on your issue. =head2 Source Code This is open source software. The code repository is available for public review and contribution under the terms of the license. L git clone https://github.com/dagolden/Test-MockRandom.git =head1 AUTHOR David Golden =head1 COPYRIGHT AND LICENSE This software is Copyright (c) 2014 by David Golden. This is free software, licensed under: The Apache License, Version 2.0, January 2004 =cut Test-MockRandom-1.01/examples/mock-rand.t000644 000765 000024 00000000411 12275406314 020512 0ustar00davidstaff000000 000000 package main; use strict; use warnings; use lib 'lib'; use Test::More tests => 2; use Test::MockRandom 'Test::Package'; srand( 0.23, 0.32 ); is( Test::Package::foo(), 0.23 ); is( Test::Package::foo(), 0.32 ); package Test::Package; sub foo { return rand() };