diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a11f19..a81fd67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,6 @@ - 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. -- Empty `Date` and `Site` headers no longer produce comma-only date lines. ### Changed diff --git a/bin/pgn_to_tex_js.ml b/bin/pgn_to_tex_js.ml index ba7e40a..253855f 100644 --- a/bin/pgn_to_tex_js.ml +++ b/bin/pgn_to_tex_js.ml @@ -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 Js.string result -(* Exporting the module to the global JavaScript scope, including when the - artifact is loaded as a CommonJS module in Node. *) -let () = - let callback = Js.wrap_callback convert_js in - Js.export "to_tex" callback; - Js.Unsafe.set Js.Unsafe.global "to_tex" callback +(* The documented Node and browser contract is a single global [to_tex] + function, so setting it on the global object is the only export needed. *) +let () = Js.Unsafe.set Js.Unsafe.global "to_tex" (Js.wrap_callback convert_js) diff --git a/lib/internal/converter.ml b/lib/internal/converter.ml index 05f1279..8b4e907 100644 --- a/lib/internal/converter.ml +++ b/lib/internal/converter.ml @@ -5,23 +5,31 @@ open Yojson.Safe.Util let path_key path_moves = String.concat " " path_moves let parse_diagrams_json diagrams_json_str = + let fail () = raise Exit in try let json = Yojson.Safe.from_string diagrams_json_str in let diagrams_list = to_list json in List.fold_left (fun acc item -> - let fen = item |> member "fen" |> to_string in - let acc = - match item |> member "ply" with - | `Int ply -> - { acc with Ast.by_ply = MoveMap.add ply fen acc.Ast.by_ply } - | _ -> acc + let fields = match item with `Assoc fields -> fields | _ -> fail () in + let fen = + match List.assoc_opt "fen" fields with + | Some (`String fen) -> fen + | _ -> fail () in - match item |> member "path" with - | `List path_items -> + let acc = + 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 { acc with Ast.by_path = PathMap.add key fen acc.Ast.by_path } - | _ -> acc) + | Some _ -> fail ()) Ast.empty_diagrams diagrams_list with _ -> Ast.empty_diagrams diff --git a/lib/internal/latex_helpers.ml b/lib/internal/latex_helpers.ml index 0186f45..9eb2cd8 100644 --- a/lib/internal/latex_helpers.ml +++ b/lib/internal/latex_helpers.ml @@ -33,11 +33,11 @@ let build_author_string author white black white_elo black_elo = (* Title string builder *) let build_title_string event title subtitle = match (event, title, subtitle) with - | event, "", "" -> "\\title{" ^ escape_tex event ^ "}" - | _event, title, "" -> "\\title{" ^ escape_tex title ^ "}" + | event, "", "" -> "\\title{" ^ event ^ "}" + | _event, "", subtitle -> "\\title{" ^ subtitle ^ "}" + | _event, title, "" -> "\\title{" ^ title ^ "}" | _event, title, subtitle -> - "\\title{" ^ escape_tex title ^ "\\\\[2ex]\\large{" ^ escape_tex subtitle - ^ "}}" + "\\title{" ^ title ^ "\\\\[2ex]\\large{" ^ subtitle ^ "}}" (* Date and Site string builder *) let build_date_site_string date site = diff --git a/test/helpers/dune b/test/helpers/dune index df2bf89..3a7c72e 100644 --- a/test/helpers/dune +++ b/test/helpers/dune @@ -1,4 +1,4 @@ (library (name helpers) - (modules latex_helper) + (modules latex_helper text_helper) (libraries pgn_to_tex_internal)) diff --git a/test/helpers/text_helper.ml b/test/helpers/text_helper.ml new file mode 100644 index 0000000..21fb915 --- /dev/null +++ b/test/helpers/text_helper.ml @@ -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 diff --git a/test/test_diagram_resolution.ml b/test/test_diagram_resolution.ml index 7aab1b9..ef9efde 100644 --- a/test/test_diagram_resolution.ml +++ b/test/test_diagram_resolution.ml @@ -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; ] diff --git a/test/test_latex.ml b/test/test_latex.ml index 9b6f4c0..8fb486d 100644 --- a/test/test_latex.ml +++ b/test/test_latex.ml @@ -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; diff --git a/test/test_lexer.ml b/test/test_lexer.ml index f62ecb9..965303b 100644 --- a/test/test_lexer.ml +++ b/test/test_lexer.ml @@ -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; diff --git a/test/test_parser.ml b/test/test_parser.ml new file mode 100644 index 0000000..2119bb2 --- /dev/null +++ b/test/test_parser.ml @@ -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; + ] diff --git a/test/test_pgn_to_tex.ml b/test/test_pgn_to_tex.ml index ed6458f..4fa9f5e 100644 --- a/test/test_pgn_to_tex.ml +++ b/test/test_pgn_to_tex.ml @@ -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; diff --git a/test/test_real_world.ml b/test/test_real_world.ml index 1b2e042..b5a5912 100644 --- a/test/test_real_world.ml +++ b/test/test_real_world.ml @@ -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 diff --git a/test/test_suite.ml b/test/test_suite.ml index 1ff0d37..8f63651 100644 --- a/test/test_suite.ml +++ b/test/test_suite.ml @@ -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;