github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/projectfile/nameval_test.go (about) 1 package projectfile 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 "gopkg.in/yaml.v2" 10 ) 11 12 func TestParseShorthandValid(t *testing.T) { 13 pl, err := parseData(pFileYAMLValid.asLongYAML(), "junk/path") 14 require.NoError(t, err, "Parse longhand file without error") 15 require.NotEmpty(t, pl.Constants, "Longhand constants are not empty") 16 for _, c := range pl.Constants { 17 require.NotEmpty(t, c.Name, "Name field of (longhand) constant is not empty") 18 require.NotEmpty(t, c.Value, "Value field of (longhand) constant is not empty") 19 } 20 21 ps, err := parseData(pFileYAMLValid.asShortYAML(), "junk/path") 22 require.NoError(t, err, "Parse shorthand file without error") 23 require.NotEmpty(t, ps.Constants, "Shorthand constants are not empty") 24 for _, c := range ps.Constants { 25 require.NotEmpty(t, c.Name, "Name field of (shorthand) constant is not empty") 26 require.NotEmpty(t, c.Value, "Value field of (shorthand) constant is not empty") 27 } 28 29 require.Equal(t, pl.Constants, ps.Constants, "Longhand constants slice is equal to shorthand constants slice") 30 } 31 32 func TestParseShorthandBadData(t *testing.T) { 33 tests := []struct { 34 name string 35 fileData pFileYAML 36 }{ 37 { 38 "array in name", 39 pFileYAML{`["test", "array", "name"]`, `valid`}, 40 }, 41 { 42 "array in value", 43 pFileYAML{`valid`, `["test", "array", "value"]`}, 44 }, 45 { 46 "new field in name", 47 pFileYAML{`- 42`, `valid`}, 48 }, 49 { 50 "new field in value", 51 pFileYAML{`valid`, `- 42`}, 52 }, 53 } 54 55 for _, tt := range tests { 56 t.Run(tt.name, func(t *testing.T) { 57 longYAML := tt.fileData.asLongYAML() 58 _, err := parseData(longYAML, "junk/path") 59 require.Error(t, err, "Parse bad longhand yaml with failure") 60 61 shortYAML := tt.fileData.asShortYAML() 62 _, shErr := parseData(shortYAML, "junk/path") 63 require.Error(t, shErr, "Parse bad shorthand yaml with failure") 64 }) 65 } 66 } 67 68 type CustomFields struct { 69 Desc string `yaml:"desc,omitempty"` 70 Truthy bool `yaml:"truthy,omitempty"` 71 } 72 73 type Custom struct { 74 NameVal `yaml:",inline"` 75 CustomFields `yaml:",inline"` 76 } 77 78 func (c *Custom) UnmarshalYAML(unmarshal func(interface{}) error) error { 79 if err := unmarshal(&c.NameVal); err != nil { 80 return err 81 } 82 if err := unmarshal(&c.CustomFields); err != nil { 83 return err 84 } 85 return nil 86 } 87 88 func TestParseShorthandSimpleStruct(t *testing.T) { 89 c := Custom{} 90 data := strings.TrimSpace(` 91 name: valueForName 92 value: valueForValue 93 desc: valueForDesc 94 truthy: true 95 ` + "\n") 96 97 err := yaml.Unmarshal([]byte(data), &c) 98 require.NoError(t, err, "Unmarshal without error") 99 100 assert.Equal(t, "valueForName", c.Name, "Name (longhand) should be set properly") 101 assert.Equal(t, "valueForValue", c.Value, "Value (longhand) should be set properly") 102 assert.Equal(t, "valueForDesc", c.Desc, "Desc (longhand) should be set properly") 103 assert.Equal(t, true, c.Truthy, "Truthy (longhand) should be set true") 104 105 c = Custom{} 106 data = strings.TrimSpace(` 107 valueForName: valueForValue 108 ` + "\n") 109 110 err = yaml.Unmarshal([]byte(data), &c) 111 require.NoError(t, err, "Unmarshal without error") 112 113 assert.Equal(t, "valueForName", c.Name, "Name (shorthand) should be set properly") 114 assert.Equal(t, "valueForValue", c.Value, "Value (shorthand) should be set properly") 115 }