Skip to content

Commit d67f242

Browse files
committed
Convert robot-name to use generator (#249)
1 parent 91627ac commit d67f242

File tree

7 files changed

+116
-51
lines changed

7 files changed

+116
-51
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
exercise: RobotName
2+
package_comment: "# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)"
3+
lib_comment: '# Find modules in the same dir as this file.'
4+
plan_comment: '# This is how many tests we expect to run.'
5+
6+
moo: true
7+
methods: new name reset_name
8+
9+
plan: 7
10+
# plan includes can_ok of `methods` plus the tests below.
11+
12+
# Tests: inline here, since there is no canonical-data.json for this exercise
13+
tests: |-
14+
my $robot = RobotName->new;
15+
isa_ok $robot, 'RobotName';
16+
17+
my $name = $robot->name;
18+
ok $robot->name =~ /[A-Z]{2}[0-9]{3}/, 'Name should match schema';
19+
is $name, $robot->name, 'Name should be persistent';
20+
ok $robot->name ne RobotName->new->name,
21+
'Robots should have different names';
22+
ok $robot->reset_name ne $name,
23+
'reset_name should change the robot name';
24+
ok $robot->name, 'reset_name should not leave the name empty';
25+
26+
example: |-
27+
has name => ( is => 'rwp' ); # rwp = read-write protected: read-only to
28+
# consumers, but settable using $self->_set_name
29+
30+
sub BUILD {
31+
my ($self) = @_;
32+
$self->reset_name;
33+
}
34+
35+
sub reset_name {
36+
my ($self) = @_;
37+
$self->_set_name( _rand_letter() . _rand_letter() . _suffix() );
38+
return $self->name;
39+
}
40+
41+
sub _rand_letter {
42+
my @letters = 'A' .. 'Z';
43+
return $letters[ int rand $#letters ];
44+
}
45+
46+
sub _suffix {
47+
return 100 + int rand 900;
48+
}
49+
50+
stub: |-
51+
has name => ( is => 'rwp' ); # rwp = read-write protected: read-only to
52+
# consumers, but settable using $self->_set_name
53+
54+
# Called automatically to initialize a new object
55+
sub BUILD {
56+
my ($self) = @_;
57+
# Add your own code here to pass the tests
58+
}
59+
60+
sub reset_name {
61+
my ($self) = @_;
62+
return undef; # Replace this with your own code to pass the tests.
63+
}

exercises/robot-name/.meta/solutions/RobotName.pm

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1+
# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)
12
package RobotName;
2-
use warnings;
3-
use strict;
3+
use Moo;
44

5-
sub new {
6-
my $class = shift;
7-
return bless {}, $class;
8-
}
5+
has name => ( is => 'rwp' ); # rwp = read-write protected: read-only to
6+
# consumers, but settable using $self->_set_name
97

10-
sub name {
11-
my $self = shift;
12-
return exists $self->{name} ? $self->{name} : $self->reset_name();
8+
sub BUILD {
9+
my ($self) = @_;
10+
$self->reset_name;
1311
}
1412

1513
sub reset_name {
16-
my $self = shift;
17-
$self->{name} = _rand_letter() . _rand_letter() . _suffix();
18-
return $self->{name};
14+
my ($self) = @_;
15+
$self->_set_name( _rand_letter() . _rand_letter() . _suffix() );
16+
return $self->name;
1917
}
2018

2119
sub _rand_letter {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../robot-name.t

exercises/robot-name/.meta/solutions/robot_name.t

Lines changed: 0 additions & 1 deletion
This file was deleted.

exercises/robot-name/RobotName.pm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This will be class 'RobotName', defined using Moo (https://perlmaven.com/oop-with-moo)
2+
package RobotName;
3+
use Moo;
4+
5+
has name => ( is => 'rwp' ); # rwp = read-write protected
6+
7+
# Called automatically to initialize a new object
8+
sub BUILD {
9+
my ($self) = @_;
10+
11+
# Add your own code here to pass the tests
12+
}
13+
14+
sub reset_name {
15+
my ($self) = @_;
16+
return undef; # Replace this with your own code to pass the tests.
17+
}
18+
19+
1;

exercises/robot-name/robot-name.t

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env perl
2+
use Test2::V0;
3+
4+
use FindBin qw($Bin);
5+
use lib $Bin, "$Bin/local/lib/perl5"; # Find modules in the same dir as this file.
6+
7+
use RobotName ();
8+
9+
plan 7; # This is how many tests we expect to run.
10+
11+
can_ok 'RobotName', qw(new name reset_name) or bail_out;
12+
13+
my $robot = RobotName->new;
14+
isa_ok $robot, 'RobotName';
15+
16+
my $name = $robot->name;
17+
ok $robot->name =~ /[A-Z]{2}[0-9]{3}/, 'Name should match schema';
18+
is $name, $robot->name, 'Name should be persistent';
19+
ok $robot->name ne RobotName->new->name,
20+
'Robots should have different names';
21+
ok $robot->reset_name ne $name,
22+
'reset_name should change the robot name';
23+
ok $robot->name, 'reset_name should not leave the name empty';

exercises/robot-name/robot_name.t

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)