sironekotoroの日記

Perl で楽をしたい

Perl入学式 #5 (自習)最終問題

テストの為にやり直し

以下の機能があるYAPCモジュールを実装してください
- 来年のYAPC::Asiaは2014年8月28日から30日に開催予定です. 日付を教えてくれるモジュールを2人で作りましょう
- YAPC::year()で年を4桁の整数で返します(テストをAの人が, コードをBの人が書きましょう)
- YAPC::month()で月を2桁の整数で返します(テストをBの人が, コードをAの人が書きましょう)
- YAPC::day()で日付を2桁の整数で返します(テストをAの人が, コードをBの人が書きましょう)
- YAPC::is_yet()で開催前か開催後かを真か偽で返します(テストをBの人が, コードをAの人が書きましょう)

  • もちろん一人なんでごりごりがんばる。

  • が、テストのところでつまずく。

    • モジュール側でわざわざTime::Pieceで開催日時を指定してたのが裏目に出た。
  • レッツやり直し

    • 開催日時をシリアル秒で認識するという・・・結局、Test::Mocktimeを使えなかった。
# last_lesson.pl
use strict;
use warnings;

use lib::YAPC;

use Time::Piece;
my $now = localtime->epoch;

print "next YAPC year is " , YAPC::year() , "\n";

print "next YAPC month is " , YAPC::month() , "\n";

print "next YAPC day is " , YAPC::day() , "\n";

print "YAPC::Asia 2014 is " , YAPC::is_yet($now) , "\n";

#YAPC.pm

use strict;
use warnings;

package YAPC {

    sub yapc2014 {
        return '2014-08-28';
    }

    sub year {
        return '2014';
    }

    sub month {
        return '08';

    }

    sub day {
        return '28,29,30';
    }

    sub is_yet {
        my $time = shift @_;
        if ( $time == 0 ) {
            return "undef";
        }
        elsif ( $time <= 1409151599 ) {
            return "yet";
        }
        elsif ( $time >= 1409151600 && $time <= 1409410799 ) {
            return "Now On";
        }
        else {
            "YAPC2014 is gone";
        }
    }

}

1;
#testYAPC.pl

use strict;
use warnings;

use lib::YAPC;

use Test::More;

# 2014/08/28 のepoch 1409151600
# 2014/08/30 のepoch 1409410800

is (YAPC::is_yet(1409151599) , 'yet') ;
is (YAPC::is_yet(1409151600) , 'Now On') ;
is (YAPC::is_yet(1409410799) , 'Now On') ;
is (YAPC::is_yet(1409410800) , 'YAPC2014 is gone') ;

done_testing();