Pacific-Design.com

    
Home Index

1. Perl5

2. Unit Test

+ Moose

Perl5 / Unit Test /

#!/usr/bin/perl
use strict;
use warnings;
use Test::More 'no_plan';
use Test::Exception;

use constant LOG_COMMENT_CHARACTER => '#';
my $foo;

sub test
{
    my $description = shift;
    my $block       = shift;

    print "\n" . LOG_COMMENT_CHARACTER . "-----" . $description . "\n";
    set_up();
    &$block;
    tear_down();
}

sub set_up
{
    $foo = Test::SillyExample::Foo->new();
}

sub tear_down
{

    #tear down code goes here
}

test(
    'foo starts life with low aspirations',
    sub {
        is($foo->bar, LOW, 'foo has low aspirations');
        is($foo->is_high_achiever, FALSE '');
    }
);

test(
    'foo raises the bar',
    sub {
        $foo->set_bar(HIGH);
        is($foo->bar, HIGH, 'Should now have high aspirations');
        ok($foo->is_high_achiever, 'Should be high achiever');
    }
);

test(
    'foo cant be demoralised',
    sub {
        $foo->set_bar(HIGH);

        throws_ok(
            sub {
                $foo->set_bar(LOW);
            },
            qr/Should not let others lower the bar/,
            'Should not allow lowering of bar'
                 );

        ok($foo->is_high_achiever, 'Should still be a high achiever');
    }
);