From 97b970ba96229d3219bb05db969a742c7b5a4b3d Mon Sep 17 00:00:00 2001 From: Chris White Date: Wed, 2 Oct 2019 21:47:50 -0400 Subject: [PATCH] Convert robot-name to use generator (#249) --- exercises/robot-name/.meta/exercise-data.yaml | 62 +++++++++++++++++++ .../robot-name/.meta/solutions/RobotName.pm | 22 +++---- .../robot-name/.meta/solutions/robot-name.t | 1 + .../robot-name/.meta/solutions/robot_name.t | 1 - exercises/robot-name/RobotName.pm | 19 ++++++ exercises/robot-name/robot-name.t | 23 +++++++ exercises/robot-name/robot_name.t | 38 ------------ 7 files changed, 115 insertions(+), 51 deletions(-) create mode 100644 exercises/robot-name/.meta/exercise-data.yaml create mode 120000 exercises/robot-name/.meta/solutions/robot-name.t delete mode 120000 exercises/robot-name/.meta/solutions/robot_name.t create mode 100644 exercises/robot-name/RobotName.pm create mode 100755 exercises/robot-name/robot-name.t delete mode 100755 exercises/robot-name/robot_name.t diff --git a/exercises/robot-name/.meta/exercise-data.yaml b/exercises/robot-name/.meta/exercise-data.yaml new file mode 100644 index 00000000..24c4194d --- /dev/null +++ b/exercises/robot-name/.meta/exercise-data.yaml @@ -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. + } diff --git a/exercises/robot-name/.meta/solutions/RobotName.pm b/exercises/robot-name/.meta/solutions/RobotName.pm index f09964d1..03165129 100644 --- a/exercises/robot-name/.meta/solutions/RobotName.pm +++ b/exercises/robot-name/.meta/solutions/RobotName.pm @@ -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 { diff --git a/exercises/robot-name/.meta/solutions/robot-name.t b/exercises/robot-name/.meta/solutions/robot-name.t new file mode 120000 index 00000000..11e18f61 --- /dev/null +++ b/exercises/robot-name/.meta/solutions/robot-name.t @@ -0,0 +1 @@ +../../robot-name.t \ No newline at end of file diff --git a/exercises/robot-name/.meta/solutions/robot_name.t b/exercises/robot-name/.meta/solutions/robot_name.t deleted file mode 120000 index 826c69d4..00000000 --- a/exercises/robot-name/.meta/solutions/robot_name.t +++ /dev/null @@ -1 +0,0 @@ -../../robot_name.t \ No newline at end of file diff --git a/exercises/robot-name/RobotName.pm b/exercises/robot-name/RobotName.pm new file mode 100644 index 00000000..d5f8f202 --- /dev/null +++ b/exercises/robot-name/RobotName.pm @@ -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; diff --git a/exercises/robot-name/robot-name.t b/exercises/robot-name/robot-name.t new file mode 100755 index 00000000..b0a3e17c --- /dev/null +++ b/exercises/robot-name/robot-name.t @@ -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'; diff --git a/exercises/robot-name/robot_name.t b/exercises/robot-name/robot_name.t deleted file mode 100755 index aed2db72..00000000 --- a/exercises/robot-name/robot_name.t +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env perl -use strict; -use warnings; - -use Test2::Bundle::More; -use FindBin qw($Bin); -use lib $Bin, "$Bin/local/lib/perl5"; - -my $module = 'RobotName'; - -plan 9; - -ok -e "$Bin/$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';