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

Function TestUnescape

escape_test.go:158–201  ·  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

156// Verifies: SYS-REQ-014 [malformed]
157// MCDC SYS-REQ-014: N/A
158func TestUnescape(t *testing.T) {
159 for _, test := range unescapeTests {
160 type bufferTestCase struct {
161 buf []byte
162 isTooSmall bool
163 }
164
165 var bufs []bufferTestCase
166
167 if len(test.in) == 0 {
168 // If the input string is length 0, only a buffer of size 0 is a meaningful test
169 bufs = []bufferTestCase{{nil, false}}
170 } else {
171 // For non-empty input strings, we can try several buffer sizes (0, len-1, len)
172 bufs = []bufferTestCase{
173 {nil, true},
174 {make([]byte, 0, len(test.in)-1), true},
175 {make([]byte, 0, len(test.in)), false},
176 }
177 }
178
179 for _, buftest := range bufs {
180 in := []byte(test.in)
181 buf := buftest.buf
182
183 out, err := Unescape(in, buf)
184 isErr := (err != nil)
185 isAlloc := !isSameMemory(out, in) && !isSameMemory(out, buf)
186
187 if isErr != test.isErr {
188 t.Errorf("Unescape(`%s`, bufsize=%d) returned isErr mismatch: expected %t, obtained %t", test.in, cap(buf), test.isErr, isErr)
189 break
190 } else if isErr {
191 continue
192 } else if !bytes.Equal(out, []byte(test.out)) {
193 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))
194 break
195 } else if isAlloc != (test.canAlloc && buftest.isTooSmall) {
196 t.Errorf("Unescape(`%s`, bufsize=%d) returned isAlloc mismatch: expected %t, obtained %t", test.in, cap(buf), buftest.isTooSmall, isAlloc)
197 break
198 }
199 }
200 }
201}

Callers

nothing calls this directly

Calls 2

UnescapeFunction · 0.85
isSameMemoryFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…