Skip to content

Commit

Permalink
Fix gaps in Blackmagic address parsing (fixes #206).
Browse files Browse the repository at this point in the history
  • Loading branch information
patfair committed Dec 1, 2024
1 parent 184a9ab commit 2baa45c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
9 changes: 6 additions & 3 deletions partner/blackmagic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
32 changes: 32 additions & 0 deletions partner/blackmagic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Team 254. All Rights Reserved.
// Author: [email protected] (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])
}
}

0 comments on commit 2baa45c

Please sign in to comment.