github.com/goreleaser/goreleaser@v1.25.1/pkg/config/config_array_test.go (about) 1 package config 2 3 import ( 4 "testing" 5 6 "github.com/goreleaser/goreleaser/internal/yaml" 7 "github.com/stretchr/testify/require" 8 ) 9 10 type Unmarshaled struct { 11 Strings StringArray `yaml:"strings,omitempty"` 12 Flags FlagArray `yaml:"flags,omitempty"` 13 } 14 15 type yamlUnmarshalTestCase struct { 16 yaml string 17 expected Unmarshaled 18 err string 19 } 20 21 var stringArrayTests = []yamlUnmarshalTestCase{ 22 { 23 "", 24 Unmarshaled{}, 25 "", 26 }, 27 { 28 "strings: []", 29 Unmarshaled{ 30 Strings: StringArray{}, 31 }, 32 "", 33 }, 34 { 35 "strings: [one two, three]", 36 Unmarshaled{ 37 Strings: StringArray{"one two", "three"}, 38 }, 39 "", 40 }, 41 { 42 "strings: one two", 43 Unmarshaled{ 44 Strings: StringArray{"one two"}, 45 }, 46 "", 47 }, 48 { 49 "strings: {key: val}", 50 Unmarshaled{}, 51 "yaml: unmarshal errors:\n line 1: cannot unmarshal !!map into string", 52 }, 53 } 54 55 var flagArrayTests = []yamlUnmarshalTestCase{ 56 { 57 "", 58 Unmarshaled{}, 59 "", 60 }, 61 { 62 "flags: []", 63 Unmarshaled{ 64 Flags: FlagArray{}, 65 }, 66 "", 67 }, 68 { 69 "flags: [one two, three]", 70 Unmarshaled{ 71 Flags: FlagArray{"one two", "three"}, 72 }, 73 "", 74 }, 75 { 76 "flags: one two", 77 Unmarshaled{ 78 Flags: FlagArray{"one", "two"}, 79 }, 80 "", 81 }, 82 { 83 "flags: {key: val}", 84 Unmarshaled{}, 85 "yaml: unmarshal errors:\n line 1: cannot unmarshal !!map into string", 86 }, 87 } 88 89 func TestStringArray(t *testing.T) { 90 for _, testCase := range stringArrayTests { 91 var actual Unmarshaled 92 93 err := yaml.UnmarshalStrict([]byte(testCase.yaml), &actual) 94 if testCase.err == "" { 95 require.NoError(t, err) 96 require.Equal(t, testCase.expected, actual) 97 } else { 98 require.EqualError(t, err, testCase.err) 99 } 100 } 101 } 102 103 func TestFlagArray(t *testing.T) { 104 for _, testCase := range flagArrayTests { 105 var actual Unmarshaled 106 107 err := yaml.UnmarshalStrict([]byte(testCase.yaml), &actual) 108 if testCase.err == "" { 109 require.NoError(t, err) 110 } else { 111 require.EqualError(t, err, testCase.err) 112 } 113 require.Equal(t, testCase.expected, actual) 114 } 115 }