github.com/bdwilliams/libcompose@v0.3.1-0.20160826154243-d81a9bdacff0/yaml/types_yaml_test.go (about)

     1  package yaml
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"gopkg.in/yaml.v2"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  type StructStringorInt struct {
    13  	Foo StringorInt
    14  }
    15  
    16  func TestStringorIntYaml(t *testing.T) {
    17  	for _, str := range []string{`{foo: 10}`, `{foo: "10"}`} {
    18  		s := StructStringorInt{}
    19  		yaml.Unmarshal([]byte(str), &s)
    20  
    21  		assert.Equal(t, StringorInt(10), s.Foo)
    22  
    23  		d, err := yaml.Marshal(&s)
    24  		assert.Nil(t, err)
    25  
    26  		s2 := StructStringorInt{}
    27  		yaml.Unmarshal(d, &s2)
    28  
    29  		assert.Equal(t, StringorInt(10), s2.Foo)
    30  	}
    31  }
    32  
    33  type StructStringorslice struct {
    34  	Foo Stringorslice
    35  }
    36  
    37  func TestStringorsliceYaml(t *testing.T) {
    38  	str := `{foo: [bar, baz]}`
    39  
    40  	s := StructStringorslice{}
    41  	yaml.Unmarshal([]byte(str), &s)
    42  
    43  	assert.Equal(t, Stringorslice{"bar", "baz"}, s.Foo)
    44  
    45  	d, err := yaml.Marshal(&s)
    46  	assert.Nil(t, err)
    47  
    48  	s2 := StructStringorslice{}
    49  	yaml.Unmarshal(d, &s2)
    50  
    51  	assert.Equal(t, Stringorslice{"bar", "baz"}, s2.Foo)
    52  }
    53  
    54  type StructSliceorMap struct {
    55  	Foos SliceorMap `yaml:"foos,omitempty"`
    56  	Bars []string   `yaml:"bars"`
    57  }
    58  
    59  func TestSliceOrMapYaml(t *testing.T) {
    60  	str := `{foos: [bar=baz, far=faz]}`
    61  
    62  	s := StructSliceorMap{}
    63  	yaml.Unmarshal([]byte(str), &s)
    64  
    65  	assert.Equal(t, SliceorMap{"bar": "baz", "far": "faz"}, s.Foos)
    66  
    67  	d, err := yaml.Marshal(&s)
    68  	assert.Nil(t, err)
    69  
    70  	s2 := StructSliceorMap{}
    71  	yaml.Unmarshal(d, &s2)
    72  
    73  	assert.Equal(t, SliceorMap{"bar": "baz", "far": "faz"}, s2.Foos)
    74  }
    75  
    76  var sampleStructSliceorMap = `
    77  foos:
    78    io.rancher.os.bar: baz
    79    io.rancher.os.far: true
    80  bars: []
    81  `
    82  
    83  func TestUnmarshalSliceOrMap(t *testing.T) {
    84  	s := StructSliceorMap{}
    85  	err := yaml.Unmarshal([]byte(sampleStructSliceorMap), &s)
    86  	assert.Equal(t, fmt.Errorf("Cannot unmarshal 'true' of type bool into a string value"), err)
    87  }
    88  
    89  func TestStr2SliceOrMapPtrMap(t *testing.T) {
    90  	s := map[string]*StructSliceorMap{"udav": {
    91  		Foos: SliceorMap{"io.rancher.os.bar": "baz", "io.rancher.os.far": "true"},
    92  		Bars: []string{},
    93  	}}
    94  	d, err := yaml.Marshal(&s)
    95  	assert.Nil(t, err)
    96  
    97  	s2 := map[string]*StructSliceorMap{}
    98  	yaml.Unmarshal(d, &s2)
    99  
   100  	assert.Equal(t, s, s2)
   101  }
   102  
   103  type StructMaporslice struct {
   104  	Foo MaporEqualSlice
   105  }
   106  
   107  func contains(list []string, item string) bool {
   108  	for _, test := range list {
   109  		if test == item {
   110  			return true
   111  		}
   112  	}
   113  	return false
   114  }
   115  
   116  func TestMaporsliceYaml(t *testing.T) {
   117  	str := `{foo: {bar: baz, far: 1, qux: null}}`
   118  
   119  	s := StructMaporslice{}
   120  	yaml.Unmarshal([]byte(str), &s)
   121  
   122  	assert.Equal(t, 3, len(s.Foo))
   123  	assert.True(t, contains(s.Foo, "bar=baz"))
   124  	assert.True(t, contains(s.Foo, "far=1"))
   125  	assert.True(t, contains(s.Foo, "qux"))
   126  
   127  	d, err := yaml.Marshal(&s)
   128  	assert.Nil(t, err)
   129  
   130  	s2 := StructMaporslice{}
   131  	yaml.Unmarshal(d, &s2)
   132  
   133  	assert.Equal(t, 3, len(s2.Foo))
   134  	assert.True(t, contains(s2.Foo, "bar=baz"))
   135  	assert.True(t, contains(s2.Foo, "far=1"))
   136  	assert.True(t, contains(s2.Foo, "qux"))
   137  }