mirror of
https://github.com/TheRealOwenRees/pgn_to_tex.git
synced 2026-11-18 04:42:07 +00:00
fix: address code review findings for public API
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
(library
|
||||
(name helpers)
|
||||
(modules latex_helper)
|
||||
(modules latex_helper text_helper)
|
||||
(libraries pgn_to_tex_internal))
|
||||
|
||||
@@ -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
|
||||
@@ -1,13 +1,6 @@
|
||||
open OUnit2
|
||||
|
||||
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 contains = Helpers.Text_helper.contains
|
||||
|
||||
let path_only_variation_json =
|
||||
{| [{"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)
|
||||
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 =
|
||||
"diagram resolution tests"
|
||||
>::: [
|
||||
@@ -108,4 +127,6 @@ let suite =
|
||||
"ply_only_entry_falls_back" >:: test_ply_only_entry_falls_back;
|
||||
"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;
|
||||
]
|
||||
|
||||
@@ -105,6 +105,10 @@ let test_date_site =
|
||||
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 =
|
||||
[
|
||||
( "Event only" >:: fun _ ->
|
||||
@@ -114,6 +118,15 @@ let test_title =
|
||||
ae "\\title{Some title\\\\[2ex]\\large{Some subtitle}}"
|
||||
(Latex_helpers.build_title_string "irrelevant" "Some title"
|
||||
"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 =
|
||||
@@ -121,6 +134,9 @@ let test_author =
|
||||
( "Author only" >:: fun _ ->
|
||||
ae "\\author{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 _ ->
|
||||
ae "\\author{}" (Latex_helpers.build_author_string "" "" "" "" "") );
|
||||
]
|
||||
@@ -174,6 +190,7 @@ let suite =
|
||||
"diagram_placement" >:: test_diagram_placement;
|
||||
"elo tests" >::: test_player_elo;
|
||||
"date & site tests" >::: test_date_site;
|
||||
"escape tex" >:: test_escape_tex;
|
||||
"title tests" >::: test_title;
|
||||
"author tests" >::: test_author;
|
||||
"diagram_test" >:: test_diagram;
|
||||
|
||||
@@ -170,9 +170,33 @@ let test_clock_in_comment _ctxt =
|
||||
in
|
||||
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 =
|
||||
"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_escaped_string" >:: test_header_escaped_string;
|
||||
"numbers_and_moves" >:: test_numbers_and_moves;
|
||||
|
||||
@@ -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
@@ -1,22 +1,7 @@
|
||||
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 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 contains = Helpers.Text_helper.contains
|
||||
|
||||
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 =
|
||||
{| [{"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 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 rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams ~clock:false pgn in
|
||||
let internal_diagrams =
|
||||
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
|
||||
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" in
|
||||
assert_bool
|
||||
(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"
|
||||
(contains ~needle:"smallboard" rendered)
|
||||
|
||||
@@ -153,8 +93,6 @@ let test_invalid_pgn_raises _ctxt =
|
||||
let suite =
|
||||
"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;
|
||||
"basic_pgn_to_tex" >:: test_basic_pgn_to_tex;
|
||||
"pgn_with_clock_to_tex" >:: test_pgn_with_clock_to_tex;
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
open OUnit2
|
||||
|
||||
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 contains = Helpers.Text_helper.contains
|
||||
|
||||
let read_file path =
|
||||
let ic = In_channel.open_text path in
|
||||
|
||||
@@ -7,6 +7,7 @@ let () =
|
||||
Test_lexer.suite;
|
||||
Test_latex.suite;
|
||||
Test_pgn_to_tex.suite;
|
||||
Test_parser.suite;
|
||||
Test_diagram_resolution.suite;
|
||||
Test_nag.suite;
|
||||
Test_real_world.suite;
|
||||
|
||||
Reference in New Issue
Block a user