fix: address code review findings for public API

This commit is contained in:
2026-10-23 16:38:24 +02:00
committed by owenrees
parent 4f50ff020e
commit 16fcaa3b13
13 changed files with 161 additions and 104 deletions
-1
View File
@@ -32,7 +32,6 @@
- A mixed `Author` and player header no longer produces an empty author line. - A mixed `Author` and player header no longer produces an empty author line.
- A `Title` without a `Subtitle` no longer emits an empty subtitle block. - A `Title` without a `Subtitle` no longer emits an empty subtitle block.
- Empty `Date` and `Site` headers no longer produce comma-only date lines.
### Changed ### Changed
+3 -6
View File
@@ -15,9 +15,6 @@ let convert_js pgn_js diagram_json_js display_clock =
let result = Pgn_to_tex.to_tex ~diagram_data ~clock:display_clock pgn in let result = Pgn_to_tex.to_tex ~diagram_data ~clock:display_clock pgn in
Js.string result Js.string result
(* Exporting the module to the global JavaScript scope, including when the (* The documented Node and browser contract is a single global [to_tex]
artifact is loaded as a CommonJS module in Node. *) function, so setting it on the global object is the only export needed. *)
let () = let () = Js.Unsafe.set Js.Unsafe.global "to_tex" (Js.wrap_callback convert_js)
let callback = Js.wrap_callback convert_js in
Js.export "to_tex" callback;
Js.Unsafe.set Js.Unsafe.global "to_tex" callback
+17 -9
View File
@@ -5,23 +5,31 @@ open Yojson.Safe.Util
let path_key path_moves = String.concat " " path_moves let path_key path_moves = String.concat " " path_moves
let parse_diagrams_json diagrams_json_str = let parse_diagrams_json diagrams_json_str =
let fail () = raise Exit in
try try
let json = Yojson.Safe.from_string diagrams_json_str in let json = Yojson.Safe.from_string diagrams_json_str in
let diagrams_list = to_list json in let diagrams_list = to_list json in
List.fold_left List.fold_left
(fun acc item -> (fun acc item ->
let fen = item |> member "fen" |> to_string in let fields = match item with `Assoc fields -> fields | _ -> fail () in
let acc = let fen =
match item |> member "ply" with match List.assoc_opt "fen" fields with
| `Int ply -> | Some (`String fen) -> fen
{ acc with Ast.by_ply = MoveMap.add ply fen acc.Ast.by_ply } | _ -> fail ()
| _ -> acc
in in
match item |> member "path" with let acc =
| `List path_items -> match List.assoc_opt "ply" fields with
| None -> acc
| Some (`Int ply) ->
{ acc with Ast.by_ply = MoveMap.add ply fen acc.Ast.by_ply }
| Some _ -> fail ()
in
match List.assoc_opt "path" fields with
| None -> acc
| Some (`List path_items) ->
let key = path_items |> List.map to_string |> path_key in let key = path_items |> List.map to_string |> path_key in
{ acc with Ast.by_path = PathMap.add key fen acc.Ast.by_path } { acc with Ast.by_path = PathMap.add key fen acc.Ast.by_path }
| _ -> acc) | Some _ -> fail ())
Ast.empty_diagrams diagrams_list Ast.empty_diagrams diagrams_list
with _ -> Ast.empty_diagrams with _ -> Ast.empty_diagrams
+4 -4
View File
@@ -33,11 +33,11 @@ let build_author_string author white black white_elo black_elo =
(* Title string builder *) (* Title string builder *)
let build_title_string event title subtitle = let build_title_string event title subtitle =
match (event, title, subtitle) with match (event, title, subtitle) with
| event, "", "" -> "\\title{" ^ escape_tex event ^ "}" | event, "", "" -> "\\title{" ^ event ^ "}"
| _event, title, "" -> "\\title{" ^ escape_tex title ^ "}" | _event, "", subtitle -> "\\title{" ^ subtitle ^ "}"
| _event, title, "" -> "\\title{" ^ title ^ "}"
| _event, title, subtitle -> | _event, title, subtitle ->
"\\title{" ^ escape_tex title ^ "\\\\[2ex]\\large{" ^ escape_tex subtitle "\\title{" ^ title ^ "\\\\[2ex]\\large{" ^ subtitle ^ "}}"
^ "}}"
(* Date and Site string builder *) (* Date and Site string builder *)
let build_date_site_string date site = let build_date_site_string date site =
+1 -1
View File
@@ -1,4 +1,4 @@
(library (library
(name helpers) (name helpers)
(modules latex_helper) (modules latex_helper text_helper)
(libraries pgn_to_tex_internal)) (libraries pgn_to_tex_internal))
+8
View File
@@ -0,0 +1,8 @@
let contains ~needle haystack =
let n = String.length needle and h = String.length haystack in
let rec loop i =
if i + n > h then false
else if String.sub haystack i n = needle then true
else loop (i + 1)
in
n = 0 || loop 0
+29 -8
View File
@@ -1,13 +1,6 @@
open OUnit2 open OUnit2
let contains ~needle haystack = let contains = Helpers.Text_helper.contains
let n = String.length needle and h = String.length haystack in
let rec loop i =
if i + n > h then false
else if String.sub haystack i n = needle then true
else loop (i + 1)
in
n = 0 || loop 0
let path_only_variation_json = let path_only_variation_json =
{| [{"fen":"8/8/8/8/8/8/4K3/7k w - - 0 1","path":["e4","e5","Nf3","d6"]}] |} {| [{"fen":"8/8/8/8/8/8/4K3/7k w - - 0 1","path":["e4","e5","Nf3","d6"]}] |}
@@ -97,6 +90,32 @@ let test_malformed_json_yields_empty_diagrams _ctxt =
expected actual) expected actual)
malformed malformed
let test_mixed_valid_and_invalid_entries_yield_empty _ctxt =
let pgn = "1. e4 e5 2. Nf3" in
let expected = Pgn_to_tex.to_tex pgn in
let valid = {|{"ply":2,"fen":"8/8/8/8/8/8/4K3/7k w - - 0 1"}|} in
let invalid_entries =
[
{|{"ply":3}|};
{|{"ply":"not-a-number","fen":"8/8/8/8/8/8/4K3/7k w - - 0 1"}|};
{|{"fen":"8/8/8/8/8/8/4K3/7k w - - 0 1","path":[1,2]}|};
{|{"fen":42}|};
]
in
List.iter
(fun invalid ->
let json = Printf.sprintf "[%s,%s]" valid invalid in
let actual =
Pgn_to_tex.to_tex
~diagram_data:(Pgn_to_tex.parse_diagrams_json json)
pgn
in
assert_equal
~printer:(fun x -> x)
~msg:("mixed valid/invalid JSON: " ^ json)
expected actual)
invalid_entries
let suite = let suite =
"diagram resolution tests" "diagram resolution tests"
>::: [ >::: [
@@ -108,4 +127,6 @@ let suite =
"ply_only_entry_falls_back" >:: test_ply_only_entry_falls_back; "ply_only_entry_falls_back" >:: test_ply_only_entry_falls_back;
"malformed_json_yields_empty_diagrams" "malformed_json_yields_empty_diagrams"
>:: test_malformed_json_yields_empty_diagrams; >:: test_malformed_json_yields_empty_diagrams;
"mixed_valid_and_invalid_entries_yield_empty"
>:: test_mixed_valid_and_invalid_entries_yield_empty;
] ]
+17
View File
@@ -105,6 +105,10 @@ let test_date_site =
ae "\\date{}" (Latex_helpers.build_date_site_string "" "") ); ae "\\date{}" (Latex_helpers.build_date_site_string "" "") );
] ]
let test_escape_tex _ctxt =
ae "\\#\\%\\{\\}\\_\\&" (Latex_helpers.escape_tex "#%{}_&");
ae "plain text" (Latex_helpers.escape_tex "plain text")
let test_title = let test_title =
[ [
( "Event only" >:: fun _ -> ( "Event only" >:: fun _ ->
@@ -114,6 +118,15 @@ let test_title =
ae "\\title{Some title\\\\[2ex]\\large{Some subtitle}}" ae "\\title{Some title\\\\[2ex]\\large{Some subtitle}}"
(Latex_helpers.build_title_string "irrelevant" "Some title" (Latex_helpers.build_title_string "irrelevant" "Some title"
"Some subtitle") ); "Some subtitle") );
( "Title without subtitle" >:: fun _ ->
ae "\\title{Some title}"
(Latex_helpers.build_title_string "irrelevant" "Some title" "") );
( "Subtitle without title" >:: fun _ ->
ae "\\title{Some subtitle}"
(Latex_helpers.build_title_string "irrelevant" "" "Some subtitle") );
( "Title is not escaped" >:: fun _ ->
ae "\\title{100% & more}"
(Latex_helpers.build_title_string "" "100% & more" "") );
] ]
let test_author = let test_author =
@@ -121,6 +134,9 @@ let test_author =
( "Author only" >:: fun _ -> ( "Author only" >:: fun _ ->
ae "\\author{Some author}" ae "\\author{Some author}"
(Latex_helpers.build_author_string "Some author" "" "" "" "") ); (Latex_helpers.build_author_string "Some author" "" "" "" "") );
( "Author wins over players" >:: fun _ ->
ae "\\author{Carol}"
(Latex_helpers.build_author_string "Carol" "Alice" "Bob" "1" "2") );
( "Nothing" >:: fun _ -> ( "Nothing" >:: fun _ ->
ae "\\author{}" (Latex_helpers.build_author_string "" "" "" "" "") ); ae "\\author{}" (Latex_helpers.build_author_string "" "" "" "" "") );
] ]
@@ -174,6 +190,7 @@ let suite =
"diagram_placement" >:: test_diagram_placement; "diagram_placement" >:: test_diagram_placement;
"elo tests" >::: test_player_elo; "elo tests" >::: test_player_elo;
"date & site tests" >::: test_date_site; "date & site tests" >::: test_date_site;
"escape tex" >:: test_escape_tex;
"title tests" >::: test_title; "title tests" >::: test_title;
"author tests" >::: test_author; "author tests" >::: test_author;
"diagram_test" >:: test_diagram; "diagram_test" >:: test_diagram;
+24
View File
@@ -170,9 +170,33 @@ let test_clock_in_comment _ctxt =
in in
assert_tokens ~ctxt:_ctxt ~input ~is_header:false ~expected assert_tokens ~ctxt:_ctxt ~input ~is_header:false ~expected
let test_header_unterminated_string_raises _ctxt =
let input = {|Event "oops|} in
let lexbuf = Sedlexing.Utf8.from_string input in
ignore (Lexer.tokenize_header lexbuf);
let exn_opt =
try
ignore (Lexer.tokenize_header lexbuf);
None
with exn -> Some exn
in
assert_bool "Expected exception on unterminated header string"
(Option.is_some exn_opt)
let test_unknown_characters_are_skipped _ctxt =
let input = "1. e4 ?! e5 @ 2. Nf3" in
let expected =
[ NUMBER "1."; MOVE "e4"; MOVE "e5"; NUMBER "2."; MOVE "Nf3"; EOF ]
in
assert_tokens ~ctxt:_ctxt ~input ~is_header:false ~expected
let suite = let suite =
"lexer tests" "lexer tests"
>::: [ >::: [
"header_unterminated_string_raises"
>:: test_header_unterminated_string_raises;
"unknown_characters_are_skipped"
>:: test_unknown_characters_are_skipped;
"header_simple" >:: test_header_simple; "header_simple" >:: test_header_simple;
"header_escaped_string" >:: test_header_escaped_string; "header_escaped_string" >:: test_header_escaped_string;
"numbers_and_moves" >:: test_numbers_and_moves; "numbers_and_moves" >:: test_numbers_and_moves;
+51
View File
@@ -0,0 +1,51 @@
open OUnit2
open Pgn_to_tex_internal
open Ast
let ae expected actual = assert_equal expected actual
let test_empty_input _ctxt =
ae { tags = []; content = []; result = None } (Parsing.parse_pgn "")
let test_result_only_game _ctxt =
ae
{ tags = []; content = [ Result "1-0" ]; result = None }
(Parsing.parse_pgn "1-0")
let test_nested_variations _ctxt =
let pgn = "1. e4 (1. d4 (1... d5) 1... e5) 1... c5" in
let expected =
{
tags = [];
content =
[
Number "1.";
Move "e4";
Variation
[
Number "1.";
Move "d4";
Variation [ Number "1..."; Move "d5" ];
Number "1...";
Move "e5";
];
Number "1...";
Move "c5";
];
result = None;
}
in
ae expected (Parsing.parse_pgn pgn)
let test_malformed_input_raises _ctxt =
assert_raises (Failure "Parse error") (fun () ->
ignore (Parsing.parse_pgn "1. e4 e5 (2. Nf3"))
let suite =
"parser tests"
>::: [
"empty_input" >:: test_empty_input;
"result_only_game" >:: test_result_only_game;
"nested_variations" >:: test_nested_variations;
"malformed_input_raises" >:: test_malformed_input_raises;
]
+5 -67
View File
@@ -1,22 +1,7 @@
open OUnit2 open OUnit2
open Pgn_to_tex_internal
open Ast
module MoveMap = Ast.MoveMap
module PathMap = Ast.PathMap
let ae expected actual = assert_equal ~printer:(fun x -> x) expected actual let ae expected actual = assert_equal ~printer:(fun x -> x) expected actual
let contains = Helpers.Text_helper.contains
let contains ~needle haystack =
let n = String.length needle and h = String.length haystack in
let rec loop i =
if i + n > h then false
else if String.sub haystack i n = needle then true
else loop (i + 1)
in
n = 0 || loop 0
let diagram_json_str =
{| [{"ply":1,"fen":"rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1"},{"ply":2,"fen":"rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2"}] |}
let game_diagram_json_str = let game_diagram_json_str =
{| [ {| [
@@ -31,61 +16,16 @@ let diagram_data = Pgn_to_tex.parse_diagrams_json game_diagram_json_str
let variation_diagram_json_str = let variation_diagram_json_str =
{| [{"ply":4,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","path":["e4","e5","Nf3","d6"]}] |} {| [{"ply":4,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","path":["e4","e5","Nf3","d6"]}] |}
let test_json_str_to_map _ctxt =
let result = Converter.parse_diagrams_json diagram_json_str in
let actual_bindings = MoveMap.bindings result.by_ply in
let expected_bindings =
[
(1, "rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1");
(2, "rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2");
]
in
let string_of_bindings pairs =
let string_of_pair (ply, fen) = Printf.sprintf "(%d, %S)" ply fen in
"[" ^ String.concat "; " (List.map string_of_pair pairs) ^ "]"
in
assert_equal expected_bindings actual_bindings ~printer:string_of_bindings
let test_json_path_to_map _ctxt =
let result = Converter.parse_diagrams_json variation_diagram_json_str in
let actual = PathMap.bindings result.by_path in
let expected =
[
( "e4 e5 Nf3 d6",
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" );
]
in
assert_equal expected actual
let test_variation_diagram_to_tex _ctxt = let test_variation_diagram_to_tex _ctxt =
let pgn = "1. e4 e5 2. Nf3 Nc6 (2... d6) 3. Bb5" in let pgn = "1. e4 e5 2. Nf3 Nc6 (2... d6) 3. Bb5" in
let diagrams = Pgn_to_tex.parse_diagrams_json variation_diagram_json_str in let diagrams = Pgn_to_tex.parse_diagrams_json variation_diagram_json_str in
let rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams ~clock:false pgn in let rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams ~clock:false pgn in
let internal_diagrams = let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" in
Converter.parse_diagrams_json variation_diagram_json_str
in
let diagram =
match
Latex_helpers.get_diagram_by_path ~path:"e4 e5 Nf3 d6" ~small:true
internal_diagrams
with
| Some d -> d
| None -> assert_failure "variation diagram not found by path"
in
(* the renderer trims the trailing newline of the diagram inside the
variation, but keeps the leading one *)
let embedded =
let len = String.length diagram in
if len > 0 && diagram.[len - 1] = '\n' then String.sub diagram 0 (len - 1)
else diagram
in
let expected_fragment = "( 2... d6" ^ embedded ^ " )" in
assert_bool assert_bool
(Printf.sprintf "variation diagram misplaced in:\n%s" rendered) (Printf.sprintf "variation diagram misplaced in:\n%s" rendered)
(contains ~needle:expected_fragment rendered); (contains
~needle:("2... d6\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen)
rendered);
assert_bool "variation diagram should use smallboard" assert_bool "variation diagram should use smallboard"
(contains ~needle:"smallboard" rendered) (contains ~needle:"smallboard" rendered)
@@ -153,8 +93,6 @@ let test_invalid_pgn_raises _ctxt =
let suite = let suite =
"pgn_to_tex tests" "pgn_to_tex tests"
>::: [ >::: [
"json_str_to_map" >:: test_json_str_to_map;
"json_path_to_map" >:: test_json_path_to_map;
"variation_diagram_to_tex" >:: test_variation_diagram_to_tex; "variation_diagram_to_tex" >:: test_variation_diagram_to_tex;
"basic_pgn_to_tex" >:: test_basic_pgn_to_tex; "basic_pgn_to_tex" >:: test_basic_pgn_to_tex;
"pgn_with_clock_to_tex" >:: test_pgn_with_clock_to_tex; "pgn_with_clock_to_tex" >:: test_pgn_with_clock_to_tex;
+1 -8
View File
@@ -1,13 +1,6 @@
open OUnit2 open OUnit2
let contains ~needle haystack = let contains = Helpers.Text_helper.contains
let n = String.length needle and h = String.length haystack in
let rec loop i =
if i + n > h then false
else if String.sub haystack i n = needle then true
else loop (i + 1)
in
n = 0 || loop 0
let read_file path = let read_file path =
let ic = In_channel.open_text path in let ic = In_channel.open_text path in
+1
View File
@@ -7,6 +7,7 @@ let () =
Test_lexer.suite; Test_lexer.suite;
Test_latex.suite; Test_latex.suite;
Test_pgn_to_tex.suite; Test_pgn_to_tex.suite;
Test_parser.suite;
Test_diagram_resolution.suite; Test_diagram_resolution.suite;
Test_nag.suite; Test_nag.suite;
Test_real_world.suite; Test_real_world.suite;