Skip to content

Commit

Permalink
Convert robot-name to use generator (#249) (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxw42 authored and Daniel Mita committed Oct 5, 2019
1 parent 8c1cc50 commit 4e5665b
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 52 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.

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 4e5665b

Please sign in to comment.