mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-05 15:06:50 +08:00
Feature/cover unit test (#81)
This commit is contained in:
@@ -1,36 +1,76 @@
|
||||
package mapUtil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMapValueExists(t *testing.T) {
|
||||
m := map[string]string{
|
||||
"foo": "bar",
|
||||
"baz": "123",
|
||||
}
|
||||
if !MapValueExists(m, "bar") {
|
||||
t.Errorf("expected MapValueExists to return true for value 'bar'")
|
||||
}
|
||||
if !MapValueExists(m, "123") {
|
||||
t.Errorf("expected MapValueExists to return true for value 123")
|
||||
}
|
||||
if MapValueExists(m, "non-existent") {
|
||||
t.Errorf("expected MapValueExists to return false for value 'non-existent'")
|
||||
}
|
||||
}
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMapKeyExists(t *testing.T) {
|
||||
m := map[string]any{
|
||||
"foo": "bar",
|
||||
"baz": 123,
|
||||
"qux": []int{1, 2, 3},
|
||||
testCases := []struct {
|
||||
name string
|
||||
inputMap map[string]string
|
||||
inputKey string
|
||||
wantRes bool
|
||||
}{
|
||||
{
|
||||
name: "存在",
|
||||
inputMap: map[string]string{
|
||||
"for": "jack",
|
||||
"bar": "123",
|
||||
},
|
||||
inputKey: "for",
|
||||
wantRes: true,
|
||||
},
|
||||
{
|
||||
name: "不存在",
|
||||
inputMap: map[string]string{
|
||||
"for": "jack",
|
||||
"bar": "123",
|
||||
},
|
||||
inputKey: "tom",
|
||||
wantRes: false,
|
||||
},
|
||||
}
|
||||
if !MapKeyExists(m, "foo") {
|
||||
t.Errorf("expected MapKeyExists to return true for key 'foo'")
|
||||
}
|
||||
if !MapKeyExists(m, "baz") {
|
||||
t.Errorf("expected MapKeyExists to return true for key 'baz'")
|
||||
}
|
||||
if MapKeyExists(m, "non-existent") {
|
||||
t.Errorf("expected MapKeyExists to return false for key 'non-existent'")
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := MapKeyExists(tc.inputMap, tc.inputKey)
|
||||
assert.Equal(t, tc.wantRes, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapValueExists(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
inputMap map[string]string
|
||||
inputValue string
|
||||
wantRes bool
|
||||
}{
|
||||
{
|
||||
name: "存在",
|
||||
inputMap: map[string]string{
|
||||
"for": "jack",
|
||||
"bar": "123",
|
||||
},
|
||||
inputValue: "jack",
|
||||
wantRes: true,
|
||||
},
|
||||
{
|
||||
name: "不存在",
|
||||
inputMap: map[string]string{
|
||||
"for": "jack",
|
||||
"bar": "123",
|
||||
},
|
||||
inputValue: "tom",
|
||||
wantRes: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := MapValueExists(tc.inputMap, tc.inputValue)
|
||||
assert.Equal(t, tc.wantRes, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user