|
| 1 | +package flagext |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "gopkg.in/yaml.v2" |
| 9 | +) |
| 10 | + |
| 11 | +func TestURLValueYAML(t *testing.T) { |
| 12 | + // Test embedding of URLValue. |
| 13 | + { |
| 14 | + type TestStruct struct { |
| 15 | + URL URLValue `yaml:"url"` |
| 16 | + } |
| 17 | + |
| 18 | + var testStruct TestStruct |
| 19 | + require.NoError(t, testStruct.URL.Set("http://google.com")) |
| 20 | + expected := []byte(`url: http://google.com |
| 21 | +`) |
| 22 | + |
| 23 | + actual, err := yaml.Marshal(testStruct) |
| 24 | + require.NoError(t, err) |
| 25 | + assert.Equal(t, expected, actual) |
| 26 | + |
| 27 | + var actualStruct TestStruct |
| 28 | + err = yaml.Unmarshal(expected, &actualStruct) |
| 29 | + require.NoError(t, err) |
| 30 | + assert.Equal(t, testStruct, actualStruct) |
| 31 | + } |
| 32 | + |
| 33 | + // Test pointers of URLValue. |
| 34 | + { |
| 35 | + type TestStruct struct { |
| 36 | + URL *URLValue `yaml:"url"` |
| 37 | + } |
| 38 | + |
| 39 | + var testStruct TestStruct |
| 40 | + testStruct.URL = &URLValue{} |
| 41 | + require.NoError(t, testStruct.URL.Set("http://google.com")) |
| 42 | + expected := []byte(`url: http://google.com |
| 43 | +`) |
| 44 | + |
| 45 | + actual, err := yaml.Marshal(testStruct) |
| 46 | + require.NoError(t, err) |
| 47 | + assert.Equal(t, expected, actual) |
| 48 | + |
| 49 | + var actualStruct TestStruct |
| 50 | + err = yaml.Unmarshal(expected, &actualStruct) |
| 51 | + require.NoError(t, err) |
| 52 | + assert.Equal(t, testStruct, actualStruct) |
| 53 | + } |
| 54 | + |
| 55 | + // Test no url set in URLValue. |
| 56 | + { |
| 57 | + type TestStruct struct { |
| 58 | + URL URLValue `yaml:"url"` |
| 59 | + } |
| 60 | + |
| 61 | + var testStruct TestStruct |
| 62 | + expected := []byte(`url: "" |
| 63 | +`) |
| 64 | + |
| 65 | + actual, err := yaml.Marshal(testStruct) |
| 66 | + require.NoError(t, err) |
| 67 | + assert.Equal(t, expected, actual) |
| 68 | + |
| 69 | + var actualStruct TestStruct |
| 70 | + err = yaml.Unmarshal(expected, &actualStruct) |
| 71 | + require.NoError(t, err) |
| 72 | + assert.Equal(t, testStruct, actualStruct) |
| 73 | + } |
| 74 | +} |
0 commit comments