Last active
January 2, 2016 04:49
-
-
Save ishiduca/8252581 to your computer and use it in GitHub Desktop.
TDD use Perl ref: http://qiita.com/ishiduca/items/c587307ef61c5cf9ae19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use utf8; | |
BEGIN { | |
binmode STDOUT, ":utf8"; | |
binmode STDERR, ":utf8"; | |
} | |
use Greeter; | |
use Test::More; | |
use Test::MockDateTime; | |
subtest 'Greeter->new で インスタンスの生成ができているか' => sub { | |
my $g = Greeter->new; | |
ok $g, 'my $g = Greeter->new'; | |
ok $g->greet, '$g has "greet" method'; | |
}; | |
subtest '$g->greet で時刻に合わせた適切な挨拶を返すか' => sub { | |
my $g = Greeter->new; | |
my $ymd = '2013-01-01'; | |
my $_050000 = "$ymd 05:00:00"; | |
on $_050000 => sub { | |
is $g->greet, 'Good Morning', "$_050000 => Good Morning"; | |
}; | |
my $_115959 = "$ymd 11:59:59"; | |
on $_115959 => sub { | |
is $g->greet, 'Good Morning', "$_115959 => Good Morning"; | |
}; | |
my $_120000 = "$ymd 12:00:00"; | |
on $_120000 => sub { | |
is $g->greet, 'Good Afternoon', "$_120000 => Good Afternoon"; | |
}; | |
my $_180000 = "$ymd 18:00:00"; | |
on $_180000 => sub { | |
is $g->greet, 'Good Evening', "$_180000 => Good Evening"; | |
}; | |
my $_045959 = "$ymd 04:59:59"; | |
on $_045959 => sub { | |
is $g->greet, 'Good Evening', "$_045959 => Good Evening"; | |
}; | |
}; | |
done_testing; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requires 'perl', '5.008001'; | |
requires 'DateTime', '1.06'; | |
on 'test' => sub { | |
requires 'Test::More', '0.98'; | |
requires 'Test::MockDateTime', '0.02'; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Greeter; | |
use 5.008005; | |
use strict; | |
use Carp; | |
our $VERSION = "0.01"; | |
sub new { | |
my $class = shift; | |
bless +{} => $class; | |
}; | |
1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ minil new Dest # Dest はディストリビューション名。適当な名前をつける | |
$ cd Dest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ vi cpanfile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cartonで依存モジュールを管理している場合 | |
$ carton exec -- prove -lv | |
# cpanm (か cpan, cpanplusでモジュール)をインストールしている場合 | |
$ prove -lv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
t/00_compile.t .. | |
ok 1 - use Greeter; | |
1..1 | |
ok | |
t/basic.t ....... | |
# Subtest: newメソッドでインスタンスを作れるか | |
ok 1 - $dest = Dest->new | |
1..1 | |
ok 1 - Subtest: newメソッドでインスタンスを作れるか | |
1..1 | |
ok | |
All tests successful. | |
Files=2, Tests=2, 0 wallclock secs ( 0.03 usr 0.01 sys + 0.31 cusr 0.03 csys = 0.38 CPU) | |
Result: PASS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pfswatch . -e carton -- prove -lv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pfswatch . -e carton -- prove -PPretty -lv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cpanm --installdeps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ carton install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ vi t/basic.t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ carton exec -- prove -lv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ carton exec -- prove -PPretty -lv # カラフルで見やすい |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ vi lib/Dest.pm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Greeter; | |
use 5.008005; | |
use strict; | |
use Carp; | |
use DateTime; | |
our $VERSION = "0.01"; | |
sub new { | |
my $class = shift; | |
bless { | |
time_zone => 'Asia/Tokyo', | |
greets => [ 'Good Morning', 'Good Afternoon', 'Good Evening' ], | |
}, $class; | |
}; | |
sub greet { | |
my $self = shift; | |
my $now = DateTime->now( time_zone => $self->{time_zone} ); | |
$self->{greets}[ $self->_get_index($now) ]; | |
}; | |
sub _get_index { | |
my $self = shift; | |
my $now = shift; | |
my $offset = ($now->hour * 60 + $now->minute); | |
($offset >= (60 * 18)) ? 2 : | |
($offset >= (60 * 12)) ? 1 : | |
($offset >= (60 * 5)) ? 0 : 2; | |
}; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment