gobot.io/x/gobot/v2@v2.1.0/platforms/audio/audio_adaptor_test.go (about)

     1  // Based on aplay audio adaptor written by @colemanserious (https://github.com/colemanserious)
     2  package audio
     3  
     4  import (
     5  	"os/exec"
     6  	"strings"
     7  	"testing"
     8  
     9  	"gobot.io/x/gobot/v2"
    10  	"gobot.io/x/gobot/v2/gobottest"
    11  )
    12  
    13  var _ gobot.Adaptor = (*Adaptor)(nil)
    14  
    15  func TestAudioAdaptor(t *testing.T) {
    16  	a := NewAdaptor()
    17  
    18  	gobottest.Assert(t, a.Connect(), nil)
    19  	gobottest.Assert(t, a.Finalize(), nil)
    20  }
    21  
    22  func TestAudioAdaptorName(t *testing.T) {
    23  	a := NewAdaptor()
    24  	gobottest.Assert(t, strings.HasPrefix(a.Name(), "Audio"), true)
    25  	a.SetName("NewName")
    26  	gobottest.Assert(t, a.Name(), "NewName")
    27  }
    28  
    29  func TestAudioAdaptorCommandsWav(t *testing.T) {
    30  	cmd, _ := CommandName("whatever.wav")
    31  	gobottest.Assert(t, cmd, "aplay")
    32  }
    33  
    34  func TestAudioAdaptorCommandsMp3(t *testing.T) {
    35  	cmd, _ := CommandName("whatever.mp3")
    36  	gobottest.Assert(t, cmd, "mpg123")
    37  }
    38  
    39  func TestAudioAdaptorCommandsUnknown(t *testing.T) {
    40  	cmd, err := CommandName("whatever.unk")
    41  	gobottest.Refute(t, cmd, "mpg123")
    42  	gobottest.Assert(t, err.Error(), "Unknown filetype for audio file.")
    43  }
    44  
    45  func TestAudioAdaptorSoundWithNoFilename(t *testing.T) {
    46  	a := NewAdaptor()
    47  
    48  	errors := a.Sound("")
    49  	gobottest.Assert(t, errors[0].Error(), "Requires filename for audio file.")
    50  }
    51  
    52  func TestAudioAdaptorSoundWithNonexistingFilename(t *testing.T) {
    53  	a := NewAdaptor()
    54  
    55  	errors := a.Sound("doesnotexist.mp3")
    56  	gobottest.Assert(t, errors[0].Error(), "stat doesnotexist.mp3: no such file or directory")
    57  }
    58  
    59  func TestAudioAdaptorSoundWithValidMP3Filename(t *testing.T) {
    60  	execCommand = gobottest.ExecCommand
    61  
    62  	a := NewAdaptor()
    63  	defer func() { execCommand = exec.Command }()
    64  
    65  	errors := a.Sound("../../examples/laser.mp3")
    66  
    67  	gobottest.Assert(t, len(errors), 0)
    68  }