MCPcopy Create free account
hub / github.com/buger/jsonparser / TestApiConsistencyEachKeyMatchesGet

Function TestApiConsistencyEachKeyMatchesGet

property_test.go:470–521  ·  view source on GitHub ↗

Verifies: SYS-REQ-008 reqproof:proptest parser.EachKey, parser.Get

(t *testing.T)

Source from the content-addressed store, hash-verified

468// Verifies: SYS-REQ-008
469// reqproof:proptest parser.EachKey, parser.Get
470func TestApiConsistencyEachKeyMatchesGet(t *testing.T) {
471 r := newRNG(jsonSeed + 17)
472 const iterations = 2000
473
474 for i := 0; i < iterations; i++ {
475 payload := randomJSONValue(r, 3)
476 terminalArray := []interface{}{
477 randomJSONValue(r, 2),
478 randomJSONValue(r, 2),
479 }
480 document := map[string]interface{}{
481 "payload": payload,
482 "terminalArray": terminalArray,
483 }
484 raw, err := json.Marshal(document)
485 if err != nil {
486 t.Fatalf("json.Marshal generated corpus item: %v", err)
487 }
488
489 var path []string
490 if i%4 == 0 {
491 // Guarantee broad coverage of the issue #232 class: an array
492 // index is the terminal path component.
493 path = []string{"terminalArray", fmt.Sprintf("[%d]", r.Intn(len(terminalArray)))}
494 } else {
495 path = append([]string{"payload"}, randomExistingJSONPath(r, payload)...)
496 }
497
498 getValue, getType, _, getErr := Get(raw, path...)
499
500 var eachValue []byte
501 var eachType ValueType
502 var eachErr error
503 callbacks := 0
504 EachKey(raw, func(index int, value []byte, valueType ValueType, err error) {
505 callbacks++
506 if index != 0 {
507 t.Errorf("EachKey callback index = %d, want 0; input=%q path=%v", index, raw, path)
508 }
509 eachValue, eachType, eachErr = value, valueType, err
510 }, path)
511
512 if callbacks != 1 {
513 t.Fatalf("EachKey callback count = %d, want 1; input=%q path=%v Get=(%q,%v,%v)",
514 callbacks, raw, path, getValue, getType, getErr)
515 }
516 if !bytes.Equal(eachValue, getValue) || eachType != getType || eachErr != getErr {
517 t.Fatalf("EachKey and Get disagree; input=%q path=%v EachKey=(%q,%v,%v) Get=(%q,%v,%v)",
518 raw, path, eachValue, eachType, eachErr, getValue, getType, getErr)
519 }
520 }
521}
522
523// ---------------------------------------------------------------------------
524// Property: Set then Get returns the set value (round-trip)

Callers

nothing calls this directly

Calls 5

newRNGFunction · 0.85
randomJSONValueFunction · 0.85
randomExistingJSONPathFunction · 0.85
GetFunction · 0.85
EachKeyFunction · 0.85

Tested by

no test coverage detected