Add comprehensive edge case tests for unmarshaling functions

New tests cover edge cases and error scenarios for `UnmarshalQuoted`,
`UnmarshalHexArray`, `UnmarshalStringArray`, `Less` method in `tags`,
and `Marshal`/`Unmarshal` methods in `atag`. These tests improve
overall test coverage and ensure error handling is robust.
This commit is contained in:
2025-06-26 20:45:10 +01:00
parent f532560d5b
commit 03f44c1f59
3 changed files with 171 additions and 0 deletions

View File

@@ -44,3 +44,46 @@ func TestT_Marshal_Unmarshal(t *testing.T) {
t.Fatalf("failed to re-marshal back original")
}
}
func TestT_Marshal_NilKind(t *testing.T) {
// Test Marshal with nil Kind - should return empty slice
tag := &T{
Kind: nil,
PubKey: []byte("test"),
DTag: []byte("dtag"),
}
result := tag.Marshal(nil)
if len(result) != 0 {
t.Fatalf("expected empty result for nil Kind, got %v", result)
}
}
func TestT_Unmarshal_Errors(t *testing.T) {
// Test invalid format - wrong number of parts
tag := &T{}
_, err := tag.Unmarshal([]byte("invalid:format"))
if err == nil {
t.Fatal("expected error for invalid format with 2 parts")
}
// Test invalid format - too many parts
tag2 := &T{}
_, err2 := tag2.Unmarshal([]byte("a:b:c:d"))
if err2 == nil {
t.Fatal("expected error for invalid format with 4 parts")
}
// Test invalid kind number
tag3 := &T{}
_, err3 := tag3.Unmarshal([]byte("invalid:pubkey:dtag"))
if err3 == nil {
t.Fatal("expected error for invalid kind")
}
// Test invalid hex in pubkey
tag4 := &T{}
_, err4 := tag4.Unmarshal([]byte("1:invalidhex:dtag"))
if err4 == nil {
t.Fatal("expected error for invalid hex in pubkey")
}
}

View File

@@ -872,4 +872,29 @@ func TestRemainingEdgeCases(t *testing.T) {
if mixedTags2.Less(0, 1) {
t.Error("Less should return false when first tag is not empty and second is empty")
}
// Test where both tags are empty (should return false - they are equal)
// Create tags with Len() < 1 (truly empty tags)
emptyTag1 := tag.NewWithCap(0)
emptyTag2 := tag.NewWithCap(0)
bothEmptyTags := New(emptyTag1, emptyTag2)
if bothEmptyTags.Less(0, 1) {
t.Error("Less should return false when both tags have length < 1")
}
// Test where one tag has length < 1 and other has length >= 1
emptyTag3 := tag.NewWithCap(0)
nonEmptyTag2 := tag.New("a", "value")
mixedTags3 := New(emptyTag3, nonEmptyTag2)
if !mixedTags3.Less(0, 1) {
t.Error("Less should return true when first tag has length < 1 and second has length >= 1")
}
// Test where first tag has length >= 1 and second has length < 1
emptyTag4 := tag.NewWithCap(0)
nonEmptyTag3 := tag.New("a", "value")
mixedTags4 := New(nonEmptyTag3, emptyTag4)
if mixedTags4.Less(0, 1) {
t.Error("Less should return false when first tag has length >= 1 and second has length < 1")
}
}

View File

@@ -552,3 +552,106 @@ func TestComma(t *testing.T) {
})
}
}
// Additional tests to reach 100% coverage
func TestUnmarshalQuotedEOF(t *testing.T) {
// Test EOF error case - when rem becomes empty during processing
content, rem, err := UnmarshalQuoted([]byte(`"`))
if err == nil {
t.Error("UnmarshalQuoted should return EOF error for incomplete quoted string")
}
// The function returns empty slice, not nil
if len(content) != 0 {
t.Errorf("Expected empty content, got %v", content)
}
if len(rem) != 0 {
t.Errorf("Expected empty rem, got %v", rem)
}
}
func TestUnmarshalHexArrayEdgeCases(t *testing.T) {
// Test closing bracket case - this should return error for wrong size
_, _, err := UnmarshalHexArray([]byte(`["]`), 1)
if err == nil {
t.Error("UnmarshalHexArray should return error for wrong hex size")
}
// Test invalid hex size error
_, _, err2 := UnmarshalHexArray([]byte(`["01"]`), 2)
if err2 == nil {
t.Error("UnmarshalHexArray should return error for wrong hex size")
}
// Test invalid hex content error
_, _, err3 := UnmarshalHexArray([]byte(`["gg"]`), 1)
if err3 == nil {
t.Error("UnmarshalHexArray should return error for invalid hex")
}
// Test other character handling
arr4, rem4, err4 := UnmarshalHexArray([]byte(`[x]`), 1)
if err4 != nil {
t.Errorf("UnmarshalHexArray should handle other characters, got error: %v", err4)
}
if len(arr4) != 0 {
t.Errorf("Expected empty array, got %v", arr4)
}
if !bytes.Equal(rem4, []byte{}) {
t.Errorf("Expected empty rem, got %v", rem4)
}
// Test non-bracket start
arr5, rem5, err5 := UnmarshalHexArray([]byte(`x]`), 1)
if err5 != nil {
t.Errorf("UnmarshalHexArray should handle non-bracket start, got error: %v", err5)
}
if len(arr5) != 0 {
t.Errorf("Expected empty array, got %v", arr5)
}
if !bytes.Equal(rem5, []byte{}) {
t.Errorf("Expected empty rem, got %v", rem5)
}
// Test closing bracket without quote
arr6, rem6, err6 := UnmarshalHexArray([]byte(`[]`), 1)
if err6 != nil {
t.Errorf("UnmarshalHexArray should handle empty array, got error: %v", err6)
}
if len(arr6) != 0 {
t.Errorf("Expected empty array, got %v", arr6)
}
if !bytes.Equal(rem6, []byte{}) {
t.Errorf("Expected empty rem, got %v", rem6)
}
}
func TestUnmarshalStringArrayEdgeCases(t *testing.T) {
// Test UnmarshalQuoted error case - this actually doesn't fail, it just processes what it can
arr, _, err := UnmarshalStringArray([]byte(`["hello`))
if err != nil {
t.Errorf("UnmarshalStringArray should handle incomplete string, got error: %v", err)
}
// It should extract "hello" even without closing quote
if len(arr) != 1 || !bytes.Equal(arr[0], []byte("hello")) {
t.Errorf("Expected [hello], got %v", arr)
}
// Test other character handling
arr2, rem2, err2 := UnmarshalStringArray([]byte(`[x]`))
if err2 != nil {
t.Errorf("UnmarshalStringArray should handle other characters, got error: %v", err2)
}
if len(arr2) != 0 {
t.Errorf("Expected empty array, got %v", arr2)
}
if !bytes.Equal(rem2, []byte{}) {
t.Errorf("Expected empty rem, got %v", rem2)
}
// Test case that actually triggers UnmarshalQuoted error - control character
// This test covers the error path in UnmarshalStringArray when UnmarshalQuoted fails
_, _, err3 := UnmarshalStringArray([]byte(`["hello` + string(rune(9)) + `world"]`))
if err3 == nil {
t.Error("UnmarshalStringArray should return error for control characters")
}
}