<commit_before>open OUnit
open Syntax

let parse_error_position_test program line character =
  let lexbuf = Lexing.from_string program in
  try
    let _ = Parse.parse lexbuf in
    assert_failure "This IDL must cause an error."
  with
  | Parse.Syntax_error pos ->
    assert_equal line pos.Lexing.pos_lnum;
    assert_equal character (pos.Lexing.pos_cnum - pos.Lexing.pos_bol)
;;

let _ = run_test_tt_main begin "parser" >::: [
  "test_parse_error_position" >:: begin fun() ->
    parse_error_position_test "  here" 1 2;
    parse_error_position_test "# comment\n here" 2 1
  end;

  "test_illegal_character" >:: begin fun() ->
    let lexbuf = Lexing.from_string "  $" in
    try
      let _ = Parse.parse lexbuf in
      assert_failure "This IDL must cause an error."
    with
    | Jdl_lexer.Illegal_character(pos, ch) ->
      assert_equal 1 pos.Lexing.pos_lnum;
      assert_equal 2 (pos.Lexing.pos_cnum - pos.Lexing.pos_bol);
      assert_equal '$' ch
  end;
] end<commit_msg>[jenerator]refactor parse_test and add some tests<commit_after>open OUnit
open Syntax

type result =
  | Success of idl
  | LexError of int * int * char
  | ParseError of int * int

let result_to_string = function
  | Success(_) -> "Success(<abst>)" (* TODO: implement idl_to_string *)
  | LexError(lnum, cnum, ch) -> Printf.sprintf "LexError(%n, %n, '%c')" lnum cnum ch
  | ParseError(lnum, cnum) -> Printf.sprintf "ParseError(%n, %n)" lnum cnum

let parse program =
  let lexbuf = Lexing.from_string program in
  try
    Success(Parse.parse lexbuf)
  with
  | Parse.Syntax_error pos ->
    Lexing.(
      ParseError(pos.pos_lnum, pos.pos_cnum - pos.pos_bol)
    )
  | Jdl_lexer.Illegal_character(pos, ch) ->
    Lexing.(
      LexError(pos.pos_lnum, pos.pos_cnum - pos.pos_bol, ch)
    )
;;

let parse_test program expected =
  assert_equal ~printer:result_to_string expected @@ parse program
;;

let parse_error_position_test program lnum cnum =
  parse_test program @@ ParseError(lnum, cnum)
;;

let lex_error_position_test program lnum cnum ch =
  parse_test program @@ LexError(lnum, cnum, ch)
;;

let parse_success_test program idl =
  parse_test program @@ Success(idl)

let _ = run_test_tt_main begin "parser" >::: [
  "test_parse_error_position" >:: begin fun() ->
    parse_error_position_test "" 1 0;
    parse_error_position_test "  here" 1 2;
    parse_error_position_test "# comment\n here" 2 1
  end;

  "test_illegal_character" >:: begin fun() ->
    lex_error_position_test "  $" 1 2 '$'
  end;

  "test_parse_success" >:: begin fun() ->
    parse_success_test "%include \"test\"" [Include("test")]
  end
] end
{'fraction_non_alphanumeric': 0.11174894756984309, 'fraction_numerical': 0.005357826253348641, 'mean_word_length': 3.6347517730496453, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4919704', 'n_tokens_mistral': 964, 'n_tokens_neox': 920, 'n_words': 230}