This shows you the differences between two versions of the page.
— |
kurs:test--more [2014/09/10 21:22] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | h2xs -AX TestDemo | ||
+ | <code> | ||
+ | package TestDemo; | ||
+ | |||
+ | use Moose; | ||
+ | use Color; | ||
+ | |||
+ | has 'testing' => (is => 'rw', isa => 'Int'); | ||
+ | |||
+ | sub ret1 { | ||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | |||
+ | sub get_color { | ||
+ | my $c = Color->new; | ||
+ | return $c; | ||
+ | } | ||
+ | |||
+ | |||
+ | sub add { | ||
+ | my $self = shift; | ||
+ | my ($a, $b) = @_; | ||
+ | return $a + $b; | ||
+ | } | ||
+ | |||
+ | |||
+ | sub _execute_ping { | ||
+ | my $self = shift; | ||
+ | my ($hostname) = @_; | ||
+ | my $pr; | ||
+ | if ($self->testing == 1) { | ||
+ | $pr = ' 0% packet loss'; | ||
+ | } else { | ||
+ | $pr = `ping -c 1 $hostname`; | ||
+ | } | ||
+ | return $pr; | ||
+ | } | ||
+ | |||
+ | |||
+ | sub _parse_ping { | ||
+ | my $self = shift; | ||
+ | my ($input) = @_; | ||
+ | return 1 if $input =~ / 0% packet loss/ms; | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | |||
+ | sub ping { | ||
+ | my $self = shift; | ||
+ | my ($hostname) = @_; | ||
+ | my $pr = $self->_execute_ping($hostname); | ||
+ | return $self->_parse_ping($pr); | ||
+ | } | ||
+ | |||
+ | 1; | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | ## t/TestDemo.t | ||
+ | use Test::More; | ||
+ | my $fixed_tests = 3; | ||
+ | |||
+ | my $t = YAML::LoadFile('t/add_data.yaml'); | ||
+ | plan tests => $fixed_tests + scalar @$t; | ||
+ | BEGIN { use_ok('TestDemo') }; | ||
+ | use YAML; | ||
+ | use Data::Dumper; | ||
+ | |||
+ | ######################### | ||
+ | |||
+ | # Insert your test code below, the Test::More module is use()ed here so read | ||
+ | # its man page ( perldoc Test::More ) for help writing this test script. | ||
+ | |||
+ | my $td = TestDemo->new(); | ||
+ | |||
+ | is($td->ret1, 1, 'Returns 1'); | ||
+ | |||
+ | isa_ok($td->get_color, 'Color', 'Is object of type Color'); | ||
+ | |||
+ | foreach (@$t) { | ||
+ | is($td->add($_->[0], $_->[1]), $_->[2], 'Add correct'); | ||
+ | } | ||
+ | |||
+ | $td->testing(1); | ||
+ | is($td->ping('8.6.5.5'), 1, 'Ping OK'); | ||
+ | |||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | ## add_data.yaml | ||
+ | - | ||
+ | - 1 | ||
+ | - 2 | ||
+ | - 3 | ||
+ | - | ||
+ | - 3 | ||
+ | - 4 | ||
+ | - 7 | ||
+ | - | ||
+ | - 3 | ||
+ | - 3 | ||
+ | - 6 | ||
+ | </code> |