aboutsummaryrefslogtreecommitdiff
path: root/test/test_template.ml
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-08-19 08:17:30 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-08-19 08:17:30 +0200
commitda9cf402ceed8102aec1a9d008f8e918a23d39f3 (patch)
tree3c9a7bd748f886633117cce7059ba304c23541cb /test/test_template.ml
parenta526d934cea17ff1b76ddad5c5f2934fb85c11cb (diff)
downloadcolitur-da9cf402ceed8102aec1a9d008f8e918a23d39f3.tar.gz
colitur-da9cf402ceed8102aec1a9d008f8e918a23d39f3.zip
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.
Diffstat (limited to 'test/test_template.ml')
-rw-r--r--test/test_template.ml79
1 files changed, 79 insertions, 0 deletions
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 "<script>") ]) in
+ Alcotest.(check string) "html escaped" "&lt;script&gt;" hout
+
+(* Literal template text is NOT escaped -- it is the author's own markup. Only
+ interpolated DATA is escaped. Getting this backwards would make every
+ template render as visible source. *)
+(* NOTE: the brief's own example here was "\textbf{{{name}}}", i.e. a literal
+ "{" immediately followed by "{{name}}". That does not parse the way the
+ brief assumes: this engine's lexer (Task 2, frozen) is greedy left-to-right
+ and has no triple-brace special case (by design -- there is no raw form),
+ so three consecutive "{" are read as an ordinary tag opener "{{" followed
+ by a path that literally starts with "{" ("{name"), which resolves to
+ nothing and renders empty, plus a stray literal "}" -- exactly what
+ test_no_raw_or_partial_form above already documents for "{{{name}}}" on
+ its own. Verified live: the brief's original line reddens with "\textbf}",
+ not "\textbf{Hilary}". Rewritten below to put a character between the
+ literal "{" and the tag's own "{{", which sidesteps the ambiguity while
+ still proving the same property: literal backslash/braces from Text survive
+ unescaped, where a wrongly-escaped Text node would turn "\textbf{" into
+ "\textbackslash{}\{". *)
+let test_literal_markup_is_not_escaped () =
+ Alcotest.(check string) "literal kept" "\\textbf{Name: Hilary}"
+ (render ~flavour:E.Latex "\\textbf{Name: {{name}}}" (obj [ ("name", T.Str "Hilary") ]))
+
+let render_suite =
+ ( "Template/render",
+ [ Alcotest.test_case "vars" `Quick test_render_var;
+ Alcotest.test_case "missing key is empty" `Quick test_missing_key_is_empty;
+ Alcotest.test_case "section iterates" `Quick test_section_iterates;
+ Alcotest.test_case "bool sections" `Quick test_bool_section;
+ Alcotest.test_case "empty list sections" `Quick test_empty_list_section_is_skipped;
+ Alcotest.test_case "outer scope visible" `Quick test_outer_scope_visible_inside_section;
+ Alcotest.test_case "data cannot escape flavour" `Quick test_data_cannot_escape_flavour;
+ Alcotest.test_case "literal markup unescaped" `Quick test_literal_markup_is_not_escaped ] )