Files
go-easy-utils/mapx/exists_test.go
2025-07-08 15:26:54 +08:00

77 lines
1.2 KiB
Go

package mapx
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestKeyExists(t *testing.T) {
testCases := []struct {
name string
input map[string]string
inputKey string
wantRes bool
}{
{
name: "存在",
input: map[string]string{
"for": "jack",
"bar": "123",
},
inputKey: "for",
wantRes: true,
},
{
name: "不存在",
input: map[string]string{
"for": "jack",
"bar": "123",
},
inputKey: "tom",
wantRes: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := KeyExists(tc.input, tc.inputKey)
assert.Equal(t, tc.wantRes, res)
})
}
}
func TestValueExists(t *testing.T) {
testCases := []struct {
name string
input map[string]string
inputValue string
wantRes bool
}{
{
name: "存在",
input: map[string]string{
"for": "jack",
"bar": "123",
},
inputValue: "jack",
wantRes: true,
},
{
name: "不存在",
input: map[string]string{
"for": "jack",
"bar": "123",
},
inputValue: "tom",
wantRes: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := ValueExists(tc.input, tc.inputValue)
assert.Equal(t, tc.wantRes, res)
})
}
}