Skip to content

Commit

Permalink
[audio] Add additional test coverage for audio adaptor
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed May 25, 2016
1 parent 9aad23b commit 7314d73
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
25 changes: 15 additions & 10 deletions platforms/audio/audio_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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
}

Expand All @@ -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.")
}
}
16 changes: 16 additions & 0 deletions platforms/audio/audio_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

0 comments on commit 7314d73

Please sign in to comment.