有一個很好的Perl模塊Time::HiRes。我在庫中大量使用它,並想寫一些測試。我發現,嘲笑的Perl time()
功能2個CPAN模塊,但他們兩人都不支持Time::HiRes:如何模擬Perl模塊時間:: HiRes
我怎麼能嘲笑Time::HiRes子gettimeofday()
?
PS我想修復我的模塊Time::ETA的測試。現在我用sleep
「模擬」,sometimes it works and sometimes it does not使用醜陋的黑客。
有一個很好的Perl模塊Time::HiRes。我在庫中大量使用它,並想寫一些測試。我發現,嘲笑的Perl time()
功能2個CPAN模塊,但他們兩人都不支持Time::HiRes:如何模擬Perl模塊時間:: HiRes
我怎麼能嘲笑Time::HiRes子gettimeofday()
?
PS我想修復我的模塊Time::ETA的測試。現在我用sleep
「模擬」,sometimes it works and sometimes it does not使用醜陋的黑客。
您可以編寫自己的模塊
與二十一點和妓女
模擬gettimeofday。通過測試:: MockTime的一些修改我寫道:
#!/usr/bin/perl
package myMockTime;
use strict;
use warnings;
use Exporter qw(import);
use Time::HiRes();
use Carp;
our @fixed =();
our $accel = 1;
our $otime = Time::HiRes::gettimeofday;
our @EXPORT_OK = qw(
set_fixed_time_of_day
gettimeofday
restore
throttle
);
sub gettimeofday() {
if (@fixed) {
return wantarray ? @fixed : "$fixed[0].$fixed[1]";
}
else {
return $otime + ((Time::HiRes::gettimeofday - $otime) * $accel);
}
}
sub set_fixed_time_of_day {
my ($time1, $time2) = @_;
if (! defined $time1 || ! defined $time2) {
croak('Incorrect usage');
}
@fixed = ($time1, $time2);
}
sub throttle {
my $self = shift @_;
return $accel unless @_;
my $new = shift @_;
$new or croak('Can not set throttle to zero');
$accel = $new;
}
sub restore {
@fixed =();
}
1;
我覺得它有很多錯誤的和不完整的functionallity,工作在這個方向
是的=)這是一個解決方案。但我希望這個問題已經解決了。我認爲最好不要編寫一個新模塊,而是嘗試修補Time :: Mock或Test :: MockTime。 – bessarabov
'使用出口商; * import = \&Exporter :: import; import()if 0;'可以寫'使用Exporter qw(import);' – ikegami
謝謝,它是從原始的Test :: MockTime + hack複製粘貼來禁用警告) – Suic
你要像'set_fixed_time_of_day'子? – Suic
我在想我想要什麼。我希望能夠在我的測試腳本中停止時間。運行'my $ ts = CORE :: time(); set_fixed_time($ ts);''(或類似的東西)'time()'和'Time :: HiRes :: gettimeofday()'將返回相同的值,直到我明確運行'$ ts ++; set_fixed_time($ ts);' – bessarabov