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

Function TestParseStringEncodingSafetyRoundtrip

obligation_property_test.go:509–550  ·  view source on GitHub ↗

Verifies: SYS-REQ-108 MCDC SYS-REQ-108: parsestring_input_has_standard_escapes=T, parsestring_roundtrip_preserves_semantics=T => TRUE

(t *testing.T)

Source from the content-addressed store, hash-verified

507// Verifies: SYS-REQ-108
508// MCDC SYS-REQ-108: parsestring_input_has_standard_escapes=T, parsestring_roundtrip_preserves_semantics=T => TRUE
509func TestParseStringEncodingSafetyRoundtrip(t *testing.T) {
510 cases := []struct {
511 name string
512 input string
513 expected string
514 }{
515 {name: "newline", input: `hello\nworld`, expected: "hello\nworld"},
516 {name: "tab", input: `col1\tcol2`, expected: "col1\tcol2"},
517 {name: "backslash", input: `path\\to\\file`, expected: `path\to\file`},
518 {name: "quote", input: `say \"hi\"`, expected: `say "hi"`},
519 {name: "unicode BMP", input: `\u0048\u0065\u006C\u006C\u006F`, expected: "Hello"},
520 {name: "solidus", input: `a\/b`, expected: "a/b"},
521 {name: "backspace", input: `a\bc`, expected: "a\bc"},
522 {name: "formfeed", input: `a\fc`, expected: "a\fc"},
523 {name: "carriage return", input: `a\rc`, expected: "a\rc"},
524 }
525 for _, tc := range cases {
526 t.Run(tc.name, func(t *testing.T) {
527 decoded, err := ParseString([]byte(tc.input))
528 if err != nil {
529 t.Fatalf("ParseString returned error: %v", err)
530 }
531 if decoded != tc.expected {
532 t.Fatalf("ParseString decoded %q, expected %q", decoded, tc.expected)
533 }
534
535 // Round-trip check: re-encode and verify the decoded string marshals
536 // back to JSON that, when decoded again, gives the same Go string.
537 reEncoded, err := json.Marshal(decoded)
538 if err != nil {
539 t.Fatalf("json.Marshal failed: %v", err)
540 }
541 var reDecoded string
542 if err := json.Unmarshal(reEncoded, &reDecoded); err != nil {
543 t.Fatalf("json.Unmarshal failed: %v", err)
544 }
545 if reDecoded != decoded {
546 t.Fatalf("Round-trip mismatch: original %q, round-tripped %q", decoded, reDecoded)
547 }
548 })
549 }
550}
551
552// =============================================================================
553// Edge case tests

Callers

nothing calls this directly

Calls 1

ParseStringFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…