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

Function TestEachKeyErr

array_each_err_test.go:134–231  ·  view source on GitHub ↗

Verifies: SYS-REQ-008 (EachKey) reqproof:proptest:skip targeted witness/regression test; not a property-test subject

(t *testing.T)

Source from the content-addressed store, hash-verified

132// Verifies: SYS-REQ-008 (EachKey)
133// reqproof:proptest:skip targeted witness/regression test; not a property-test subject
134func TestEachKeyErr(t *testing.T) {
135 t.Run("normal iteration", func(t *testing.T) {
136 data := []byte(`{"first":1,"nested":{"second":"two"},"third":true}`)
137 paths := [][]string{{"first"}, {"nested", "second"}, {"third"}}
138 var indices []int
139 var values []string
140
141 err := EachKeyErr(data, func(idx int, value []byte, vt ValueType, err error) error {
142 indices = append(indices, idx)
143 values = append(values, string(value))
144 return nil
145 }, paths...)
146
147 if err != nil {
148 t.Fatalf("EachKeyErr() error = %v, want nil", err)
149 }
150 if want := []int{0, 1, 2}; !reflect.DeepEqual(indices, want) {
151 t.Fatalf("EachKeyErr() indices = %v, want %v", indices, want)
152 }
153 if want := []string{"1", "two", "true"}; !reflect.DeepEqual(values, want) {
154 t.Fatalf("EachKeyErr() values = %q, want %q", values, want)
155 }
156 })
157
158 t.Run("callback error stops iteration", func(t *testing.T) {
159 wantErr := errors.New("stop after second key")
160 data := []byte(`{"first":1,"second":2,"third":3}`)
161 paths := [][]string{{"first"}, {"second"}, {"third"}}
162 var calls int
163
164 err := EachKeyErr(data, func(idx int, value []byte, vt ValueType, err error) error {
165 calls++
166 if calls == 2 {
167 return wantErr
168 }
169 return nil
170 }, paths...)
171
172 if !errors.Is(err, wantErr) {
173 t.Fatalf("EachKeyErr() error = %v, want %v", err, wantErr)
174 }
175 if calls != 2 {
176 t.Fatalf("EachKeyErr() callbacks = %d, want 2", calls)
177 }
178 })
179
180 t.Run("io EOF stops gracefully", func(t *testing.T) {
181 data := []byte(`{"first":1,"second":2}`)
182 paths := [][]string{{"first"}, {"second"}}
183 var calls int
184
185 err := EachKeyErr(data, func(idx int, value []byte, vt ValueType, err error) error {
186 calls++
187 return io.EOF
188 }, paths...)
189
190 if err != nil {
191 t.Fatalf("EachKeyErr() error = %v, want nil", err)

Callers

nothing calls this directly

Calls 1

EachKeyErrFunction · 0.85

Tested by

no test coverage detected