Sunday, January 16, 2011

Writing Perl Tests with Test::More

#!/usr/bin/perl

package MyMaths;
use strict;

sub new
{
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my $self = {};

  bless($self, $class);
  return $self;
}

sub add
{
   my $self = shift;
   my $total = 0;
   foreach my $factor (@_)
{
   $total += $factor;
}
   return $total;
}
 
1;



#!/usr/bin/perl
 
use Test::More tests=>6;
use MyMaths;

$mymaths = new MyMaths;
is( $mymaths->add(1,2,3), 6, "1 + 2 + 3 = 6");
is( $mymaths->add(6,2), 8,"6 + 2 = 8");
is( $mymaths->add(1,2,3,4), 10, "1 + 2 + 3 + 4 = 10");
is( $mymaths->add(1,2), 3, "1 + 2 = 3");
is( $mymaths->add(2), 2, "2 = 2");
is( $mymaths->add(2,-1), 1, "2 + -1 = 1");

No comments: