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

Function TestArrayEachErr

array_each_err_test.go:12–130  ·  view source on GitHub ↗

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

(t *testing.T)

Source from the content-addressed store, hash-verified

10// Verifies: SYS-REQ-004 (ArrayEach)
11// reqproof:proptest:skip targeted witness/regression test; not a property-test subject
12func TestArrayEachErr(t *testing.T) {
13 t.Run("normal iteration", func(t *testing.T) {
14 var values []string
15 count, err := ArrayEachErr([]byte(`[1, "two", true, null]`), func(value []byte, dataType ValueType, offset int, err error) error {
16 if err != nil {
17 t.Fatalf("callback error = %v, want nil", err)
18 }
19 values = append(values, string(value))
20 return nil
21 })
22
23 if err != nil {
24 t.Fatalf("ArrayEachErr() error = %v, want nil", err)
25 }
26 if count != 4 {
27 t.Fatalf("ArrayEachErr() count = %d, want 4", count)
28 }
29 if want := []string{"1", "two", "true", "null"}; !reflect.DeepEqual(values, want) {
30 t.Fatalf("ArrayEachErr() values = %q, want %q", values, want)
31 }
32 })
33
34 t.Run("callback error stops iteration", func(t *testing.T) {
35 wantErr := errors.New("stop after third element")
36 var values []string
37 count, err := ArrayEachErr([]byte(`[1, 2, 3, 4, 5]`), func(value []byte, dataType ValueType, offset int, err error) error {
38 values = append(values, string(value))
39 if len(values) == 3 {
40 return wantErr
41 }
42 return nil
43 })
44
45 if !errors.Is(err, wantErr) {
46 t.Fatalf("ArrayEachErr() error = %v, want %v", err, wantErr)
47 }
48 if count != 3 {
49 t.Fatalf("ArrayEachErr() count = %d, want 3", count)
50 }
51 if want := []string{"1", "2", "3"}; !reflect.DeepEqual(values, want) {
52 t.Fatalf("ArrayEachErr() values = %q, want %q", values, want)
53 }
54 })
55
56 t.Run("io EOF stops gracefully", func(t *testing.T) {
57 var values []string
58 count, err := ArrayEachErr([]byte(`[1, 2, 3, 4]`), func(value []byte, dataType ValueType, offset int, err error) error {
59 values = append(values, string(value))
60 if len(values) == 2 {
61 return io.EOF
62 }
63 return nil
64 })
65
66 if err != nil {
67 t.Fatalf("ArrayEachErr() error = %v, want nil", err)
68 }
69 if count != 2 {

Callers

nothing calls this directly

Calls 1

ArrayEachErrFunction · 0.85

Tested by

no test coverage detected