From: Leon Brocard Date: Tue, 10 Nov 2009 14:25:37 +0000 (+0000) Subject: Move audio initing out to a test library to remove duplicated code X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9a09ec834024fb7b34d0dda5af129851d39f7b7b;p=sdlgit%2FSDL_perl.git Move audio initing out to a test library to remove duplicated code --- diff --git a/t/lib/SDL/TestTool.pm b/t/lib/SDL/TestTool.pm index d397e33..54ff3b1 100644 --- a/t/lib/SDL/TestTool.pm +++ b/t/lib/SDL/TestTool.pm @@ -1,75 +1,14 @@ +package SDL::TestTool; use strict; use warnings; -use vars qw/$VERSION @ISA @EXPORT_OK %EXPORT_TAGS $CarpLevel/; -use Exporter; -use Carp qw/croak/; -@ISA = 'Exporter'; -@EXPORT_OK = qw/capture test_init/; -%EXPORT_TAGS = (all => \@EXPORT_OK); - - -use lib '../../../'; #../../../t/lib/SDL -use SDL; - -###CREDITS GOES TO DAVID GOLDEN FOR THE FOLLOWING CODE -### http://search.cpan.org/dist/IO-CaptureOutput/lib/IO/CaptureOutput.pod -### We stole it as we only need capture an nothing more from that awesome module -sub _capture (&@) { ## no critic - my ($code, $output, $error, $output_file, $error_file) = @_; - - # check for valid combinations of input - { - local $Carp::CarpLevel = 1; - my $error = _validate($output, $error, $output_file, $error_file); - croak $error if $error; - } - - # if either $output or $error are defined, then we need a variable for - # results; otherwise we only capture to files and don't waste memory - if ( defined $output || defined $error ) { - for ($output, $error) { - $_ = \do { my $s; $s = ''} unless ref $_; - $$_ = '' if $_ != \undef && !defined($$_); - } - } - - # merge if same refs for $output and $error or if both are undef -- - # i.e. capture \&foo, undef, undef, $merged_file - # this means capturing into separate files *requires* at least one - # capture variable - my $should_merge = - (defined $error && defined $output && $output == $error) || - ( !defined $output && !defined $error ) || - 0; - - my ($capture_out, $capture_err); - - # undef means capture anonymously; anything other than \undef means - # capture to that ref; \undef means skip capture - if ( !defined $output || $output != \undef ) { - $capture_out = IO::CaptureOutput::_proxy->new( - 'STDOUT', $output, undef, $output_file - ); - } - if ( !defined $error || $error != \undef ) { - $capture_err = IO::CaptureOutput::_proxy->new( - 'STDERR', $error, ($should_merge ? 'STDOUT' : undef), $error_file - ); - } - - # now that output capture is setup, call the subroutine - # results get read when IO::CaptureOutput::_proxy objects go out of scope - &$code(); -} - -# Extra indirection for symmetry with capture_exec, etc. Gets error reporting -# to the right level -sub capture (&@) { ## no critic - return &_capture; +use IO::CaptureOutput qw(capture); +use SDL; + +sub init_audio { + my $stdout = ''; + my $stderr = ''; + capture { SDL::init(SDL_INIT_AUDIO) } \$stdout, \$stderr; + return ( $stderr ne '' ); } - -sub test_init -{ - -} +1; diff --git a/t/mixer_mixchunk.t b/t/mixer_mixchunk.t index 73e94a8..3d79ff2 100644 --- a/t/mixer_mixchunk.t +++ b/t/mixer_mixchunk.t @@ -1,30 +1,19 @@ #!perl use strict; use warnings; +use lib 't/lib'; use SDL; use SDL::Mixer::MixChunk; +use SDL::TestTool; use Test::More; use IO::CaptureOutput qw(capture); -sub test_audio -{ - my $stdout = '' ; - my $stderr = '' ; - capture { SDL::init(SDL_INIT_AUDIO) } \$stdout, \$stderr; - SDL::quit(); - return ($stderr ne '' ); +if ( SDL::TestTool->init_audio ) { + plan( skip_all => 'Failed to init sound' ); +} else { + plan( tests => 6 ); } -if ( test_audio ) -{ - plan ( skip_all => 'Failed to init sound' ); -} -elsif(SDL::init(SDL_INIT_AUDIO) >= 0) - { plan( tests => 6 ) } -else - { - plan ( skip_all => 'Failed to init sound' ); - } is( SDL::MixOpenAudio( 44100, SDL::Constants::AUDIO_S16, 2, 4096 ), 0, 'MixOpenAudio passed' ); diff --git a/t/mixer_mixmusic.t b/t/mixer_mixmusic.t index 3dba3fe..4a0eab7 100644 --- a/t/mixer_mixmusic.t +++ b/t/mixer_mixmusic.t @@ -1,32 +1,20 @@ #!perl use strict; use warnings; +use lib 't/lib'; use SDL; use SDL::Mixer::MixMusic; +use SDL::TestTool; use Test::More; use IO::CaptureOutput qw(capture); -sub test_audio -{ - my $stdout = '' ; - my $stderr = '' ; - capture { SDL::init(SDL_INIT_AUDIO) } \$stdout, \$stderr; - SDL::quit(); - return ($stderr ne '' ); +if ( SDL::TestTool->init_audio ) { + plan( skip_all => 'Failed to init sound' ); +} else { + plan( tests => 3 ); } -if ( test_audio ) -{ - plan ( skip_all => 'Failed to init sound' ); -} -elsif(SDL::init(SDL_INIT_AUDIO) >= 0) - { plan( tests => 3 ) } -else - { - plan ( skip_all => 'Failed to init sound' ); - } - - is( SDL::MixOpenAudio( 44100, SDL::Constants::AUDIO_S16, 2, 4096 ), +is( SDL::MixOpenAudio( 44100, SDL::Constants::AUDIO_S16, 2, 4096 ), 0, 'MixOpenAudio passed' ); my $mix_music = SDL::MixLoadMUS('test/data/sample.wav');