diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-11-24 21:48:01 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-11-24 22:00:09 +0900 |
| commit | 9f7b0c2f46305f8a321791d6fcedc3df7f966783 (patch) | |
| tree | b565a4a7d8e77f8f6717e46837f6b7d6ee174fc7 /internal/pipewire/pod_test.go | |
| parent | 3e87187c4c4eb43ed1c9dc1c0c6613e5bed1dc6c (diff) | |
internal/pipewire: add type constants
This change also centralises encoding testing.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pipewire/pod_test.go')
| -rw-r--r-- | internal/pipewire/pod_test.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/pipewire/pod_test.go b/internal/pipewire/pod_test.go new file mode 100644 index 00000000..a7982865 --- /dev/null +++ b/internal/pipewire/pod_test.go @@ -0,0 +1,55 @@ +package pipewire_test + +import ( + "encoding" + "reflect" + "testing" +) + +type encodingTestCases[V any, S interface { + encoding.BinaryMarshaler + encoding.BinaryUnmarshaler + *V +}] []struct { + // Uninterpreted name of subtest. + name string + // Encoded data. + wantData []byte + // Value corresponding to wantData. + value V + // Expected decoding error. Skips encoding check if non-nil. + wantErr error +} + +// run runs all test cases as subtests of [testing.T]. +func (testCases encodingTestCases[V, S]) run(t *testing.T) { + t.Helper() + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + t.Run("decode", func(t *testing.T) { + t.Parallel() + + var value V + if err := S(&value).UnmarshalBinary(tc.wantData); err != nil { + t.Fatalf("UnmarshalBinary: error = %v", err) + } + if !reflect.DeepEqual(&value, &tc.value) { + t.Fatalf("UnmarshalBinary: %#v, want %#v", value, tc.value) + } + }) + + t.Run("encode", func(t *testing.T) { + t.Parallel() + + if gotData, err := S(&tc.value).MarshalBinary(); err != nil { + t.Fatalf("MarshalBinary: error = %v", err) + } else if string(gotData) != string(tc.wantData) { + t.Fatalf("MarshalBinary: %#v, want %#v", gotData, tc.wantData) + } + }) + }) + } +} |
