compareGetToOracle compares jsonparser.Get's output to the expected Go value (the reference oracle's prediction). Returns true iff they agree.
(got []byte, dt ValueType, gerr error, expected interface{})
| 344 | // compareGetToOracle compares jsonparser.Get's output to the expected Go |
| 345 | // value (the reference oracle's prediction). Returns true iff they agree. |
| 346 | func compareGetToOracle(got []byte, dt ValueType, gerr error, expected interface{}) bool { |
| 347 | // If the path didn't resolve, the oracle said it should exist. |
| 348 | if gerr != nil { |
| 349 | return false |
| 350 | } |
| 351 | expectedBytes, mErr := json.Marshal(expected) |
| 352 | if mErr != nil { |
| 353 | return true // generator produced an unmarshalable value; skip |
| 354 | } |
| 355 | switch dt { |
| 356 | case String: |
| 357 | // jsonparser strips quotes but does NOT process escapes; the raw |
| 358 | // body bytes round-trip through json.Unmarshal when re-quoted. |
| 359 | wrapped := make([]byte, 0, len(got)+2) |
| 360 | wrapped = append(wrapped, '"') |
| 361 | wrapped = append(wrapped, got...) |
| 362 | wrapped = append(wrapped, '"') |
| 363 | var gs string |
| 364 | if err := json.Unmarshal(wrapped, &gs); err != nil { |
| 365 | return false |
| 366 | } |
| 367 | // expected is the Go string (or other type). Compare via canonical form. |
| 368 | var es string |
| 369 | if err := json.Unmarshal(expectedBytes, &es); err != nil { |
| 370 | return false |
| 371 | } |
| 372 | return gs == es |
| 373 | case Number, Boolean, Null: |
| 374 | // got is raw JSON for these. Canonicalize both sides to normalize |
| 375 | // `1.0` vs `1`, `1e10` vs `10000000000`, etc. |
| 376 | canonGot, err := canonicalizeJSON(got) |
| 377 | if err != nil { |
| 378 | return false |
| 379 | } |
| 380 | canonExp, err := canonicalizeJSON(expectedBytes) |
| 381 | if err != nil { |
| 382 | return true |
| 383 | } |
| 384 | return bytes.Equal(canonGot, canonExp) |
| 385 | case Object, Array: |
| 386 | // Both sides parse to the same canonical form. |
| 387 | canonGot, err := canonicalizeJSON(got) |
| 388 | if err != nil { |
| 389 | return false |
| 390 | } |
| 391 | canonExp, err := canonicalizeJSON(expectedBytes) |
| 392 | if err != nil { |
| 393 | return true |
| 394 | } |
| 395 | return bytes.Equal(canonGot, canonExp) |
| 396 | case NotExist, Unknown: |
| 397 | return false |
| 398 | } |
| 399 | return false |
| 400 | } |
| 401 | |
| 402 | // setPathSemanticallyValid reports whether applying Set/Delete with `path` |
| 403 | // to a doc marshaled from `v` is well-defined: each array-index component |
no test coverage detected