From da9cf402ceed8102aec1a9d008f8e918a23d39f3 Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Wed, 19 Aug 2026 08:17:30 +0200 Subject: feat(render): template renderer with mandatory escaping Every interpolated value is escaped for the template's flavour; the template's own literal text never is, because that is the author's markup. There is no raw form, so a template cannot opt out. Scope is a stack with outward fallback, so a grid template can reach the year number from inside a week without the view duplicating it into every cell. A missing key renders empty -- the one deliberate silence, so a template survives a rite that does not set every optional field. Mutation-tested: dropping the Escape.apply call reddens the data-cannot-escape-flavour case. --- test/test_colitur.ml | 3 +- test/test_template.ml | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_colitur.ml b/test/test_colitur.ml index a264174..f12dab5 100644 --- a/test/test_colitur.ml +++ b/test/test_colitur.ml @@ -8,4 +8,5 @@ let () = ("lectionary", Test_lectionary.suite); ("lectionary-ef", Test_lectionary_ef.suite); Test_escape.suite; - Test_template.suite ] + Test_template.suite; + Test_template.render_suite ] diff --git a/test/test_template.ml b/test/test_template.ml index cfb3b04..b83c378 100644 --- a/test/test_template.ml +++ b/test/test_template.ml @@ -84,3 +84,82 @@ let suite = Alcotest.test_case "unterminated tag errors" `Quick test_unterminated_tag_is_an_error; Alcotest.test_case "no raw or partial form" `Quick test_no_raw_or_partial_form; Alcotest.test_case "empty path errors" `Quick test_empty_path_is_an_error ] ) + +module E = Colitur_render.Escape + +let render ?(flavour = E.None_) tpl v = + match T.render_string ~flavour tpl v with Ok s -> s | Error e -> Alcotest.failf "render: %s" e + +let obj kvs = T.Obj kvs + +let test_render_var () = + Alcotest.(check string) "var" "Hilary" (render "{{name}}" (obj [ ("name", T.Str "Hilary") ])); + Alcotest.(check string) "dotted" "Hilarii" + (render "{{name.la}}" (obj [ ("name", obj [ ("la", T.Str "Hilarii") ]) ])) + +(* A missing key renders empty. This is the ONE silent case, and it is + deliberate: a template written for a rite that does not set every optional + field must still render (spec section 5). *) +let test_missing_key_is_empty () = + Alcotest.(check string) "missing" "[]" (render "[{{nope}}]" (obj [ ("name", T.Str "x") ])) + +let test_section_iterates () = + let v = obj [ ("days", T.List [ obj [ ("dom", T.Str "1") ]; obj [ ("dom", T.Str "2") ] ]) ] in + Alcotest.(check string) "iterate" "1|2|" (render "{{#days}}{{dom}}|{{/days}}" v) + +let test_bool_section () = + Alcotest.(check string) "true" "yes" (render "{{#f}}yes{{/f}}" (obj [ ("f", T.Bool true) ])); + Alcotest.(check string) "false" "" (render "{{#f}}yes{{/f}}" (obj [ ("f", T.Bool false) ])); + Alcotest.(check string) "inverted true" "" (render "{{^f}}no{{/f}}" (obj [ ("f", T.Bool true) ])); + Alcotest.(check string) "inverted false" "no" (render "{{^f}}no{{/f}}" (obj [ ("f", T.Bool false) ])) + +let test_empty_list_section_is_skipped () = + Alcotest.(check string) "empty list" "" (render "{{#days}}x{{/days}}" (obj [ ("days", T.List []) ])); + Alcotest.(check string) "inverted empty list" "none" + (render "{{^days}}none{{/days}}" (obj [ ("days", T.List []) ])) + +let test_outer_scope_visible_inside_section () = + let v = obj [ ("year", T.Str "2027"); ("days", T.List [ obj [ ("dom", T.Str "1") ] ]) ] in + Alcotest.(check string) "outer visible" "2027-1" + (render "{{#days}}{{year}}-{{dom}}{{/days}}" v) + +(* THE safety property (spec section 9.2): a data value can never escape its + flavour. This is the test that must redden if any escape rule is broken. *) +let test_data_cannot_escape_flavour () = + let nasty = obj [ ("name", T.Str "A & B \\ 50% {x} $y_z") ] in + let out = render ~flavour:E.Latex "{{name}}" nasty in + Alcotest.(check string) "fully escaped" "A \\& B \\textbackslash{} 50\\% \\{x\\} \\$y\\_z" out; + let hout = render ~flavour:E.Html "{{name}}" (obj [ ("name", T.Str "