1
exercism fetch perl5 robot-name

robot_name.t

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env perl
use strict;
use warnings;

use Test::More;
use FindBin;
my $dir;
BEGIN { $dir = $FindBin::Bin . '/' };
use lib $dir;

my $module = $ENV{EXERCISM} ? 'Example' : 'RobotName';

plan tests => 9;

ok -e "${dir}${module}.pm", "Missing $module.pm"
    or BAIL_OUT("You need to create a module called $module.pm with a constructor and methods called name() and reset_name().");

eval "use $module";
ok !$@, "Cannot load $module.pm"
    or BAIL_OUT("Does $module.pm compile?  Does it end with 1; ?");

can_ok($module, 'new')  or BAIL_OUT("Missing package $module with sub sub new()");
can_ok($module, 'name')  or BAIL_OUT("Missing package $module with sub sub name()");
can_ok($module, 'reset_name') or BAIL_OUT("Missing package $module with sub sub reset()");

# robot method tests
my $robot = $module->new;
my $name = $robot->name;

ok $robot->name =~ /[A-Z]{2}[0-9]{3}/, 'Name should match schema';
is $name, $robot->name, 'Name should be persistent';
ok $robot->name ne $module->new->name, 'Robots should have different names';
ok $robot->reset_name ne $name, 'reset_name should change the robot name';