2024-01-17 14:26:33 +00:00
|
|
|
package yamlsaver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2024-03-25 11:01:09 +00:00
|
|
|
assert "github.com/databricks/cli/libs/dyn/dynassert"
|
2024-08-29 13:02:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-01-17 14:26:33 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarshalNilValue(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-01-17 14:26:33 +00:00
|
|
|
var nilValue = dyn.NilValue
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(nilValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "null", v.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalIntValue(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-07-16 11:27:27 +00:00
|
|
|
var intValue = dyn.V(1)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(intValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", v.Value)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalFloatValue(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-07-16 11:27:27 +00:00
|
|
|
var floatValue = dyn.V(1.0)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(floatValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", v.Value)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalBoolValue(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-07-16 11:27:27 +00:00
|
|
|
var boolValue = dyn.V(true)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(boolValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "true", v.Value)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalTimeValue(t *testing.T) {
|
2024-08-29 13:02:34 +00:00
|
|
|
tm, err := dyn.NewTime("1970-01-01")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-08-29 13:02:34 +00:00
|
|
|
var timeValue = dyn.V(tm)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(timeValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
2024-08-29 13:02:34 +00:00
|
|
|
assert.Equal(t, "1970-01-01", v.Value)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalSequenceValue(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-01-17 14:26:33 +00:00
|
|
|
var sequenceValue = dyn.NewValue(
|
|
|
|
[]dyn.Value{
|
2024-07-16 11:27:27 +00:00
|
|
|
dyn.NewValue("value1", []dyn.Location{{File: "file", Line: 1, Column: 2}}),
|
|
|
|
dyn.NewValue("value2", []dyn.Location{{File: "file", Line: 2, Column: 2}}),
|
2024-01-17 14:26:33 +00:00
|
|
|
},
|
2024-07-16 11:27:27 +00:00
|
|
|
[]dyn.Location{{File: "file", Line: 1, Column: 2}},
|
2024-01-17 14:26:33 +00:00
|
|
|
)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(sequenceValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, yaml.SequenceNode, v.Kind)
|
|
|
|
assert.Equal(t, "value1", v.Content[0].Value)
|
|
|
|
assert.Equal(t, "value2", v.Content[1].Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalStringValue(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-07-16 11:27:27 +00:00
|
|
|
var stringValue = dyn.V("value")
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(stringValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "value", v.Value)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalMapValue(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-01-17 14:26:33 +00:00
|
|
|
var mapValue = dyn.NewValue(
|
|
|
|
map[string]dyn.Value{
|
2024-07-16 11:27:27 +00:00
|
|
|
"key3": dyn.NewValue("value3", []dyn.Location{{File: "file", Line: 3, Column: 2}}),
|
|
|
|
"key2": dyn.NewValue("value2", []dyn.Location{{File: "file", Line: 2, Column: 2}}),
|
|
|
|
"key1": dyn.NewValue("value1", []dyn.Location{{File: "file", Line: 1, Column: 2}}),
|
2024-01-17 14:26:33 +00:00
|
|
|
},
|
2024-07-16 11:27:27 +00:00
|
|
|
[]dyn.Location{{File: "file", Line: 1, Column: 2}},
|
2024-01-17 14:26:33 +00:00
|
|
|
)
|
2024-07-16 11:27:27 +00:00
|
|
|
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(mapValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, yaml.MappingNode, v.Kind)
|
|
|
|
assert.Equal(t, "key1", v.Content[0].Value)
|
|
|
|
assert.Equal(t, "value1", v.Content[1].Value)
|
|
|
|
|
|
|
|
assert.Equal(t, "key2", v.Content[2].Value)
|
|
|
|
assert.Equal(t, "value2", v.Content[3].Value)
|
|
|
|
|
|
|
|
assert.Equal(t, "key3", v.Content[4].Value)
|
|
|
|
assert.Equal(t, "value3", v.Content[5].Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalNestedValues(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-01-17 14:26:33 +00:00
|
|
|
var mapValue = dyn.NewValue(
|
|
|
|
map[string]dyn.Value{
|
|
|
|
"key1": dyn.NewValue(
|
|
|
|
map[string]dyn.Value{
|
2024-07-16 11:27:27 +00:00
|
|
|
"key2": dyn.NewValue("value", []dyn.Location{{File: "file", Line: 1, Column: 2}}),
|
2024-01-17 14:26:33 +00:00
|
|
|
},
|
2024-07-16 11:27:27 +00:00
|
|
|
[]dyn.Location{{File: "file", Line: 1, Column: 2}},
|
2024-01-17 14:26:33 +00:00
|
|
|
),
|
|
|
|
},
|
2024-07-16 11:27:27 +00:00
|
|
|
[]dyn.Location{{File: "file", Line: 1, Column: 2}},
|
2024-01-17 14:26:33 +00:00
|
|
|
)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(mapValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, yaml.MappingNode, v.Kind)
|
|
|
|
assert.Equal(t, "key1", v.Content[0].Value)
|
|
|
|
assert.Equal(t, yaml.MappingNode, v.Content[1].Kind)
|
|
|
|
assert.Equal(t, "key2", v.Content[1].Content[0].Value)
|
|
|
|
assert.Equal(t, "value", v.Content[1].Content[1].Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalHexadecimalValueIsQuoted(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-07-16 11:27:27 +00:00
|
|
|
var hexValue = dyn.V(0x123)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(hexValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "291", v.Value)
|
|
|
|
assert.Equal(t, yaml.Style(0), v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
var stringValue = dyn.V("0x123")
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err = s.toYamlNode(stringValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "0x123", v.Value)
|
|
|
|
assert.Equal(t, yaml.DoubleQuotedStyle, v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalBinaryValueIsQuoted(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-07-16 11:27:27 +00:00
|
|
|
var binaryValue = dyn.V(0b101)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(binaryValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "5", v.Value)
|
|
|
|
assert.Equal(t, yaml.Style(0), v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
var stringValue = dyn.V("0b101")
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err = s.toYamlNode(stringValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "0b101", v.Value)
|
|
|
|
assert.Equal(t, yaml.DoubleQuotedStyle, v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalOctalValueIsQuoted(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-07-16 11:27:27 +00:00
|
|
|
var octalValue = dyn.V(0123)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(octalValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "83", v.Value)
|
|
|
|
assert.Equal(t, yaml.Style(0), v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
var stringValue = dyn.V("0123")
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err = s.toYamlNode(stringValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "0123", v.Value)
|
|
|
|
assert.Equal(t, yaml.DoubleQuotedStyle, v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalFloatValueIsQuoted(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-07-16 11:27:27 +00:00
|
|
|
var floatValue = dyn.V(1.0)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(floatValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", v.Value)
|
|
|
|
assert.Equal(t, yaml.Style(0), v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
var stringValue = dyn.V("1.0")
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err = s.toYamlNode(stringValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1.0", v.Value)
|
|
|
|
assert.Equal(t, yaml.DoubleQuotedStyle, v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalBoolValueIsQuoted(t *testing.T) {
|
2024-02-15 15:03:19 +00:00
|
|
|
s := NewSaver()
|
2024-07-16 11:27:27 +00:00
|
|
|
var boolValue = dyn.V(true)
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err := s.toYamlNode(boolValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "true", v.Value)
|
|
|
|
assert.Equal(t, yaml.Style(0), v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
var stringValue = dyn.V("true")
|
2024-02-15 15:03:19 +00:00
|
|
|
v, err = s.toYamlNode(stringValue)
|
2024-01-17 14:26:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "true", v.Value)
|
|
|
|
assert.Equal(t, yaml.DoubleQuotedStyle, v.Style)
|
|
|
|
assert.Equal(t, yaml.ScalarNode, v.Kind)
|
|
|
|
}
|
2024-02-15 15:03:19 +00:00
|
|
|
|
|
|
|
func TestCustomStylingWithNestedMap(t *testing.T) {
|
|
|
|
s := NewSaverWithStyle(map[string]yaml.Style{
|
|
|
|
"styled": yaml.DoubleQuotedStyle,
|
|
|
|
})
|
|
|
|
|
|
|
|
var styledMap = dyn.NewValue(
|
|
|
|
map[string]dyn.Value{
|
2024-07-16 11:27:27 +00:00
|
|
|
"key1": dyn.NewValue("value1", []dyn.Location{{File: "file", Line: 1, Column: 2}}),
|
|
|
|
"key2": dyn.NewValue("value2", []dyn.Location{{File: "file", Line: 2, Column: 2}}),
|
2024-02-15 15:03:19 +00:00
|
|
|
},
|
2024-07-16 11:27:27 +00:00
|
|
|
[]dyn.Location{{File: "file", Line: -2, Column: 2}},
|
2024-02-15 15:03:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var unstyledMap = dyn.NewValue(
|
|
|
|
map[string]dyn.Value{
|
2024-07-16 11:27:27 +00:00
|
|
|
"key3": dyn.NewValue("value3", []dyn.Location{{File: "file", Line: 1, Column: 2}}),
|
|
|
|
"key4": dyn.NewValue("value4", []dyn.Location{{File: "file", Line: 2, Column: 2}}),
|
2024-02-15 15:03:19 +00:00
|
|
|
},
|
2024-07-16 11:27:27 +00:00
|
|
|
[]dyn.Location{{File: "file", Line: -1, Column: 2}},
|
2024-02-15 15:03:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var val = dyn.NewValue(
|
|
|
|
map[string]dyn.Value{
|
|
|
|
"styled": styledMap,
|
|
|
|
"unstyled": unstyledMap,
|
|
|
|
},
|
2024-07-16 11:27:27 +00:00
|
|
|
[]dyn.Location{{File: "file", Line: 1, Column: 2}},
|
2024-02-15 15:03:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
mv, err := s.toYamlNode(val)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that the styled map is quoted
|
|
|
|
v := mv.Content[1]
|
|
|
|
|
|
|
|
assert.Equal(t, yaml.MappingNode, v.Kind)
|
|
|
|
assert.Equal(t, "key1", v.Content[0].Value)
|
|
|
|
assert.Equal(t, "value1", v.Content[1].Value)
|
|
|
|
assert.Equal(t, yaml.DoubleQuotedStyle, v.Content[0].Style)
|
|
|
|
assert.Equal(t, yaml.DoubleQuotedStyle, v.Content[1].Style)
|
|
|
|
|
|
|
|
assert.Equal(t, "key2", v.Content[2].Value)
|
|
|
|
assert.Equal(t, "value2", v.Content[3].Value)
|
|
|
|
assert.Equal(t, yaml.DoubleQuotedStyle, v.Content[2].Style)
|
|
|
|
assert.Equal(t, yaml.DoubleQuotedStyle, v.Content[3].Style)
|
|
|
|
|
|
|
|
// Check that the unstyled map is not quoted
|
|
|
|
v = mv.Content[3]
|
|
|
|
|
|
|
|
assert.Equal(t, yaml.MappingNode, v.Kind)
|
|
|
|
assert.Equal(t, "key3", v.Content[0].Value)
|
|
|
|
assert.Equal(t, "value3", v.Content[1].Value)
|
|
|
|
assert.Equal(t, yaml.Style(0), v.Content[0].Style)
|
|
|
|
assert.Equal(t, yaml.Style(0), v.Content[1].Style)
|
|
|
|
|
|
|
|
assert.Equal(t, "key4", v.Content[2].Value)
|
|
|
|
assert.Equal(t, "value4", v.Content[3].Value)
|
|
|
|
assert.Equal(t, yaml.Style(0), v.Content[2].Style)
|
|
|
|
assert.Equal(t, yaml.Style(0), v.Content[3].Style)
|
|
|
|
}
|