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.
This commit is contained in:
Winni Neessen 2024-10-25 09:48:00 +02:00
parent eebbaa2513
commit c58d52e49a
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -8,14 +8,13 @@ import (
"testing" "testing"
) )
// TestImportance_StringFuncs tests the different string method of the Importance object func TestImportance_Stringer(t *testing.T) {
func TestImportance_StringFuncs(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
imp Importance imp Importance
wantns string wantnum string
xprio string xprio string
want string want string
}{ }{
{"Importance: Non-Urgent", ImportanceNonUrgent, "0", "5", "non-urgent"}, {"Importance: Non-Urgent", ImportanceNonUrgent, "0", "5", "non-urgent"},
{"Importance: Low", ImportanceLow, "0", "5", "low"}, {"Importance: Low", ImportanceLow, "0", "5", "low"},
@ -24,22 +23,35 @@ func TestImportance_StringFuncs(t *testing.T) {
{"Importance: Urgent", ImportanceUrgent, "1", "1", "urgent"}, {"Importance: Urgent", ImportanceUrgent, "1", "1", "urgent"},
{"Importance: Unknown", 9, "", "", ""}, {"Importance: Unknown", 9, "", "", ""},
} }
for _, tt := range tests { t.Run("String", func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { for _, tt := range tests {
if tt.imp.NumString() != tt.wantns { t.Run(tt.name, func(t *testing.T) {
t.Errorf("wrong number string for Importance returned. Expected: %s, got: %s", if tt.imp.String() != tt.want {
tt.wantns, tt.imp.NumString()) t.Errorf("wrong string for Importance returned. Expected: %s, got: %s", tt.want, tt.imp.String())
} }
if tt.imp.XPrioString() != tt.xprio { })
t.Errorf("wrong x-prio string for Importance returned. Expected: %s, got: %s", }
tt.xprio, tt.imp.XPrioString()) })
} t.Run("NumString", func(t *testing.T) {
if tt.imp.String() != tt.want { for _, tt := range tests {
t.Errorf("wrong string for Importance returned. Expected: %s, got: %s", t.Run(tt.name, func(t *testing.T) {
tt.want, tt.imp.String()) 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 // TestAddrHeader_String tests the string method of the AddrHeader object