diff --git a/partner/blackmagic.go b/partner/blackmagic.go index 4f788a4a..5086e796 100644 --- a/partner/blackmagic.go +++ b/partner/blackmagic.go @@ -25,9 +25,12 @@ type BlackmagicClient struct { // Creates a new Blackmagic client with the given device addresses as a comma-separated string. func NewBlackmagicClient(addresses string) *BlackmagicClient { - deviceAddresses := strings.Split(addresses, ",") - for i, address := range deviceAddresses { - deviceAddresses[i] = strings.TrimSpace(address) + var deviceAddresses []string + for _, address := range strings.Split(addresses, ",") { + trimmedAddress := strings.TrimSpace(address) + if trimmedAddress != "" { + deviceAddresses = append(deviceAddresses, trimmedAddress) + } } return &BlackmagicClient{deviceAddresses: deviceAddresses} } diff --git a/partner/blackmagic_test.go b/partner/blackmagic_test.go new file mode 100644 index 00000000..a43a1868 --- /dev/null +++ b/partner/blackmagic_test.go @@ -0,0 +1,32 @@ +// Copyright 2024 Team 254. All Rights Reserved. +// Author: pat@patfairbank.com (Patrick Fairbank) + +package partner + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewBlackmagicClient(t *testing.T) { + // Test with an empty address. + client := NewBlackmagicClient("") + assert.Equal(t, 0, len(client.deviceAddresses)) + + // Test with whitespace in the address. + client = NewBlackmagicClient(" ") + assert.Equal(t, 0, len(client.deviceAddresses)) + + // Test with a single address. + client = NewBlackmagicClient("1.2.3.4") + if assert.Equal(t, 1, len(client.deviceAddresses)) { + assert.Equal(t, "1.2.3.4", client.deviceAddresses[0]) + } + + // Test with multiple addresses. + client = NewBlackmagicClient(" 1.2.3.4 , 5.6.7.8 ") + if assert.Equal(t, 2, len(client.deviceAddresses)) { + assert.Equal(t, "1.2.3.4", client.deviceAddresses[0]) + assert.Equal(t, "5.6.7.8", client.deviceAddresses[1]) + } +}