t_sexp.ml 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. open Lib
  2. type entry = {
  3. name : string;
  4. country : string option;
  5. email : string option
  6. }
  7. (* [@@deriving sexp] *)
  8. (* https://gitlab.inria.fr/bmontagu/sexp_decode
  9. via https://discuss.ocaml.org/t/combinator-library-for-extracting-data-for-s-exps/10153/5 *)
  10. let test_sexp_a () =
  11. let open Sexp_decode in
  12. Logr.info (fun m -> m "test_sexp_a");
  13. let entry_decoder : entry decoder =
  14. field "entry"
  15. @@ let* name = field "name" atom in
  16. let* country = maybe @@ field "country" atom in
  17. let+ email = maybe @@ field "email" atom in
  18. { name; country; email } in
  19. let address_book_decoder (* : address_book decoder *) = list entry_decoder in
  20. let _r = run address_book_decoder (Atom "a") in
  21. Assrt.equals_string __LOC__ "a" "a";
  22. assert true
  23. module D = Decoders_ezjsonm.Decode
  24. (* https://discuss.ocaml.org/t/combinator-library-for-extracting-data-for-s-exps/10153 *)
  25. let test_decoders () =
  26. let open D in
  27. let _announce : entry decoder =
  28. let* _ = field "type" string
  29. and* name = field "actor" string in
  30. succeed ({name; country = None; email = None;})
  31. in
  32. let r = (D.decode_string (_announce ) "((type "") (actor uhu))")
  33. |> Result.get_ok in
  34. r.name
  35. |> Assrt.equals_string __LOC__ "u"
  36. let () =
  37. Unix.chdir "../../../test/";
  38. test_sexp_a ();
  39. test_decoders ();
  40. assert true