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 3, 2019
1 parent 91627ac commit 97b970b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 51 deletions.
62 changes: 62 additions & 0 deletions exercises/robot-name/.meta/exercise-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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;
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 RobotName->new->name,
'Robots should have different names';
ok $robot->reset_name ne $name,
'reset_name should change the robot name';
ok $robot->name, 'reset_name should not leave the name empty';
example: |-
has name => ( is => 'rwp' ); # rwp = read-write protected: read-only to
# consumers, but settable using $self->_set_name
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 100 + int rand 900;
}
stub: |-
has name => ( is => 'rwp' ); # rwp = read-write protected
# Called automatically to initialize a new object
sub BUILD {
my ($self) = @_;
# Add your own code here to pass the tests
}
sub reset_name {
my ($self) = @_;
return undef; # Replace this with your own code to pass the tests.
}
22 changes: 10 additions & 12 deletions exercises/robot-name/.meta/solutions/RobotName.pm
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# 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;
}
has name => ( is => 'rwp' ); # rwp = read-write protected: read-only to
# consumers, but settable using $self->_set_name

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 Down
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.

19 changes: 19 additions & 0 deletions exercises/robot-name/RobotName.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)
package RobotName;
use Moo;

has name => ( is => 'rwp' ); # rwp = read-write protected

# Called automatically to initialize a new object
sub BUILD {
my ($self) = @_;

# Add your own code here to pass the tests
}

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

1;
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;
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 RobotName->new->name,
'Robots should have different names';
ok $robot->reset_name ne $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 97b970b

Please sign in to comment.