File: Time.pm

package info (click to toggle)
libtest-time-perl 0.092-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: perl: 4,540; makefile: 2
file content (103 lines) | stat: -rw-r--r-- 1,784 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package Test::Time;
use strict;
use warnings;

use Test::More;

our $VERSION = '0.092';
our $time = CORE::time();

my $pkg = __PACKAGE__;
my $in_effect = 1;

sub in_effect {
	$in_effect;
}

sub __time () {
	if (in_effect) {
		$time;
	} else {
		CORE::time();
	}
}

sub __sleep (;$) {
	if (in_effect) {
		my $sleep = shift || 1;
		$time += $sleep;
		note "sleep $sleep";
	} else {
		CORE::sleep(shift);
	}
}

sub __localtime (;$) {
	my $arg = shift;
	if (in_effect) {
		$arg ||= $time;
	}
	return defined $arg ? CORE::localtime($arg) : CORE::localtime();
}

sub import {
	my ($class, %opts) = @_;
	$in_effect = 1;
	$time = $opts{time} if defined $opts{time};

	*CORE::GLOBAL::time = \&__time;
	*CORE::GLOBAL::sleep = \&__sleep;
	*CORE::GLOBAL::localtime = \&__localtime;
};

sub unimport {
	$in_effect = 0;
}

1;
__END__

=encoding utf8

=head1 NAME

Test::Time - Overrides the time() and sleep() core functions for testing

=head1 SYNOPSIS

    use Test::Time;

    # Freeze time
    my $now = time();

    # Increment internal time (returns immediately)
    sleep 1;

    # Return internal time incremented by 1
    my $then = time();


=head1 DESCRIPTION

Test::Time can be used to test modules that deal with time. Once you C<use> this
module, all references to C<time>, C<localtime> and C<sleep> will be internalized.
You can set custom time by passing time => number after the C<use> statement:

    use Test::Time time => 1;

    my $now = time;    # $now is equal to 1
    sleep 300;         # returns immediately, displaying a note
    my $then = time;   # $then equals to 301

=head1 AUTHOR

cho45 E<lt>cho45@lowreal.netE<gt>

=head1 SEE ALSO

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut