Test-MockTime-0.15/0000755000076400007640000000000012572035610012515 5ustar davedaveTest-MockTime-0.15/MANIFEST0000644000076400007640000000043412572035610013647 0ustar davedaveMANIFEST README Makefile.PL Changes t/export.t t/test.t t/pod.t t/string-time.t t/prototypes.t lib/Test/MockTime.pm META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Test-MockTime-0.15/Changes0000644000076400007640000000321712572034612014014 0ustar davedaveCHANGES ------- 0.15 - 03 Sep 2015 * Corrected README. Merged POD into pm file 0.14 - 03 Sep 2015 * fixing POD code examples as per RT #104088. 0.13 - 07 Aug 2014 * explicit license in META as per RT #97806 * included other updates to Makefile.PL details (author, etc) 0.12 - 09 Apr 2009 * removing t/DateCalc.t and lib/Test/MockTime/DateCalc.pm to allow Kevin Ryde to maintain a separate module. 0.11 - 08 Apr 2009 * now including it in Manifest file 0.10 - 08 Apr 2009 * including t/DateCalc.t and lib/Test/MockTime/DateCalc.pm from Kevin Ryde in an attempt to work with Date::Calc * This changeset may need to be broken out into a separate module, depending on how much work is required on it 0.09 - 30 June 2008 * including t/pod.t 0.08 - 30 June 2008 * fixing Pod with a patch from Ansgar Burchardt. 0.07 - 05 September 2007 * including the missing t/prototypes.t into the MANIFEST so it actually gets included. *ZONK* 0.06 - 04 September 2007 * fixing a call to Exporter to allow Test::MockTime on perl 5.6.1 to run, although still with warnings about prototypes * including the missing t/prototypes.t that Peter sent me. 0.05 - 28 December 2006 * Patch from Peter du Marchie van Voorthuysen to give the correct function prototypes for gmtime, localtime and time * now only requiring Time::Piece when using specs 0.04 - 31 July 2006 * Patch from Michael Hendricks to allow importing all subroutines 0.03 - 30 July 2006 * Patch from Michael Hendricks to allow dates in formats other than seconds since the unix epoch Test-MockTime-0.15/META.json0000664000076400007640000000167212572035610014146 0ustar davedave{ "abstract" : "Replaces actual time with simulated time ", "author" : [ "David Dick " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.04, CPAN::Meta::Converter version 2.150001", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Test-MockTime", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Test::More" : "0", "Time::Local" : "0", "Time::Piece" : "1.08" } } }, "release_status" : "stable", "version" : "0.15" } Test-MockTime-0.15/META.yml0000664000076400007640000000104412572035610013767 0ustar davedave--- abstract: 'Replaces actual time with simulated time ' author: - 'David Dick ' build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.04, CPAN::Meta::Converter version 2.150001' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Test-MockTime no_index: directory: - t - inc requires: Test::More: '0' Time::Local: '0' Time::Piece: '1.08' version: '0.15' Test-MockTime-0.15/lib/0000755000076400007640000000000012572035610013263 5ustar davedaveTest-MockTime-0.15/lib/Test/0000755000076400007640000000000012572035610014202 5ustar davedaveTest-MockTime-0.15/lib/Test/MockTime.pm0000755000076400007640000001476112572035574016275 0ustar davedavepackage Test::MockTime; use strict; use warnings; use Carp(); use Exporter(); *import = \&Exporter::import; our @EXPORT_OK = qw( set_relative_time set_absolute_time set_fixed_time restore_time ); our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, ); our $VERSION = '0.15'; our $offset = 0; our $fixed = undef; BEGIN { *CORE::GLOBAL::time = \&Test::MockTime::time; *CORE::GLOBAL::localtime = \&Test::MockTime::localtime; *CORE::GLOBAL::gmtime = \&Test::MockTime::gmtime; } sub set_relative_time { my ($relative) = @_; if ( ( $relative eq __PACKAGE__ ) || ( UNIVERSAL::isa( $relative, __PACKAGE__ ) ) ) { Carp::carp("Test::MockTime::set_relative_time called incorrectly\n"); } $offset = $_[-1]; # last argument. might have been called in a OO syntax? return $offset; } sub _time { my ( $time, $spec ) = @_; if ( $time !~ /\A -? \d+ \z/xms ) { $spec ||= '%Y-%m-%dT%H:%M:%SZ'; } if ($spec) { require Time::Piece; $time = Time::Piece->strptime( $time, $spec )->epoch(); } return $time; } sub set_absolute_time { my ( $time, $spec ) = @_; if ( ( $time eq __PACKAGE__ ) || ( UNIVERSAL::isa( $time, __PACKAGE__ ) ) ) { Carp::carp("Test::MockTime::set_absolute_time called incorrectly\n"); } $time = _time( $time, $spec ); $offset = $time - CORE::time; return $offset; } sub set_fixed_time { my ( $time, $spec ) = @_; if ( ( $time eq __PACKAGE__ ) || ( UNIVERSAL::isa( $time, __PACKAGE__ ) ) ) { Carp::carp("Test::MockTime::set_fixed_time called incorrectly\n"); } $time = _time( $time, $spec ); $fixed = $time; return $fixed; } sub time() { if ( defined $fixed ) { return $fixed; } else { return ( CORE::time + $offset ); } } sub localtime (;$) { my ($time) = @_; if ( !defined $time ) { $time = Test::MockTime::time(); } return CORE::localtime($time); } sub gmtime (;$) { my ($time) = @_; if ( !defined $time ) { $time = Test::MockTime::time(); } return CORE::gmtime($time); } sub restore { $offset = 0; $fixed = undef; return; } *restore_time = \&restore; 1; __END__ =head1 NAME Test::MockTime - Replaces actual time with simulated time =head1 VERSION Version 0.15 =head1 SYNOPSIS use Test::MockTime qw( :all ); set_relative_time(-600); # do some tests depending on time increasing from 600 seconds ago set_absolute_time(0); # do some more tests depending on time starting from the epoch # epoch may vary according to platform. see perlport. set_fixed_time(CORE::time()); # do some more tests depending on time staying at the current actual time set_absolute_time('1970-01-01T00:00:00Z'); # do some tests depending on time starting at Unix epoch time set_fixed_time('01/01/1970 00:00:00', '%m/%d/%Y %H:%M:%S'); # do some tests depending on time staying at the Unix epoch time restore_time(); # resume normal service =head1 DESCRIPTION This module was created to enable test suites to test code at specific points in time. Specifically it overrides localtime, gmtime and time at compile time and then relies on the user supplying a mock time via set_relative_time, set_absolute_time or set_fixed_time to alter future calls to gmtime,time or localtime. =head1 SUBROUTINES/METHODS =over =item set_absolute_time If given a single, numeric argument, the argument is an absolute time (for example, if 0 is supplied, the absolute time will be the epoch), and calculates the offset to allow subsequent calls to time, gmtime and localtime to reflect this. for example, in the following code Test::MockTime::set_absolute_time(0); my ($start) = time; sleep 2; my ($end) = time; The $end variable should contain 2 seconds past the epoch; If given two arguments, the first argument is taken to be an absolute time in some string format (for example, "01/01/1970 00:00:00"). The second argument is taken to be a C format string (for example, "%m/%d/%Y %H:%M:%S"). If a single argument is given, but that argument is not numeric, a C format string of "%Y-%m-%dT%H:%M:%SZ" is assumed. for example, in the following code Test::MockTime::set_absolute_time('1970-01-01T00:00:00Z'); my ($start) = time; sleep 2; my ($end) = time; The $end variable should contain 2 seconds past the Unix epoch; =item set_relative_time($relative) takes as an argument an relative value from current time (for example, if -10 is supplied, current time be converted to actual machine time - 10 seconds) and calculates the offset to allow subsequent calls to time,gmtime and localtime to reflect this. for example, in the following code my ($start) = time; Test::MockTime::set_relative_time(-600); sleep 600; my ($end) = time; The $end variable should contain either the same or very similar values to the $start variable. =item set_fixed_time If given a single, numeric argument, the argument is an absolute time (for example, if 0 is supplied, the absolute time will be the epoch). All subsequent calls to gmtime, localtime and time will return this value. for example, in the following code Test::MockTime::set_fixed_time(time) my ($start) = time; sleep 3; my ($end) = time; the $end variable and the $start variable will contain the same results If given two arguments, the first argument is taken to be an absolute time in some string format (for example, "01/01/1970 00:00:00"). The second argument is taken to be a C format string (for example, "%m/%d/%Y %H:%M:%S"). If a single argument is given, but that argument is not numeric, a C format string of "%Y-%m-%dT%H:%M:%SZ" is assumed. =item restore() restore the default time handling values. C is an alias. When exported with the 'all' tag, this subroutine is exported as C. =back =head1 CONFIGURATION AND ENVIRONMENT Test::MockTime requires no configuration files or environment variables. =head1 DEPENDENCIES Test::MockTime depends on the following non-core Perl modules. =over =item * L =back =head1 INCOMPATIBILITIES None reported =head1 BUGS AND LIMITATIONS Probably. =head1 AUTHOR David Dick =head1 LICENSE AND COPYRIGHT This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut =head1 ACKNOWLEDGEMENTS Thanks to a use.perl.org journal entry by Geoffrey Young. Test-MockTime-0.15/Makefile.PL0000644000076400007640000000071112572034517014473 0ustar davedave#!perl use strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( 'NAME' => 'Test::MockTime', 'AUTHOR' => q{David Dick }, 'VERSION_FROM' => 'lib/Test/MockTime.pm', 'ABSTRACT_FROM' => 'lib/Test/MockTime.pm', ($ExtUtils::MakeMaker::VERSION >= 6.3002 ? ('LICENSE'=> 'perl') : ()), 'PREREQ_PM' => { 'Test::More' => 0, 'Time::Local' => 0, 'Time::Piece' => '1.08', }, ); Test-MockTime-0.15/README0000644000076400007640000001112512572035576013410 0ustar davedaveNAME Test::MockTime - Replaces actual time with simulated time VERSION Version 0.15 SYNOPSIS use Test::MockTime qw( :all ); set_relative_time(-600); # do some tests depending on time increasing from 600 seconds ago set_absolute_time(0); # do some more tests depending on time starting from the epoch # epoch may vary according to platform. see perlport. set_fixed_time(CORE::time()); # do some more tests depending on time staying at the current actual time set_absolute_time('1970-01-01T00:00:00Z'); # do some tests depending on time starting at Unix epoch time set_fixed_time('01/01/1970 00:00:00', '%m/%d/%Y %H:%M:%S'); # do some tests depending on time staying at the Unix epoch time restore_time(); # resume normal service DESCRIPTION This module was created to enable test suites to test code at specific points in time. Specifically it overrides localtime, gmtime and time at compile time and then relies on the user supplying a mock time via set_relative_time, set_absolute_time or set_fixed_time to alter future calls to gmtime,time or localtime. SUBROUTINES/METHODS set_absolute_time If given a single, numeric argument, the argument is an absolute time (for example, if 0 is supplied, the absolute time will be the epoch), and calculates the offset to allow subsequent calls to time, gmtime and localtime to reflect this. for example, in the following code Test::MockTime::set_absolute_time(0); my ($start) = time; sleep 2; my ($end) = time; The $end variable should contain 2 seconds past the epoch; If given two arguments, the first argument is taken to be an absolute time in some string format (for example, "01/01/1970 00:00:00"). The second argument is taken to be a strptime format string (for example, "%m/%d/%Y %H:%M:%S"). If a single argument is given, but that argument is not numeric, a strptime format string of "%Y-%m-%dT%H:%M:%SZ" is assumed. for example, in the following code Test::MockTime::set_absolute_time('1970-01-01T00:00:00Z'); my ($start) = time; sleep 2; my ($end) = time; The $end variable should contain 2 seconds past the Unix epoch; set_relative_time($relative) takes as an argument an relative value from current time (for example, if -10 is supplied, current time be converted to actual machine time - 10 seconds) and calculates the offset to allow subsequent calls to time,gmtime and localtime to reflect this. for example, in the following code my ($start) = time; Test::MockTime::set_relative_time(-600); sleep 600; my ($end) = time; The $end variable should contain either the same or very similar values to the $start variable. set_fixed_time If given a single, numeric argument, the argument is an absolute time (for example, if 0 is supplied, the absolute time will be the epoch). All subsequent calls to gmtime, localtime and time will return this value. for example, in the following code Test::MockTime::set_fixed_time(time) my ($start) = time; sleep 3; my ($end) = time; the $end variable and the $start variable will contain the same results If given two arguments, the first argument is taken to be an absolute time in some string format (for example, "01/01/1970 00:00:00"). The second argument is taken to be a strptime format string (for example, "%m/%d/%Y %H:%M:%S"). If a single argument is given, but that argument is not numeric, a strptime format string of "%Y-%m-%dT%H:%M:%SZ" is assumed. restore() restore the default time handling values. restore_time is an alias. When exported with the 'all' tag, this subroutine is exported as restore_time. CONFIGURATION AND ENVIRONMENT Test::MockTime requires no configuration files or environment variables. DEPENDENCIES Test::MockTime depends on the following non-core Perl modules. * Time::Piece 1.08 or greater INCOMPATIBILITIES None reported BUGS AND LIMITATIONS Probably. AUTHOR David Dick LICENSE AND COPYRIGHT This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. ACKNOWLEDGEMENTS Thanks to a use.perl.org journal entry by Geoffrey Young. Test-MockTime-0.15/t/0000755000076400007640000000000012572035610012760 5ustar davedaveTest-MockTime-0.15/t/export.t0000644000076400007640000000034310466723657014505 0ustar davedaveuse strict; use warnings; use Test::More tests => 1; use Test::MockTime qw( :all ); eval{ set_relative_time(1); set_absolute_time(2); set_fixed_time(3); restore_time; }; is( $@, q{}, ':all export tag works' ); Test-MockTime-0.15/t/string-time.t0000644000076400007640000000526010466723657015431 0ustar davedave#! /usr/bin/perl use Test::MockTime(); use Test::More(tests => 18); use Time::Local(); use strict; use warnings; my ($mock, $real); # determine the correct epoch value for our test time my $TRUE = Time::Local::timegm(19, 25, 3, 30, 6, 2006); # set_absolute_time with a defaulted date spec Test::MockTime::set_absolute_time('2006-07-30T03:25:19Z'); $mock = time; ok(($mock >= $TRUE) && ($mock <= $TRUE+1), "Absolute time works"); sleep 2; $mock = time; ok(($mock >= $TRUE+2) && ($mock <= $TRUE+3), "Absolute time is still in sync after two seconds sleep:$mock"); $mock = Time::Local::timelocal(localtime); $real = Time::Local::timelocal(CORE::localtime); ok($mock <= $real, "localtime seems ok"); # set_absolute_time with an explicit date spec Test::MockTime::set_absolute_time('03:25:19 07/30/2006', '%H:%M:%S %m/%d/%Y'); $mock = time; ok(($mock >= $TRUE) && ($mock <= $TRUE+1), "Absolute time with explicit date specworks"); sleep 2; $mock = time; ok(($mock >= $TRUE+2) && ($mock <= $TRUE+3), "Absolute time is still in sync after two seconds sleep:$mock"); $real = Time::Local::timelocal(CORE::localtime); ok($mock <= $real, "localtime seems ok"); # try set_fixed_time with a defaulted date spec Test::MockTime::set_fixed_time('2006-07-30T03:25:19Z'); $real = time; sleep 2; $mock = time; cmp_ok($mock, '==', $real, "time is fixed"); cmp_ok($mock, '==', $TRUE, "time is fixed correctly"); Test::MockTime::set_fixed_time('2006-07-30T03:25:19Z'); $mock = Time::Local::timelocal(localtime()); sleep 2; $real = Time::Local::timelocal(localtime); cmp_ok($mock, '==', $real, "localtime is fixed"); cmp_ok($mock, '==', $TRUE, "localtime is fixed correctly"); Test::MockTime::set_fixed_time('2006-07-30T03:25:19Z'); $mock = Time::Local::timegm(gmtime); sleep 2; $real = Time::Local::timegm(gmtime); cmp_ok($mock, '==', $real, "gmtime is fixed"); cmp_ok($mock, '==', $TRUE, "gmtime is fixed correctly"); # try set_fixed_time with an explicit date spec Test::MockTime::set_fixed_time('03:25:19 07/30/2006', '%H:%M:%S %m/%d/%Y'); $real = time; sleep 2; $mock = time; cmp_ok($mock, '==', $real, "time is fixed with explicit date spec"); cmp_ok($mock, '==', $TRUE, "time is fixed correctly"); Test::MockTime::set_fixed_time('03:25:19 07/30/2006', '%H:%M:%S %m/%d/%Y'); $mock = Time::Local::timelocal(localtime()); sleep 2; $real = Time::Local::timelocal(localtime); cmp_ok($mock, '==', $real, "localtime is fixed"); cmp_ok($mock, '==', $TRUE, "localtime is fixed correctly"); Test::MockTime::set_fixed_time('03:25:19 07/30/2006', '%H:%M:%S %m/%d/%Y'); $mock = Time::Local::timegm(gmtime); sleep 2; $real = Time::Local::timegm(gmtime); cmp_ok($mock, '==', $real, "gmtime is fixed"); cmp_ok($mock, '==', $TRUE, "gmtime is fixed correctly"); Test-MockTime-0.15/t/pod.t0000644000076400007640000000020311032000756013713 0ustar davedaveuse Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); Test-MockTime-0.15/t/prototypes.t0000644000076400007640000000076410544724361015411 0ustar davedave#! /usr/bin/perl use Test::MockTime ':all'; use Test::More tests => 3; use strict; use warnings; set_fixed_time(2); my $four = time + 2; is($four, 4, "time() does not try so slurp any arguments"); my @arr = (0, 1, 2); my $got = localtime @arr; my $expect = localtime scalar @arr; is($got, $expect, "localtime() treats its argument as an expression"); $got = gmtime @arr; $expect = gmtime scalar @arr; is($got, $expect, "gmtime() treats its argument as an expression"); Test-MockTime-0.15/t/test.t0000755000076400007640000000406210217203001014112 0ustar davedave#! /usr/bin/perl use Test::MockTime(); use Test::More(tests => 11); use Time::Local(); use strict; use warnings; my ($mock, $real); $mock = time; $real = CORE::time; ok(($mock == $real) || (($mock + 1) == $real) || (($mock - 1) == $real), "Starting time " . localtime($mock)); # previous two statements might go over a second boundary Test::MockTime::set_relative_time(-600); $mock = time; $real = CORE::time; sleep 2; $mock = time; $mock += 600; $real = CORE::time; ok(($mock == $real) || (($mock + 1) == $real) || (($mock - 1) == $real), "Set time to be 10 minutes ago, slept for two seconds, time was still in sync"); $mock = Time::Local::timelocal(localtime); $real = Time::Local::timelocal(CORE::localtime); $mock = $mock + 600; ok(($mock == $real) || (($mock + 1) == $real) || (($mock - 1) == $real), "localtime was also still in sync"); Test::MockTime::set_relative_time(+2); $mock = time; $real = CORE::time; sleep 2; $mock = time; $real = CORE::time; ok($mock >= ($real + 1), "Set time to be 2 seconds in the future, slept for three seconds, time was still in front"); $mock = Time::Local::timelocal(localtime); $real = Time::Local::timelocal(CORE::localtime); ok($mock >= ($real + 1), "localtime was also still in front"); Test::MockTime::set_absolute_time(100); $mock = time; ok(($mock >= 100) && ($mock <= 101), "Absolute time works"); sleep 2; $mock = time; ok(($mock >= 102) && ($mock <= 103), "Absolute time is still in sync after two seconds sleep:$mock"); $mock = Time::Local::timelocal(localtime); $real = Time::Local::timelocal(CORE::localtime); ok($mock <= $real, "localtime seems ok"); Test::MockTime::set_fixed_time(CORE::time); $real = time; sleep 2; $mock = time; ok($mock == $real, "fixed time correctly"); Test::MockTime::set_fixed_time(CORE::time); $mock = Time::Local::timelocal(localtime()); sleep 2; $real = Time::Local::timelocal(localtime); ok($mock eq $real, "fixed localtime correctly"); Test::MockTime::set_fixed_time(CORE::time); $mock = Time::Local::timegm(gmtime); sleep 2; $real = Time::Local::timegm(gmtime); ok($mock eq $real, "fixed gmtime correctly");