From 7314d73fd622a70af7cad0881100857937a1a69f Mon Sep 17 00:00:00 2001 From: deadprogram Date: Tue, 24 May 2016 17:57:59 -0700 Subject: [PATCH] [audio] Add additional test coverage for audio adaptor Signed-off-by: deadprogram --- platforms/audio/audio_adaptor.go | 25 +++++++++++++++---------- platforms/audio/audio_adaptor_test.go | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/platforms/audio/audio_adaptor.go b/platforms/audio/audio_adaptor.go index 3d2210ae..82883585 100644 --- a/platforms/audio/audio_adaptor.go +++ b/platforms/audio/audio_adaptor.go @@ -30,7 +30,6 @@ func (a *AudioAdaptor) Connect() []error { return nil } func (a *AudioAdaptor) Finalize() []error { return nil } func (a *AudioAdaptor) Sound(fileName string) []error { - var errorsList []error if fileName == "" { @@ -47,15 +46,10 @@ func (a *AudioAdaptor) Sound(fileName string) []error { } // command to play audio file based on file type - fileType := path.Ext(fileName) - var commandName string - if fileType == ".mp3" { - commandName = "mpg123" - } else if fileType == ".wav" { - commandName = "aplay" - } else { - log.Println("Unknown filetype for audio file.") - errorsList = append(errorsList, errors.New("Unknown filetype for audio file.")) + commandName, err := CommandName(fileName) + if err != nil { + log.Println(err) + errorsList = append(errorsList, err) return errorsList } @@ -70,3 +64,14 @@ func (a *AudioAdaptor) Sound(fileName string) []error { // Need to return to fulfill function sig, even though returning an empty return nil } + +func CommandName(fileName string) (commandName string, err error) { + fileType := path.Ext(fileName) + if fileType == ".mp3" { + return "mpg123", nil + } else if fileType == ".wav" { + return "aplay", nil + } else { + return "", errors.New("Unknown filetype for audio file.") + } +} diff --git a/platforms/audio/audio_adaptor_test.go b/platforms/audio/audio_adaptor_test.go index 3961f64d..41679533 100644 --- a/platforms/audio/audio_adaptor_test.go +++ b/platforms/audio/audio_adaptor_test.go @@ -16,3 +16,19 @@ func TestAudioAdaptor(t *testing.T) { gobottest.Assert(t, len(a.Finalize()), 0) } + +func TestAudioAdaptorCommandsWav(t *testing.T) { + cmd, _ := CommandName("whatever.wav") + gobottest.Assert(t, cmd, "aplay") +} + +func TestAudioAdaptorCommandsMp3(t *testing.T) { + cmd, _ := CommandName("whatever.mp3") + gobottest.Assert(t, cmd, "mpg123") +} + +func TestAudioAdaptorCommandsUnknown(t *testing.T) { + cmd, err := CommandName("whatever.unk") + gobottest.Refute(t, cmd, "mpg123") + gobottest.Assert(t, err.Error(), "Unknown filetype for audio file.") +}