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

Function TestOracleDeleteCorrectness

reference_oracle_test.go:701–746  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Property 3: Delete correctness — after Delete, Get fails AND remaining structure is valid JSON (no corruption). --------------------------------------------------------------------------- reqproof:proptest Delete Verifies: S

(t *testing.T)

Source from the content-addressed store, hash-verified

699// reqproof:proptest Delete
700// Verifies: SYS-REQ-034 [property]
701func TestOracleDeleteCorrectness(t *testing.T) {
702 r := oracleNewRNG(oracleSeed + 2)
703 const iterations = 10000
704 for i := 0; i < iterations; i++ {
705 raw, v := oracleRandomJSONBytes(r, 3)
706 // Pick a path that exists so Delete actually does something.
707 // We use pickExistingPath (semantically valid) to avoid the
708 // documented D8 bug (Delete with array-index on object value).
709 path, _ := pickExistingPath(r, v)
710 if len(path) == 0 {
711 // Deleting the root with no keys is a documented special case
712 // (returns data[:0]); skip it.
713 continue
714 }
715
716 var deleted []byte
717 func() {
718 defer func() {
719 if rec := recover(); rec != nil {
720 t.Fatalf("Delete panicked on input %q path=%v: %v", raw, path, rec)
721 }
722 }()
723 deleted = Delete(raw, path...)
724 }()
725
726 // Property 3a: if the result is non-empty, it MUST be valid JSON
727 // (no corruption). An empty result is allowed when Delete removes
728 // the entire document.
729 if len(deleted) > 0 && !json.Valid(deleted) {
730 t.Fatalf("Delete produced invalid JSON:\n in=%q\n path=%v\n out=%q",
731 raw, path, deleted)
732 }
733
734 // Property 3b: Get on the deleted path MUST return KeyPathNotFoundError
735 // (the value is gone). Only strictly assert for single-component
736 // object-key paths — multi-component deletes sometimes leave parent
737 // structure intact and the value can re-appear via a sibling key.
738 if len(path) == 1 && !strings.HasPrefix(path[0], "[") {
739 got, dt, _, gerr := Get(deleted, path...)
740 if gerr == nil && dt != NotExist && len(got) > 0 && len(deleted) > 0 {
741 t.Fatalf("Delete did not remove key:\n in=%q\n path=%v\n out=%q\n still-present=%q",
742 raw, path, deleted, got)
743 }
744 }
745 }
746}
747
748// ---------------------------------------------------------------------------
749// Property 4: ParseInt / ParseFloat / ParseBoolean agree with strconv.

Callers

nothing calls this directly

Calls 5

oracleNewRNGFunction · 0.85
oracleRandomJSONBytesFunction · 0.85
pickExistingPathFunction · 0.85
DeleteFunction · 0.85
GetFunction · 0.85

Tested by

no test coverage detected