61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package utils
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetOrDefault(t *testing.T) {
|
|
type args struct {
|
|
data map[string]any
|
|
key string
|
|
defaultVal any
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want any
|
|
}{
|
|
{
|
|
name: "Test 1: Nil map",
|
|
args: args{
|
|
data: nil,
|
|
key: "key1",
|
|
defaultVal: "default",
|
|
},
|
|
want: "default",
|
|
},
|
|
{
|
|
name: "Test 2: Existing key",
|
|
args: args{
|
|
data: map[string]interface{}{
|
|
"key1": "value1",
|
|
"key2": "value2",
|
|
},
|
|
key: "key1",
|
|
defaultVal: "default",
|
|
},
|
|
want: "value1",
|
|
},
|
|
{
|
|
name: "Test 3: Non-existing key",
|
|
args: args{
|
|
data: map[string]interface{}{
|
|
"key1": "value1",
|
|
"key2": "value2",
|
|
},
|
|
key: "key3",
|
|
defaultVal: "default",
|
|
},
|
|
want: "default",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := GetOrDefault(tt.args.data, tt.args.key, tt.args.defaultVal); !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("GetOrDefault() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|