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

Function TestUnescape

escape_test.go:172–215  ·  view source on GitHub ↗

Verifies: SYS-REQ-014 [malformed] MCDC SYS-REQ-014: N/A

(t *testing.T)

Source from the content-addressed store, hash-verified

170// Verifies: SYS-REQ-014 [malformed]
171// MCDC SYS-REQ-014: N/A
172func TestUnescape(t *testing.T) {
173 for _, test := range unescapeTests {
174 type bufferTestCase struct {
175 buf []byte
176 isTooSmall bool
177 }
178
179 var bufs []bufferTestCase
180
181 if len(test.in) == 0 {
182 // If the input string is length 0, only a buffer of size 0 is a meaningful test
183 bufs = []bufferTestCase{{nil, false}}
184 } else {
185 // For non-empty input strings, we can try several buffer sizes (0, len-1, len)
186 bufs = []bufferTestCase{
187 {nil, true},
188 {make([]byte, 0, len(test.in)-1), true},
189 {make([]byte, 0, len(test.in)), false},
190 }
191 }
192
193 for _, buftest := range bufs {
194 in := []byte(test.in)
195 buf := buftest.buf
196
197 out, err := Unescape(in, buf)
198 isErr := (err != nil)
199 isAlloc := !isSameMemory(out, in) && !isSameMemory(out, buf)
200
201 if isErr != test.isErr {
202 t.Errorf("Unescape(`%s`, bufsize=%d) returned isErr mismatch: expected %t, obtained %t", test.in, cap(buf), test.isErr, isErr)
203 break
204 } else if isErr {
205 continue
206 } else if !bytes.Equal(out, []byte(test.out)) {
207 t.Errorf("Unescape(`%s`, bufsize=%d) returned unescaped mismatch: expected `%s` (%v, len %d), obtained `%s` (%v, len %d)", test.in, cap(buf), test.out, []byte(test.out), len(test.out), string(out), out, len(out))
208 break
209 } else if isAlloc != (test.canAlloc && buftest.isTooSmall) {
210 t.Errorf("Unescape(`%s`, bufsize=%d) returned isAlloc mismatch: expected %t, obtained %t", test.in, cap(buf), buftest.isTooSmall, isAlloc)
211 break
212 }
213 }
214 }
215}
216
217// =============================================================================
218// Lone-Unicode-surrogate regression tests (DEFECT-260727-SNGT).

Callers

nothing calls this directly

Calls 2

UnescapeFunction · 0.85
isSameMemoryFunction · 0.85

Tested by

no test coverage detected