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

Function TestPropertyGetRoundTrip

property_test.go:171–203  ·  view source on GitHub ↗

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

(t *testing.T)

Source from the content-addressed store, hash-verified

169// reqproof:proptest parser.Get
170// Verifies: SYS-REQ-001 [property]
171func TestPropertyGetRoundTrip(t *testing.T) {
172 r := newRNG(jsonSeed)
173 const iterations = 2000
174 for i := 0; i < iterations; i++ {
175 val := randomJSONValue(r, 2)
176 raw, err := json.Marshal(val)
177 if err != nil {
178 continue
179 }
180 // Get on the whole document with no keys must return the root scalar
181 // or the nearest JSON value, without panicking.
182 got, dt, _, gerr := Get(raw)
183 if gerr != nil {
184 // Some scalars (e.g. standalone strings) may not round-trip
185 // through Get-with-no-keys deterministically; the invariant we
186 // assert is no-panic + determinism, checked below.
187 continue
188 }
189 // Determinism: re-run and compare.
190 got2, dt2, _, gerr2 := Get(raw)
191 if !bytes.Equal(got, got2) || dt != dt2 || (gerr == nil) != (gerr2 == nil) {
192 t.Fatalf("Get non-deterministic on input %q: (%q,%v) vs (%q,%v)", raw, got, dt, got2, dt2)
193 }
194 // The returned value, when re-marshaled, must describe a JSON value of
195 // the advertised type.
196 switch dt {
197 case Number, Boolean, Null:
198 if len(got) == 0 {
199 t.Fatalf("Get returned empty value for type %v on %q", dt, raw)
200 }
201 }
202 }
203}
204
205// ---------------------------------------------------------------------------
206// 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