1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-11 21:17:19 +00:00

Fix possible panic (#34508)

This commit is contained in:
Lunny Xiao
2025-05-22 05:59:42 -07:00
committed by GitHub
parent 0d1d57c5bf
commit 06ccda06c4
3 changed files with 65 additions and 25 deletions

26
modules/util/map_test.go Normal file
View File

@ -0,0 +1,26 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package util
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetMapValueOrDefault(t *testing.T) {
testMap := map[string]any{
"key1": "value1",
"key2": 42,
"key3": nil,
}
assert.Equal(t, "value1", GetMapValueOrDefault(testMap, "key1", "default"))
assert.Equal(t, 42, GetMapValueOrDefault(testMap, "key2", 0))
assert.Equal(t, "default", GetMapValueOrDefault(testMap, "key4", "default"))
assert.Equal(t, 100, GetMapValueOrDefault(testMap, "key5", 100))
assert.Equal(t, "default", GetMapValueOrDefault(testMap, "key3", "default"))
}