2017-07-09 22:17:20 +02:00
|
|
|
package utils
|
2017-06-23 00:26:16 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-08-20 23:45:25 +02:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
|
2017-06-23 00:26:16 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const mapYaml = "a: 1\nb: 2"
|
|
|
|
const listYaml = "- a\n- b"
|
|
|
|
|
|
|
|
func TestUnmarshalYAML(t *testing.T) {
|
|
|
|
var d interface{}
|
|
|
|
err := UnmarshalYAMLInterface([]byte(mapYaml), &d)
|
|
|
|
require.NoError(t, err)
|
|
|
|
switch d := d.(type) {
|
2017-08-20 23:45:25 +02:00
|
|
|
case yaml.MapSlice:
|
2017-06-23 00:26:16 +02:00
|
|
|
require.Len(t, d, 2)
|
2017-08-20 23:45:25 +02:00
|
|
|
require.Equal(t, yaml.MapItem{Key: "a", Value: 1}, d[0])
|
2017-06-23 00:26:16 +02:00
|
|
|
default:
|
2017-08-20 23:45:25 +02:00
|
|
|
require.IsType(t, map[interface{}]interface{}{}, d)
|
2017-06-23 00:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = UnmarshalYAMLInterface([]byte(listYaml), &d)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.IsType(t, d, []interface{}{})
|
|
|
|
switch d := d.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
require.Len(t, d, 2)
|
|
|
|
require.Equal(t, "a", d[0])
|
|
|
|
default:
|
|
|
|
require.IsType(t, d, map[interface{}]interface{}{})
|
|
|
|
}
|
|
|
|
}
|