type_of.ml 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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|} (message Warn)
  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 increment_string () = _test_instruction {|$a += 1|} (message' Error)
  65. let test =
  66. ( "Typechecking",
  67. [
  68. Alcotest.test_case "Assign str to int" `Quick type_mismatch;
  69. Alcotest.test_case "$str = int" `Quick assign_int_str;
  70. Alcotest.test_case "Assign array" `Quick type_mismatch2;
  71. Alcotest.test_case "Conversion" `Quick type_conversion;
  72. Alcotest.test_case "Conversion'" `Quick type_conversion';
  73. Alcotest.test_case "Comparaison" `Quick type_comparaison;
  74. Alcotest.test_case "eq(str, int)" `Quick type_comparaison_eq;
  75. Alcotest.test_case "eq(int, str)" `Quick type_comparaison_eq';
  76. Alcotest.test_case "gte(str, int)" `Quick type_comparaison_gte;
  77. Alcotest.test_case "gte(int, str)" `Quick type_comparaison_gte';
  78. Alcotest.test_case "gt(str, int)" `Quick type_comparaison_gt;
  79. Alcotest.test_case "gt(int, str)" `Quick type_comparaison_gt';
  80. Alcotest.test_case "Comparaison Mismatch" `Quick type_comparaison_mismatch;
  81. Alcotest.test_case "Wrong predicate" `Quick wrong_predicate;
  82. Alcotest.test_case "+(int, str)" `Quick concat_text;
  83. Alcotest.test_case "str += int" `Quick increment_string;
  84. ] )