@@ -282,6 +282,55 @@ foo:
282282 }
283283}
284284
285+ func TestRemoveKey (t * testing.T ) {
286+ tests := []struct {
287+ name string
288+ in string
289+ key string
290+ expectedOut string
291+ expectedRemovedKey string
292+ expectedRemovedValue string
293+ }{
294+ {
295+ name : "Key not present" ,
296+ in : "foo: 1" ,
297+ key : "bar" ,
298+ },
299+ {
300+ name : "Key present" ,
301+ in : "foo: 1\n bar: 2\n baz: 3\n " ,
302+ key : "bar" ,
303+ expectedOut : "foo: 1\n baz: 3\n " ,
304+ expectedRemovedKey : "bar" ,
305+ expectedRemovedValue : "2" ,
306+ },
307+ }
308+
309+ for _ , test := range tests {
310+ t .Run (test .name , func (t * testing.T ) {
311+ node := unmarshalForTest (t , test .in )
312+ removedKey , removedValue := RemoveKey (node .Content [0 ], test .key )
313+ if test .expectedOut == "" {
314+ unmodifiedOriginal := unmarshalForTest (t , test .in )
315+ assert .Equal (t , unmodifiedOriginal , node )
316+ } else {
317+ result := marshalForTest (t , & node )
318+ assert .Equal (t , test .expectedOut , result )
319+ }
320+ if test .expectedRemovedKey == "" {
321+ assert .Nil (t , removedKey )
322+ } else {
323+ assert .Equal (t , test .expectedRemovedKey , removedKey .Value )
324+ }
325+ if test .expectedRemovedValue == "" {
326+ assert .Nil (t , removedValue )
327+ } else {
328+ assert .Equal (t , test .expectedRemovedValue , removedValue .Value )
329+ }
330+ })
331+ }
332+ }
333+
285334func unmarshalForTest (t * testing.T , input string ) yaml.Node {
286335 t .Helper ()
287336 var node yaml.Node
0 commit comments