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

Function TestEachKeyEmptyKeyPathComponent

empty_key_path_test.go:138–169  ·  view source on GitHub ↗

============================================================================= EachKey — empty key component emits no callback (SYS-REQ-008) ============================================================================= Verifies: SYS-REQ-008 [boundary] An empty-string path component cannot address an

(t *testing.T)

Source from the content-addressed store, hash-verified

136// must skip the path (missing-request => no callback) and must not panic on
137// the `p[level][0]` dereference.
138func TestEachKeyEmptyKeyPathComponent(t *testing.T) {
139 cases := []struct {
140 name string
141 data string
142 paths [][]string
143 }{
144 {name: "single empty path on array root", data: `[1,2,3]`, paths: [][]string{{""}}},
145 {name: "empty path mixed with valid path", data: `{"a":1,"b":2}`, paths: [][]string{{""}, {"a"}}},
146 {name: "empty leading component", data: `{"a":1}`, paths: [][]string{{"", "a"}}},
147 {name: "empty trailing component", data: `{"a":[1]}`, paths: [][]string{{"a", ""}}},
148 }
149 for _, tc := range cases {
150 t.Run(tc.name, func(t *testing.T) {
151 emptyCalled := false
152 runNoPanic(t, tc.name, func() {
153 EachKey([]byte(tc.data), func(idx int, val []byte, dt ValueType, err error) {
154 // Callbacks may fire for the valid path, but the empty path
155 // must never resolve to a real callback slot.
156 if len(tc.paths[idx]) == 0 || tc.paths[idx][0] == "" {
157 emptyCalled = true
158 }
159 }, tc.paths...)
160 })
161 if emptyCalled {
162 t.Fatalf("EachKey(%q,%v) emitted a callback for an empty path component", tc.data, tc.paths)
163 }
164 // No assertion on the valid-path callback: it may or may not fire
165 // depending on the case; the regression gate is "no panic, no
166 // callback for the empty component".
167 })
168 }
169}
170
171// =============================================================================
172// Set — empty key component produces a defined document, no panic (SYS-REQ-009)

Callers

nothing calls this directly

Calls 2

runNoPanicFunction · 0.85
EachKeyFunction · 0.85

Tested by

no test coverage detected