How to use Slice method of td Package

Best Go-testdeep code snippet using td.Slice

update_test.go

Source:update_test.go Github

copy

Full Screen

...39 },40 MapIntVal: map[string]int{41 "key1": intVal1,42 },43 SliceVal: []string{strVal1},44 }45 data, _ := json.Marshal(strVal2)46 err := UpdateJSON(td, []string{"strVal"}, data, UpdateOp)47 if assert.NoError(t, err) {48 assert.Equal(t, strVal2, td.StrVal)49 }50 err = UpdateJSON(td, []string{"NoTagVal"}, data, UpdateOp)51 if assert.NoError(t, err) {52 assert.Equal(t, strVal2, td.NoTagVal)53 }54 err = UpdateJSON(td, []string{"mapVal", "key1"}, data, UpdateOp)55 if assert.NoError(t, err) {56 assert.Equal(t, strVal2, td.MapVal["key1"])57 }58 err = UpdateJSON(td, []string{"sliceVal", "0"}, data, UpdateOp)59 if assert.NoError(t, err) {60 assert.Equal(t, strVal2, td.SliceVal[0])61 }62 data, _ = json.Marshal(intVal2)63 err = UpdateJSON(td, []string{"intVal"}, data, UpdateOp)64 if assert.NoError(t, err) {65 assert.Equal(t, intVal2, td.IntVal)66 }67 err = UpdateJSON(td, []string{"mapIntVal", "key1"}, data, UpdateOp)68 if assert.NoError(t, err) {69 assert.Equal(t, intVal2, td.MapIntVal["key1"])70 }71}72func TestUpdateMap(t *testing.T) {73 strVal1 := "strVal1"74 strVal2 := "strVal2"75 mapVal := map[string]string{76 "key1": strVal1,77 "key2": strVal2,78 }79 mapPtrVal := map[string]*string{80 "key1": &strVal1,81 "key2": &strVal2,82 }83 mapMapVal := map[string]map[string]string{84 "key1": mapVal,85 "key2": mapVal,86 }87 mapPtrMapVal := map[string]*map[string]string{88 "key1": &mapVal,89 "key2": &mapVal,90 }91 sliceVal := []string{strVal1, strVal2}92 mapSliceVal := map[string][]string{93 "key1": sliceVal,94 "key2": sliceVal,95 }96 arrVal := [2]string{strVal1, strVal2}97 mapArrVal := map[string][2]string{98 "key1": arrVal,99 "key2": arrVal,100 }101 mapPtrSliceVal := map[string]*[]string{102 "key1": &sliceVal,103 "key2": &sliceVal,104 }105 td := &TypeDeep{}106 data, _ := json.Marshal(mapVal)107 err := UpdateJSON(td, []string{"mapVal"}, data, UpdateOp)108 if assert.NoError(t, err) {109 assert.EqualValues(t, mapVal, td.MapVal)110 }111 err = UpdateJSON(td, []string{"ptrMapVal"}, data, UpdateOp)112 if assert.NoError(t, err) {113 assert.EqualValues(t, &mapVal, td.PtrMapVal)114 }115 err = UpdateJSON(td, []string{"mapPtrVal"}, data, UpdateOp)116 if assert.NoError(t, err) {117 assert.EqualValues(t, mapPtrVal, td.MapPtrVal)118 }119 data, _ = json.Marshal(mapMapVal)120 err = UpdateJSON(td, []string{"mapMapVal"}, data, UpdateOp)121 if assert.NoError(t, err) {122 assert.EqualValues(t, mapMapVal, td.MapMapVal)123 }124 err = UpdateJSON(td, []string{"mapPtrMapVal"}, data, UpdateOp)125 if assert.NoError(t, err) {126 assert.EqualValues(t, mapPtrMapVal, td.MapPtrMapVal)127 }128 data, _ = json.Marshal(mapSliceVal)129 err = UpdateJSON(td, []string{"mapSliceVal"}, data, UpdateOp)130 if assert.NoError(t, err) {131 assert.EqualValues(t, mapSliceVal, td.MapSliceVal)132 }133 err = UpdateJSON(td, []string{"mapArrVal"}, data, UpdateOp)134 if assert.NoError(t, err) {135 assert.EqualValues(t, mapArrVal, td.MapArrVal)136 }137 data, _ = json.Marshal(mapPtrSliceVal)138 err = UpdateJSON(td, []string{"mapPtrSliceVal"}, data, UpdateOp)139 if assert.NoError(t, err) {140 assert.EqualValues(t, mapPtrSliceVal, td.MapPtrSliceVal)141 }142 // Second level.143 newMapVal := map[string]string{144 "key1": "newStrVal1",145 "key2": "newStrVal2",146 }147 data, _ = json.Marshal(newMapVal)148 err = UpdateJSON(td, []string{"mapMapVal", "key1"}, data, UpdateOp)149 if assert.NoError(t, err) {150 assert.EqualValues(t, newMapVal, td.MapMapVal["key1"])151 }152 err = UpdateJSON(td, []string{"mapPtrMapVal", "key1"}, data, UpdateOp)153 if assert.NoError(t, err) {154 assert.EqualValues(t, &newMapVal, td.MapPtrMapVal["key1"])155 }156}157func TestUpdateSlice(t *testing.T) {158 strVal0 := "strVal0"159 strVal1 := "strVal1"160 strVal2 := "strVal2"161 sliceVal0 := []string{strVal0}162 sliceVal := []string{strVal1, strVal2}163 arrVal := [2]string{strVal1, strVal2}164 slicePtrVal := []*string{&strVal1, &strVal2}165 mapVal := map[string]string{166 "key1": strVal1,167 "key2": strVal2,168 }169 mapSliceVal := map[string][]string{"key1": sliceVal0}170 mapPtrSliceVal := map[string]*[]string{"key1": &sliceVal0}171 sliceMapVal := []map[string]string{mapVal, mapVal}172 slicePtrMapVal := []*map[string]string{&mapVal, &mapVal}173 sliceSliceVal := [][]string{sliceVal, sliceVal}174 slicePtrSliceVal := []*[]string{&sliceVal, &sliceVal}175 td := &TypeDeep{176 MapSliceVal: mapSliceVal,177 MapPtrSliceVal: mapPtrSliceVal,178 }179 data, _ := json.Marshal(arrVal)180 err := UpdateJSON(td, []string{"arrVal"}, data, UpdateOp)181 if assert.NoError(t, err) {182 assert.EqualValues(t, arrVal, td.ArrVal)183 }184 data, _ = json.Marshal(sliceVal)185 err = UpdateJSON(td, []string{"sliceVal"}, data, UpdateOp)186 if assert.NoError(t, err) {187 assert.EqualValues(t, sliceVal, td.SliceVal)188 }189 err = UpdateJSON(td, []string{"slicePtrVal"}, data, UpdateOp)190 if assert.NoError(t, err) {191 assert.EqualValues(t, slicePtrVal, td.SlicePtrVal)192 }193 err = UpdateJSON(td, []string{"mapSliceVal", "key1"}, data, UpdateOp)194 if assert.NoError(t, err) {195 assert.EqualValues(t, sliceVal, td.MapSliceVal["key1"])196 }197 err = UpdateJSON(td, []string{"mapPtrSliceVal", "key1"}, data, UpdateOp)198 if assert.NoError(t, err) {199 assert.EqualValues(t, &sliceVal, td.MapPtrSliceVal["key1"])200 }201 data, _ = json.Marshal(sliceMapVal)202 err = UpdateJSON(td, []string{"sliceMapVal"}, data, UpdateOp)203 if assert.NoError(t, err) {204 assert.EqualValues(t, sliceMapVal, td.SliceMapVal)205 }206 err = UpdateJSON(td, []string{"slicePtrMapVal"}, data, UpdateOp)207 if assert.NoError(t, err) {208 assert.EqualValues(t, slicePtrMapVal, td.SlicePtrMapVal)209 }210 data, _ = json.Marshal(sliceSliceVal)211 err = UpdateJSON(td, []string{"sliceSliceVal"}, data, UpdateOp)212 if assert.NoError(t, err) {213 assert.EqualValues(t, sliceSliceVal, td.SliceSliceVal)214 }215 err = UpdateJSON(td, []string{"slicePtrSliceVal"}, data, UpdateOp)216 if assert.NoError(t, err) {217 assert.EqualValues(t, slicePtrSliceVal, td.SlicePtrSliceVal)218 }219}220func TestUpdateIntf(t *testing.T) {221 strVal := "strVal1"222 intVal := 5.0223 arrIntfVal := [2]interface{}{strVal, intVal}224 sliceIntfVal := []interface{}{intVal, strVal}225 mapIntfVal := map[string]interface{}{226 "key1": strVal,227 "key2": intVal,228 }229 mapArrIntfVal := map[string][2]interface{}{230 "key1": arrIntfVal,231 "key2": arrIntfVal,232 }233 mapSliceIntfVal := map[string][]interface{}{234 "key1": sliceIntfVal,235 "key2": sliceIntfVal,236 }237 mapPtrSliceIntfVal := map[string]*[]interface{}{238 "key1": &sliceIntfVal,239 "key2": &sliceIntfVal,240 }241 mapMapIntfVal := map[string]map[string]interface{}{242 "key1": mapIntfVal,243 "key2": mapIntfVal,244 }245 mapPtrMapIntfVal := map[string]*map[string]interface{}{246 "key1": &mapIntfVal,247 "key2": &mapIntfVal,248 }249 td := &TypeDeep{}250 data, _ := json.Marshal(arrIntfVal)251 err := UpdateJSON(td, []string{"arrIntfVal"}, data, UpdateOp)252 if assert.NoError(t, err) {253 assert.EqualValues(t, arrIntfVal, td.ArrIntfVal)254 }255 data, _ = json.Marshal(sliceIntfVal)256 err = UpdateJSON(td, []string{"sliceIntfVal"}, data, UpdateOp)257 if assert.NoError(t, err) {258 assert.EqualValues(t, sliceIntfVal, td.SliceIntfVal)259 }260 data, _ = json.Marshal(mapIntfVal)261 err = UpdateJSON(td, []string{"mapIntfVal"}, data, UpdateOp)262 if assert.NoError(t, err) {263 assert.EqualValues(t, mapIntfVal, td.MapIntfVal)264 }265 data, _ = json.Marshal(mapArrIntfVal)266 err = UpdateJSON(td, []string{"mapArrIntfVal"}, data, UpdateOp)267 if assert.NoError(t, err) {268 assert.EqualValues(t, mapArrIntfVal, td.MapArrIntfVal)269 }270 data, _ = json.Marshal(mapSliceIntfVal)271 err = UpdateJSON(td, []string{"mapSliceIntfVal"}, data, UpdateOp)272 if assert.NoError(t, err) {273 assert.EqualValues(t, mapSliceIntfVal, td.MapSliceIntfVal)274 }275 data, _ = json.Marshal(mapPtrSliceIntfVal)276 err = UpdateJSON(td, []string{"mapPtrSliceIntfVal"}, data, UpdateOp)277 if assert.NoError(t, err) {278 assert.EqualValues(t, mapPtrSliceIntfVal, td.MapPtrSliceIntfVal)279 }280 data, _ = json.Marshal(mapMapIntfVal)281 err = UpdateJSON(td, []string{"mapMapIntfVal"}, data, UpdateOp)282 if assert.NoError(t, err) {283 assert.EqualValues(t, mapMapIntfVal, td.MapMapIntfVal)284 }285 data, _ = json.Marshal(mapPtrMapIntfVal)286 err = UpdateJSON(td, []string{"mapPtrMapIntfVal"}, data, UpdateOp)287 if assert.NoError(t, err) {288 assert.EqualValues(t, mapPtrMapIntfVal, td.MapPtrMapIntfVal)289 }290 // Second level.291 newSliceIntfVal := []interface{}{6.0, 7.0, "newStrVal"}292 data, _ = json.Marshal(newSliceIntfVal)293 err = UpdateJSON(td, []string{"mapSliceIntfVal", "key1"}, data, UpdateOp)294 if assert.NoError(t, err) {295 assert.EqualValues(t, newSliceIntfVal, td.MapSliceIntfVal["key1"])296 }297 err = UpdateJSON(td, []string{"mapPtrSliceIntfVal", "key1"}, data, UpdateOp)298 if assert.NoError(t, err) {299 assert.EqualValues(t, &newSliceIntfVal, td.MapPtrSliceIntfVal["key1"])300 }301 newMapIntfVal := map[string]interface{}{302 "key3": 6.0,303 "key4": 7.0,304 "key5": "newStrVal",305 }306 data, _ = json.Marshal(newMapIntfVal)307 err = UpdateJSON(td, []string{"mapMapIntfVal", "key1"}, data, UpdateOp)308 if assert.NoError(t, err) {309 assert.EqualValues(t, newMapIntfVal, td.MapMapIntfVal["key1"])310 }311 err = UpdateJSON(td, []string{"mapPtrMapIntfVal", "key1"}, data, UpdateOp)312 if assert.NoError(t, err) {313 assert.EqualValues(t, &newMapIntfVal, td.MapPtrMapIntfVal["key1"])314 }315}316func TestUpdateDeep(t *testing.T) {317 strVal1 := "strVal1"318 strVal2 := "strVal2"319 intVal := 5320 mapVal := map[string]string{321 "key1": strVal1,322 "key2": strVal2,323 }324 td1 := TypeDeep{325 StrVal: strVal1,326 IntVal: intVal,327 MapVal: mapVal,328 PtrMapVal: &mapVal,329 }330 sliceDeep := []TypeDeep{td1, td1}331 arrDeep := [2]TypeDeep{td1, td1}332 slicePtrDeep := []*TypeDeep{&td1, &td1}333 arrPtrDeep := [2]*TypeDeep{&td1, &td1}334 mapDeep := map[string]TypeDeep{335 "key1": td1,336 "key2": td1,337 }338 mapPtrDeep := map[string]*TypeDeep{339 "key1": &td1,340 "key2": &td1,341 }342 mapArrDeep := map[string][2]TypeDeep{343 "key1": arrDeep,344 "key2": arrDeep,345 }346 mapSlicePtrDeep := map[string][]*TypeDeep{347 "key1": slicePtrDeep,348 "key2": slicePtrDeep,349 }350 mapMapDeep := map[string]map[string]TypeDeep{351 "key1": mapDeep,352 "key2": mapDeep,353 }354 mapPtrMapDeep := map[string]*map[string]TypeDeep{355 "key1": &mapDeep,356 "key2": &mapDeep,357 }358 mapMapPtrDeep := map[string]map[string]*TypeDeep{359 "key1": mapPtrDeep,360 "key2": mapPtrDeep,361 }362 td := &TypeDeep{363 PtrDeep: &td1,364 SliceDeep: sliceDeep,365 SlicePtrDeep: slicePtrDeep,366 ArrPtrDeep: arrPtrDeep,367 MapDeep: mapDeep,368 MapPtrDeep: mapPtrDeep,369 MapArrDeep: mapArrDeep,370 MapSlicePtrDeep: mapSlicePtrDeep,371 MapMapDeep: mapMapDeep,372 MapPtrMapDeep: mapPtrMapDeep,373 MapMapPtrDeep: mapMapPtrDeep,374 }375 // One level376 td1.StrVal = "strNew"377 data, _ := json.Marshal(td1)378 err := UpdateJSON(td, []string{"ptrDeep"}, data, UpdateOp)379 if assert.NoError(t, err) {380 assert.EqualValues(t, &td1, td.PtrDeep)381 }382 // Two levels383 sliceDeep = []TypeDeep{td1, td1}384 data, _ = json.Marshal(sliceDeep)385 err = UpdateJSON(td, []string{"ptrDeep", "sliceDeep"}, data, UpdateOp)386 if assert.NoError(t, err) {387 assert.EqualValues(t, sliceDeep, td.PtrDeep.SliceDeep)388 }389 slicePtrDeep = []*TypeDeep{&td1, &td1}390 data, _ = json.Marshal(slicePtrDeep)391 err = UpdateJSON(td, []string{"ptrDeep", "slicePtrDeep"}, data, UpdateOp)392 if assert.NoError(t, err) {393 assert.EqualValues(t, slicePtrDeep, td.PtrDeep.SlicePtrDeep)394 }395 arrPtrDeep = [2]*TypeDeep{&td1, &td1}396 data, _ = json.Marshal(arrPtrDeep)397 err = UpdateJSON(td, []string{"ptrDeep", "arrPtrDeep"}, data, UpdateOp)398 if assert.NoError(t, err) {399 assert.EqualValues(t, arrPtrDeep, td.PtrDeep.ArrPtrDeep)400 }401 mapDeep = map[string]TypeDeep{402 "key3": td1,403 "key4": td1,404 }405 data, _ = json.Marshal(mapDeep)406 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep"}, data, UpdateOp)407 if assert.NoError(t, err) {408 assert.EqualValues(t, mapDeep, td.PtrDeep.MapDeep)409 }410 mapPtrDeep = map[string]*TypeDeep{411 "key3": &td1,412 "key4": &td1,413 }414 data, _ = json.Marshal(mapPtrDeep)415 err = UpdateJSON(td, []string{"ptrDeep", "mapPtrDeep"}, data, UpdateOp)416 if assert.NoError(t, err) {417 assert.EqualValues(t, mapPtrDeep, td.PtrDeep.MapPtrDeep)418 }419 mapArrDeep = map[string][2]TypeDeep{420 "key3": arrDeep,421 "key4": arrDeep,422 }423 data, _ = json.Marshal(mapArrDeep)424 err = UpdateJSON(td, []string{"ptrDeep", "mapArrDeep"}, data, UpdateOp)425 if assert.NoError(t, err) {426 assert.EqualValues(t, mapArrDeep, td.PtrDeep.MapArrDeep)427 }428 mapSlicePtrDeep = map[string][]*TypeDeep{429 "key3": slicePtrDeep,430 "key4": slicePtrDeep,431 }432 data, _ = json.Marshal(mapSlicePtrDeep)433 err = UpdateJSON(td, []string{"ptrDeep", "mapSlicePtrDeep"}, data, UpdateOp)434 if assert.NoError(t, err) {435 assert.EqualValues(t, mapSlicePtrDeep, td.PtrDeep.MapSlicePtrDeep)436 }437 mapMapDeep = map[string]map[string]TypeDeep{438 "key3": mapDeep,439 "key4": mapDeep,440 }441 data, _ = json.Marshal(mapMapDeep)442 err = UpdateJSON(td, []string{"ptrDeep", "mapMapDeep"}, data, UpdateOp)443 if assert.NoError(t, err) {444 assert.EqualValues(t, mapMapDeep, td.PtrDeep.MapMapDeep)445 }446 mapPtrMapDeep = map[string]*map[string]TypeDeep{447 "key3": &mapDeep,448 "key4": &mapDeep,449 }450 data, _ = json.Marshal(mapPtrMapDeep)451 err = UpdateJSON(td, []string{"ptrDeep", "mapPtrMapDeep"}, data, UpdateOp)452 if assert.NoError(t, err) {453 assert.EqualValues(t, mapPtrMapDeep, td.PtrDeep.MapPtrMapDeep)454 }455 mapMapPtrDeep = map[string]map[string]*TypeDeep{456 "key3": mapPtrDeep,457 "key4": mapPtrDeep,458 }459 data, _ = json.Marshal(mapMapPtrDeep)460 err = UpdateJSON(td, []string{"ptrDeep", "mapMapPtrDeep"}, data, UpdateOp)461 if assert.NoError(t, err) {462 assert.EqualValues(t, mapMapPtrDeep, td.PtrDeep.MapMapPtrDeep)463 }464 // Three levels465 td1.StrVal = "strNewNew"466 data, _ = json.Marshal(td1)467 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3"}, data, UpdateOp)468 if assert.NoError(t, err) {469 assert.EqualValues(t, td1, td.PtrDeep.MapDeep["key3"])470 }471 err = UpdateJSON(td, []string{"ptrDeep", "mapPtrDeep", "key3"}, data, UpdateOp)472 if assert.NoError(t, err) {473 assert.EqualValues(t, &td1, td.PtrDeep.MapPtrDeep["key3"])474 }475 arrDeep = [2]TypeDeep{td1, td1}476 data, _ = json.Marshal(arrDeep)477 err = UpdateJSON(td, []string{"ptrDeep", "mapArrDeep", "key3"}, data, UpdateOp)478 if assert.NoError(t, err) {479 assert.EqualValues(t, arrDeep, td.PtrDeep.MapArrDeep["key3"])480 }481 slicePtrDeep = []*TypeDeep{&td1, &td1}482 data, _ = json.Marshal(slicePtrDeep)483 err = UpdateJSON(td, []string{"ptrDeep", "mapSlicePtrDeep", "key3"}, data, UpdateOp)484 if assert.NoError(t, err) {485 assert.EqualValues(t, slicePtrDeep, td.PtrDeep.MapSlicePtrDeep["key3"])486 }487 mapDeep = map[string]TypeDeep{488 "key3": td1,489 "key4": td1,490 }491 data, _ = json.Marshal(mapDeep)492 err = UpdateJSON(td, []string{"ptrDeep", "mapMapDeep", "key3"}, data, UpdateOp)493 if assert.NoError(t, err) {494 assert.EqualValues(t, mapDeep, td.PtrDeep.MapMapDeep["key3"])495 }496 err = UpdateJSON(td, []string{"ptrDeep", "mapPtrMapDeep", "key3"}, data, UpdateOp)497 if assert.NoError(t, err) {498 assert.EqualValues(t, &mapDeep, td.PtrDeep.MapPtrMapDeep["key3"])499 }500 mapPtrDeep = map[string]*TypeDeep{501 "key3": &td1,502 "key4": &td1,503 }504 err = UpdateJSON(td, []string{"ptrDeep", "mapMapPtrDeep", "key3"}, data, UpdateOp)505 if assert.NoError(t, err) {506 assert.EqualValues(t, mapPtrDeep, td.PtrDeep.MapMapPtrDeep["key3"])507 }508 // Four levels509 td1.StrVal = "strNewNewNew"510 data, _ = json.Marshal(td1)511 err = UpdateJSON(td, []string{"ptrDeep", "sliceDeep", "0", "ptrDeep"}, data, UpdateOp)512 if assert.NoError(t, err) {513 assert.EqualValues(t, &td1, td.PtrDeep.SliceDeep[0].PtrDeep)514 }515 err = UpdateJSON(td, []string{"ptrDeep", "slicePtrDeep", "0", "ptrDeep"}, data, UpdateOp)516 if assert.NoError(t, err) {517 assert.EqualValues(t, &td1, td.PtrDeep.SlicePtrDeep[0].PtrDeep)518 }519 err = UpdateJSON(td, []string{"ptrDeep", "arrPtrDeep", "0", "ptrDeep"}, data, UpdateOp)520 if assert.NoError(t, err) {521 assert.EqualValues(t, &td1, td.PtrDeep.ArrPtrDeep[0].PtrDeep)522 }523 data, _ = json.Marshal(td1)524 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "ptrDeep"}, data, UpdateOp)525 if assert.NoError(t, err) {526 assert.EqualValues(t, &td1, td.PtrDeep.MapDeep["key3"].PtrDeep)527 }528 err = UpdateJSON(td, []string{"ptrDeep", "mapPtrDeep", "key3", "ptrDeep"}, data, UpdateOp)529 if assert.NoError(t, err) {530 assert.EqualValues(t, &td1, td.PtrDeep.MapPtrDeep["key3"].PtrDeep)531 }532 sliceDeep = []TypeDeep{td1, td1}533 data, _ = json.Marshal(sliceDeep)534 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "sliceDeep"}, data, UpdateOp)535 if assert.NoError(t, err) {536 assert.EqualValues(t, sliceDeep, td.PtrDeep.MapDeep["key3"].SliceDeep)537 }538 slicePtrDeep = []*TypeDeep{&td1, &td1}539 data, _ = json.Marshal(slicePtrDeep)540 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "slicePtrDeep"}, data, UpdateOp)541 if assert.NoError(t, err) {542 assert.EqualValues(t, slicePtrDeep, td.PtrDeep.MapDeep["key3"].SlicePtrDeep)543 }544 arrPtrDeep = [2]*TypeDeep{&td1, &td1}545 data, _ = json.Marshal(arrPtrDeep)546 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "arrPtrDeep"}, data, UpdateOp)547 if assert.NoError(t, err) {548 assert.EqualValues(t, arrPtrDeep, td.PtrDeep.MapDeep["key3"].ArrPtrDeep)549 }550 mapDeep = map[string]TypeDeep{551 "key5": td1,552 "key6": td1,553 }554 data, _ = json.Marshal(mapDeep)555 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "mapDeep"}, data, UpdateOp)556 if assert.NoError(t, err) {557 assert.EqualValues(t, mapDeep, td.PtrDeep.MapDeep["key3"].MapDeep)558 }559 mapPtrDeep = map[string]*TypeDeep{560 "key3": &td1,561 "key4": &td1,562 }563 data, _ = json.Marshal(mapPtrDeep)564 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "mapPtrDeep"}, data, UpdateOp)565 if assert.NoError(t, err) {566 assert.EqualValues(t, mapPtrDeep, td.PtrDeep.MapDeep["key3"].MapPtrDeep)567 }568 arrDeep = [2]TypeDeep{td1, td1}569 mapArrDeep = map[string][2]TypeDeep{570 "key3": arrDeep,571 "key4": arrDeep,572 }573 data, _ = json.Marshal(mapArrDeep)574 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "mapArrDeep"}, data, UpdateOp)575 if assert.NoError(t, err) {576 assert.EqualValues(t, mapArrDeep, td.PtrDeep.MapDeep["key3"].MapArrDeep)577 }578 slicePtrDeep = []*TypeDeep{&td1, &td1}579 data, _ = json.Marshal(slicePtrDeep)580 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "slicePtrDeep"}, data, UpdateOp)581 if assert.NoError(t, err) {582 assert.EqualValues(t, slicePtrDeep, td.PtrDeep.MapDeep["key3"].SlicePtrDeep)583 }584 mapMapDeep = map[string]map[string]TypeDeep{585 "key3": mapDeep,586 "key4": mapDeep,587 }588 data, _ = json.Marshal(mapMapDeep)589 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "mapMapDeep"}, data, UpdateOp)590 if assert.NoError(t, err) {591 assert.EqualValues(t, mapMapDeep, td.PtrDeep.MapDeep["key3"].MapMapDeep)592 }593 mapPtrMapDeep = map[string]*map[string]TypeDeep{594 "key3": &mapDeep,595 "key4": &mapDeep,596 }597 data, _ = json.Marshal(mapPtrMapDeep)598 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "mapPtrMapDeep"}, data, UpdateOp)599 if assert.NoError(t, err) {600 assert.EqualValues(t, mapPtrMapDeep, td.PtrDeep.MapDeep["key3"].MapPtrMapDeep)601 }602 mapMapPtrDeep = map[string]map[string]*TypeDeep{603 "key3": mapPtrDeep,604 "key4": mapPtrDeep,605 }606 data, _ = json.Marshal(mapMapPtrDeep)607 err = UpdateJSON(td, []string{"ptrDeep", "mapDeep", "key3", "mapMapPtrDeep"}, data, UpdateOp)608 if assert.NoError(t, err) {609 assert.EqualValues(t, mapMapPtrDeep, td.PtrDeep.MapDeep["key3"].MapMapPtrDeep)610 }611 // Five levels612 td1.StrVal = "strNewNewNewNew"613 data, _ = json.Marshal(td1)614 err = UpdateJSON(td, []string{"ptrDeep", "sliceDeep", "0", "ptrDeep", "ptrDeep"}, data, UpdateOp)615 if assert.NoError(t, err) {616 assert.EqualValues(t, &td1, td.PtrDeep.SliceDeep[0].PtrDeep.PtrDeep)617 }618 err = UpdateJSON(td, []string{"ptrDeep", "mapArrDeep", "key3", "0", "ptrDeep"}, data, UpdateOp)619 if assert.NoError(t, err) {620 assert.EqualValues(t, &td1, td.PtrDeep.MapArrDeep["key3"][0].PtrDeep)621 }622 err = UpdateJSON(td, []string{"ptrDeep", "mapSlicePtrDeep", "key3", "0", "ptrDeep"}, data, UpdateOp)623 if assert.NoError(t, err) {624 assert.EqualValues(t, &td1, td.PtrDeep.MapSlicePtrDeep["key3"][0].PtrDeep)625 }626 err = UpdateJSON(td, []string{"ptrDeep", "mapMapDeep", "key3", "key4", "ptrDeep"}, data, UpdateOp)627 if assert.NoError(t, err) {628 assert.EqualValues(t, &td1, td.PtrDeep.MapMapDeep["key3"]["key4"].PtrDeep)629 }630 err = UpdateJSON(td, []string{"ptrDeep", "mapMapPtrDeep", "key3", "key4", "ptrDeep"}, data, UpdateOp)631 if assert.NoError(t, err) {632 assert.EqualValues(t, &td1, td.PtrDeep.MapMapPtrDeep["key3"]["key4"].PtrDeep)633 }634}635func TestUpdateFails(t *testing.T) {636 strVal1 := "strVal1"637 strVal2 := "strVal2"638 mapVal := map[string]string{639 "key1": strVal1,640 "key2": strVal2,641 }642 td := &TypeDeep{}643 err := UpdateJSON(td, []string{"bogus"}, nil, UpdateOp)644 if assert.Error(t, err) {645 assert.IsType(t, &KeyNotFoundError{}, err)646 }647 err = UpdateJSON(td, []string{"strVal", "bogus"}, nil, UpdateOp)648 if assert.Error(t, err) {649 assert.IsType(t, &KeyNotTraversableError{}, err)650 }651 err = UpdateJSON(td, []string{"strVal"}, json.RawMessage("strVal"), UpdateOp)652 if assert.Error(t, err) {653 assert.IsType(t, &json.SyntaxError{}, err) // TODO654 }655 err = UpdateJSON(td, []string{"sliceVal", "x"}, nil, UpdateOp)656 if assert.Error(t, err) {657 assert.IsType(t, &KeyNotFoundError{}, err)658 }659 err = UpdateJSON(td, []string{"sliceVal", "10"}, nil, UpdateOp)660 if assert.Error(t, err) {661 assert.IsType(t, &KeyNotFoundError{}, err)662 }663 // TODO: update on empty map to result in KeyNotFoundError? Currently works as Create.664 //data, _ := json.Marshal(mapVal)665 //err = UpdateJSON(td, []string{"mapVal"}, data, UpdateOp)666 //if assert.Error(t, err) {667 // assert.IsType(t, &KeyNotFoundError{}, err)668 //}669 err = UpdateJSON(td, []string{"mapVal", "bogus"}, nil, UpdateOp)670 if assert.Error(t, err) {671 assert.IsType(t, &KeyNotFoundError{}, err)672 }673 td.MapVal = mapVal674 err = UpdateJSON(td, []string{"mapVal", "key1"}, json.RawMessage("bogus"), UpdateOp)675 if assert.Error(t, err) {676 assert.IsType(t, &json.SyntaxError{}, err)677 }678}679func TestCreateFails(t *testing.T) {680 strVal0 := "strVal0"681 strVal1 := "strVal1"682 strVal2 := "strVal2"683 sliceVal0 := []string{strVal0}684 sliceSliceVal0 := [][]string{sliceVal0}685 mapVal0 := map[string]string{686 "key1": strVal1,687 "key2": strVal2,688 }689 sliceMapVal0 := []map[string]string{mapVal0}690 mapSliceVal0 := map[string][]string{"key1": sliceVal0}691 mapMapVal0 := map[string]map[string]string{692 "key0": mapVal0,693 }694 td := &TypeDeep{695 SliceSliceVal: sliceSliceVal0,696 SliceMapVal: sliceMapVal0,697 MapSliceVal: mapSliceVal0,698 MapMapVal: mapMapVal0,699 }700 err := UpdateJSON(td, []string{"bogus"}, nil, CreateOp)701 if assert.Error(t, err) {702 assert.IsType(t, &KeyNotFoundError{}, err)703 }704 err = UpdateJSON(td, []string{"strVal"}, nil, CreateOp)705 if assert.Error(t, err) {706 assert.IsType(t, &KeyExistsError{}, err)707 }708 err = UpdateJSON(td, []string{"strVal", "bogus"}, nil, CreateOp)709 if assert.Error(t, err) {710 assert.IsType(t, &KeyNotTraversableError{}, err)711 }712 err = UpdateJSON(td, []string{"mapVal", "new"}, json.RawMessage("bogus"), CreateOp)713 if assert.Error(t, err) {714 assert.IsType(t, &json.SyntaxError{}, err)715 }716 err = UpdateJSON(td, []string{"sliceVal", "x"}, nil, CreateOp)717 if assert.Error(t, err) {718 assert.IsType(t, &KeyNotFoundError{}, err)719 }720 data, _ := json.Marshal([]int{5, 6})721 err = UpdateJSON(td, []string{"sliceVal"}, data, CreateOp)722 if assert.Error(t, err) {723 assert.IsType(t, &json.UnmarshalTypeError{}, err)724 }725 err = UpdateJSON(td, []string{"sliceSliceVal", "0"}, data, CreateOp)726 if assert.Error(t, err) {727 assert.IsType(t, &json.UnmarshalTypeError{}, err)728 }729 err = UpdateJSON(td, []string{"sliceMapVal", "0"}, data, CreateOp)730 if assert.Error(t, err) {731 assert.IsType(t, &OperationForbiddenError{}, err)732 }733 err = UpdateJSON(td, []string{"mapSliceVal", "key1"}, data, CreateOp)734 if assert.Error(t, err) {735 assert.IsType(t, &json.UnmarshalTypeError{}, err)736 }737 err = UpdateJSON(td, []string{"mapMapVal", "key0"}, data, CreateOp)738 if assert.Error(t, err) {739 assert.IsType(t, &KeyExistsError{}, err)740 }741}742func TestCreateSuccessful(t *testing.T) {743 strVal0 := "strVal0"744 strVal1 := "strVal1"745 strVal2 := "strVal2"746 mapVal := map[string]string{747 "key1": strVal1,748 "key2": strVal2,749 }750 sliceVal0 := []string{strVal0}751 sliceVal := []string{strVal1, strVal2}752 slicePtrVal0 := []*string{&strVal0}753 slicePtrVal := []*string{&strVal1, &strVal2}754 arrVal := [2]string{strVal1, strVal2}755 sliceMapVal0 := []map[string]string{mapVal}756 sliceMapVal := []map[string]string{mapVal, mapVal}757 slicePtrMapVal0 := []*map[string]string{&mapVal}758 slicePtrMapVal := []*map[string]string{&mapVal, &mapVal}759 sliceSliceVal0 := [][]string{sliceVal0}760 sliceSliceVal := [][]string{sliceVal, sliceVal}761 slicePtrSliceVal0 := []*[]string{&sliceVal}762 slicePtrSliceVal := []*[]string{&sliceVal, &sliceVal}763 sliceIntfVal0 := []interface{}{strVal0, 1}764 sliceIntfVal := []interface{}{strVal1, 5.5}765 var intfVal interface{} = sliceIntfVal766 mapSliceVal0 := map[string][]string{"key1": sliceVal0}767 mapPtrSliceVal0 := map[string]*[]string{"key1": &sliceVal0}768 td := &TypeDeep{769 SliceVal: sliceVal0,770 SlicePtrVal: slicePtrVal0,771 SliceMapVal: sliceMapVal0,772 SlicePtrMapVal: slicePtrMapVal0,773 SliceSliceVal: sliceSliceVal0,774 SlicePtrSliceVal: slicePtrSliceVal0,775 SliceIntfVal: sliceIntfVal0,776 MapSliceVal: mapSliceVal0,777 MapPtrSliceVal: mapPtrSliceVal0,778 }779 // One level.780 data, _ := json.Marshal(sliceVal)781 err := UpdateJSON(td, []string{"sliceVal"}, data, CreateOp)782 if assert.NoError(t, err) {783 assert.EqualValues(t, append(sliceVal0, sliceVal...), td.SliceVal)784 }785 err = UpdateJSON(td, []string{"slicePtrVal"}, data, CreateOp)786 if assert.NoError(t, err) {787 assert.EqualValues(t, append(slicePtrVal0, slicePtrVal...), td.SlicePtrVal)788 }789 data, _ = json.Marshal(sliceMapVal)790 err = UpdateJSON(td, []string{"sliceMapVal"}, data, CreateOp)791 if assert.NoError(t, err) {792 assert.EqualValues(t, append(sliceMapVal0, sliceMapVal...), td.SliceMapVal)793 }794 err = UpdateJSON(td, []string{"slicePtrMapVal"}, data, CreateOp)795 if assert.NoError(t, err) {796 assert.EqualValues(t, append(slicePtrMapVal0, slicePtrMapVal...), td.SlicePtrMapVal)797 }798 data, _ = json.Marshal(sliceSliceVal)799 err = UpdateJSON(td, []string{"sliceSliceVal"}, data, CreateOp)800 if assert.NoError(t, err) {801 assert.EqualValues(t, append(sliceSliceVal0, sliceSliceVal...), td.SliceSliceVal)802 }803 err = UpdateJSON(td, []string{"slicePtrSliceVal"}, data, CreateOp)804 if assert.NoError(t, err) {805 assert.EqualValues(t, append(slicePtrSliceVal0, slicePtrSliceVal...), td.SlicePtrSliceVal)806 }807 data, _ = json.Marshal(sliceIntfVal)808 err = UpdateJSON(td, []string{"sliceIntfVal"}, data, CreateOp)809 if assert.NoError(t, err) {810 assert.EqualValues(t, append(sliceIntfVal0, sliceIntfVal...), td.SliceIntfVal)811 }812 // Two levels.813 data, _ = json.Marshal(sliceVal)814 err = UpdateJSON(td, []string{"sliceSliceVal", "0"}, data, CreateOp)815 if assert.NoError(t, err) {816 assert.EqualValues(t, append(sliceVal0, sliceVal...), td.SliceSliceVal[0])817 }818 /* TODO: handle pointers to slices.819 err = UpdateJSON(td, []string{"slicePtrSliceVal", "0"}, data, CreateOp)820 if assert.NoError(t, err) {821 assert.EqualValues(t, append(sliceVal0, sliceVal...), td.SlicePtrSliceVal[0])822 }823 */824 data, _ = json.Marshal(mapVal)825 err = UpdateJSON(td, []string{"mapMapVal", "new"}, data, CreateOp)826 if assert.NoError(t, err) {827 assert.EqualValues(t, mapVal, td.MapMapVal["new"])828 }829 err = UpdateJSON(td, []string{"mapPtrMapVal", "new"}, data, CreateOp)830 if assert.NoError(t, err) {831 assert.EqualValues(t, &mapVal, td.MapPtrMapVal["new"])832 }833 data, _ = json.Marshal(sliceVal)834 err = UpdateJSON(td, []string{"mapSliceVal", "key1"}, data, CreateOp)835 if assert.NoError(t, err) {836 assert.EqualValues(t, append(sliceVal0, sliceVal...), td.MapSliceVal["key1"])837 }838 /* TODO: handle pointers to slices.839 err = UpdateJSON(td, []string{"mapPtrSliceVal", "key1"}, data, CreateOp)840 if assert.NoError(t, err) {841 assert.EqualValues(t, &sliceVal, td.MapPtrSliceVal["key1"])842 }843 */844 err = UpdateJSON(td, []string{"mapArrVal", "new"}, data, CreateOp)845 if assert.NoError(t, err) {846 assert.EqualValues(t, arrVal, td.MapArrVal["new"])847 }848 data, _ = json.Marshal(intfVal)849 err = UpdateJSON(td, []string{"mapIntfVal", "new"}, data, CreateOp)850 if assert.NoError(t, err) {851 assert.EqualValues(t, intfVal, td.MapIntfVal["new"])852 }853}854func TestSetSuccessful(t *testing.T) {855 strVal0 := "strVal0"856 strVal1 := "strVal1"857 strVal2 := "strVal2"858 mapVal0 := map[string]string{859 "key0": strVal0,860 }861 mapVal := map[string]string{862 "key1": strVal1,863 "key2": strVal2,864 }865 sliceMapVal0 := []map[string]string{mapVal0}866 mapMapVal0 := map[string]map[string]string{867 "key0": mapVal0,868 }869 td := &TypeDeep{870 MapVal: mapVal0,871 SliceMapVal: sliceMapVal0,872 MapMapVal: mapMapVal0,873 }874 data, _ := json.Marshal(mapVal)875 err := UpdateJSON(td, []string{"mapVal"}, data, SetOp)876 if assert.NoError(t, err) && assert.Equal(t, 3, len(td.MapVal)) {877 assert.Equal(t, mapVal0["key0"], td.MapVal["key0"])878 assert.Equal(t, mapVal["key1"], td.MapVal["key1"])879 assert.Equal(t, mapVal["key2"], td.MapVal["key2"])880 }881 err = UpdateJSON(td, []string{"sliceMapVal", "0"}, data, SetOp)882 if assert.NoError(t, err) && assert.Equal(t, 3, len(td.SliceMapVal[0])) {883 assert.Equal(t, mapVal0["key0"], td.SliceMapVal[0]["key0"])884 assert.Equal(t, mapVal["key1"], td.SliceMapVal[0]["key1"])885 assert.Equal(t, mapVal["key2"], td.SliceMapVal[0]["key2"])886 }887 err = UpdateJSON(td, []string{"mapMapVal", "key0"}, data, SetOp)888 if assert.NoError(t, err) && assert.Equal(t, 3, len(td.MapMapVal["key0"])) {889 assert.Equal(t, mapVal0["key0"], td.MapMapVal["key0"]["key0"])890 assert.Equal(t, mapVal["key1"], td.MapMapVal["key0"]["key1"])891 assert.Equal(t, mapVal["key2"], td.MapMapVal["key0"]["key2"])892 }893}894func TestSetFails(t *testing.T) {895 strVal1 := "strVal1"896 strVal2 := "strVal2"897 mapVal := map[string]string{898 "key1": strVal1,899 "key2": strVal2,900 }901 sliceMapVal0 := []map[string]string{mapVal}902 mapMapVal0 := map[string]map[string]string{903 "key0": mapVal,904 }905 td := &TypeDeep{906 MapVal: mapVal,907 SliceMapVal: sliceMapVal0,908 MapMapVal: mapMapVal0,909 }910 data, _ := json.Marshal([]int{5, 6})911 err := UpdateJSON(td, []string{"mapVal"}, data, SetOp)912 if assert.Error(t, err) {913 assert.IsType(t, &json.UnmarshalTypeError{}, err)914 }915 err = UpdateJSON(td, []string{"sliceMapVal", "0"}, data, SetOp)916 if assert.Error(t, err) {917 assert.IsType(t, &json.UnmarshalTypeError{}, err)918 }919 err = UpdateJSON(td, []string{"mapMapVal", "key0"}, data, SetOp)920 if assert.Error(t, err) {921 assert.IsType(t, &json.UnmarshalTypeError{}, err)922 }923}924func TestDeleteFails(t *testing.T) {925 strVal1 := "strVal1"926 strVal2 := "strVal2"927 intVal := 5928 mapVal := map[string]string{929 "key1": strVal1,930 "key2": strVal2,931 }932 sliceVal := []string{strVal1, strVal2}933 mapSliceIntfVal := map[string][]interface{}{934 "key1": []interface{}{strVal1, intVal},935 "key2": []interface{}{strVal2, intVal},936 }937 mapMapIntfVal := map[string]map[string]interface{}{938 "key1": map[string]interface{}{939 "key2": strVal1,940 "key3": sliceVal,941 },942 }943 td1 := TypeDeep{}944 mapDeep := map[string]TypeDeep{945 "key1": td1,946 "key2": td1,947 }948 td := &TypeDeep{949 StrVal: strVal1,950 IntVal: intVal,951 MapVal: mapVal,952 PtrMapVal: &mapVal,953 SliceVal: sliceVal,954 MapSliceIntfVal: mapSliceIntfVal,955 MapMapIntfVal: mapMapIntfVal,956 MapDeep: mapDeep,957 }958 err := UpdateJSON(td, []string{"bogus"}, nil, DeleteOp)959 if assert.Error(t, err) {960 assert.IsType(t, &KeyNotFoundError{}, err)961 }962 err = UpdateJSON(td, []string{"strVal"}, nil, DeleteOp)963 if assert.Error(t, err) {964 assert.IsType(t, &OperationForbiddenError{}, err)965 assert.EqualError(t, err, "forbidden operation Delete on key strVal of type optikon.TypeDeep")966 assert.Equal(t, "strVal", err.(*OperationForbiddenError).Key())967 assert.Equal(t, "optikon.TypeDeep", err.(*OperationForbiddenError).KeyType().String())968 assert.Equal(t, DeleteOp, err.(*OperationForbiddenError).Operation())969 }970 err = UpdateJSON(td, []string{"strVal", "bogus"}, nil, DeleteOp)971 if assert.Error(t, err) {972 assert.IsType(t, &KeyNotTraversableError{}, err)973 }974 err = UpdateJSON(td, []string{"sliceVal"}, nil, DeleteOp)975 if assert.Error(t, err) {976 assert.IsType(t, &OperationForbiddenError{}, err)977 }978 err = UpdateJSON(td, []string{"sliceVal", "x"}, nil, DeleteOp)979 if assert.Error(t, err) {980 assert.IsType(t, &KeyNotFoundError{}, err)981 }982 err = UpdateJSON(td, []string{"sliceVal", "10"}, nil, DeleteOp)983 if assert.Error(t, err) {984 assert.IsType(t, &KeyNotFoundError{}, err)985 }986 err = UpdateJSON(td, []string{"sliceVal", "0"}, nil, DeleteOp)987 if assert.Error(t, err) {988 assert.IsType(t, &OperationForbiddenError{}, err)989 }990 err = UpdateJSON(td, []string{"mapVal", "bogus"}, nil, DeleteOp)991 if assert.Error(t, err) {992 assert.IsType(t, &KeyNotFoundError{}, err)993 }994 err = UpdateJSON(td, []string{"ptrDeep"}, nil, DeleteOp)995 if assert.Error(t, err) {996 assert.IsType(t, &OperationForbiddenError{}, err)997 }998 err = UpdateJSON(td, []string{"mapDeep"}, nil, DeleteOp)999 if assert.Error(t, err) {1000 assert.IsType(t, &OperationForbiddenError{}, err)1001 }1002 err = UpdateJSON(td, []string{"mapDeep", "key1", "bogus"}, nil, DeleteOp)1003 if assert.Error(t, err) {1004 assert.IsType(t, &KeyNotFoundError{}, err)1005 }1006 err = UpdateJSON(td, []string{"mapDeep", "key1", "strVal"}, nil, DeleteOp)1007 if assert.Error(t, err) {1008 assert.IsType(t, &OperationForbiddenError{}, err)1009 }1010 err = UpdateJSON(td, []string{"mapDeep", "key1", "sliceVal"}, nil, DeleteOp)1011 if assert.Error(t, err) {1012 assert.IsType(t, &OperationForbiddenError{}, err)1013 }1014 err = UpdateJSON(td, []string{"mapDeep", "key1", "sliceVal", "0"}, nil, DeleteOp)1015 if assert.Error(t, err) {1016 assert.IsType(t, &KeyNotFoundError{}, err)1017 }1018 // TODO: IntfVal - check delete if actual data permits it?1019 err = UpdateJSON(td, []string{"mapSliceIntfVal", "key1", "0"}, nil, DeleteOp)1020 if assert.Error(t, err) {1021 assert.IsType(t, &OperationForbiddenError{}, err)1022 }1023 err = UpdateJSON(td, []string{"mapMapIntfVal", "key1", "key3", "0"}, nil, DeleteOp)1024 if assert.Error(t, err) {1025 assert.IsType(t, &OperationForbiddenError{}, err)1026 }1027 err = UpdateJSON(td, []string{"mapMapIntfVal", "key1", "key3", "3"}, nil, DeleteOp)1028 if assert.Error(t, err) {1029 assert.IsType(t, &KeyNotFoundError{}, err)1030 }1031}1032func TestDeleteSuccessful(t *testing.T) {1033 strVal1 := "strVal1"1034 strVal2 := "strVal2"1035 intVal := 51036 mapVal := map[string]string{1037 "key1": strVal1,1038 "key2": strVal2,1039 }1040 mapPtrVal := map[string]*string{1041 "key1": &strVal1,1042 "key2": &strVal2,1043 }1044 mapMapVal := map[string]map[string]string{1045 "key1": mapVal,1046 "key2": mapVal,1047 }1048 mapPtrMapVal := map[string]*map[string]string{1049 "key1": &mapVal,1050 "key2": &mapVal,1051 }1052 sliceVal := []string{strVal1, strVal2}1053 mapSliceVal := map[string][]string{1054 "key1": sliceVal,1055 "key2": sliceVal,1056 }1057 mapPtrSliceVal := map[string]*[]string{1058 "key1": &sliceVal,1059 "key2": &sliceVal,1060 }1061 slicePtrVal := []*string{&strVal1, &strVal2}1062 sliceMapVal := []map[string]string{mapVal, mapVal}1063 slicePtrMapVal := []*map[string]string{&mapVal, &mapVal}1064 sliceSliceVal := [][]string{sliceVal, sliceVal}1065 slicePtrSliceVal := []*[]string{&sliceVal, &sliceVal}1066 sliceIntfVal := []interface{}{strVal1, &mapSliceVal}1067 td1 := TypeDeep{1068 MapVal: mapVal,1069 PtrMapVal: &mapVal,1070 MapPtrVal: mapPtrVal,1071 MapMapVal: mapMapVal,1072 }1073 mapDeep := map[string]TypeDeep{1074 "key1": td1,1075 "key2": td1,1076 }1077 mapPtrDeep := map[string]*TypeDeep{1078 "key1": &td1,1079 "key2": &td1,1080 }1081 td := &TypeDeep{1082 StrVal: strVal1,1083 IntVal: intVal,1084 MapVal: mapVal,1085 PtrMapVal: &mapVal,1086 MapPtrVal: mapPtrVal,1087 MapMapVal: mapMapVal,1088 MapPtrMapVal: mapPtrMapVal,1089 MapSliceVal: mapSliceVal,1090 MapPtrSliceVal: mapPtrSliceVal,1091 SliceVal: sliceVal,1092 SlicePtrVal: slicePtrVal,1093 SliceMapVal: sliceMapVal,1094 SlicePtrMapVal: slicePtrMapVal,1095 SliceSliceVal: sliceSliceVal,1096 SlicePtrSliceVal: slicePtrSliceVal,1097 SliceIntfVal: sliceIntfVal,1098 //MapIntfVal1099 //PtrMapIntfVal1100 //MapSliceIntfVal1101 //MapPtrSliceIntfVal1102 //MapMapIntfVal1103 //MapPtrMapIntfVal1104 PtrDeep: &td1,1105 //SliceDeep1106 //SlicePtrDeep1107 MapDeep: mapDeep,1108 MapPtrDeep: mapPtrDeep,1109 //MapSlicePtrDeep1110 //MapMapDeep1111 //MapPtrMapDeep1112 //MapMapPtrDeep1113 }1114 // One level.1115 err := UpdateJSON(td, []string{"mapVal", "key1"}, nil, DeleteOp)1116 if assert.NoError(t, err) {1117 _, ok := td.MapVal["key1"]1118 assert.False(t, ok)1119 }1120 err = UpdateJSON(td, []string{"ptrMapVal", "key2"}, nil, DeleteOp)1121 if assert.NoError(t, err) {1122 _, ok := (*td.PtrMapVal)["key2"]1123 assert.False(t, ok)1124 }1125 mapVal["key1"] = strVal11126 mapVal["key2"] = strVal21127 err = UpdateJSON(td, []string{"mapPtrVal", "key1"}, nil, DeleteOp)1128 if assert.NoError(t, err) {1129 _, ok := td.MapPtrVal["key1"]1130 assert.False(t, ok)1131 }1132 err = UpdateJSON(td, []string{"mapMapVal", "key1"}, nil, DeleteOp)1133 if assert.NoError(t, err) {1134 _, ok := td.MapMapVal["key1"]1135 assert.False(t, ok)1136 }1137 err = UpdateJSON(td, []string{"mapPtrMapVal", "key1"}, nil, DeleteOp)1138 if assert.NoError(t, err) {1139 _, ok := td.MapPtrMapVal["key1"]1140 assert.False(t, ok)1141 }1142 // Two levels.1143 // TODO: can actually handle deleting indexed elements in the slice, but the1144 // delete should probably be done by value and not by index.1145 //err = UpdateJSON(td, []string{"sliceIntfVal", "0"}, nil, DeleteOp)1146 //if assert.NoError(t, err) {1147 // assert.Equal(t, 1, len(td.SliceIntfVal))1148 //}1149 err = UpdateJSON(td, []string{"mapDeep", "key1"}, nil, DeleteOp)1150 if assert.NoError(t, err) {1151 _, ok := td.MapDeep["key1"]1152 assert.False(t, ok)1153 }1154 err = UpdateJSON(td, []string{"mapPtrDeep", "key1"}, nil, DeleteOp)1155 if assert.NoError(t, err) {1156 _, ok := td.MapPtrDeep["key1"]1157 assert.False(t, ok)1158 }1159 // Three levels.1160 err = UpdateJSON(td, []string{"ptrDeep", "mapPtrVal", "key2"}, nil, DeleteOp)1161 if assert.NoError(t, err) {...

Full Screen

Full Screen

select_test.go

Source:select_test.go Github

copy

Full Screen

...12 PtrMapVal *map[string]string `json:"ptrMapVal"`13 MapPtrVal map[string]*string `json:"mapPtrVal"`14 MapMapVal map[string]map[string]string `json:"mapMapVal"`15 MapPtrMapVal map[string]*map[string]string `json:"mapPtrMapVal"`16 MapSliceVal map[string][]string `json:"mapSliceVal"`17 MapArrVal map[string][2]string `json:"mapArrVal"`18 MapPtrSliceVal map[string]*[]string `json:"mapPtrSliceVal"`19 SliceVal []string `json:"sliceVal"`20 ArrVal [2]string `json:"arrVal"`21 SlicePtrVal []*string `json:"slicePtrVal"`22 SliceMapVal []map[string]string `json:"sliceMapVal"`23 SlicePtrMapVal []*map[string]string `json:"slicePtrMapVal"`24 SliceSliceVal [][]string `json:"sliceSliceVal"`25 SlicePtrSliceVal []*[]string `json:"slicePtrSliceVal"`26 IntfVal interface{} `json:"intfVal"`27 ArrIntfVal [2]interface{} `json:"arrIntfVal"`28 SliceIntfVal []interface{} `json:"sliceIntfVal"`29 MapIntfVal map[string]interface{} `json:"mapIntfVal"`30 PtrMapIntfVal *map[string]interface{} `json:"ptrMapIntfVal"`31 MapArrIntfVal map[string][2]interface{} `json:"mapArrIntfVal"`32 MapSliceIntfVal map[string][]interface{} `json:"mapSliceIntfVal"`33 MapPtrSliceIntfVal map[string]*[]interface{} `json:"mapPtrSliceIntfVal"`34 MapMapIntfVal map[string]map[string]interface{} `json:"mapMapIntfVal"`35 MapPtrMapIntfVal map[string]*map[string]interface{} `json:"mapPtrMapIntfVal"`36 PtrDeep *TypeDeep `json:"ptrDeep"`37 SliceDeep []TypeDeep `json:"sliceDeep"`38 SlicePtrDeep []*TypeDeep `json:"slicePtrDeep"`39 ArrPtrDeep [2]*TypeDeep `json:"arrPtrDeep"`40 MapDeep map[string]TypeDeep `json:"mapDeep"`41 MapPtrDeep map[string]*TypeDeep `json:"mapPtrDeep"`42 MapArrDeep map[string][2]TypeDeep `json:"mapArrDeep"`43 MapSlicePtrDeep map[string][]*TypeDeep `json:"mapSlicePtrDeep"`44 MapMapDeep map[string]map[string]TypeDeep `json:"mapMapDeep"`45 MapPtrMapDeep map[string]*map[string]TypeDeep `json:"mapPtrMapDeep"`46 MapMapPtrDeep map[string]map[string]*TypeDeep `json:"mapMapPtrDeep"`47}48func TestSelectPrimitive(t *testing.T) {49 td := &TypeDeep{50 StrVal: "strVal",51 IntVal: 5,52 }53 partOut, err := Select(td, []string{"strVal"})54 if assert.NoError(t, err) {55 assert.Equal(t, td.StrVal, partOut)56 }57 partOut, err = Select(td, []string{"intVal"})58 if assert.NoError(t, err) {59 assert.Equal(t, td.IntVal, partOut)60 }61 partOut, err = Select(td, []string{"NoTagVal"})62 if assert.NoError(t, err) {63 assert.Equal(t, td.NoTagVal, partOut)64 }65}66func TestSelectMap(t *testing.T) {67 strVal1 := "strVal1"68 strVal2 := "strVal2"69 mapVal := map[string]string{70 "key1": strVal1,71 "key2": strVal2,72 }73 mapPtrVal := map[string]*string{74 "key1": &strVal1,75 "key2": &strVal2,76 }77 mapMapVal := map[string]map[string]string{78 "key1": mapVal,79 "key2": mapVal,80 }81 mapPtrMapVal := map[string]*map[string]string{82 "key1": &mapVal,83 "key2": &mapVal,84 }85 sliceVal := []string{strVal1, strVal2}86 mapSliceVal := map[string][]string{87 "key1": sliceVal,88 "key2": sliceVal,89 }90 arrVal := [2]string{strVal1, strVal2}91 mapArrVal := map[string][2]string{92 "key1": arrVal,93 "key2": arrVal,94 }95 mapPtrSliceVal := map[string]*[]string{96 "key1": &sliceVal,97 "key2": &sliceVal,98 }99 td := &TypeDeep{100 MapVal: mapVal,101 PtrMapVal: &mapVal,102 MapPtrVal: mapPtrVal,103 MapMapVal: mapMapVal,104 MapPtrMapVal: mapPtrMapVal,105 MapSliceVal: mapSliceVal,106 MapArrVal: mapArrVal,107 MapPtrSliceVal: mapPtrSliceVal,108 }109 partOut, err := Select(td, []string{"mapVal"})110 if assert.NoError(t, err) {111 assert.EqualValues(t, mapVal, partOut)112 }113 partOut, err = Select(td, []string{"ptrMapVal"})114 if assert.NoError(t, err) {115 assert.EqualValues(t, &mapVal, partOut)116 }117 partOut, err = Select(td, []string{"mapPtrVal"})118 if assert.NoError(t, err) {119 assert.EqualValues(t, mapPtrVal, partOut)120 }121 partOut, err = Select(td, []string{"mapMapVal"})122 if assert.NoError(t, err) {123 assert.EqualValues(t, mapMapVal, partOut)124 }125 partOut, err = Select(td, []string{"mapPtrMapVal"})126 if assert.NoError(t, err) {127 assert.EqualValues(t, mapPtrMapVal, partOut)128 }129 partOut, err = Select(td, []string{"mapSliceVal"})130 if assert.NoError(t, err) {131 assert.EqualValues(t, mapSliceVal, partOut)132 }133 partOut, err = Select(td, []string{"mapArrVal"})134 if assert.NoError(t, err) {135 assert.EqualValues(t, mapArrVal, partOut)136 }137 partOut, err = Select(td, []string{"mapPtrSliceVal"})138 if assert.NoError(t, err) {139 assert.EqualValues(t, mapPtrSliceVal, partOut)140 }141}142func TestSelectSlice(t *testing.T) {143 strVal1 := "strVal1"144 strVal2 := "strVal2"145 sliceVal := []string{strVal1, strVal2}146 arrVal := [2]string{strVal1, strVal2}147 slicePtrVal := []*string{&strVal1, &strVal2}148 mapVal := map[string]string{149 "key1": strVal1,150 "key2": strVal2,151 }152 sliceMapVal := []map[string]string{mapVal, mapVal}153 slicePtrMapVal := []*map[string]string{&mapVal, &mapVal}154 sliceSliceVal := [][]string{sliceVal, sliceVal}155 slicePtrSliceVal := []*[]string{&sliceVal, &sliceVal}156 td := &TypeDeep{157 SliceVal: sliceVal,158 ArrVal: arrVal,159 SlicePtrVal: slicePtrVal,160 SliceMapVal: sliceMapVal,161 SlicePtrMapVal: slicePtrMapVal,162 SliceSliceVal: sliceSliceVal,163 SlicePtrSliceVal: slicePtrSliceVal,164 }165 partOut, err := Select(td, []string{"sliceVal"})166 if assert.NoError(t, err) {167 assert.EqualValues(t, sliceVal, partOut)168 }169 partOut, err = Select(td, []string{"arrVal"})170 if assert.NoError(t, err) {171 assert.EqualValues(t, arrVal, partOut)172 }173 partOut, err = Select(td, []string{"slicePtrVal"})174 if assert.NoError(t, err) {175 assert.EqualValues(t, slicePtrVal, partOut)176 }177 partOut, err = Select(td, []string{"sliceMapVal"})178 if assert.NoError(t, err) {179 assert.EqualValues(t, sliceMapVal, partOut)180 }181 partOut, err = Select(td, []string{"slicePtrMapVal"})182 if assert.NoError(t, err) {183 assert.EqualValues(t, slicePtrMapVal, partOut)184 }185 partOut, err = Select(td, []string{"sliceSliceVal"})186 if assert.NoError(t, err) {187 assert.EqualValues(t, sliceSliceVal, partOut)188 }189 partOut, err = Select(td, []string{"slicePtrSliceVal"})190 if assert.NoError(t, err) {191 assert.EqualValues(t, slicePtrSliceVal, partOut)192 }193}194func TestSelectIntf(t *testing.T) {195 strVal := "strVal1"196 intVal := 5197 arrIntfVal := [2]interface{}{strVal, intVal}198 sliceIntfVal := []interface{}{intVal, strVal}199 mapIntfVal := map[string]interface{}{200 "key1": strVal,201 "key2": intVal,202 }203 mapArrIntfVal := map[string][2]interface{}{204 "key1": arrIntfVal,205 "key2": arrIntfVal,206 }207 mapSliceIntfVal := map[string][]interface{}{208 "key1": sliceIntfVal,209 "key2": sliceIntfVal,210 }211 mapPtrSliceIntfVal := map[string]*[]interface{}{212 "key1": &sliceIntfVal,213 "key2": &sliceIntfVal,214 }215 mapMapIntfVal := map[string]map[string]interface{}{216 "key1": mapIntfVal,217 "key2": mapIntfVal,218 }219 mapPtrMapIntfVal := map[string]*map[string]interface{}{220 "key1": &mapIntfVal,221 "key2": &mapIntfVal,222 }223 td := &TypeDeep{224 IntfVal: strVal,225 ArrIntfVal: arrIntfVal,226 SliceIntfVal: sliceIntfVal,227 MapIntfVal: mapIntfVal,228 PtrMapIntfVal: &mapIntfVal,229 MapArrIntfVal: mapArrIntfVal,230 MapSliceIntfVal: mapSliceIntfVal,231 MapPtrSliceIntfVal: mapPtrSliceIntfVal,232 MapMapIntfVal: mapMapIntfVal,233 MapPtrMapIntfVal: mapPtrMapIntfVal,234 }235 partOut, err := Select(td, []string{"intfVal"})236 if assert.NoError(t, err) {237 assert.EqualValues(t, strVal, partOut)238 }239 partOut, err = Select(td, []string{"arrIntfVal"})240 if assert.NoError(t, err) {241 assert.EqualValues(t, arrIntfVal, partOut)242 }243 partOut, err = Select(td, []string{"mapIntfVal"})244 if assert.NoError(t, err) {245 assert.EqualValues(t, mapIntfVal, partOut)246 }247 partOut, err = Select(td, []string{"ptrMapIntfVal"})248 if assert.NoError(t, err) {249 assert.EqualValues(t, &mapIntfVal, partOut)250 }251 partOut, err = Select(td, []string{"mapSliceIntfVal"})252 if assert.NoError(t, err) {253 assert.EqualValues(t, mapSliceIntfVal, partOut)254 }255 partOut, err = Select(td, []string{"mapPtrSliceIntfVal"})256 if assert.NoError(t, err) {257 assert.EqualValues(t, mapPtrSliceIntfVal, partOut)258 }259 partOut, err = Select(td, []string{"mapMapIntfVal"})260 if assert.NoError(t, err) {261 assert.EqualValues(t, mapMapIntfVal, partOut)262 }263 partOut, err = Select(td, []string{"mapPtrMapIntfVal"})264 if assert.NoError(t, err) {265 assert.EqualValues(t, mapPtrMapIntfVal, partOut)266 }267}268func TestSelectDeep(t *testing.T) {269 strVal1 := "strVal1"270 strVal2 := "strVal2"271 intVal := 5272 mapVal := map[string]string{273 "key1": strVal1,274 "key2": strVal2,275 }276 sliceVal := []string{strVal1, strVal2}277 td1 := TypeDeep{278 StrVal: strVal1,279 IntVal: intVal,280 MapVal: mapVal,281 PtrMapVal: &mapVal,282 SliceVal: sliceVal,283 }284 sliceDeep := []TypeDeep{td1, td1}285 arrDeep := [2]TypeDeep{td1, td1}286 slicePtrDeep := []*TypeDeep{&td1, &td1}287 arrPtrDeep := [2]*TypeDeep{&td1, &td1}288 mapDeep := map[string]TypeDeep{289 "key1": td1,290 "key2": td1,291 }292 mapPtrDeep := map[string]*TypeDeep{293 "key1": &td1,294 "key2": &td1,295 }296 mapArrDeep := map[string][2]TypeDeep{297 "key1": arrDeep,298 "key2": arrDeep,299 }300 mapSlicePtrDeep := map[string][]*TypeDeep{301 "key1": slicePtrDeep,302 "key2": slicePtrDeep,303 }304 mapMapDeep := map[string]map[string]TypeDeep{305 "key1": mapDeep,306 "key2": mapDeep,307 }308 mapPtrMapDeep := map[string]*map[string]TypeDeep{309 "key1": &mapDeep,310 "key2": &mapDeep,311 }312 mapMapPtrDeep := map[string]map[string]*TypeDeep{313 "key1": mapPtrDeep,314 "key2": mapPtrDeep,315 }316 td := &TypeDeep{317 PtrDeep: &td1,318 SliceDeep: sliceDeep,319 SlicePtrDeep: slicePtrDeep,320 ArrPtrDeep: arrPtrDeep,321 MapDeep: mapDeep,322 MapPtrDeep: mapPtrDeep,323 MapArrDeep: mapArrDeep,324 MapSlicePtrDeep: mapSlicePtrDeep,325 MapMapDeep: mapMapDeep,326 MapPtrMapDeep: mapPtrMapDeep,327 MapMapPtrDeep: mapMapPtrDeep,328 }329 partOut, err := Select(td, []string{"ptrDeep"})330 if assert.NoError(t, err) {331 assert.EqualValues(t, &td1, partOut)332 }333 partOut, err = Select(td, []string{"sliceDeep"})334 if assert.NoError(t, err) {335 assert.EqualValues(t, sliceDeep, partOut)336 }337 partOut, err = Select(td, []string{"slicePtrDeep"})338 if assert.NoError(t, err) {339 assert.EqualValues(t, slicePtrDeep, partOut)340 }341 partOut, err = Select(td, []string{"arrPtrDeep"})342 if assert.NoError(t, err) {343 assert.EqualValues(t, arrPtrDeep, partOut)344 }345 partOut, err = Select(td, []string{"mapDeep"})346 if assert.NoError(t, err) {347 assert.EqualValues(t, mapDeep, partOut)348 }349 partOut, err = Select(td, []string{"mapPtrDeep"})350 if assert.NoError(t, err) {351 assert.EqualValues(t, mapPtrDeep, partOut)352 }353 partOut, err = Select(td, []string{"mapArrDeep"})354 if assert.NoError(t, err) {355 assert.EqualValues(t, mapArrDeep, partOut)356 }357 partOut, err = Select(td, []string{"mapSlicePtrDeep"})358 if assert.NoError(t, err) {359 assert.EqualValues(t, mapSlicePtrDeep, partOut)360 }361 partOut, err = Select(td, []string{"mapMapDeep"})362 if assert.NoError(t, err) {363 assert.EqualValues(t, mapMapDeep, partOut)364 }365 partOut, err = Select(td, []string{"mapMapPtrDeep"})366 if assert.NoError(t, err) {367 assert.EqualValues(t, mapMapPtrDeep, partOut)368 }369 // Now traverse one level.370 partOut, err = Select(td, []string{"ptrDeep", "strVal"})371 if assert.NoError(t, err) {372 assert.EqualValues(t, td.PtrDeep.StrVal, partOut)373 }374 partOut, err = Select(td, []string{"sliceDeep", "0"})375 if assert.NoError(t, err) {376 assert.EqualValues(t, td1, partOut)377 }378 partOut, err = Select(td, []string{"slicePtrDeep", "1"})379 if assert.NoError(t, err) {380 assert.EqualValues(t, &td1, partOut)381 }382 partOut, err = Select(td, []string{"arrPtrDeep", "0"})383 if assert.NoError(t, err) {384 assert.EqualValues(t, &td1, partOut)385 }386 partOut, err = Select(td, []string{"mapDeep", "key1"})387 if assert.NoError(t, err) {388 assert.EqualValues(t, td1, partOut)389 }390 partOut, err = Select(td, []string{"mapPtrDeep", "key1"})391 if assert.NoError(t, err) {392 assert.EqualValues(t, &td1, partOut)393 }394 partOut, err = Select(td, []string{"mapArrDeep", "key1"})395 if assert.NoError(t, err) {396 assert.EqualValues(t, arrDeep, partOut)397 }398 partOut, err = Select(td, []string{"mapSlicePtrDeep", "key1"})399 if assert.NoError(t, err) {400 assert.EqualValues(t, slicePtrDeep, partOut)401 }402 partOut, err = Select(td, []string{"mapMapDeep", "key1"})403 if assert.NoError(t, err) {404 assert.EqualValues(t, mapDeep, partOut)405 }406 partOut, err = Select(td, []string{"mapMapPtrDeep", "key1"})407 if assert.NoError(t, err) {408 assert.EqualValues(t, mapPtrDeep, partOut)409 }410 // Now traverse two levels.411 partOut, err = Select(td, []string{"sliceDeep", "0", "strVal"})412 if assert.NoError(t, err) {413 assert.EqualValues(t, td1.StrVal, partOut)414 }415 partOut, err = Select(td, []string{"slicePtrDeep", "1", "intVal"})416 if assert.NoError(t, err) {417 assert.EqualValues(t, td1.IntVal, partOut)418 }419 partOut, err = Select(td, []string{"arrPtrDeep", "0", "intVal"})420 if assert.NoError(t, err) {421 assert.EqualValues(t, td1.IntVal, partOut)422 }423 partOut, err = Select(td, []string{"mapDeep", "key1", "intVal"})424 if assert.NoError(t, err) {425 assert.EqualValues(t, td1.IntVal, partOut)426 }427 partOut, err = Select(td, []string{"mapPtrDeep", "key1", "intVal"})428 if assert.NoError(t, err) {429 assert.EqualValues(t, td1.IntVal, partOut)430 }431 partOut, err = Select(td, []string{"mapArrDeep", "key1", "0"})432 if assert.NoError(t, err) {433 assert.EqualValues(t, td1, partOut)434 }435 partOut, err = Select(td, []string{"mapSlicePtrDeep", "key1", "0"})436 if assert.NoError(t, err) {437 assert.EqualValues(t, &td1, partOut)438 }439 partOut, err = Select(td, []string{"mapMapDeep", "key1", "key2"})440 if assert.NoError(t, err) {441 assert.EqualValues(t, td1, partOut)442 }443 partOut, err = Select(td, []string{"mapMapPtrDeep", "key1", "key2"})444 if assert.NoError(t, err) {445 assert.EqualValues(t, &td1, partOut)446 }447 // Now traverse three levels.448 partOut, err = Select(td, []string{"sliceDeep", "0", "mapVal", "key1"})449 if assert.NoError(t, err) {450 assert.EqualValues(t, strVal1, partOut)451 }452 partOut, err = Select(td, []string{"slicePtrDeep", "1", "mapVal", "key1"})453 if assert.NoError(t, err) {454 assert.EqualValues(t, strVal1, partOut)455 }456 partOut, err = Select(td, []string{"arrPtrDeep", "0", "mapVal", "key1"})457 if assert.NoError(t, err) {458 assert.EqualValues(t, strVal1, partOut)459 }460 partOut, err = Select(td, []string{"mapDeep", "key1", "mapVal", "key2"})461 if assert.NoError(t, err) {462 assert.EqualValues(t, strVal2, partOut)463 }464 partOut, err = Select(td, []string{"mapPtrDeep", "key1", "mapVal", "key1"})465 if assert.NoError(t, err) {466 assert.EqualValues(t, strVal1, partOut)467 }468 partOut, err = Select(td, []string{"mapArrDeep", "key1", "0", "strVal"})469 if assert.NoError(t, err) {470 assert.EqualValues(t, strVal1, partOut)471 }472 partOut, err = Select(td, []string{"mapSlicePtrDeep", "key1", "0", "intVal"})473 if assert.NoError(t, err) {474 assert.EqualValues(t, intVal, partOut)475 }476 partOut, err = Select(td, []string{"mapMapDeep", "key1", "key2", "strVal"})477 if assert.NoError(t, err) {478 assert.EqualValues(t, strVal1, partOut)479 }480 partOut, err = Select(td, []string{"mapMapPtrDeep", "key1", "key2", "strVal"})481 if assert.NoError(t, err) {482 assert.EqualValues(t, strVal1, partOut)483 }484}485func TestSelectFails(t *testing.T) {486 strVal1 := "strVal1"487 strVal2 := "strVal2"488 mapVal := map[string]string{489 "key1": strVal1,490 "key2": strVal2,491 }492 mapPtrVal := map[string]*string{493 "key1": &strVal1,494 "key2": &strVal2,495 }496 mapMapVal := map[string]map[string]string{497 "key1": mapVal,498 "key2": mapVal,499 }500 mapPtrMapVal := map[string]*map[string]string{501 "key1": &mapVal,502 "key2": &mapVal,503 }504 sliceVal := []string{strVal1, strVal2}505 mapSliceVal := map[string][]string{506 "key1": sliceVal,507 "key2": sliceVal,508 }509 arrVal := [2]string{strVal1, strVal2}510 mapArrVal := map[string][2]string{511 "key1": arrVal,512 "key2": arrVal,513 }514 mapPtrSliceVal := map[string]*[]string{515 "key1": &sliceVal,516 "key2": &sliceVal,517 }518 slicePtrVal := []*string{&strVal1, &strVal2}519 sliceMapVal := []map[string]string{mapVal, mapVal}520 slicePtrMapVal := []*map[string]string{&mapVal, &mapVal}521 sliceSliceVal := [][]string{sliceVal, sliceVal}522 slicePtrSliceVal := []*[]string{&sliceVal, &sliceVal}523 td := &TypeDeep{524 StrVal: strVal1,525 IntVal: 5,526 SliceVal: sliceVal,527 ArrVal: arrVal,528 SlicePtrVal: slicePtrVal,529 SliceMapVal: sliceMapVal,530 SlicePtrMapVal: slicePtrMapVal,531 SliceSliceVal: sliceSliceVal,532 SlicePtrSliceVal: slicePtrSliceVal,533 MapVal: mapVal,534 PtrMapVal: &mapVal,535 MapPtrVal: mapPtrVal,536 MapMapVal: mapMapVal,537 MapPtrMapVal: mapPtrMapVal,538 MapSliceVal: mapSliceVal,539 MapArrVal: mapArrVal,540 MapPtrSliceVal: mapPtrSliceVal,541 }542 _, err := Select(td, []string{"bogus"})543 if assert.Error(t, err) {544 assert.IsType(t, &KeyNotFoundError{}, err)545 assert.EqualError(t, err, "key not found: bogus")546 assert.Equal(t, "bogus", err.(*KeyNotFoundError).Key())547 }548 _, err = Select(td, []string{"strVal", "dummy"})549 if assert.Error(t, err) {550 assert.IsType(t, &KeyNotTraversableError{}, err)551 assert.EqualError(t, err, "key not traversable: dummy")552 assert.Equal(t, "dummy", err.(*KeyNotTraversableError).Key())553 }554 _, err = Select(td, []string{"sliceVal", "x"})555 if assert.Error(t, err) {556 assert.IsType(t, &KeyNotFoundError{}, err)557 }558 _, err = Select(td, []string{"sliceVal", "10"})559 if assert.Error(t, err) {560 assert.IsType(t, &KeyNotFoundError{}, err)561 }562 _, err = Select(td, []string{"sliceMapVal", "0", "dummy"})563 if assert.Error(t, err) {564 assert.IsType(t, &KeyNotFoundError{}, err)565 }566 _, err = Select(td, []string{"sliceSliceVal", "0", "x"})567 if assert.Error(t, err) {568 assert.IsType(t, &KeyNotFoundError{}, err)569 }570 _, err = Select(td, []string{"sliceSliceVal", "0", "10"})571 if assert.Error(t, err) {572 assert.IsType(t, &KeyNotFoundError{}, err)573 }574 _, err = Select(td, []string{"mapMapVal", "key1", "x"})575 if assert.Error(t, err) {576 assert.IsType(t, &KeyNotFoundError{}, err)577 }578 _, err = Select(td, []string{"mapMapVal", "key1", "key1", "strVal", "dummy"})579 if assert.Error(t, err) {580 assert.IsType(t, &KeyNotTraversableError{}, err)581 }582 _, err = Select(td, []string{"mapSliceVal", "key1", "x"})583 if assert.Error(t, err) {584 assert.IsType(t, &KeyNotFoundError{}, err)585 }586 _, err = Select(td, []string{"mapSliceVal", "key1", "10"})587 if assert.Error(t, err) {588 assert.IsType(t, &KeyNotFoundError{}, err)589 }590 _, err = Select(td, []string{"ptrDeep", "strVal"})591 if assert.Error(t, err) {592 assert.IsType(t, &KeyNotFoundError{}, err)593 }594}...

Full Screen

Full Screen

gitignore_test.go

Source:gitignore_test.go Github

copy

Full Screen

2import "testing"3func TestGitIgnore(t *testing.T) {4 testdata := []struct {5 givePath string6 giveSlice []string7 wontSlice []string8 }{9 {"testdata/ignores", []string{"ignore.test", "notIgnore.txt"}, []string{"notIgnore.txt"}},10 {"testdata/archives", []string{"ignore.test", "notIgnore.txt"}, []string{"notIgnore.txt", "ignore.test"}}, // no ignore11 }12 for _, td := range testdata {13 ig := newIgnore(td.givePath)14 gotSlice := ig.Filter(td.giveSlice)15 if len(gotSlice) != len(td.wontSlice) {16 t.Errorf("got slice length did not match: wont %d, got %d", len(td.wontSlice), len(gotSlice))17 }18 for _, gotItem := range gotSlice {19 found := false20 for _, wontItem := range td.wontSlice {21 if gotItem == wontItem {22 found = true23 break24 }25 }26 if !found {27 t.Errorf("gotItem: %s not found in %v", gotItem, td.wontSlice)28 break29 }30 }31 }32}...

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 s := []int{2, 3, 5, 7, 11, 13}4 fmt.Println("s ==", s)5 for i := 0; i < len(s); i++ {6 fmt.Printf("s[%d] == %d\n", i, s[i])7 }8 fmt.Println("s[1:4] ==", s[1:4])9 fmt.Println("s[:3] ==", s[:3])10 fmt.Println("s[4:] ==", s[4:])11 s = append(s, 17)12 fmt.Println("s ==", s)13 s = append(s, 19, 23, 29)14 fmt.Println("s ==", s)15 c := make([]int, len(s))16 copy(c, s)17 fmt.Println("c ==", c)18 fmt.Println("l ==", l)19 fmt.Println("l ==", l)20}21import "fmt"22type Vertex struct {23}24var m = map[string]Vertex{25 "Bell Labs": Vertex{26 },27 "Google": Vertex{28 },29}30func main()

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := colly.NewCollector()4 c.OnHTML("td", func(e *colly.HTMLElement) {5 fmt.Println(e.Text)6 })7}

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 data := [][]string{4 []string{"1", "2", "3", "4", "5"},5 []string{"1", "2", "3", "4", "5"},6 []string{"1", "2", "3", "4", "5"},7 []string{"1", "2", "3", "4", "5"},8 []string{"1", "2", "3", "4", "5"},9 }10 table := tablewriter.NewWriter(os.Stdout)11 table.SetHeader([]string{"1", "2", "3", "4", "5"})

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}4 fmt.Println(b)5}6import "fmt"7func main() {8 a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}9 fmt.Println(b)10 b = append(b, 11, 12, 13)11 fmt.Println(b)12 fmt.Println(a)13}14import "fmt"15func main() {16 a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}17 fmt.Println(b)18 b = append(b, 11, 12, 13)19 fmt.Println(b)20 fmt.Println(a)21 c := append(b, 14, 15, 16)22 fmt.Println(c)23 fmt.Println(b)24 fmt.Println(a)25}26import "fmt"27func main() {28 a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}29 fmt.Println(b)30 b = append(b, 11, 12, 13)31 fmt.Println(b)32 fmt.Println(a)33 c := append(b, 14, 15, 16)34 fmt.Println(c)35 fmt.Println(b)36 fmt.Println(a)37 d := append(c, 17, 18, 19)38 fmt.Println(d)39 fmt.Println(c)40 fmt.Println(b)41 fmt.Println(a)42}43import "fmt"44func main() {

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 td := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}4 fmt.Println("Before: ", td)5 fmt.Println("After: ", floats.Slice(td, 2, 5))6}7import (8func main() {9 td := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}10 fmt.Println("Before: ", td)11 fmt.Println("After: ", floats.SliceTrunc(td, 2, 5))12}13import (14func main() {15 td := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}16 fmt.Println("Before: ", td)17 fmt.Println("After: ", floats.SliceGrow(td, 2, 5))18}19import (20func main() {21 td := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}22 fmt.Println("Before:

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 td := []int{1,2,3,4,5,6,7,8,9,10}4 fmt.Println(td)5 fmt.Println(td[1:4])6 fmt.Println(td[:3])7 fmt.Println(td[4:])8 fmt.Println(td[:])9}10import "fmt"11func main() {12 td := map[string]int{"a":1, "b":2, "c":3}13 fmt.Println(td)14 fmt.Println(td["a"])15 fmt.Println(td["b"])16 fmt.Println(td["c"])17}18import "fmt"19type td struct {20}21func main() {22 td1 := td{a:1, b:"a"}23 fmt.Println(td1)24 fmt.Println(td1.a)25 fmt.Println(td1.b)26}27{1 a}28import "fmt"29type td interface {30 td1()31}32type td2 struct {33}34func (td2) td1() {35 fmt.Println("td1")36}37func main() {38 td = td2{a:1}39 td.td1()40}41import "fmt"42type td struct {43}44func (td) td1() {45 fmt.Println("td1")46}47func main() {48 td1 := td{a:1}49 td1.td1()50}

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 a := td.New(1, 2, 3, 4, 5)4 fmt.Println("a = ", a)5 fmt.Println("a[1:3] = ", a[1:3])6 fmt.Println("a[1:] = ", a[1:])7 fmt.Println("a[:3] = ", a[:3])8}9import (10func main() {11 a := td.New(1, 2, 3, 4, 5)12 fmt.Println("a = ", a)13 fmt.Println("a[1:3] = ", a[1:3])14 fmt.Println("a[1:] = ", a[1:])15 fmt.Println("a[:3] = ", a[:3])16 fmt.Println("a[1:3] = ", a[1:3])17 fmt.Println("a[1:3] = ", a[1:3])18}19import (20func main() {21 a := td.New(1, 2, 3, 4, 5)22 fmt.Println("a = ", a)23 fmt.Println("a[1:3] = ", a[1:3])24 fmt.Println("a[1:] = ", a[1:])25 fmt.Println("a[:3] = ", a[:3])26 fmt.Println("a[1:3] = ", a[1:3])27 fmt.Println("a[1:3] = ", a[1:3])28 fmt.Println("a[1:3] = ", a[1:3])29}30import (31func main() {32 a := td.New(1, 2, 3, 4, 5)33 fmt.Println("a = ", a)34 fmt.Println("a[1:3] = ", a[1:3])35 fmt.Println("a[1:] = ", a[1:])36 fmt.Println("a[:3] = ", a[:3])37 fmt.Println("a[1:3] = ",

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3for i := 0; i < 5; i++ {4}5fmt.Println("a = ", a)6}7import "fmt"8func main() {9a := []int{1, 2, 3, 4, 5}10fmt.Println("a = ", a)11fmt.Println("b = ", b)12}13import "fmt"14func main() {15a := [5]int{1, 2, 3, 4, 5}16fmt.Println("a = ", a)17fmt.Println("b = ", b)18}

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t := td.New()4 s := []int{1, 2, 3}5 t.Set("s", s)6 fmt.Println(t.Get("s").Slice())7}8import (9func main() {10 t := td.New()11 t.Set("s", []int{1, 2, 3})12 fmt.Println(t.Get("s").Slice())13}14import (15func main() {16 t := td.New()17 t.Set("s", []int{1, 2, 3})18 fmt.Println(t.Get("s").Slice())19}20import (21func main() {22 t := td.New()23 t.Set("s", []int{1, 2, 3})24 fmt.Println(t.Get("s").Slice())25}26import (27func main() {28 t := td.New()

Full Screen

Full Screen

Slice

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 a = append(a, 1)4 a = append(a, 2)5 a = append(a, 3)6 a = append(a, 4)7 a = append(a, 5)8 fmt.Println(a)9}10import (11func main() {12 a = append(a, 1)13 a = append(a, 2)14 a = append(a, 3)15 a = append(a, 4)16 a = append(a, 5)17 fmt.Println(a)18 fmt.Println(len(a))19 fmt.Println(cap(a))20}21import (22func main() {23 a = append(a, 1)24 a = append(a, 2)25 a = append(a, 3)26 a = append(a, 4)27 a = append(a, 5)28 fmt.Println(a)29 fmt.Println(len(a))30 fmt.Println(cap(a))31 fmt.Println(a[1:4])32 fmt.Println(a[1:5])33 fmt.Println(a[:3])34 fmt.Println(a[2:])35}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Go-testdeep automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful