Skip to content

Commit

Permalink
Refactor clock (exercism#458)
Browse files Browse the repository at this point in the history
* Refactor clock + add vendor/cache

* Remove vendor cache

* Run new perltidy config on clock test

* Update cpanfile and dereference args
  • Loading branch information
m-dango authored Jun 20, 2022
1 parent b4fe25c commit ff8efa5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 84 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ bin/configlet
bin/configlet.exe
.problem-specifications

# Carton-installed files
# Carton/Carmel-installed files
local/
cpanfile.snapshot
.carmel/

# Ditto, for exercises
**/cpanfile.snapshot
**/local/
**/.carmel/
36 changes: 10 additions & 26 deletions exercises/practice/clock/.meta/exercise-data.yaml
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
plan: 4
plan: 53
methods: new time add_minutes subtract_minutes
modules:
- use: List::Util qw(any)
tests: |-
subtest create => sub {
plan 20;
for my $case ( grep { $_->{property} eq 'create' } @test_cases ) {
for my $case (@test_cases) {
if ($case->{property} eq 'create') {
is(
Clock->new( $case->{input} ),
Clock->new( %{ $case->{input} } ),
object {
prop blessed => 'Clock';
call time => $case->{expected};
},
$case->{description}
);
}
};
subtest 'add/subtract' => sub {
plan 16;
for my $case (
grep {
my $prop = $_->{property};
any { $prop eq $_ } qw< add subtract >;
} @test_cases
)
{
elsif (any { $case->{property} eq $_ } qw<add subtract> ) {
is(
Clock->new(
{ hour => $case->{input}{hour},
minute => $case->{input}{minute},
}
hour => $case->{input}{hour},
minute => $case->{input}{minute},
),
# Check that the add/subtract_minutes methods
Expand All @@ -45,13 +33,9 @@ tests: |-
$case->{description}
);
}
};
subtest equal => sub {
plan 16;
for my $case ( grep { $_->{property} eq 'equal' } @test_cases ) {
elsif ($case->{property} eq 'equal') {
my ( $clock1, $clock2 )
= ( map { Clock->new($_) }
= ( map { Clock->new(%{$_}) }
@{ $case->{input} }{qw<clock1 clock2>} );
if ( $case->{expected} ) {
is $clock1, $clock2, $case->{description};
Expand All @@ -60,7 +44,7 @@ tests: |-
isnt $clock1, $clock2, $case->{description};
}
}
};
}
moo: true

Expand Down
94 changes: 38 additions & 56 deletions exercises/practice/clock/clock.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,50 @@ use Clock ();
use List::Util qw(any);

my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
plan 4;
plan 53;

can_ok 'Clock', qw<new time add_minutes subtract_minutes> or bail_out;

subtest create => sub {
plan 20;
for my $case ( grep { $_->{property} eq 'create' } @test_cases ) {
is(
Clock->new( $case->{input} ),
object {
prop blessed => 'Clock';
call time => $case->{expected};
},
$case->{description}
);
}
};

subtest 'add/subtract' => sub {
plan 16;
for my $case (
grep {
my $prop = $_->{property};
any { $prop eq $_ } qw< add subtract >;
} @test_cases
)
{
is(
Clock->new(
{ hour => $case->{input}{hour},
minute => $case->{input}{minute},
}
),

# Check that the add/subtract_minutes methods
# return a Clock object with the correct time
object {
call [ $case->{property} . '_minutes',
$case->{input}{value} ] => object {
prop blessed => 'Clock';
call time => $case->{expected};
};
},
$case->{description}
);
}
};
for my $case (@test_cases) {
if ( $case->{property} eq 'create' ) {
is( Clock->new( %{ $case->{input} } ),
object {
prop blessed => 'Clock';
call time => $case->{expected};
},
$case->{description}
);
}
elsif ( any { $case->{property} eq $_ } qw<add subtract> ) {
is( Clock->new(
hour => $case->{input}{hour},
minute => $case->{input}{minute},
),

subtest equal => sub {
plan 16;
for my $case ( grep { $_->{property} eq 'equal' } @test_cases ) {
my ( $clock1, $clock2 )
= ( map { Clock->new($_) }
@{ $case->{input} }{qw<clock1 clock2>} );
if ( $case->{expected} ) {
is $clock1, $clock2, $case->{description};
# Check that the add/subtract_minutes methods
# return a Clock object with the correct time
object {
call [ $case->{property} . '_minutes',
$case->{input}{value} ] => object {
prop blessed => 'Clock';
call time => $case->{expected};
};
},
$case->{description}
);
}
else {
isnt $clock1, $clock2, $case->{description};
elsif ( $case->{property} eq 'equal' ) {
my ( $clock1, $clock2 )
= ( map { Clock->new( %{$_} ) }
@{ $case->{input} }{qw<clock1 clock2>} );
if ( $case->{expected} ) {
is $clock1, $clock2, $case->{description};
}
else {
isnt $clock1, $clock2, $case->{description};
}
}
}
};
}

__DATA__
[
Expand Down
3 changes: 2 additions & 1 deletion exercises/practice/clock/cpanfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
recommends 'Moo'; # https://perldoc.pl/Moo
suggests 'Mo'; # https://perldoc.pl/Mo
suggests 'Moose'; # https://perldoc.pl/Moose
suggests 'Class::Tiny'; # https://perldoc.pl/Class::Tiny
suggests 'Object::Pad' # https://perldoc.pl/Object::Pad
suggests 'Class::Tiny'; # https://perldoc.pl/Class::Tiny#Why-this-instead-of-Moose-or-Moo?

0 comments on commit ff8efa5

Please sign in to comment.