Skip to content

Commit db72a5a

Browse files
committed
Convert robot-name to use generator (#249)
1 parent 91627ac commit db72a5a

File tree

10 files changed

+115
-53
lines changed

10 files changed

+115
-53
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
tmp
55
bin/configlet
66
bin/configlet.exe
7+
**/cpanfile.snapshot
8+
**/local
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
exercise: RobotName
2+
package_comment: "# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)"
3+
lib_comment: '# Find modules in the same dir as this file.'
4+
plan_comment: '# This is how many tests we expect to run.'
5+
6+
moo: true
7+
methods: new name reset_name
8+
9+
plan: 7
10+
# plan includes can_ok of `methods` plus the tests below.
11+
12+
# Tests: inline here, since there is no canonical-data.json for this exercise
13+
tests: |-
14+
my $robot = RobotName->new;
15+
isa_ok $robot, 'RobotName';
16+
17+
my $name = $robot->name;
18+
like $robot->name, qr/^[A-Z]{2}[0-9]{3}$/, 'Name should match schema';
19+
is $name, $robot->name, 'Name should be persistent';
20+
isnt $robot->name, RobotName->new->name,
21+
'Robots should have different names';
22+
isnt $robot->reset_name, $name,
23+
'reset_name should change the robot name';
24+
ok $robot->name, 'reset_name should not leave the name empty';
25+
26+
example: |-
27+
# Declare a "name" attribute that is is 'rwp', read-write protected:
28+
# read-only to consumers, but settable using $self->_set_name
29+
has name => ( is => 'rwp' );
30+
31+
sub BUILD {
32+
my ($self) = @_;
33+
$self->reset_name;
34+
}
35+
36+
sub reset_name {
37+
my ($self) = @_;
38+
$self->_set_name( _rand_letter() . _rand_letter() . _suffix() );
39+
return $self->name;
40+
}
41+
42+
sub _rand_letter {
43+
my @letters = 'A' .. 'Z';
44+
return $letters[ int rand $#letters ];
45+
}
46+
47+
sub _suffix {
48+
return sprintf('%03d', int rand 1000);
49+
}
50+
51+
stub: |-
52+
# Declare a "name" attribute that is is 'rwp', read-write protected:
53+
# read-only to consumers, but settable using $self->_set_name
54+
has name => ( is => 'rwp' );
55+
56+
sub reset_name {
57+
my ($self) = @_;
58+
return undef; # Replace this with your own code to pass the tests.
59+
}
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1+
# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)
12
package RobotName;
2-
use warnings;
3-
use strict;
3+
use Moo;
44

5-
sub new {
6-
my $class = shift;
7-
return bless {}, $class;
8-
}
5+
# Declare a "name" attribute that is is 'rwp', read-write protected:
6+
# read-only to consumers, but settable using $self->_set_name
7+
has name => ( is => 'rwp' );
98

10-
sub name {
11-
my $self = shift;
12-
return exists $self->{name} ? $self->{name} : $self->reset_name();
9+
sub BUILD {
10+
my ($self) = @_;
11+
$self->reset_name;
1312
}
1413

1514
sub reset_name {
16-
my $self = shift;
17-
$self->{name} = _rand_letter() . _rand_letter() . _suffix();
18-
return $self->{name};
15+
my ($self) = @_;
16+
$self->_set_name( _rand_letter() . _rand_letter() . _suffix() );
17+
return $self->name;
1918
}
2019

2120
sub _rand_letter {
@@ -24,7 +23,7 @@ sub _rand_letter {
2423
}
2524

2625
sub _suffix {
27-
return 100 + int rand 900;
26+
return sprintf( '%03d', int rand 1000 );
2827
}
2928

3029
1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../robot-name.t

exercises/robot-name/.meta/solutions/robot_name.t

Lines changed: 0 additions & 1 deletion
This file was deleted.

exercises/robot-name/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ The first time you boot them up, a random name is generated in the format
88
of two uppercase letters followed by three digits, such as RX837 or BC811.
99

1010
Every once in a while we need to reset a robot to its factory settings,
11-
which means that their name gets wiped. The next time you ask, it will
11+
which means that its name gets wiped. The next time you ask, it will
1212
respond with a new random name.
1313

1414
The names must be random: they should not follow a predictable sequence.
1515
Random names means a risk of collisions. Your solution must ensure that
1616
every existing robot has a unique name.
17+
1718
## Source
1819

1920
A debugging session with Paul Blackwell at gSchool. [http://gschool.it](http://gschool.it)
2021

2122
## Submitting Incomplete Solutions
23+
2224
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

exercises/robot-name/RobotName.pm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)
2+
package RobotName;
3+
use Moo;
4+
5+
# Declare a "name" attribute that is is 'rwp', read-write protected:
6+
# read-only to consumers, but settable using $self->_set_name
7+
has name => ( is => 'rwp' );
8+
9+
sub reset_name {
10+
my ($self) = @_;
11+
return undef; # Replace this with your own code to pass the tests.
12+
}
13+
14+
1;

exercises/robot-name/cpanfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requires 'Moo'; # https://perldoc.pl/Moo

exercises/robot-name/robot-name.t

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env perl
2+
use Test2::V0;
3+
4+
use FindBin qw($Bin);
5+
use lib $Bin, "$Bin/local/lib/perl5"; # Find modules in the same dir as this file.
6+
7+
use RobotName ();
8+
9+
plan 7; # This is how many tests we expect to run.
10+
11+
can_ok 'RobotName', qw(new name reset_name) or bail_out;
12+
13+
my $robot = RobotName->new;
14+
isa_ok $robot, 'RobotName';
15+
16+
my $name = $robot->name;
17+
like $robot->name, qr/^[A-Z]{2}[0-9]{3}$/, 'Name should match schema';
18+
is $name, $robot->name, 'Name should be persistent';
19+
isnt $robot->name, RobotName->new->name,
20+
'Robots should have different names';
21+
isnt $robot->reset_name, $name,
22+
'reset_name should change the robot name';
23+
ok $robot->name, 'reset_name should not leave the name empty';

exercises/robot-name/robot_name.t

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)