From 03da20fc3949c85508fc4d60a18e7a7b448130fa Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 25 Oct 2024 19:43:05 +0200 Subject: [PATCH] Add unit tests for Msg_ToIgnoreInvalid behavior Introduces new test cases to verify the Msg_ToIgnoreInvalid function handles various scenarios correctly. These tests check for behavior with valid addresses, invalid addresses, and a mix of both. --- msg_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/msg_test.go b/msg_test.go index 565eaff..45935b3 100644 --- a/msg_test.go +++ b/msg_test.go @@ -1495,7 +1495,81 @@ func TestMsg_AddToFormat(t *testing.T) { } func TestMsg_ToIgnoreInvalid(t *testing.T) { - + t.Run("ToIgnoreInvalid with valid address", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + message.ToIgnoreInvalid("toni.tester@example.com") + addresses, ok := message.addrHeader[HeaderTo] + if !ok { + t.Fatalf("failed to set ToIgnoreInvalid, addrHeader field is not set") + } + if len(addresses) != 1 { + t.Fatalf("failed to set ToIgnoreInvalid, addrHeader value count is: %d, want: 1", len(addresses)) + } + if addresses[0].Address != "toni.tester@example.com" { + t.Errorf("failed to set ToIgnoreInvalid, addrHeader value is %s, want: %s", addresses[0].Address, + "toni.tester@example.com") + } + if addresses[0].String() != "" { + t.Errorf("failed to set ToIgnoreInvalid, addrHeader value is %s, want: %s", addresses[0].String(), + "") + } + if addresses[0].Name != "" { + t.Errorf("failed to set ToIgnoreInvalid, addrHeader name is %s, want: empty", addresses[0].Name) + } + }) + t.Run("ToIgnoreInvalid with invalid address", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + message.ToIgnoreInvalid("invalid") + addresses, ok := message.addrHeader[HeaderTo] + if !ok { + t.Fatalf("failed to set ToIgnoreInvalid, addrHeader field is not set") + } + if len(addresses) != 0 { + t.Fatalf("failed to set ToIgnoreInvalid, addrHeader value count is: %d, want: 0", len(addresses)) + } + }) + t.Run("ToIgnoreInvalid with valid and invalid addresses", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + message.ToIgnoreInvalid("toni.tester@example.com", "invalid", "tina.tester@example.com") + addresses, ok := message.addrHeader[HeaderTo] + if !ok { + t.Fatalf("failed to set ToIgnoreInvalid, addrHeader field is not set") + } + if len(addresses) != 2 { + t.Fatalf("failed to set ToIgnoreInvalid, addrHeader value count is: %d, want: 2", len(addresses)) + } + if addresses[0].Address != "toni.tester@example.com" { + t.Errorf("failed to set ToIgnoreInvalid, addrHeader value is %s, want: %s", addresses[0].Address, + "toni.tester@example.com") + } + if addresses[0].String() != "" { + t.Errorf("failed to set ToIgnoreInvalid, addrHeader value is %s, want: %s", addresses[0].String(), + "") + } + if addresses[0].Name != "" { + t.Errorf("failed to set ToIgnoreInvalid, addrHeader name is %s, want: empty", addresses[0].Name) + } + if addresses[1].Address != "tina.tester@example.com" { + t.Errorf("failed to set ToIgnoreInvalid, addrHeader value is %s, want: %s", addresses[1].Address, + "tina.tester@example.com") + } + if addresses[1].String() != "" { + t.Errorf("failed to set ToIgnoreInvalid, addrHeader value is %s, want: %s", addresses[1].String(), + "") + } + if addresses[1].Name != "" { + t.Errorf("failed to set ToIgnoreInvalid, addrHeader name is %s, want: empty", addresses[1].Name) + } + }) } /*