package adif import ( "strings" "testing" "unicode/utf8" ) // Loggers (notably Log4OM's UDP/exported ADIF) sometimes declare a field // length as the CHARACTER count instead of the UTF-8 byte count, truncating // multibyte values mid-rune. The parser must recover the full value. func TestCharCountLengthRepair(t *testing.T) { cases := []struct{ name, wantQTH, wantName, adi string }{ { name: "latin", wantQTH: "Tóalmás", // 7 chars / 9 bytes, declared 7 wantName: "Laci Budai", adi: "\nHA5XYTóalmásLaci Budai\n", }, { name: "cyrillic", wantQTH: "Дзержинск", // 9 chars / 18 bytes, declared 9 wantName: "Александр Чайка", // 15 chars / 29 bytes, declared 15 adi: "\nUA3TFSАлександр ЧайкаДзержинск\n", }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { var got Record if err := Parse(strings.NewReader(c.adi), func(r Record) error { got = r; return nil }); err != nil { t.Fatalf("parse: %v", err) } if got["qth"] != c.wantQTH { t.Errorf("qth = %q, want %q", got["qth"], c.wantQTH) } if got["name"] != c.wantName { t.Errorf("name = %q, want %q", got["name"], c.wantName) } if !utf8.ValidString(got["name"]) || !utf8.ValidString(got["qth"]) { t.Errorf("result not valid UTF-8: name=%q qth=%q", got["name"], got["qth"]) } }) } }