Skip to content

Commit

Permalink
Convert robot-name to use generator (exercism#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxw42 committed Oct 4, 2019
1 parent 91627ac commit bbd4224
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 53 deletions.
59 changes: 59 additions & 0 deletions exercises/robot-name/.meta/exercise-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
exercise: RobotName
package_comment: "# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)"
lib_comment: '# Find modules in the same dir as this file.'
plan_comment: '# This is how many tests we expect to run.'

moo: true
methods: new name reset_name

plan: 7
# plan includes can_ok of `methods` plus the tests below.

# Tests: inline here, since there is no canonical-data.json for this exercise
tests: |-
my $robot = RobotName->new;
isa_ok $robot, 'RobotName';
my $name = $robot->name;
like $robot->name, qr/^[A-Z]{2}[0-9]{3}$/, 'Name should match schema';
is $name, $robot->name, 'Name should be persistent';
isnt $robot->name, RobotName->new->name,
'Robots should have different names';
isnt $robot->reset_name, $name,
'reset_name should change the robot name';
ok $robot->name, 'reset_name should not leave the name empty';
example: |-
# Declare a "name" attribute that is is 'rwp', read-write protected:
# read-only to consumers, but settable using $self->_set_name
has name => ( is => 'rwp' );
sub BUILD {
my ($self) = @_;
$self->reset_name;
}
sub reset_name {
my ($self) = @_;
$self->_set_name( _rand_letter() . _rand_letter() . _suffix() );
return $self->name;
}
sub _rand_letter {
my @letters = 'A' .. 'Z';
return $letters[ int rand $#letters ];
}
sub _suffix {
return sprintf('%03d', int rand 1000);
}
stub: |-
# Declare a "name" attribute that is is 'rwp', read-write protected:
# read-only to consumers, but settable using $self->_set_name
has name => ( is => 'rwp' );
sub reset_name {
my ($self) = @_;
return undef; # Replace this with your own code to pass the tests.
}
25 changes: 12 additions & 13 deletions exercises/robot-name/.meta/solutions/RobotName.pm
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)
package RobotName;
use warnings;
use strict;
use Moo;

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

sub name {
my $self = shift;
return exists $self->{name} ? $self->{name} : $self->reset_name();
sub BUILD {
my ($self) = @_;
$self->reset_name;
}

sub reset_name {
my $self = shift;
$self->{name} = _rand_letter() . _rand_letter() . _suffix();
return $self->{name};
my ($self) = @_;
$self->_set_name( _rand_letter() . _rand_letter() . _suffix() );
return $self->name;
}

sub _rand_letter {
Expand All @@ -24,7 +23,7 @@ sub _rand_letter {
}

sub _suffix {
return 100 + int rand 900;
return sprintf( '%03d', int rand 1000 );
}

1;
1 change: 1 addition & 0 deletions exercises/robot-name/.meta/solutions/robot-name.t
1 change: 0 additions & 1 deletion exercises/robot-name/.meta/solutions/robot_name.t

This file was deleted.

4 changes: 3 additions & 1 deletion exercises/robot-name/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ The first time you boot them up, a random name is generated in the format
of two uppercase letters followed by three digits, such as RX837 or BC811.

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

The names must be random: they should not follow a predictable sequence.
Random names means a risk of collisions. Your solution must ensure that
every existing robot has a unique name.

## Source

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

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.
14 changes: 14 additions & 0 deletions exercises/robot-name/RobotName.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)
package RobotName;
use Moo;

# Declare a "name" attribute that is is 'rwp', read-write protected:
# read-only to consumers, but settable using $self->_set_name
has name => ( is => 'rwp' );

sub reset_name {
my ($self) = @_;
return undef; # Replace this with your own code to pass the tests.
}

1;
1 change: 1 addition & 0 deletions exercises/robot-name/cpanfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requires 'Moo'; # https://perldoc.pl/Moo
23 changes: 23 additions & 0 deletions exercises/robot-name/robot-name.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env perl
use Test2::V0;

use FindBin qw($Bin);
use lib $Bin, "$Bin/local/lib/perl5"; # Find modules in the same dir as this file.

use RobotName ();

plan 7; # This is how many tests we expect to run.

can_ok 'RobotName', qw(new name reset_name) or bail_out;

my $robot = RobotName->new;
isa_ok $robot, 'RobotName';

my $name = $robot->name;
like $robot->name, qr/^[A-Z]{2}[0-9]{3}$/, 'Name should match schema';
is $name, $robot->name, 'Name should be persistent';
isnt $robot->name, RobotName->new->name,
'Robots should have different names';
isnt $robot->reset_name, $name,
'reset_name should change the robot name';
ok $robot->name, 'reset_name should not leave the name empty';
38 changes: 0 additions & 38 deletions exercises/robot-name/robot_name.t

This file was deleted.

0 comments on commit bbd4224

Please sign in to comment.