From c8a2fc2ecb5ca4811997fa8c8fe9e269028fe712 Mon Sep 17 00:00:00 2001 From: syuparn Date: Fri, 3 Nov 2023 10:39:15 +0900 Subject: [PATCH] vcsim: Fix PropertyCollector to handle empty property Closes: #3275 Signed-off-by: syuparn --- simulator/property_collector.go | 6 ++++ simulator/property_collector_test.go | 42 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/simulator/property_collector.go b/simulator/property_collector.go index 851a32f7b..4f4b8124b 100644 --- a/simulator/property_collector.go +++ b/simulator/property_collector.go @@ -248,10 +248,16 @@ func toString(v *string) string { } func lcFirst(s string) string { + if len(s) < 1 { + return s + } return strings.ToLower(s[:1]) + s[1:] } func ucFirst(s string) string { + if len(s) < 1 { + return s + } return strings.ToUpper(s[:1]) + s[1:] } diff --git a/simulator/property_collector_test.go b/simulator/property_collector_test.go index be3ae34df..9e400d1e8 100644 --- a/simulator/property_collector_test.go +++ b/simulator/property_collector_test.go @@ -1709,3 +1709,45 @@ func TestPropertyCollectorNoPathSet(t *testing.T) { t.Fatalf("len(content)=%d", len(content)) } } + +func TestLcFirst(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {input: "ABC", expected: "aBC"}, + {input: "abc", expected: "abc"}, + {input: "", expected: ""}, + } + + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + actual := lcFirst(tt.input) + + if actual != tt.expected { + t.Errorf("%q != %q", actual, tt.expected) + } + }) + } +} + +func TestUcFirst(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {input: "ABC", expected: "ABC"}, + {input: "abc", expected: "Abc"}, + {input: "", expected: ""}, + } + + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + actual := ucFirst(tt.input) + + if actual != tt.expected { + t.Errorf("%q != %q", actual, tt.expected) + } + }) + } +}