type_of.ml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. module Check = Make_checkTest.M (Qsp_syntax.Type_of)
  2. let _position = (Lexing.dummy_pos, Lexing.dummy_pos)
  3. let message level =
  4. [
  5. Qsp_syntax.Report.
  6. {
  7. level;
  8. loc = _position;
  9. message = "The type Integer is expected but got String";
  10. };
  11. ]
  12. let message' level =
  13. [
  14. Qsp_syntax.Report.
  15. {
  16. level;
  17. loc = _position;
  18. message = "The type String is expected but got Integer";
  19. };
  20. ]
  21. let _test_instruction : string -> Qsp_syntax.Report.t list -> unit =
  22. Check._test_instruction
  23. let type_mismatch () = _test_instruction {|abc = 'ABC'|} (message Error)
  24. let assign_int_str () = _test_instruction {|$abc = 123|} []
  25. let type_mismatch2 () = _test_instruction {|abc[''] = $Var|} (message Warn)
  26. let type_conversion () =
  27. _test_instruction {|abc = '123'|}
  28. [
  29. {
  30. level = Warn;
  31. loc = _position;
  32. message = "The type Integer is expected but got Integer as String";
  33. };
  34. ]
  35. (** This expression is not considered as a string *)
  36. let type_conversion' () = _test_instruction {|abc = '<<123>>'|} []
  37. let type_comparaison () = _test_instruction {|(abc = '123')|} []
  38. let type_comparaison_eq () = _test_instruction {|($abc = 123)|} (message Warn)
  39. let type_comparaison_eq' () = _test_instruction {|(1 = "abc")|} (message' Error)
  40. let type_comparaison_gte () = _test_instruction {|($abc >= 123)|} (message Warn)
  41. let type_comparaison_gte' () =
  42. _test_instruction {|(1 >= "abc")|} (message' Error)
  43. let type_comparaison_gt () = _test_instruction {|($abc > 123)|} (message Warn)
  44. let type_comparaison_gt' () = _test_instruction {|(123 > 'a')|} (message' Error)
  45. let type_comparaison_mismatch () =
  46. _test_instruction {|(abc = 'ABC')|}
  47. [
  48. {
  49. level = Warn;
  50. loc = _position;
  51. message = "The type String is expected but got Integer";
  52. };
  53. ]
  54. let wrong_predicate () =
  55. _test_instruction {| if $var and 1: 0 |}
  56. [
  57. {
  58. level = Warn;
  59. loc = _position;
  60. message = "The type Bool is expected but got String";
  61. };
  62. ]
  63. let concat_text () = _test_instruction {|$a = 'A' + 1|} []
  64. let test =
  65. ( "Typechecking",
  66. [
  67. Alcotest.test_case "Assign str to int" `Quick type_mismatch;
  68. Alcotest.test_case "Assign int to str" `Quick assign_int_str;
  69. Alcotest.test_case "Assign array" `Quick type_mismatch2;
  70. Alcotest.test_case "Conversion" `Quick type_conversion;
  71. Alcotest.test_case "Conversion'" `Quick type_conversion';
  72. Alcotest.test_case "Comparaison" `Quick type_comparaison;
  73. Alcotest.test_case "eq(str, int)" `Quick type_comparaison_eq;
  74. Alcotest.test_case "eq(int, str)" `Quick type_comparaison_eq';
  75. Alcotest.test_case "gte(str, int)" `Quick type_comparaison_gte;
  76. Alcotest.test_case "gte(int, str)" `Quick type_comparaison_gte';
  77. Alcotest.test_case "gt(str, int)" `Quick type_comparaison_gt;
  78. Alcotest.test_case "gt(int, str)" `Quick type_comparaison_gt';
  79. Alcotest.test_case "Comparaison Mismatch" `Quick type_comparaison_mismatch;
  80. Alcotest.test_case "Wrong predicate" `Quick wrong_predicate;
  81. Alcotest.test_case "+(int, str)" `Quick concat_text;
  82. ] )