From dde0e0150344cecfcad5c81df3d00f216dbecbbb Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 6 Jun 2016 20:46:01 +0100 Subject: [PATCH] Bug 16289: Refactore code and add tests Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- C4/Serials.pm | 30 +++++------------ t/Serials/GetNextSeq.t | 76 +++++++++++++++++++++--------------------- 2 files changed, 46 insertions(+), 60 deletions(-) diff --git a/C4/Serials.pm b/C4/Serials.pm index 755fc93cf1..5c942b26bc 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -2498,7 +2498,7 @@ sub _numeration { $num_type //= ''; $locale ||= 'en'; my $string; - if ( $num_type =~ /^dayname$/ ) { + if ( $num_type =~ /^dayname$/ or $num_type =~ /^dayabrv$/ ) { # 1970-11-01 was a Sunday $value = $value % 7; my $dt = DateTime->new( @@ -2507,33 +2507,19 @@ sub _numeration { day => $value + 1, locale => $locale, ); - $string = $dt->strftime("%A"); - } elsif ( $num_type =~ /^dayabrv$/ ) { - # 1970-11-01 was a Sunday - $value = $value % 7; - my $dt = DateTime->new( - year => 1970, - month => 11, - day => $value + 1, - locale => $locale, - ); - $string = $dt->strftime("%a"); - } elsif ( $num_type =~ /^monthname$/ ) { - $value = $value % 12; - my $dt = DateTime->new( - year => 1970, - month => $value + 1, - locale => $locale, - ); - $string = $dt->strftime("%B"); - } elsif ( $num_type =~ /^monthabrv$/ ) { + $string = $num_type =~ /^dayname$/ + ? $dt->strftime("%A") + : $dt->strftime("%a"); + } elsif ( $num_type =~ /^monthname$/ or $num_type =~ /^monthabrv$/ ) { $value = $value % 12; my $dt = DateTime->new( year => 1970, month => $value + 1, locale => $locale, ); - $string = $dt->strftime("%b"); + $string = $num_type =~ /^monthname$/ + ? $dt->strftime("%B") + : $dt->strftime("%b"); } elsif ( $num_type =~ /^season$/ ) { my @seasons= qw( Spring Summer Fall Winter ); $value = $value % 4; diff --git a/t/Serials/GetNextSeq.t b/t/Serials/GetNextSeq.t index 2f3a1c3555..8fc201a9e7 100644 --- a/t/Serials/GetNextSeq.t +++ b/t/Serials/GetNextSeq.t @@ -43,96 +43,96 @@ $subscription = { locale => 'C', }; $pattern = { - add1 => 1, add2 => 0, add3 => 0, - every1 => 1, every2 => 0, every3 => 0, - whenmorethan1 => 7, whenmorethan2 => 0, whenmorethan3 => 0, - setto1 => 1, setto2 => 0, setto3 => 0, - numberingmethod => 'X: {X}', + add1 => 1, add2 => 1, add3 => 0, + every1 => 1, every2 => 1, every3 => 0, + whenmorethan1 => 7, whenmorethan2 => 7, whenmorethan3 => 0, + setto1 => 1, setto2 => 1, setto3 => 0, + numberingmethod => 'dayname: {X} | dayabrv: {Y}', numbering1 => 'dayname', - numbering2 => '', + numbering2 => 'dayabrv', numbering3 => '', }; $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Tuesday'); +is($seq, 'dayname: Tuesday | dayabrv: Tue'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Wednesday'); +is($seq, 'dayname: Wednesday | dayabrv: Wed'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Thursday'); +is($seq, 'dayname: Thursday | dayabrv: Thu'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Friday'); +is($seq, 'dayname: Friday | dayabrv: Fri'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Saturday'); +is($seq, 'dayname: Saturday | dayabrv: Sat'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Sunday'); +is($seq, 'dayname: Sunday | dayabrv: Sun'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Monday'); +is($seq, 'dayname: Monday | dayabrv: Mon'); # TEST CASE 3 - 1 variable, use 'monthname' numbering, from 0 to 11 by step of 2 $subscription = { - lastvalue1 => 0, lastvalue2 => 1, lastvalue3 => 1, + lastvalue1 => 0, lastvalue2 => 0, lastvalue3 => 0, innerloop1 => 0, innerloop2 => 0, innerloop3 => 0, skip_serialseq => 0, irregularity => '', locale => 'C', # locale set to 'C' to ensure we'll have english strings }; $pattern = { - add1 => 2, add2 => 0, add3 => 0, - every1 => 1, every2 => 0, every3 => 0, - whenmorethan1 => 11, whenmorethan2 => 0, whenmorethan3 => 0, - setto1 => 0, setto2 => 0, setto3 => 0, - numberingmethod => 'X: {X}', + add1 => 2, add2 => 2, add3 => 0, + every1 => 1, every2 => 1, every3 => 0, + whenmorethan1 => 11, whenmorethan2 => 11, whenmorethan3 => 0, + setto1 => 0, setto2 => 0, setto3 => 0, + numberingmethod => 'monthname: {X} | monthabrv: {Y}', numbering1 => 'monthname', - numbering2 => '', + numbering2 => 'monthabrv', numbering3 => '', }; $seq = _next_seq($subscription, $pattern); -is($seq, 'X: March'); +is($seq, 'monthname: March | monthabrv: Mar'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: May'); +is($seq, 'monthname: May | monthabrv: May'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: July'); +is($seq, 'monthname: July | monthabrv: Jul'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: September'); +is($seq, 'monthname: September | monthabrv: Sep'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: November'); +is($seq, 'monthname: November | monthabrv: Nov'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: January'); +is($seq, 'monthname: January | monthabrv: Jan'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: March'); +is($seq, 'monthname: March | monthabrv: Mar'); # TEST CASE 4 - 1 variable, use 'season' numbering, from 0 to 3 # Months starts at 0, this implies subscription's lastvalue1 should be 0, # together with setto1 and whenmorethan1 should be 11 $subscription = { - lastvalue1 => 0, lastvalue2 => 1, lastvalue3 => 1, + lastvalue1 => 0, lastvalue2 => 0, lastvalue3 => 0, innerloop1 => 0, innerloop2 => 0, innerloop3 => 0, skip_serialseq => 0, irregularity => '', locale => 'C', # locale set to 'C' to ensure we'll have english strings }; $pattern = { - add1 => 1, add2 => 0, add3 => 0, - every1 => 1, every2 => 0, every3 => 0, - whenmorethan1 => 3, whenmorethan2 => 0, whenmorethan3 => 0, + add1 => 1, add2 => 1, add3 => 0, + every1 => 1, every2 => 1, every3 => 0, + whenmorethan1 => 3, whenmorethan2 => 3, whenmorethan3 => 0, setto1 => 0, setto2 => 0, setto3 => 0, - numberingmethod => 'X: {X}', + numberingmethod => 'season: {X} | seasonabrv: {Y}', numbering1 => 'season', - numbering2 => '', + numbering2 => 'seasonabrv', numbering3 => '', }; $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Summer'); +is($seq, 'season: Summer | seasonabrv: Sum'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Fall'); +is($seq, 'season: Fall | seasonabrv: Fal'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Winter'); +is($seq, 'season: Winter | seasonabrv: Win'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Spring'); +is($seq, 'season: Spring | seasonabrv: Spr'); $seq = _next_seq($subscription, $pattern); -is($seq, 'X: Summer'); +is($seq, 'season: Summer | seasonabrv: Sum'); # TEST CASE 5 - 2 variables, from 1 to 12, and from 1 to 4 $subscription = { -- 2.39.5