From c58d52e49a478aa71a090c301c992b7f8a48455e Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 25 Oct 2024 09:48:00 +0200 Subject: [PATCH] Refactor TestImportance_StringFuncs in header_test.go Separated test cases for String, NumString, and XPrioString methods of the Importance object into distinct sub-tests. Improved readability and maintainability by grouping similar assertions together. --- header_test.go | 58 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/header_test.go b/header_test.go index a060ae6..832c63d 100644 --- a/header_test.go +++ b/header_test.go @@ -8,14 +8,13 @@ import ( "testing" ) -// TestImportance_StringFuncs tests the different string method of the Importance object -func TestImportance_StringFuncs(t *testing.T) { +func TestImportance_Stringer(t *testing.T) { tests := []struct { - name string - imp Importance - wantns string - xprio string - want string + name string + imp Importance + wantnum string + xprio string + want string }{ {"Importance: Non-Urgent", ImportanceNonUrgent, "0", "5", "non-urgent"}, {"Importance: Low", ImportanceLow, "0", "5", "low"}, @@ -24,22 +23,35 @@ func TestImportance_StringFuncs(t *testing.T) { {"Importance: Urgent", ImportanceUrgent, "1", "1", "urgent"}, {"Importance: Unknown", 9, "", "", ""}, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if tt.imp.NumString() != tt.wantns { - t.Errorf("wrong number string for Importance returned. Expected: %s, got: %s", - tt.wantns, tt.imp.NumString()) - } - if tt.imp.XPrioString() != tt.xprio { - t.Errorf("wrong x-prio string for Importance returned. Expected: %s, got: %s", - tt.xprio, tt.imp.XPrioString()) - } - if tt.imp.String() != tt.want { - t.Errorf("wrong string for Importance returned. Expected: %s, got: %s", - tt.want, tt.imp.String()) - } - }) - } + t.Run("String", func(t *testing.T) { + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.imp.String() != tt.want { + t.Errorf("wrong string for Importance returned. Expected: %s, got: %s", tt.want, tt.imp.String()) + } + }) + } + }) + t.Run("NumString", func(t *testing.T) { + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.imp.NumString() != tt.wantnum { + t.Errorf("wrong number string for Importance returned. Expected: %s, got: %s", tt.wantnum, + tt.imp.NumString()) + } + }) + } + }) + t.Run("XPrioString", func(t *testing.T) { + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.imp.XPrioString() != tt.xprio { + t.Errorf("wrong x-prio string for Importance returned. Expected: %s, got: %s", tt.xprio, + tt.imp.XPrioString()) + } + }) + } + }) } // TestAddrHeader_String tests the string method of the AddrHeader object