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

Function TestPropertyTypedAccessorsRoundTrip

property_test.go:211–253  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Property: typed accessors agree with encoding/json on typed leaves --------------------------------------------------------------------------- reqproof:proptest parser.GetString, parser.GetInt, parser.GetFloat, parser.GetBo

(t *testing.T)

Source from the content-addressed store, hash-verified

209// reqproof:proptest parser.GetString, parser.GetInt, parser.GetFloat, parser.GetBoolean, parser.GetUnsafeString
210// Verifies: SYS-REQ-002 [property]
211func TestPropertyTypedAccessorsRoundTrip(t *testing.T) {
212 r := newRNG(jsonSeed + 1)
213 const iterations = 2000
214 for i := 0; i < iterations; i++ {
215 raw, obj := randomObjectJSONBytes(r, 2)
216 for k, v := range obj {
217 switch val := v.(type) {
218 case string:
219 got, err := GetString(raw, k)
220 if err == nil && got != val {
221 t.Fatalf("GetString mismatch on key=%q input=%q: got=%q want=%q", k, raw, got, val)
222 }
223 // GetUnsafeString does NOT process escapes; only compare on the
224 // no-escape case where unsafe and safe paths must agree.
225 if !strings.ContainsAny(val, "\\\"\n\t\r") {
226 if u, err := GetUnsafeString(raw, k); err == nil {
227 if u != val {
228 t.Fatalf("GetUnsafeString mismatch on key=%q input=%q: got=%q want=%q", k, raw, u, val)
229 }
230 }
231 }
232 case float64:
233 // encoding/json marshals all numbers as float64. Try int first
234 // when the value is integral, then float.
235 if val == float64(int64(val)) {
236 if got, err := GetInt(raw, k); err == nil && got != int64(val) {
237 t.Fatalf("GetInt mismatch on key=%q input=%q: got=%d want=%d", k, raw, got, int64(val))
238 }
239 }
240 if got, err := GetFloat(raw, k); err == nil {
241 if got != val {
242 t.Fatalf("GetFloat mismatch on key=%q input=%q: got=%g want=%g", k, raw, got, val)
243 }
244 }
245 case bool:
246 got, err := GetBoolean(raw, k)
247 if err == nil && got != val {
248 t.Fatalf("GetBoolean mismatch on key=%q input=%q: got=%v want=%v", k, raw, got, val)
249 }
250 }
251 }
252 }
253}
254
255// ---------------------------------------------------------------------------
256// 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