get.porter.sh/porter@v1.3.0/pkg/yaml/map_test.go (about)

     1  package yaml
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  type mymap map[string]interface{}
    12  
    13  func (m *mymap) UnmarshalYAML(unmarshal func(interface{}) error) error {
    14  	raw, err := UnmarshalMap(unmarshal)
    15  	if err != nil {
    16  		return err
    17  	}
    18  
    19  	*m = raw
    20  	return nil
    21  }
    22  
    23  func TestUnmarshalMap(t *testing.T) {
    24  	var m mymap
    25  
    26  	testYaml, err := os.ReadFile("testdata/custom.yaml")
    27  	require.NoError(t, err, "could not read testdata")
    28  
    29  	err = Unmarshal([]byte(testYaml), &m)
    30  	require.NoError(t, err, "Unmarshal failed")
    31  
    32  	wantM := mymap{
    33  		"root": map[string]interface{}{
    34  			"array": []interface{}{
    35  				map[string]interface{}{
    36  					"map": map[string]interface{}{
    37  						"a":          "a",
    38  						"1":          1,
    39  						"true":       true,
    40  						"yes":        "yes",
    41  						"null":       nil,
    42  						"typesArray": []interface{}{"a", 1, true, "yes", nil, []interface{}{1, 2}},
    43  						"map": map[string]interface{}{
    44  							"a":          "a",
    45  							"1":          1,
    46  							"true":       true,
    47  							"yes":        "yes",
    48  							"null":       nil,
    49  							"typesArray": []interface{}{"a", 1, true, "yes", nil, []interface{}{1, 2}},
    50  						},
    51  					},
    52  				},
    53  			},
    54  		},
    55  	}
    56  	assert.Equal(t, wantM, m)
    57  }