summaryrefslogtreecommitdiff
path: root/test/test_emit.ml
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_emit.ml')
-rw-r--r--test/test_emit.ml34
1 files changed, 34 insertions, 0 deletions
diff --git a/test/test_emit.ml b/test/test_emit.ml
index a8fd45c..63bb3d0 100644
--- a/test/test_emit.ml
+++ b/test/test_emit.ml
@@ -1,6 +1,7 @@
module V = Colitur_render.View
module Csv = Colitur_render.Emit_csv
module Json = Colitur_render.Emit_json
+module Xml = Colitur_render.Emit_xml
let view_2027 () = Test_view.view_of 2027
@@ -75,3 +76,36 @@ let suite =
Alcotest.test_case "json parses back" `Quick test_json_parses_back;
Alcotest.test_case "json escapes" `Quick test_json_escapes;
Alcotest.test_case "json passes utf8 through" `Quick test_utf8_passes_through_json ] )
+
+(* Substring search shared by the two XML well-formedness checks below, in
+ place of the brief's inline recursive finder -- same property, clearer to
+ read. *)
+let contains ~needle hay =
+ let n = String.length needle and h = String.length hay in
+ let rec go i = i + n <= h && (String.sub hay i n = needle || go (i + 1)) in
+ go 0
+
+let test_xml_shape () =
+ let out = Xml.year (view_2027 ()) in
+ Alcotest.(check bool) "declaration" true
+ (String.length out > 5 && String.sub out 0 5 = "<?xml");
+ Alcotest.(check bool) "root element" true
+ (contains ~needle:"<calendar rite=\"ef\" year=\"2027\">" out)
+
+(* Every '<' in the output must open a tag: an unescaped '<' inside a feast
+ name is the failure that makes a whole feed unparseable. *)
+let test_xml_tags_balance () =
+ let out = Xml.year (view_2027 ()) in
+ let opens = ref 0 and closes = ref 0 in
+ String.iter (fun c -> if c = '<' then incr opens else if c = '>' then incr closes) out;
+ Alcotest.(check int) "every < has a >" !opens !closes
+
+let test_xml_escapes_data () =
+ Alcotest.(check string) "amp" "a &amp; b" (Xml.escape "a & b");
+ Alcotest.(check string) "angle" "&lt;x&gt;" (Xml.escape "<x>")
+
+let xml_suite =
+ ( "Emit/xml",
+ [ Alcotest.test_case "shape" `Quick test_xml_shape;
+ Alcotest.test_case "tags balance" `Quick test_xml_tags_balance;
+ Alcotest.test_case "escapes data" `Quick test_xml_escapes_data ] )