exercise: Clock version: 1 plan: 57 subs: new time add_minutes subtract_minutes tests: |- foreach (@{$C_DATA->{cases}}) { foreach (@{$_->{cases}}) { if ($_->{property} eq 'create') { is Clock->new($_->{input})->time, $_->{expected}, $_->{description}; } elsif ($_->{property} eq 'add' || $_->{property} eq 'subtract') { my $clock = Clock->new({ hour => $_->{input}{hour}, minute => $_->{input}{minute}, }); if ($_->{property} eq 'subtract') { $clock->subtract_minutes($_->{input}{value}) } elsif ($_->{property} eq 'add' ) { $clock->add_minutes( $_->{input}{value}) } is $clock->time, $_->{expected}, $_->{description}; } elsif ($_->{property} eq 'equal') { ok $_->{expected} == (Clock->new($_->{input}{clock1})->time eq Clock->new($_->{input}{clock2})->time), $_->{description}; } } } is Clock->new({hour => 0, minute => 0})->add_minutes(65)->time, '01:05', 'add_minutes method can be chained'; is Clock->new({hour => 0, minute => 0})->subtract_minutes(65)->time, '22:55', 'subtract_minutes method can be chained'; stub: |- sub new { my ($class, $attributes) = @_; return bless $attributes, $class; } sub time { my ($self) = @_; } sub add_minutes { my ($self, $amount) = @_; } sub subtract_minutes { my ($self, $amount) = @_; } example: |- use POSIX; sub new { my ($class, $attributes) = @_; my $self = { hour => $attributes->{hour} || 0, minute => $attributes->{minute} || 0, }; return bless($self, $class)->BUILD; } sub time { my ($self) = @_; return sprintf '%02d:%02d', $self->{hour}, $self->{minute}; } sub add_minutes { my ($self, $amount) = @_; $self->{minute} += $amount; return $self->BUILD; } sub subtract_minutes { my ($self, $amount) = @_; return $self->add_minutes(-$amount); } sub BUILD { my ($self) = @_; $self->{hour} = ( $self->{hour} + floor($self->{minute} / 60) ) % 24; $self->{minute} %= 60; return $self; }