From 63f600eef437a2d4265790272f7dac34dee42e90 Mon Sep 17 00:00:00 2001 From: Mengjiao Liu Date: Fri, 11 Mar 2022 16:23:03 +0800 Subject: [PATCH] Improve unit test coverage and readability in `pkg/util/netsh/` --- pkg/util/netsh/netsh_test.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/util/netsh/netsh_test.go b/pkg/util/netsh/netsh_test.go index 40508fc29ff..7b184a98043 100644 --- a/pkg/util/netsh/netsh_test.go +++ b/pkg/util/netsh/netsh_test.go @@ -441,27 +441,43 @@ func TestCheckIPExists(t *testing.T) { func TestGetIP(t *testing.T) { testcases := []struct { + name string showAddress string expectAddress string }{ { + name: "IP address displayed in Chinese", showAddress: "IP 地址: 10.96.0.2", expectAddress: "10.96.0.2", }, { + name: "IP address displayed in English", showAddress: "IP Address: 10.96.0.3", expectAddress: "10.96.0.3", }, { + name: "IP address without spaces", showAddress: "IP Address:10.96.0.4", expectAddress: "10.96.0.4", }, + { + name: "Only 'IP Address:' field exists", + showAddress: "IP Address:", + expectAddress: "", + }, + { + name: "IP address without ':' separator", + showAddress: "IP Address10.6.9.2", + expectAddress: "", + }, } for _, tc := range testcases { - address := getIP(tc.showAddress) - if address != tc.expectAddress { - t.Errorf("expected address=%q, got %q", tc.expectAddress, address) - } + t.Run(tc.name, func(t *testing.T) { + address := getIP(tc.showAddress) + if address != tc.expectAddress { + t.Errorf("expected address=%q, got %q", tc.expectAddress, address) + } + }) } }