t_ezjsonm.ml 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. let test_error () =
  2. let j = {|not valid json|} |> Ezjsonm.from_string_result in
  3. (match j with
  4. | Error (`Error (((1,1),(1,3)),`Illegal_literal l)) ->
  5. l |> Assrt.equals_string __LOC__ "not"
  6. | _ -> failwith __LOC__);
  7. (match j with
  8. | Error e ->
  9. e
  10. |> Ezjsonm.read_error_description
  11. |> Assrt.equals_string __LOC__ {|illegal literal (not)|}
  12. | _ -> failwith __LOC__);
  13. ()
  14. let test_constructing () =
  15. `O
  16. [
  17. ("id", `String "398eb027");
  18. ("name", `String "John Doe");
  19. ( "pages",
  20. `O
  21. [
  22. ("id", Ezjsonm.int 1); ("title", `String "The Art of Flipping Coins");
  23. ] );
  24. ]
  25. |> Ezjsonm.to_string ~minify:true
  26. |> Assrt.equals_string __LOC__
  27. "{\"id\":\"398eb027\",\"name\":\"John \
  28. Doe\",\"pages\":{\"id\":1,\"title\":\"The Art of Flipping Coins\"}}"
  29. let test_load_peertube () =
  30. let extract3tries k0 k1 j =
  31. match Ezjsonm.find j [ k0 ] with
  32. | `String s -> Some s
  33. | `A (`String s :: _) -> Some s
  34. | `A ((`O _ as hd) :: _) -> (
  35. (* ignore 'type' *)
  36. match Ezjsonm.find hd [ k1 ] with `String s -> Some s | _ -> None)
  37. | _ -> None
  38. in
  39. let ic = open_in "data/peertube-video.json" in
  40. ic |> Ezjsonm.from_channel
  41. |> extract3tries "attributedTo" "id"
  42. |> Option.get |> Uri.of_string |> Uri.to_string
  43. |> Assrt.equals_string __LOC__
  44. "https://tube.network.europa.eu/accounts/edps";
  45. close_in ic;
  46. let ic = open_in "data/ap/actor/peertube.0.json" in
  47. let _a = Ezjsonm.from_channel ic in
  48. close_in ic;
  49. assert true
  50. let () =
  51. Unix.chdir "../../../test/";
  52. test_error ();
  53. test_constructing ();
  54. test_load_peertube ();
  55. assert true