@@ -24,7 +24,7 @@ import (
24
24
"google.golang.org/protobuf/types/known/structpb"
25
25
)
26
26
27
- func TestParseProject (t * testing.T ) {
27
+ func TestToProtoStruct (t * testing.T ) {
28
28
t .Parallel ()
29
29
30
30
cases := []struct {
@@ -82,3 +82,61 @@ func TestParseProject(t *testing.T) {
82
82
})
83
83
}
84
84
}
85
+
86
+ func TestUnmarshalYAML (t * testing.T ) {
87
+ t .Parallel ()
88
+
89
+ cases := []struct {
90
+ name string
91
+ b []byte
92
+ want * structpb.Struct
93
+ wantErrSubstr string
94
+ }{
95
+ {
96
+ name : "success" ,
97
+ b : []byte (`foo: bar
98
+ slice:
99
+ - abc
100
+ - xyz
101
+ num: 1
102
+ bool: true
103
+ ` ),
104
+ want : & structpb.Struct {
105
+ Fields : map [string ]* structpb.Value {
106
+ "foo" : structpb .NewStringValue ("bar" ),
107
+ "slice" : structpb .NewListValue (& structpb.ListValue {
108
+ Values : []* structpb.Value {
109
+ structpb .NewStringValue ("abc" ),
110
+ structpb .NewStringValue ("xyz" ),
111
+ },
112
+ }),
113
+ "num" : structpb .NewNumberValue (1 ),
114
+ "bool" : structpb .NewBoolValue (true ),
115
+ },
116
+ },
117
+ },
118
+ {
119
+ name : "invalid_yaml_error" ,
120
+ b : []byte ("foobar: {}{}" ),
121
+ want : & structpb.Struct {},
122
+ wantErrSubstr : "failed to unmarshal yaml" ,
123
+ },
124
+ }
125
+
126
+ for _ , tc := range cases {
127
+ tc := tc
128
+
129
+ t .Run (tc .name , func (t * testing.T ) {
130
+ t .Parallel ()
131
+
132
+ var msg structpb.Struct
133
+ err := UnmarshalYAML (tc .b , & msg )
134
+ if diff := testutil .DiffErrString (err , tc .wantErrSubstr ); diff != "" {
135
+ t .Errorf ("unexpected error: %s" , diff )
136
+ }
137
+ if diff := cmp .Diff (tc .want , & msg , protocmp .Transform ()); diff != "" {
138
+ t .Errorf ("UnmarshalYAML (-want,+got):\n %s" , diff )
139
+ }
140
+ })
141
+ }
142
+ }
0 commit comments