From 10b92b60364e77b31e98b3bc14426d1d71f3be13 Mon Sep 17 00:00:00 2001 From: Chin-Ya Huang Date: Fri, 20 Sep 2024 13:42:17 +0800 Subject: [PATCH] test(utils): sort keys longhorn/longhorn-8430 Signed-off-by: Chin-Ya Huang --- utils/misc_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/utils/misc_test.go b/utils/misc_test.go index 1164138c..dbda4f19 100644 --- a/utils/misc_test.go +++ b/utils/misc_test.go @@ -243,3 +243,50 @@ func (s *TestSuite) TestConvertTypeToString(c *C) { c.Assert(result, Equals, testCase.expected, Commentf(test.ErrResultFmt, testName)) } } + +func (s *TestSuite) TestSortKeys(c *C) { + type testCase struct { + inputMap map[any]any + + expected []any + expectError bool + } + testCases := map[string]testCase{ + "SortKeys(...): string": { + inputMap: map[any]any{ + "b": "", + "c": "", + "a": "", + }, + expected: []any{"a", "b", "c"}, + }, + "SortKeys(...): uint64": { + inputMap: map[any]any{ + uint64(2): "", + uint64(1): "", + uint64(3): "", + }, + expected: []any{uint64(1), uint64(2), uint64(3)}, + }, + "SortKeys(...): empty map": { + inputMap: map[any]any{}, + expected: []any{}, + }, + "SortKeys(...): nil map": { + inputMap: nil, + expectError: true, + }, + } + for testName, testCase := range testCases { + c.Logf("testing utils.%v", testName) + + result, err := SortKeys(testCase.inputMap) + if testCase.expectError { + c.Assert(err, NotNil, Commentf(test.ErrResultFmt, testName)) + continue + } + c.Assert(err, IsNil, Commentf(test.ErrResultFmt, testName)) + + c.Assert(result, DeepEquals, testCase.expected, Commentf(test.ErrResultFmt, testName)) + } +}