forked from exercism/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert robot-name to use generator (exercism#249)
- Loading branch information
Showing
8 changed files
with
110 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../robot-name.t |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requires 'Moo'; # https://perldoc.pl/Moo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file was deleted.
Oops, something went wrong.