2023-11-03 19:15:47 +00:00
|
|
|
package merge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2024-03-25 11:01:09 +00:00
|
|
|
assert "github.com/databricks/cli/libs/dyn/dynassert"
|
2023-11-03 19:15:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMergeMaps(t *testing.T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
v1 := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V("bar"),
|
|
|
|
"bar": dyn.V("baz"),
|
2023-11-03 19:15:47 +00:00
|
|
|
})
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
v2 := dyn.V(map[string]dyn.Value{
|
|
|
|
"bar": dyn.V("qux"),
|
|
|
|
"qux": dyn.V("foo"),
|
2023-11-03 19:15:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Merge v2 into v1.
|
|
|
|
{
|
|
|
|
out, err := Merge(v1, v2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, map[string]any{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "qux",
|
|
|
|
"qux": "foo",
|
|
|
|
}, out.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge v1 into v2.
|
|
|
|
{
|
|
|
|
out, err := Merge(v2, v1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, map[string]any{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
"qux": "foo",
|
|
|
|
}, out.AsAny())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeMapsNil(t *testing.T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
v := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V("bar"),
|
2023-11-03 19:15:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Merge nil into v.
|
|
|
|
{
|
2023-12-22 13:20:45 +00:00
|
|
|
out, err := Merge(v, dyn.NilValue)
|
2023-11-03 19:15:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, map[string]any{
|
|
|
|
"foo": "bar",
|
|
|
|
}, out.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge v into nil.
|
|
|
|
{
|
2023-12-22 13:20:45 +00:00
|
|
|
out, err := Merge(dyn.NilValue, v)
|
2023-11-03 19:15:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, map[string]any{
|
|
|
|
"foo": "bar",
|
|
|
|
}, out.AsAny())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeMapsError(t *testing.T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
v := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V("bar"),
|
2023-11-03 19:15:47 +00:00
|
|
|
})
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
other := dyn.V("string")
|
2023-11-03 19:15:47 +00:00
|
|
|
|
|
|
|
// Merge a string into v.
|
|
|
|
{
|
|
|
|
out, err := Merge(v, other)
|
|
|
|
assert.EqualError(t, err, "cannot merge map with string")
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.NilValue, out)
|
2023-11-03 19:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeSequences(t *testing.T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
v1 := dyn.V([]dyn.Value{
|
|
|
|
dyn.V("bar"),
|
|
|
|
dyn.V("baz"),
|
2023-11-03 19:15:47 +00:00
|
|
|
})
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
v2 := dyn.V([]dyn.Value{
|
|
|
|
dyn.V("qux"),
|
|
|
|
dyn.V("foo"),
|
2023-11-03 19:15:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Merge v2 into v1.
|
|
|
|
{
|
|
|
|
out, err := Merge(v1, v2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []any{
|
|
|
|
"bar",
|
|
|
|
"baz",
|
|
|
|
"qux",
|
|
|
|
"foo",
|
|
|
|
}, out.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge v1 into v2.
|
|
|
|
{
|
|
|
|
out, err := Merge(v2, v1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []any{
|
|
|
|
"qux",
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"baz",
|
|
|
|
}, out.AsAny())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeSequencesNil(t *testing.T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
v := dyn.V([]dyn.Value{
|
|
|
|
dyn.V("bar"),
|
2023-11-03 19:15:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Merge nil into v.
|
|
|
|
{
|
2023-12-22 13:20:45 +00:00
|
|
|
out, err := Merge(v, dyn.NilValue)
|
2023-11-03 19:15:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []any{
|
|
|
|
"bar",
|
|
|
|
}, out.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge v into nil.
|
|
|
|
{
|
2023-12-22 13:20:45 +00:00
|
|
|
out, err := Merge(dyn.NilValue, v)
|
2023-11-03 19:15:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []any{
|
|
|
|
"bar",
|
|
|
|
}, out.AsAny())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeSequencesError(t *testing.T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
v := dyn.V([]dyn.Value{
|
|
|
|
dyn.V("bar"),
|
2023-11-03 19:15:47 +00:00
|
|
|
})
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
other := dyn.V("string")
|
2023-11-03 19:15:47 +00:00
|
|
|
|
|
|
|
// Merge a string into v.
|
|
|
|
{
|
|
|
|
out, err := Merge(v, other)
|
|
|
|
assert.EqualError(t, err, "cannot merge sequence with string")
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.NilValue, out)
|
2023-11-03 19:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergePrimitives(t *testing.T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
v1 := dyn.V("bar")
|
|
|
|
v2 := dyn.V("baz")
|
2023-11-03 19:15:47 +00:00
|
|
|
|
|
|
|
// Merge v2 into v1.
|
|
|
|
{
|
|
|
|
out, err := Merge(v1, v2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "baz", out.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge v1 into v2.
|
|
|
|
{
|
|
|
|
out, err := Merge(v2, v1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar", out.AsAny())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergePrimitivesNil(t *testing.T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
v := dyn.V("bar")
|
2023-11-03 19:15:47 +00:00
|
|
|
|
|
|
|
// Merge nil into v.
|
|
|
|
{
|
2023-12-22 13:20:45 +00:00
|
|
|
out, err := Merge(v, dyn.NilValue)
|
2023-11-03 19:15:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar", out.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge v into nil.
|
|
|
|
{
|
2023-12-22 13:20:45 +00:00
|
|
|
out, err := Merge(dyn.NilValue, v)
|
2023-11-03 19:15:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar", out.AsAny())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergePrimitivesError(t *testing.T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
v := dyn.V("bar")
|
|
|
|
other := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V("bar"),
|
2023-11-03 19:15:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Merge a map into v.
|
|
|
|
{
|
|
|
|
out, err := Merge(v, other)
|
|
|
|
assert.EqualError(t, err, "cannot merge string with map")
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.NilValue, out)
|
2023-11-03 19:15:47 +00:00
|
|
|
}
|
|
|
|
}
|