92 lines
1.8 KiB
Go
92 lines
1.8 KiB
Go
/*
|
|
* ============================================================================
|
|
* Projekt.....: rs2322tcp
|
|
* Datei.......: main_test.go
|
|
* Copyright (C) 2026 Dieter Lang
|
|
*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
* Beschreibung:
|
|
* Unit-Tests für die Hex-Eingabe des Geräte-Simulators.
|
|
*
|
|
* Getestet werden gültige Bytefolgen, optionale Leerzeichen, Groß- und
|
|
* Kleinschreibung sowie typische ungültige Eingaben.
|
|
*
|
|
* ============================================================================
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseHexBytes(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
want []byte
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "mehrere Bytes mit Leerzeichen",
|
|
input: "46 41 00 10 0D",
|
|
want: []byte{0x46, 0x41, 0x00, 0x10, 0x0D},
|
|
},
|
|
{
|
|
name: "mehrere Bytes ohne Leerzeichen",
|
|
input: "464100100D",
|
|
want: []byte{0x46, 0x41, 0x00, 0x10, 0x0D},
|
|
},
|
|
{
|
|
name: "gemischte Groß- und Kleinschreibung",
|
|
input: "0a FF 01 b7",
|
|
want: []byte{0x0A, 0xFF, 0x01, 0xB7},
|
|
},
|
|
{
|
|
name: "ein Byte",
|
|
input: "00",
|
|
want: []byte{0x00},
|
|
},
|
|
{
|
|
name: "leere Eingabe",
|
|
input: "",
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "nur Leerzeichen",
|
|
input: " ",
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "ungültiges Hex-Zeichen",
|
|
input: "GG",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "ungerade Hex-Länge",
|
|
input: "4",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "dreistellige Eingabe",
|
|
input: "100",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := parseHexBytes(tt.input)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("Fehlerstatus: got %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
|
|
if !bytes.Equal(got, tt.want) {
|
|
t.Fatalf("Ergebnis: got % X, want % X", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|