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

Function TestPropertyGetRoundTrip

property_test.go:156–188  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Property: Get round-trips on encoding/json-marshaled scalars --------------------------------------------------------------------------- reqproof:proptest Get Verifies: SYS-REQ-001 [property]

(t *testing.T)

Source from the content-addressed store, hash-verified

154// reqproof:proptest Get
155// Verifies: SYS-REQ-001 [property]
156func TestPropertyGetRoundTrip(t *testing.T) {
157 r := newRNG(jsonSeed)
158 const iterations = 2000
159 for i := 0; i < iterations; i++ {
160 val := randomJSONValue(r, 2)
161 raw, err := json.Marshal(val)
162 if err != nil {
163 continue
164 }
165 // Get on the whole document with no keys must return the root scalar
166 // or the nearest JSON value, without panicking.
167 got, dt, _, gerr := Get(raw)
168 if gerr != nil {
169 // Some scalars (e.g. standalone strings) may not round-trip
170 // through Get-with-no-keys deterministically; the invariant we
171 // assert is no-panic + determinism, checked below.
172 continue
173 }
174 // Determinism: re-run and compare.
175 got2, dt2, _, gerr2 := Get(raw)
176 if !bytes.Equal(got, got2) || dt != dt2 || (gerr == nil) != (gerr2 == nil) {
177 t.Fatalf("Get non-deterministic on input %q: (%q,%v) vs (%q,%v)", raw, got, dt, got2, dt2)
178 }
179 // The returned value, when re-marshaled, must describe a JSON value of
180 // the advertised type.
181 switch dt {
182 case Number, Boolean, Null:
183 if len(got) == 0 {
184 t.Fatalf("Get returned empty value for type %v on %q", dt, raw)
185 }
186 }
187 }
188}
189
190// ---------------------------------------------------------------------------
191// Property: typed accessors agree with encoding/json on typed leaves

Callers

nothing calls this directly

Calls 3

newRNGFunction · 0.85
randomJSONValueFunction · 0.85
GetFunction · 0.85

Tested by

no test coverage detected