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

Function TestOracleSetPr286Regression

reference_oracle_test.go:619–692  ·  view source on GitHub ↗

TestOracleSetPr286Regression is the explicit regression case for PR #286. The bug: Set([1,2], 9, "[5]") silently produced [9] (data loss). Correct behavior: either Set returns KeyPathNotFoundError (out-of-range index) WITHOUT modifying the input, or Set succeeds and Get("[5]") returns 9. Top-level

(t *testing.T)

Source from the content-addressed store, hash-verified

617// reqproof:proptest Set
618// Verifies: SYS-REQ-009 [property]
619func TestOracleSetPr286Regression(t *testing.T) {
620 cases := []struct {
621 name string
622 doc string
623 val string
624 path []string
625 fixed bool // true if this case is asserted strictly
626 knownBug string // non-empty if this case documents an open bug
627 }{
628 {"pr286-top-level-oob", `[1,2]`, `9`, []string{"[5]"}, true, ""},
629 {"pr286-top-level-far", `[1,2,3]`, `9`, []string{"[99]"}, true, ""},
630 {"pr286-top-level-len", `[1,2,3]`, `9`, []string{"[3]"}, true, ""},
631 {"pr286-empty-array-index", `[]`, `9`, []string{"[0]"}, true, ""},
632 // STILL-OPEN BUG: Set with nested OOB array index silently destroys
633 // the parent array. See divergence D7 in the file header.
634 {"pr286-nested-oob-OPEN-BUG", `{"a":[1,2]}`, `9`, []string{"a", "[99]"},
635 false, "Set({\"a\":[1,2]}, 9, \"a\", \"[99]\") returns {\"a\":[9]} (data loss)"},
636 }
637 for _, tc := range cases {
638 t.Run(tc.name, func(t *testing.T) {
639 doc := []byte(tc.doc)
640 var out []byte
641 var err error
642 func() {
643 defer func() {
644 if rec := recover(); rec != nil {
645 t.Fatalf("Set panicked: in=%q path=%v val=%q: %v", doc, tc.path, tc.val, rec)
646 }
647 }()
648 out, err = Set(doc, []byte(tc.val), tc.path...)
649 }()
650
651 if err != nil {
652 // Set rejected — contract satisfied. Verify original is intact.
653 if !json.Valid(doc) {
654 t.Fatalf("Set rejection corrupted original: doc=%q", doc)
655 }
656 if out != nil && !bytes.Equal(out, doc) {
657 t.Fatalf("Set rejected but mutated bytes: in=%q out=%q", doc, out)
658 }
659 return
660 }
661 // Set succeeded — output MUST be valid JSON.
662 if !json.Valid(out) {
663 t.Fatalf("Set produced invalid JSON: in=%q path=%v val=%q out=%q",
664 doc, tc.path, tc.val, out)
665 }
666
667 // THE PR #286 INVARIANT: Get on the same path MUST return the
668 // set value. If it doesn't, Set silently lost data.
669 got, _, _, gerr := Get(out, tc.path...)
670 if gerr != nil {
671 if tc.knownBug != "" {
672 t.Logf("KNOWN BUG (open): %s. Path=%v, in=%q, out=%q. "+
673 "When this bug is fixed, replace this t.Logf with t.Fatalf.",
674 tc.knownBug, tc.path, doc, out)
675 return
676 }

Callers

nothing calls this directly

Calls 2

SetFunction · 0.85
GetFunction · 0.85

Tested by

no test coverage detected