|
1 | 1 | package pipeline_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "encoding/json"
|
| 6 | + "gopkg.in/yaml.v3" |
5 | 7 | "log"
|
6 | 8 | "os"
|
7 | 9 | "path/filepath"
|
@@ -892,3 +894,54 @@ func BenchmarkClearSpacesAtLineEndings(b *testing.B) {
|
892 | 894 | pipeline.ClearSpacesAtLineEndings(content)
|
893 | 895 | }
|
894 | 896 | }
|
| 897 | + |
| 898 | +func TestDefaultTrueBool_MarshalYAML(t *testing.T) { |
| 899 | + t.Parallel() |
| 900 | + |
| 901 | + type testStruct struct { |
| 902 | + Field1 string `yaml:"field1"` |
| 903 | + Bool pipeline.DefaultTrueBool `yaml:"bool,omitempty"` |
| 904 | + } |
| 905 | + |
| 906 | + trueVal := true |
| 907 | + falseVal := false |
| 908 | + |
| 909 | + tests := []struct { |
| 910 | + name string |
| 911 | + input testStruct |
| 912 | + want string |
| 913 | + }{ |
| 914 | + { |
| 915 | + name: "true values should be omitted", |
| 916 | + input: testStruct{ |
| 917 | + Field1: "field1", |
| 918 | + Bool: pipeline.DefaultTrueBool{Value: &trueVal}, |
| 919 | + }, |
| 920 | + want: "field1: field1\n", |
| 921 | + }, |
| 922 | + { |
| 923 | + name: "false values should be marshalled", |
| 924 | + input: testStruct{ |
| 925 | + Field1: "field1", |
| 926 | + Bool: pipeline.DefaultTrueBool{Value: &falseVal}, |
| 927 | + }, |
| 928 | + want: "field1: field1\nbool: false\n", |
| 929 | + }, |
| 930 | + } |
| 931 | + |
| 932 | + for _, tt := range tests { |
| 933 | + t.Run(tt.name, func(t *testing.T) { |
| 934 | + t.Parallel() |
| 935 | + |
| 936 | + buf := bytes.NewBuffer(nil) |
| 937 | + enc := yaml.NewEncoder(buf) |
| 938 | + enc.SetIndent(2) |
| 939 | + |
| 940 | + err := enc.Encode(tt.input) |
| 941 | + require.NoError(t, err) |
| 942 | + |
| 943 | + yamlConfig := buf.Bytes() |
| 944 | + assert.Equal(t, tt.want, string(yamlConfig)) |
| 945 | + }) |
| 946 | + } |
| 947 | +} |
0 commit comments