github.com/connyay/libcompose@v0.4.0/yaml/command_test.go (about) 1 package yaml 2 3 import ( 4 "strings" 5 "testing" 6 7 "gopkg.in/yaml.v2" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 type StructCommand struct { 13 Entrypoint Command `yaml:"entrypoint,flow,omitempty"` 14 Command Command `yaml:"command,flow,omitempty"` 15 } 16 17 var sampleStructCommand = `command: bash` 18 19 func TestUnmarshalCommand(t *testing.T) { 20 s := &StructCommand{} 21 err := yaml.Unmarshal([]byte(sampleStructCommand), s) 22 23 assert.Nil(t, err) 24 assert.Equal(t, Command{"bash"}, s.Command) 25 assert.Nil(t, s.Entrypoint) 26 bytes, err := yaml.Marshal(s) 27 assert.Nil(t, err) 28 29 s2 := &StructCommand{} 30 err = yaml.Unmarshal(bytes, s2) 31 32 assert.Nil(t, err) 33 assert.Equal(t, Command{"bash"}, s2.Command) 34 assert.Nil(t, s2.Entrypoint) 35 } 36 37 var sampleEmptyCommand = `{}` 38 39 func TestUnmarshalEmptyCommand(t *testing.T) { 40 s := &StructCommand{} 41 err := yaml.Unmarshal([]byte(sampleEmptyCommand), s) 42 43 assert.Nil(t, err) 44 assert.Nil(t, s.Command) 45 46 bytes, err := yaml.Marshal(s) 47 assert.Nil(t, err) 48 assert.Equal(t, "{}", strings.TrimSpace(string(bytes))) 49 50 s2 := &StructCommand{} 51 err = yaml.Unmarshal(bytes, s2) 52 53 assert.Nil(t, err) 54 assert.Nil(t, s2.Command) 55 }