|
| 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 | + like $robot->name, qr/^[A-Z]{2}[0-9]{3}$/, 'Name should match schema'; |
| 19 | + is $name, $robot->name, 'Name should be persistent'; |
| 20 | + isnt $robot->name, RobotName->new->name, |
| 21 | + 'Robots should have different names'; |
| 22 | + isnt $robot->reset_name, $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 | + # Declare a "name" attribute that is is 'rwp', read-write protected: |
| 28 | + # read-only to consumers, but settable using $self->_set_name |
| 29 | + has name => ( is => 'rwp' ); |
| 30 | +
|
| 31 | + sub BUILD { |
| 32 | + my ($self) = @_; |
| 33 | + $self->reset_name; |
| 34 | + } |
| 35 | +
|
| 36 | + sub reset_name { |
| 37 | + my ($self) = @_; |
| 38 | + $self->_set_name( _rand_letter() . _rand_letter() . _suffix() ); |
| 39 | + return $self->name; |
| 40 | + } |
| 41 | +
|
| 42 | + sub _rand_letter { |
| 43 | + my @letters = 'A' .. 'Z'; |
| 44 | + return $letters[ int rand $#letters ]; |
| 45 | + } |
| 46 | +
|
| 47 | + sub _suffix { |
| 48 | + return sprintf('%03d', int rand 1000); |
| 49 | + } |
| 50 | +
|
| 51 | +stub: |- |
| 52 | + # Declare a "name" attribute that is is 'rwp', read-write protected: |
| 53 | + # read-only to consumers, but settable using $self->_set_name |
| 54 | + has name => ( is => 'rwp' ); |
| 55 | +
|
| 56 | + sub reset_name { |
| 57 | + my ($self) = @_; |
| 58 | + return undef; # Replace this with your own code to pass the tests. |
| 59 | + } |
0 commit comments