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

Function TestPropertyTypedAccessorsRoundTrip

property_test.go:196–238  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Property: typed accessors agree with encoding/json on typed leaves --------------------------------------------------------------------------- reqproof:proptest GetString, GetInt, GetFloat, GetBoolean, GetUnsafeString Verif

(t *testing.T)

Source from the content-addressed store, hash-verified

194// reqproof:proptest GetString, GetInt, GetFloat, GetBoolean, GetUnsafeString
195// Verifies: SYS-REQ-002 [property]
196func TestPropertyTypedAccessorsRoundTrip(t *testing.T) {
197 r := newRNG(jsonSeed + 1)
198 const iterations = 2000
199 for i := 0; i < iterations; i++ {
200 raw, obj := randomObjectJSONBytes(r, 2)
201 for k, v := range obj {
202 switch val := v.(type) {
203 case string:
204 got, err := GetString(raw, k)
205 if err == nil && got != val {
206 t.Fatalf("GetString mismatch on key=%q input=%q: got=%q want=%q", k, raw, got, val)
207 }
208 // GetUnsafeString does NOT process escapes; only compare on the
209 // no-escape case where unsafe and safe paths must agree.
210 if !strings.ContainsAny(val, "\\\"\n\t\r") {
211 if u, err := GetUnsafeString(raw, k); err == nil {
212 if u != val {
213 t.Fatalf("GetUnsafeString mismatch on key=%q input=%q: got=%q want=%q", k, raw, u, val)
214 }
215 }
216 }
217 case float64:
218 // encoding/json marshals all numbers as float64. Try int first
219 // when the value is integral, then float.
220 if val == float64(int64(val)) {
221 if got, err := GetInt(raw, k); err == nil && got != int64(val) {
222 t.Fatalf("GetInt mismatch on key=%q input=%q: got=%d want=%d", k, raw, got, int64(val))
223 }
224 }
225 if got, err := GetFloat(raw, k); err == nil {
226 if got != val {
227 t.Fatalf("GetFloat mismatch on key=%q input=%q: got=%g want=%g", k, raw, got, val)
228 }
229 }
230 case bool:
231 got, err := GetBoolean(raw, k)
232 if err == nil && got != val {
233 t.Fatalf("GetBoolean mismatch on key=%q input=%q: got=%v want=%v", k, raw, got, val)
234 }
235 }
236 }
237 }
238}
239
240// ---------------------------------------------------------------------------
241// Property: ParseInt/ParseFloat/ParseBoolean agree with strconv reference

Callers

nothing calls this directly

Calls 7

newRNGFunction · 0.85
randomObjectJSONBytesFunction · 0.85
GetStringFunction · 0.85
GetUnsafeStringFunction · 0.85
GetIntFunction · 0.85
GetFloatFunction · 0.85
GetBooleanFunction · 0.85

Tested by

no test coverage detected