Something went wrong. Try again.
XML parser, serializer, and typed codecs
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire <thomas@gazagnaire.org>. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(* Codec-level tests: combinators, interpretation, attr/El builders. The top-level test_xml.ml integrates codecs with top-level I/O; this file pins per-module behaviour directly on [Xml.Codec] / [Xml.El] / [Xml.Attr]. *)
let decode_ok codec s = match Xml.of_string codec s with | Ok v -> v | Error e -> Alcotest.failf "decode: %a" Xml.Error.pp e
let test_primitives () = Alcotest.(check string) "string" "hi" (decode_ok (Xml.Codec.element "t" Xml.Codec.string) "<t>hi</t>"); Alcotest.(check int) "int" 42 (decode_ok (Xml.Codec.element "t" Xml.Codec.int) "<t>42</t>"); Alcotest.(check (float 0.001)) "float" 3.14 (decode_ok (Xml.Codec.element "t" Xml.Codec.float) "<t>3.14</t>"); Alcotest.(check bool) "bool" true (decode_ok (Xml.Codec.element "t" Xml.Codec.bool) "<t>true</t>")
let test_list () = let codec = Xml.Codec.element "xs" (Xml.Codec.list (Xml.Codec.element "x" Xml.Codec.int)) in let v = decode_ok codec "<xs><x>1</x><x>2</x><x>3</x></xs>" in Alcotest.(check (list int)) "list" [ 1; 2; 3 ] v
let test_option () = let codec = Xml.Codec.element "t" (Xml.Codec.option Xml.Codec.string) in Alcotest.(check (option string)) "some" (Some "hi") (decode_ok codec "<t>hi</t>")
let test_map () = let codec = Xml.Codec.( element "n" (map ~dec:(fun x -> x * 2) ~enc:(fun x -> x / 2) int)) in let v = decode_ok codec "<n>21</n>" in Alcotest.(check int) "map dec" 42 v; Alcotest.(check string) "map enc" "<n>21</n>" (Xml.to_string codec 42)
type point = { x : float; y : float }
let test_el_builder () = let codec = Xml.Codec.( element "p" El.( obj (fun x y -> { x; y }) |> attr "x" Attr.float ~enc:(fun p -> p.x) |> attr "y" Attr.float ~enc:(fun p -> p.y) |> finish)) in let v = decode_ok codec {|<p x="1.5" y="2.5"/>|} in Alcotest.(check (float 0.001)) "x" 1.5 v.x; Alcotest.(check (float 0.001)) "y" 2.5 v.y; let s = Xml.to_string codec v in let v' = decode_ok codec s in Alcotest.(check (float 0.001)) "roundtrip x" v.x v'.x
(* A float is written so that it reads back as itself: the shortest of 15 or 17 significant digits that does (IEEE 754-2019 section 5.12.2), never [string_of_float]'s 12, which reads 1/3 back as 0.333333333333. *)let test_float_roundtrip () = let text = Xml.Codec.element "t" Xml.Codec.float in let attr = Xml.Codec.( element "p" El.(obj Fun.id |> attr "x" Attr.float ~enc:Fun.id |> finish)) in List.iter (fun x -> let name = Fmt.str "%h" x in Alcotest.(check (float 0.)) ("text " ^ name) x (decode_ok text (Xml.to_string text x)); Alcotest.(check (float 0.)) ("attribute " ^ name) x (decode_ok attr (Xml.to_string attr x))) [ 1. /. 3.; 4.62968866591e-195; 0x1.fffffffffffffp+1023; 5e-324; -0.1 ]; Alcotest.(check string) "shortest" "<t>0.1</t>" (Xml.to_string text 0.1)
let test_kind () = Alcotest.(check string) "string" "string" (Xml.Codec.kind Xml.Codec.string); Alcotest.(check string) "int" "int" (Xml.Codec.kind Xml.Codec.int); Alcotest.(check string) "element" "element <foo> of int" (Xml.Codec.kind (Xml.Codec.element "foo" Xml.Codec.int))
let test_error_on_malformed () = match Xml.of_string (Xml.Codec.element "t" Xml.Codec.int) "<t>abc</t>" with | Ok _ -> Alcotest.fail "expected error on non-integer" | Error _ -> ()
let test_skip_subtree () = (* [Xml.Codec.skip] decodes any element to [()] and discards its body. Useful for filtering unneeded element classes while iterating. *) let codec = Xml.Codec.element "root" (Xml.Codec.list Xml.Codec.skip) in let xml = {|<root><a>one</a><b x="1"><nested/></b><c/></root>|} in match Xml.of_string codec xml with | Ok units -> Alcotest.(check int) "3 children skipped" 3 (List.length units) | Error e -> Alcotest.failf "skip list: %a" Xml.Error.pp e
let test_skip_in_obj () = (* [Xml.Codec.skip] inside an [El] builder lets you ignore a required child's body. *) let codec = Xml.Codec.( element "root" El.( obj (fun name () -> name) |> child "name" string ~enc:Fun.id |> child "trace" Xml.Codec.skip ~enc:(fun _ -> ()) |> finish)) in let xml = "<root><name>alice</name><trace><call>f</call><call>g</call></trace></root>" in match Xml.of_string codec xml with | Ok name -> Alcotest.(check string) "name extracted" "alice" name | Error e -> Alcotest.failf "skip in obj: %a" Xml.Error.pp e
let suite = ( "codec", [ Alcotest.test_case "primitives" `Quick test_primitives; Alcotest.test_case "float roundtrip" `Quick test_float_roundtrip; Alcotest.test_case "list" `Quick test_list; Alcotest.test_case "option" `Quick test_option; Alcotest.test_case "map" `Quick test_map; Alcotest.test_case "El builder" `Quick test_el_builder; Alcotest.test_case "kind" `Quick test_kind; Alcotest.test_case "error on malformed int" `Quick test_error_on_malformed; Alcotest.test_case "skip subtree" `Quick test_skip_subtree; Alcotest.test_case "skip inside El builder" `Quick test_skip_in_obj; ] )