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

Function TestSetAutoCoerce_KI3

set_spec_test.go:118–153  ·  view source on GitHub ↗

TestSetAutoCoerce_KI3 locks the auto-coerce semantics that fix KI-3. When a path component expects one container type but the existing structure is the other, Set replaces the mismatched container with a fresh container of the path's expected type and then performs a normal insertion. Each case asse

(t *testing.T)

Source from the content-addressed store, hash-verified

116//
117// Reproduces: KI-3 (fixed)
118func TestSetAutoCoerce_KI3(t *testing.T) {
119 cases := []struct {
120 name string
121 in string
122 setValue string
123 keys []string
124 getPath []string
125 expected string
126 }{
127 {"object_root_to_array", `{}`, `9`, []string{"[5]"}, []string{"[0]"}, `[9]`},
128 {"array_root_to_object", `[]`, `9`, []string{"key"}, []string{"key"}, `{"key":9}`},
129 {"nested_object_to_array", `{"a":{}}`, `9`, []string{"a", "[5]"}, []string{"a", "[0]"}, `{"a":[9]}`},
130 {"nested_array_to_object", `{"a":[]}`, `9`, []string{"a", "key"}, []string{"a", "key"}, `{"a":{"key":9}}`},
131 }
132 for _, c := range cases {
133 t.Run(c.name, func(t *testing.T) {
134 out, err := Set([]byte(c.in), []byte(c.setValue), c.keys...)
135 if err != nil {
136 t.Fatalf("Set(%s,%v) returned error: %v", c.in, c.keys, err)
137 }
138 if !json.Valid(out) {
139 t.Fatalf("Set(%s,%v) -> %s: invalid JSON (KI-3 malformed-output regression)", c.in, c.keys, string(out))
140 }
141 if string(out) != c.expected {
142 t.Fatalf("Set(%s,%v) = %s; expected %s", c.in, c.keys, string(out), c.expected)
143 }
144 got, _, _, gErr := Get(out, c.getPath...)
145 if gErr != nil {
146 t.Fatalf("Get(%s,%v) returned error: %v", string(out), c.getPath, gErr)
147 }
148 if string(got) != c.setValue {
149 t.Fatalf("Get(%s,%v) = %q; expected %q", string(out), c.getPath, string(got), c.setValue)
150 }
151 })
152 }
153}
154
155// TestDeleteMalformedJSONRegression_SYS_REQ_010 is the Go-level witness for
156// SYS-REQ-010's malformed_input obligation. The four FuzzPathMutation corpus

Callers

nothing calls this directly

Calls 2

SetFunction · 0.85
GetFunction · 0.85

Tested by

no test coverage detected