simple-learning

Simple learning web program
git clone git://git.laack.co/simple-learning.git
Log | Files | Refs | README | LICENSE

commit 1d53a76f3e4e038e0eb1e1a0a02b2143ed2ecf2a
parent 682e57c913c1cf04de33a1a056ae4b396faf7e01
Author: Andrew Laack <andrew@laack.co>
Date:   Sun,  6 Sep 2026 10:57:30 -0500

Refactoring to use prefix matching in tests

Diffstat:
Mlearning/lib/parser.ml | 51++++++++++++++-------------------------------------
Mlearning/test/test_line_parsing.ml | 6++++--
2 files changed, 18 insertions(+), 39 deletions(-)

diff --git a/learning/lib/parser.ml b/learning/lib/parser.ml @@ -39,49 +39,25 @@ let permute_line line line_type = let cut = line_format_char_cut line_type in String.sub line cut (String.length line - cut) -(**[is_hidden line preformatted] checks if [line] is a hidden line type. *) -let is_hidden line preformatted = - ((not preformatted) && Shared.match_on_prefix line "?? ") - -(**[is_list line preformatted] checks if [line] is a list line type. *) -let is_list line preformatted = - ((not preformatted) && Shared.match_on_prefix line "* ") - -let is_quote line preformatted = - ((not preformatted) && Shared.match_on_prefix line "> ") - -let is_embedded_link line preformatted = - ((not preformatted) && Shared.match_on_prefix line "=>e ") - -let is_link line preformatted = - ((not preformatted) && Shared.match_on_prefix line "=> ") - -let is_h1 line preformatted = - ((not preformatted) && Shared.match_on_prefix line "# ") - -let is_h2 line preformatted = - ((not preformatted) && Shared.match_on_prefix line "## ") - -let is_h3 line preformatted = - ((not preformatted) && Shared.match_on_prefix line "### ") - -let is_preformatted _ preformatted = preformatted +let match_prefix_not_formatted line prefix preformatted = + if prefix = "" then preformatted else + ((not preformatted) && Shared.match_on_prefix line prefix) let get_line_type line preformatted = let funs = [ - (Hidden, is_hidden); - (Preformatted, is_preformatted); - (ListItem, is_list); - (Quote, is_quote); - (Embedded, is_embedded_link); - (Link, is_link); - (HeaderL1, is_h1); - (HeaderL2, is_h2); - (HeaderL3, is_h3); + (Hidden, "?? "); + (Preformatted, ""); + (ListItem, "* "); + (Quote, "> "); + (Embedded, "=>e "); + (Link, "=> "); + (HeaderL1, "# "); + (HeaderL2, "## "); + (HeaderL3, "### "); ] in - let lt = List.find_opt (fun (_, f) -> f line preformatted) funs in + let lt = List.find_opt (fun (_, pre) -> match_prefix_not_formatted line pre preformatted) funs in match lt with Some v -> fst v | None -> Normal let parse_simple_line line preformatted = @@ -89,6 +65,7 @@ let parse_simple_line line preformatted = { line_type = cl_type; content = permute_line line cl_type } +(*TODO: Stop special casing this. *) let is_format_line cl = String.length cl = 3 && cl.[0] = '`' && cl.[1] = '`' && cl.[2] = '`' diff --git a/learning/test/test_line_parsing.ml b/learning/test/test_line_parsing.ml @@ -1,8 +1,10 @@ open Learning + let test_line_parsing () = - assert (Parser.is_preformatted "" true); - assert (not (Parser.is_preformatted "" false)); + + assert ((Parser.get_line_type "" true) = Preformatted); + assert ((Parser.get_line_type "" false) = Normal); (* Line-based type tests.*) (* Only ``` is a format line, no additional characters.*)