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

Function TestEachKeyMultiPathInvariants

path_fuzz_test.go:780–821  ·  view source on GitHub ↗

TestEachKeyMultiPathInvariants closes the EachKey multi-path blind spot deterministically. EachKey's purpose is to resolve MULTIPLE key paths in one traversal; the existing fuzz harnesses test only single-path. This test verifies the multi-path callback contract: each path that matches fires the cal

(t *testing.T)

Source from the content-addressed store, hash-verified

778//
779// Verifies: SYS-REQ-008 (EachKey multi-path traversal correctness).
780func TestEachKeyMultiPathInvariants(t *testing.T) {
781 data := []byte(`{"a":1,"b":2,"nested":{"x":10,"y":20},"arr":[100,200,300]}`)
782 paths := [][]string{
783 {"a"},
784 {"b"},
785 {"missing_top"},
786 {"nested", "x"},
787 {"nested", "missing_nested"},
788 {"nested", "y"},
789 {"arr", "[1]"},
790 }
791 matched := make(map[int]int) // idx → number of times callback fired
792 callbackCount := 0
793 EachKey(data, func(idx int, value []byte, vt ValueType, err error) {
794 matched[idx]++
795 callbackCount++
796 // No-error invariant on matched paths.
797 if err != nil {
798 t.Errorf("EachKey callback for path %d returned error %v (data=%q)",
799 idx, err, data)
800 }
801 if len(value) == 0 {
802 t.Errorf("EachKey callback for path %d returned empty value (data=%q)",
803 idx, data)
804 }
805 }, paths...)
806
807 // Each matching path should fire at most once. (We can't assert "exactly
808 // once" without knowing which paths matched, but we CAN assert no path
809 // fires more than once — that would be a state-leakage bug.)
810 for idx, count := range matched {
811 if count > 1 {
812 t.Errorf("EachKey path %d fired %d times (expected at most 1): data=%q paths=%v",
813 idx, count, data, paths)
814 }
815 }
816 // Sanity: at least the trivially-matching paths must have fired.
817 if callbackCount == 0 {
818 t.Errorf("EachKey multi-path: callback never fired despite some paths matching: data=%q paths=%v",
819 data, paths)
820 }
821}

Callers

nothing calls this directly

Calls 1

EachKeyFunction · 0.85

Tested by

no test coverage detected