| 8 | ) |
| 9 | |
| 10 | func Test_CIDRSliceCSV_YamlMarshalling(t *testing.T) { |
| 11 | type TestStruct struct { |
| 12 | CIDRs CIDRSliceCSV `yaml:"cidrs"` |
| 13 | } |
| 14 | |
| 15 | tests := map[string]struct { |
| 16 | input string |
| 17 | expected []string |
| 18 | }{ |
| 19 | "should marshal empty config": { |
| 20 | input: "cidrs: \"\"\n", |
| 21 | expected: nil, |
| 22 | }, |
| 23 | "should marshal single value": { |
| 24 | input: "cidrs: 127.0.0.1/32\n", |
| 25 | expected: []string{"127.0.0.1/32"}, |
| 26 | }, |
| 27 | "should marshal multiple comma-separated values": { |
| 28 | input: "cidrs: 127.0.0.1/32,10.0.10.0/28,fdf8:f53b:82e4::/100,192.168.0.0/20\n", |
| 29 | expected: []string{"127.0.0.1/32", "10.0.10.0/28", "fdf8:f53b:82e4::/100", "192.168.0.0/20"}, |
| 30 | }, |
| 31 | } |
| 32 | |
| 33 | for name, tc := range tests { |
| 34 | t.Run(name, func(t *testing.T) { |
| 35 | // Unmarshal. |
| 36 | actual := TestStruct{} |
| 37 | err := yaml.Unmarshal([]byte(tc.input), &actual) |
| 38 | assert.NoError(t, err) |
| 39 | |
| 40 | assert.Len(t, actual.CIDRs, len(tc.expected)) |
| 41 | for idx, cidr := range actual.CIDRs { |
| 42 | assert.Equal(t, tc.expected[idx], cidr.String()) |
| 43 | } |
| 44 | |
| 45 | // Marshal. |
| 46 | out, err := yaml.Marshal(actual) |
| 47 | assert.NoError(t, err) |
| 48 | assert.Equal(t, tc.input, string(out)) |
| 49 | }) |
| 50 | } |
| 51 | } |