From 69c5f43cbf697ec4ddaf465fd423ce1fbf393838 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 25 Oct 2024 11:36:17 +0200 Subject: [PATCH] Refactor test cases in header_test.go Consolidate repetitive test case definitions into separate slices for readability and maintainability. This change simplifies the addition of new test cases and reduces redundancy. --- header_test.go | 99 ++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/header_test.go b/header_test.go index bfe1d12..02bab82 100644 --- a/header_test.go +++ b/header_test.go @@ -8,6 +8,54 @@ import ( "testing" ) +var ( + genHeaderTests = []struct { + name string + header Header + want string + }{ + {"Header: Content-Description", HeaderContentDescription, "Content-Description"}, + {"Header: Content-Disposition", HeaderContentDisposition, "Content-Disposition"}, + {"Header: Content-ID", HeaderContentID, "Content-ID"}, + {"Header: Content-Language", HeaderContentLang, "Content-Language"}, + {"Header: Content-Location", HeaderContentLocation, "Content-Location"}, + {"Header: Content-Transfer-Encoding", HeaderContentTransferEnc, "Content-Transfer-Encoding"}, + {"Header: Content-Type", HeaderContentType, "Content-Type"}, + {"Header: Date", HeaderDate, "Date"}, + { + "Header: Disposition-Notification-To", HeaderDispositionNotificationTo, + "Disposition-Notification-To", + }, + {"Header: Importance", HeaderImportance, "Importance"}, + {"Header: In-Reply-To", HeaderInReplyTo, "In-Reply-To"}, + {"Header: List-Unsubscribe", HeaderListUnsubscribe, "List-Unsubscribe"}, + {"Header: List-Unsubscribe-Post", HeaderListUnsubscribePost, "List-Unsubscribe-Post"}, + {"Header: Message-ID", HeaderMessageID, "Message-ID"}, + {"Header: MIME-Version", HeaderMIMEVersion, "MIME-Version"}, + {"Header: Organization", HeaderOrganization, "Organization"}, + {"Header: Precedence", HeaderPrecedence, "Precedence"}, + {"Header: Priority", HeaderPriority, "Priority"}, + {"Header: References", HeaderReferences, "References"}, + {"Header: Reply-To", HeaderReplyTo, "Reply-To"}, + {"Header: Subject", HeaderSubject, "Subject"}, + {"Header: User-Agent", HeaderUserAgent, "User-Agent"}, + {"Header: X-Auto-Response-Suppress", HeaderXAutoResponseSuppress, "X-Auto-Response-Suppress"}, + {"Header: X-Mailer", HeaderXMailer, "X-Mailer"}, + {"Header: X-MSMail-Priority", HeaderXMSMailPriority, "X-MSMail-Priority"}, + {"Header: X-Priority", HeaderXPriority, "X-Priority"}, + } + addrHeaderTests = []struct { + name string + ah AddrHeader + want string + }{ + {"Address header: From", HeaderFrom, "From"}, + {"Address header: To", HeaderTo, "To"}, + {"Address header: Cc", HeaderCc, "Cc"}, + {"Address header: Bcc", HeaderBcc, "Bcc"}, + } +) + func TestImportance_Stringer(t *testing.T) { tests := []struct { name string @@ -55,17 +103,7 @@ func TestImportance_Stringer(t *testing.T) { } func TestAddrHeader_Stringer(t *testing.T) { - tests := []struct { - name string - ah AddrHeader - want string - }{ - {"Address header: From", HeaderFrom, "From"}, - {"Address header: To", HeaderTo, "To"}, - {"Address header: Cc", HeaderCc, "Cc"}, - {"Address header: Bcc", HeaderBcc, "Bcc"}, - } - for _, tt := range tests { + for _, tt := range addrHeaderTests { t.Run(tt.name, func(t *testing.T) { if tt.ah.String() != tt.want { t.Errorf("wrong string for AddrHeader returned. Expected: %s, got: %s", @@ -76,44 +114,11 @@ func TestAddrHeader_Stringer(t *testing.T) { } func TestHeader_Stringer(t *testing.T) { - tests := []struct { - name string - h Header - want string - }{ - {"Header: Content-Description", HeaderContentDescription, "Content-Description"}, - {"Header: Content-Disposition", HeaderContentDisposition, "Content-Disposition"}, - {"Header: Content-ID", HeaderContentID, "Content-ID"}, - {"Header: Content-Language", HeaderContentLang, "Content-Language"}, - {"Header: Content-Location", HeaderContentLocation, "Content-Location"}, - {"Header: Content-Transfer-Encoding", HeaderContentTransferEnc, "Content-Transfer-Encoding"}, - {"Header: Content-Type", HeaderContentType, "Content-Type"}, - {"Header: Date", HeaderDate, "Date"}, - {"Header: Disposition-Notification-To", HeaderDispositionNotificationTo, - "Disposition-Notification-To"}, - {"Header: Importance", HeaderImportance, "Importance"}, - {"Header: In-Reply-To", HeaderInReplyTo, "In-Reply-To"}, - {"Header: List-Unsubscribe", HeaderListUnsubscribe, "List-Unsubscribe"}, - {"Header: List-Unsubscribe-Post", HeaderListUnsubscribePost, "List-Unsubscribe-Post"}, - {"Header: Message-ID", HeaderMessageID, "Message-ID"}, - {"Header: MIME-Version", HeaderMIMEVersion, "MIME-Version"}, - {"Header: Organization", HeaderOrganization, "Organization"}, - {"Header: Precedence", HeaderPrecedence, "Precedence"}, - {"Header: Priority", HeaderPriority, "Priority"}, - {"Header: References", HeaderReferences, "References"}, - {"Header: Reply-To", HeaderReplyTo, "Reply-To"}, - {"Header: Subject", HeaderSubject, "Subject"}, - {"Header: User-Agent", HeaderUserAgent, "User-Agent"}, - {"Header: X-Auto-Response-Suppress", HeaderXAutoResponseSuppress, "X-Auto-Response-Suppress"}, - {"Header: X-Mailer", HeaderXMailer, "X-Mailer"}, - {"Header: X-MSMail-Priority", HeaderXMSMailPriority, "X-MSMail-Priority"}, - {"Header: X-Priority", HeaderXPriority, "X-Priority"}, - } - for _, tt := range tests { + for _, tt := range genHeaderTests { t.Run(tt.name, func(t *testing.T) { - if tt.h.String() != tt.want { + if tt.header.String() != tt.want { t.Errorf("wrong string for Header returned. Expected: %s, got: %s", - tt.want, tt.h.String()) + tt.want, tt.header.String()) } }) }