MCPcopy
hub / github.com/buger/jsonparser / TestInternalSearchHelperEdges

Function TestInternalSearchHelperEdges

mcdc_supplement_test.go:14–90  ·  view source on GitHub ↗

Verifies: STK-REQ-001 [malformed] MCDC STK-REQ-001: N/A Verifies: STK-REQ-005 [malformed] MCDC STK-REQ-005: N/A

(t *testing.T)

Source from the content-addressed store, hash-verified

12// Verifies: STK-REQ-005 [malformed]
13// MCDC STK-REQ-005: N/A
14func TestInternalSearchHelperEdges(t *testing.T) {
15 if got := findTokenStart(nil, ','); got != 0 {
16 t.Fatalf("findTokenStart(nil, ',') = %d, want 0", got)
17 }
18 if got := lastToken(nil); got != -1 {
19 t.Fatalf("lastToken(nil) = %d, want -1", got)
20 }
21
22 t.Run("findKeyStart", func(t *testing.T) {
23 cases := []struct {
24 name string
25 data string
26 key string
27 wantFound bool
28 wantErr error
29 }{
30 {name: "whitespace only", data: " \n\t", key: "a", wantErr: KeyPathNotFoundError},
31 {name: "array root branch is tolerated", data: `[{"a":1}]`, key: "a", wantErr: KeyPathNotFoundError},
32 {name: "escaped key is decoded", data: `{"a\nb":1}`, key: "a\nb", wantFound: true},
33 {name: "malformed escaped key is rejected", data: `{"\uD800":1}`, key: "x", wantErr: KeyPathNotFoundError},
34 {name: "missing value after key", data: `{"a"`, key: "a", wantErr: KeyPathNotFoundError},
35 {name: "requested key has malformed escape", data: `{"a":1}`, key: `\uD800`, wantErr: KeyPathNotFoundError},
36 {name: "unterminated key string", data: `{"a`, key: "a", wantErr: KeyPathNotFoundError},
37 }
38
39 for _, tc := range cases {
40 t.Run(tc.name, func(t *testing.T) {
41 offset, err := findKeyStart([]byte(tc.data), tc.key)
42 if tc.wantErr != nil {
43 if !errors.Is(err, tc.wantErr) {
44 t.Fatalf("findKeyStart(%q, %q) error = %v, want %v", tc.data, tc.key, err, tc.wantErr)
45 }
46 if offset != -1 {
47 t.Fatalf("findKeyStart(%q, %q) offset = %d, want -1", tc.data, tc.key, offset)
48 }
49 return
50 }
51
52 if err != nil {
53 t.Fatalf("findKeyStart(%q, %q) returned error: %v", tc.data, tc.key, err)
54 }
55 if !tc.wantFound || offset < 0 {
56 t.Fatalf("findKeyStart(%q, %q) offset = %d, want found offset", tc.data, tc.key, offset)
57 }
58 })
59 }
60 })
61
62 t.Run("searchKeys", func(t *testing.T) {
63 cases := []struct {
64 name string
65 data string
66 keys []string
67 want bool
68 }{
69 {name: "missing value after key", data: `{"a"`, keys: []string{"a"}, want: false},
70 {name: "malformed escaped key", data: `{"\uD800":1}`, keys: []string{"x"}, want: false},
71 {name: "nested mismatch still finds later path", data: `{"x":{"b":1},"a":{"b":2}}`, keys: []string{"a", "b"}, want: true},

Callers

nothing calls this directly

Calls 4

findTokenStartFunction · 0.85
lastTokenFunction · 0.85
findKeyStartFunction · 0.85
searchKeysFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…