type_of.ml 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 integer_as_string =
  22. [
  23. Qsp_syntax.Report.
  24. {
  25. level = Warn;
  26. loc = _position;
  27. message = "The type Integer is expected but got Integer as String";
  28. };
  29. ]
  30. let _test_instruction : string -> Qsp_syntax.Report.t list -> unit =
  31. Check._test_instruction
  32. let type_mismatch () = _test_instruction {|abc = 'ABC'|} (message Error)
  33. let assign_int_str () = _test_instruction {|$abc = 123|} (message Warn)
  34. let type_mismatch2 () = _test_instruction {|abc[''] = $Var|} (message Warn)
  35. let type_conversion () = _test_instruction {|abc = '123'|} integer_as_string
  36. let type_conversion' () =
  37. _test_instruction {|abc = '<<123>>'|} integer_as_string
  38. let type_comparaison () = _test_instruction {|(abc = '123')|} []
  39. let type_comparaison_eq () = _test_instruction {|($abc = 123)|} (message Warn)
  40. let type_comparaison_eq' () = _test_instruction {|(1 = "abc")|} (message' Error)
  41. let type_comparaison_gte () = _test_instruction {|($abc >= 123)|} (message Warn)
  42. let type_comparaison_gte' () =
  43. _test_instruction {|(1 >= "abc")|} (message' Error)
  44. let type_comparaison_gt () = _test_instruction {|($abc > 123)|} (message Warn)
  45. let type_comparaison_gt' () = _test_instruction {|(123 > 'a')|} (message' Error)
  46. let type_comparaison_mismatch () =
  47. _test_instruction {|(abc = 'ABC')|}
  48. [
  49. {
  50. level = Warn;
  51. loc = _position;
  52. message = "The type String is expected but got Integer";
  53. };
  54. ]
  55. let wrong_predicate () =
  56. _test_instruction {| if $var and 1: 0 |}
  57. [
  58. {
  59. level = Warn;
  60. loc = _position;
  61. message = "The type Bool is expected but got String";
  62. };
  63. ]
  64. let concat_text () = _test_instruction {|$a = 'A' + 1|} []
  65. let increment_string () = _test_instruction {|$a += 1|} (message' Error)
  66. let test =
  67. ( "Typechecking",
  68. [
  69. Alcotest.test_case "Assign str to int" `Quick type_mismatch;
  70. Alcotest.test_case "$str = int" `Quick assign_int_str;
  71. Alcotest.test_case "Assign array" `Quick type_mismatch2;
  72. Alcotest.test_case "Conversion" `Quick type_conversion;
  73. Alcotest.test_case "Conversion'" `Quick type_conversion';
  74. Alcotest.test_case "Comparaison" `Quick type_comparaison;
  75. Alcotest.test_case "eq(str, int)" `Quick type_comparaison_eq;
  76. Alcotest.test_case "eq(int, str)" `Quick type_comparaison_eq';
  77. Alcotest.test_case "gte(str, int)" `Quick type_comparaison_gte;
  78. Alcotest.test_case "gte(int, str)" `Quick type_comparaison_gte';
  79. Alcotest.test_case "gt(str, int)" `Quick type_comparaison_gt;
  80. Alcotest.test_case "gt(int, str)" `Quick type_comparaison_gt';
  81. Alcotest.test_case "Comparaison Mismatch" `Quick type_comparaison_mismatch;
  82. Alcotest.test_case "Wrong predicate" `Quick wrong_predicate;
  83. Alcotest.test_case "+(int, str)" `Quick concat_text;
  84. Alcotest.test_case "str += int" `Quick increment_string;
  85. ] )