2023-11-15 09:19:51 +00:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-11-15 09:19:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertFromTypedToTypedEqual[T any](t *testing.T, src T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
nv, err := FromTyped(src, dyn.NilValue)
|
2023-11-15 09:19:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var dst T
|
|
|
|
err = ToTyped(&dst, nv)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdditional(t *testing.T) {
|
|
|
|
type StructType struct {
|
|
|
|
Str string `json:"str"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tmp struct {
|
|
|
|
MapToPointer map[string]*string `json:"map_to_pointer"`
|
|
|
|
SliceOfPointer []*string `json:"slice_of_pointer"`
|
|
|
|
NestedStruct StructType `json:"nested_struct"`
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty map", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
MapToPointer: map[string]*string{},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-01-31 14:25:13 +00:00
|
|
|
t.Run("map with empty string value", func(t *testing.T) {
|
|
|
|
s := ""
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
MapToPointer: map[string]*string{
|
|
|
|
"key": &s,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-11-15 09:19:51 +00:00
|
|
|
t.Run("map with nil value", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
MapToPointer: map[string]*string{
|
|
|
|
"key": nil,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty slice", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
SliceOfPointer: []*string{},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with nil value", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
SliceOfPointer: []*string{nil},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|