t_xmlm.ml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. open Seppo_lib
  2. (* https://erratique.ch/software/xmlm/doc/Xmlm/index.html#outns *)
  3. let test_ezxmlm_out () =
  4. Logr.info (fun m -> m "%s.%s" "xmlm_test" "text_ezxmlm_out");
  5. []
  6. |> Ezxmlm.to_string
  7. |> Assrt.equals_string __LOC__ "";
  8. [ `El ((("", "uhu"), []), []) ]
  9. |> Ezxmlm.to_string
  10. |> Assrt.equals_string __LOC__ "<uhu/>";
  11. [ `El ((("", "e"), [ (("", "a"), "v") ]), [ `Data "foo" ]) ]
  12. |> Ezxmlm.to_string
  13. |> Assrt.equals_string __LOC__ "<e a=\"v\">foo</e>";
  14. [ `El ((("", "e"), [ (("", "xmlns"), "A") ]), [ `Data "foo" ]) ]
  15. |> Ezxmlm.to_string
  16. |> Assrt.equals_string __LOC__ "<e xmlns=\"A\">foo</e>";
  17. [ `El ((("A", "e"), [((Xmlm.ns_xmlns, "xmlns"), "A")]), [`Data "foo"]) ]
  18. |> Ezxmlm.to_string
  19. |> Assrt.equals_string __LOC__ "<e xmlns=\"A\">foo</e>";
  20. [ `El ((("N", "e"), [((Xmlm.ns_xmlns, "n"), "N")]), [`Data "foo"]) ]
  21. |> Ezxmlm.to_string
  22. |> Assrt.equals_string __LOC__ "<n:e xmlns:n=\"N\">foo</n:e>";
  23. assert true
  24. let test_ezxmlm_out2() =
  25. Logr.info (fun m -> m "%s.%s" "xmlm_test" "text_ezxmlm_out2");
  26. [`El ((("","elm"),[(("","nam"),"val")]),[])]
  27. |> Ezxmlm.to_string
  28. |> Assrt.equals_string __LOC__ {|<elm nam="val"/>|};
  29. [`El ((("uri 2","root"),[
  30. ((Xmlm.ns_xmlns,"n2"),"uri 2");
  31. ]),[
  32. `El ((("uri 2","c1"),[
  33. ]),[
  34. `Data "uh";
  35. `Data "u";
  36. ])]);
  37. ]
  38. |> Ezxmlm.to_string ~decl:true
  39. |> Assrt.equals_string __LOC__ {|<?xml version="1.0" encoding="UTF-8"?>
  40. <n2:root xmlns:n2="uri 2"><n2:c1>uhu</n2:c1></n2:root>|};
  41. assert true
  42. let test_xmlm_signals () =
  43. Logr.info (fun m -> m "%s.%s" "xmlm_test" "test_xmlm_signals");
  44. let dst = Buffer.create 1024 in
  45. Buffer.add_string dst {|<?xml version="1.0" encoding="UTF-8"?>
  46. |};
  47. Buffer.add_string dst "<!-- huhu -->\n";
  48. let o = Xmlm.make_output ~decl:false ~indent:(Some 2) (`Buffer dst) in
  49. let out = Xmlm.output o in
  50. out (`Dtd None);
  51. out (`El_start (("uri","e"),[
  52. ((Xmlm.ns_xmlns,"n"),"uri");
  53. ("","k"),"v"]));
  54. out (`Data "uhu");
  55. out `El_end;
  56. dst
  57. |> Buffer.to_bytes
  58. |> Bytes.to_string
  59. |> Assrt.equals_string __LOC__ {|<?xml version="1.0" encoding="UTF-8"?>
  60. <!-- huhu -->
  61. <n:e xmlns:n="uri" k="v">
  62. uhu
  63. </n:e>|};
  64. assert true
  65. let test_xmlm_frag () =
  66. Logr.info (fun m -> m "%s.%s" "xmlm_test" "test_xmlm_signals");
  67. let x : _ Xmlm.frag = `El ((("uri", "e"), [
  68. ((Xmlm.ns_xmlns, "n"), "uri");
  69. (("", "k"), "v");
  70. ]), [`Data "uhu"]) in
  71. let dst = Buffer.create 1024 in
  72. Buffer.add_string dst {|<?xml version="1.0" encoding="UTF-8"?>
  73. |};
  74. Buffer.add_string dst "<!-- huhu -->\n";
  75. let o = Xmlm.make_output ~decl:false ~indent:(Some 2) (`Buffer dst) in
  76. Xmlm.output_doc_tree (fun x -> x) o (None,x);
  77. dst
  78. |> Buffer.to_bytes
  79. |> Bytes.to_string
  80. |> Assrt.equals_string __LOC__ {|<?xml version="1.0" encoding="UTF-8"?>
  81. <!-- huhu -->
  82. <n:e xmlns:n="uri" k="v">
  83. uhu
  84. </n:e>|};
  85. assert true
  86. let () =
  87. Unix.chdir "../../../test/";
  88. test_ezxmlm_out ();
  89. test_ezxmlm_out2 ();
  90. test_xmlm_signals ();
  91. test_xmlm_frag ();
  92. assert true