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

Function TestRemoval4_EachKey_TracePath

dead_code_audit_test.go:676–721  ·  view source on GitHub ↗

============================================================================= CRITICAL PATH: Verify EachKey correctly handles the removed block-skip by walking through the exact scenario step by step ============================================================================= Verifies: SYS-REQ-008

(t *testing.T)

Source from the content-addressed store, hash-verified

674
675// Verifies: SYS-REQ-008 [boundary]
676func TestRemoval4_EachKey_TracePath(t *testing.T) {
677 // {"skip":{"n":1},"want":"ok"}
678 // When EachKey processes "skip" and match==-1:
679 // - i is at ':' (the colon after "skip")
680 // - tokenOffset := nextToken(data[i+1:]) — finds '{' at offset 0
681 // - i += 0 (tokenOffset is 0, but wait: nextToken skips whitespace,
682 // and data[i+1] = '{', which is not whitespace, so nextToken returns 0)
683 // - BUT: i += tokenOffset means i is still at ':'. No, wait:
684 // the code says `i += tokenOffset`, not `i = tokenOffset`.
685 // If i was at position of ':', say position 7 in {"skip":{"n":1},"want":"ok"}
686 // then data[i+1:] starts with '{"n":1},"want":"ok"}'
687 // nextToken returns 0 (first char '{' is not whitespace)
688 // i += 0 → i is still 7 (the colon position)
689 //
690 // Then we hit `switch data[i]` where data[7] = ':'
691 // ':' is not in {'{', '}', '[', '"'} so no i-- adjustment.
692 //
693 // Then the outer loop does i++ → i = 8, which is '{'.
694 // The outer switch hits case '{': → level++.
695 // The parser then navigates the nested object naturally.
696 //
697 // The OLD code had: if data[i] == '{' { blockSkip = blockEnd(...); i += blockSkip + 1 }
698 // This would have jumped past the entire nested object.
699 // The NEW code relies on the natural '{' handler in the outer switch.
700 //
701 // Both should work, but the new code is O(n) walking character by character
702 // through the nested object, while the old code was O(n) via blockEnd.
703 // Functionally equivalent.
704
705 data := []byte(`{"skip":{"n":1},"want":"ok"}`)
706 paths := [][]string{{"want"}}
707
708 var found bool
709 EachKey(data, func(idx int, value []byte, vt ValueType, err error) {
710 if idx == 0 {
711 found = true
712 if string(value) != "ok" {
713 t.Fatalf("expected 'ok', got %q", value)
714 }
715 }
716 }, paths...)
717
718 if !found {
719 t.Fatal("UNSAFE: EachKey failed the trace path test")
720 }
721}
722
723// Test with value types that aren't objects — numbers, arrays, strings, bools
724// Verifies: SYS-REQ-008 [boundary]

Callers

nothing calls this directly

Calls 1

EachKeyFunction · 0.85

Tested by

no test coverage detected