From: Marius Gavrilescu Date: Sat, 1 Aug 2015 21:52:32 +0000 (+0300) Subject: Make tests and code slightly shorter X-Git-Tag: 1.000~5 X-Git-Url: http://git.ieval.ro/?p=app-musicexpo.git;a=commitdiff_plain;h=b484a12921835e84ece975361da2c7b209beba6b Make tests and code slightly shorter --- diff --git a/lib/App/MusicExpo.pm b/lib/App/MusicExpo.pm index 3fccfbb..a25a132 100644 --- a/lib/App/MusicExpo.pm +++ b/lib/App/MusicExpo.pm @@ -40,13 +40,13 @@ sub flacinfo{ freeze +{ format => 'FLAC', - title => $flac->tags('TITLE') // '?', - artist => $flac->tags('ARTIST') // '?', - year => $flac->tags('DATE') // '?', - album => $flac->tags('ALBUM') // '?', - tracknumber => $flac->tags('TRACKNUMBER') // '?', - tracktotal => $flac->tags('TRACKTOTAL') // '?', - genre => $flac->tags('GENRE') // '?', + title => $flac->tags('TITLE'), + artist => $flac->tags('ARTIST'), + year => $flac->tags('DATE'), + album => $flac->tags('ALBUM'), + tracknumber => $flac->tags('TRACKNUMBER'), + tracktotal => $flac->tags('TRACKTOTAL'), + genre => $flac->tags('GENRE'), file => scalar fileparse $file, } } @@ -58,13 +58,13 @@ sub mp3info{ freeze +{ format => 'MP3', - title => $tag{TITLE} || '?', - artist => $tag{ARTIST} || '?', - year => $tag{YEAR} || '?', - album => $tag{ALBUM} || '?', - tracknumber => $trkn[0] || '?', - tracktotal => $trkn[1] || '?', - genre => $tag{GENRE} || '?', + title => $tag{TITLE}, + artist => $tag{ARTIST}, + year => $tag{YEAR}, + album => $tag{ALBUM}, + tracknumber => $trkn[0], + tracktotal => $trkn[1], + genre => $tag{GENRE}, file => scalar fileparse $file, } } @@ -75,13 +75,13 @@ sub vorbisinfo{ freeze +{ format => 'Vorbis', - title => $ogg->comment('TITLE') || '?', - artist => $ogg->comment('artist') || '?', - year => $ogg->comment('DATE') || '?', - album => $ogg->comment('ALBUM') || '?', - tracknumber => $ogg->comment('TRACKNUMBER') || '?', - tracktotal => $ogg->comment('TRACKTOTAL') || '?', - genre => $ogg->comment('GENRE') || '?', + title => $ogg->comment('TITLE'), + artist => $ogg->comment('artist'), + year => $ogg->comment('DATE'), + album => $ogg->comment('ALBUM'), + tracknumber => $ogg->comment('TRACKNUMBER'), + tracktotal => $ogg->comment('TRACKTOTAL'), + genre => $ogg->comment('GENRE'), file => scalar fileparse $file, } } @@ -100,13 +100,13 @@ sub mp4info{ freeze +{ format => mp4_format $info{ENCODING}, - title => $tag{TITLE} || '?', - artist => $tag{ARTIST} || '?', - year => $tag{YEAR} || '?', - album => $tag{ALBUM} || '?', - tracknumber => $tag{TRACKNUM} || '?', - tracktotal => ($tag{TRKN} ? $tag{TRKN}->[1] : undef) || '?', - genre => $tag{GENRE} || '?', + title => $tag{TITLE}, + artist => $tag{ARTIST}, + year => $tag{YEAR}, + album => $tag{ALBUM}, + tracknumber => $tag{TRACKNUM}, + tracktotal => ($tag{TRKN} ? $tag{TRKN}->[1] : undef), + genre => $tag{GENRE}, file => scalar fileparse $file, }; } @@ -150,7 +150,7 @@ sub run { my @files; for (sort keys %files) { my @versions = @{$files{$_}}; - my %entry = (%{$versions[0]}, formats => []); + my %entry = (formats => [], map { $_ => '?' } qw/title artist year album tracknumber tracktotal genre/); for my $ver (@versions) { push @{$entry{formats}}, {format => $ver->{format}, file => $ver->{file}}; for my $key (keys %$ver) { diff --git a/t/App-MusicExpo.t b/t/App-MusicExpo.t index fb43504..09b2bde 100644 --- a/t/App-MusicExpo.t +++ b/t/App-MusicExpo.t @@ -4,51 +4,33 @@ use warnings; use Test::More tests => 37; +use Scalar::Util qw/looks_like_number/; use Storable qw/thaw/; BEGIN { use_ok('App::MusicExpo'); } -my $flacinfo = thaw App::MusicExpo::flacinfo 'empty.flac'; -my $mp3info = thaw App::MusicExpo::mp3info 'empty3.mp3'; -my $vorbisinfo = thaw App::MusicExpo::vorbisinfo 'empty.ogg'; -my $mp4info = thaw App::MusicExpo::mp4info 'empty4.aac'; - -is $flacinfo->{format}, 'FLAC', 'flacinfo format'; -is $flacinfo->{title}, 'Cellule', 'flacinfo title'; -is $flacinfo->{artist}, 'Silence', 'flacinfo artist'; -is $flacinfo->{year}, 2005, 'flacinfo year'; -is $flacinfo->{album}, 'L\'autre endroit', 'flacinfo album'; -is $flacinfo->{tracknumber}, '01', 'flacinfo tracknumber'; -is $flacinfo->{tracktotal}, '09', 'flacinfo tracktotal'; -is $flacinfo->{genre}, 'Electro', 'flacinfo genre'; -is $flacinfo->{file}, 'empty.flac', 'flacinfo path'; - -is $mp3info->{format}, 'MP3', 'mp3info format'; -is $mp3info->{title}, 'Cellule', 'mp3info title'; -is $mp3info->{artist}, 'Silence', 'mp3info artist'; -is $mp3info->{year}, 2005, 'mp3info year'; -is $mp3info->{album}, 'L\'autre endroit', 'mp3info album'; -is $mp3info->{tracknumber}, '01', 'mp3info tracknumber'; -is $mp3info->{tracktotal}, '09', 'mp3info tracktotal'; -is $mp3info->{genre}, 'Electro', 'mp3info genre'; -is $mp3info->{file}, 'empty3.mp3', 'mp3info path'; - -is $vorbisinfo->{format}, 'Vorbis', 'vorbisinfo format'; -is $vorbisinfo->{title}, 'Cellule', 'vorbisinfo title'; -is $vorbisinfo->{artist}, 'Silence', 'vorbisinfo artist'; -is $vorbisinfo->{year}, 2005, 'vorbisinfo year'; -is $vorbisinfo->{album}, 'L\'autre endroit', 'vorbisinfo album'; -is $vorbisinfo->{tracknumber}, '01', 'vorbisinfo tracknumber'; -is $vorbisinfo->{tracktotal}, '09', 'vorbisinfo tracktotal'; -is $vorbisinfo->{genre}, 'Electro', 'vorbisinfo genre'; -is $vorbisinfo->{file}, 'empty.ogg', 'vorbisinfo path'; - -is $mp4info->{format}, 'AAC', 'mp4info format'; -is $mp4info->{title}, 'Cellule', 'mp4info title'; -is $mp4info->{artist}, 'Silence', 'mp4info artist'; -is $mp4info->{year}, 2005, 'mp4info year'; -is $mp4info->{album}, 'L\'autre endroit', 'mp4info album'; -is $mp4info->{tracknumber}, '1', 'mp4info tracknumber'; -is $mp4info->{tracktotal}, '9', 'mp4info tracktotal'; -is $mp4info->{genre}, 'Electro', 'mp4info genre'; -is $mp4info->{file}, 'empty4.aac', 'mp4info path'; +my %data = ( + title => 'Cellule', + artist => 'Silence', + year => 2005, + album => 'L\'autre endroit', + tracknumber => 1, + tracktotal => 9, + genre => 'Electro' +); + +sub test { + my ($format, $sub, $file) = @_; + my $info = thaw $sub->($file); + is $info->{format}, $format, "$format format"; + for (sort keys %data) { + my $op = looks_like_number $data{$_} ? '==' : 'eq'; + cmp_ok $info->{$_}, $op, $data{$_}, "$format $_" + } + is $info->{file}, $file, "$format file"; +} + +test FLAC => \&App::MusicExpo::flacinfo, 'empty.flac'; +test MP3 => \&App::MusicExpo::mp3info, 'empty3.mp3'; +test Vorbis => \&App::MusicExpo::vorbisinfo, 'empty.ogg'; +test AAC => \&App::MusicExpo::mp4info, 'empty4.aac';