package v01 import ( "fmt" "sync" "testing" ) func TestConfigPatch(t *testing.T) { cases := map[string]struct { cfg config patch interface{} want config }{ "nil patch": { cfg: config{Quiet: true}, patch: nil, want: config{Quiet: true}, }, "[] patch": { cfg: config{Quiet: true}, patch: []interface{}{}, want: config{Quiet: true}, }, "set fake field": { cfg: config{Quiet: true}, patch: []interface{}{ map[string]interface{}{"op": "add", "path": "/Fake", "value": true}, }, want: config{Quiet: true}, }, "remove field": { cfg: config{Quiet: true}, patch: []interface{}{ map[string]interface{}{"op": "remove", "path": "/Quiet"}, }, want: config{Quiet: false}, }, "replace field with valid": { cfg: config{Quiet: true}, patch: []interface{}{ map[string]interface{}{"op": "replace", "path": "/Quiet", "value": false}, }, want: config{Quiet: false}, }, "replace field with invalid": { cfg: config{Quiet: true}, patch: []interface{}{ map[string]interface{}{"op": "replace", "path": "/Quiet", "value": "teehee"}, }, want: config{Quiet: true}, }, "test and noop": { cfg: config{Quiet: true}, patch: []interface{}{ map[string]interface{}{"op": "test", "path": "/Quiet", "value": false}, map[string]interface{}{"op": "replace", "path": "/Quiet", "value": false}, }, want: config{Quiet: true}, }, "test and apply": { cfg: config{Quiet: true}, patch: []interface{}{ map[string]interface{}{"op": "test", "path": "/Quiet", "value": true}, map[string]interface{}{"op": "replace", "path": "/Quiet", "value": false}, }, want: config{Quiet: false}, }, } for name, d := range cases { c := d t.Run(name, func(t *testing.T) { c.cfg.lock = &sync.Mutex{} got := c.cfg.WithPatch(c.patch) got.lock = nil c.want.lock = nil if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", c.want) { t.Errorf("(%+v).Patch(%+v) want %+v, got %+v", c.cfg, c.patch, c.want, got) } }) } }