type_of.ml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 for_ok () = _test_instruction {|for i = 0 to 5 step 1: msg i|} []
  67. let for_start_string () =
  68. _test_instruction {|for i = '' to 5: msg i|} (message Warn)
  69. let for_to_string () =
  70. _test_instruction {|for i = 0 to '': msg i|} (message Warn)
  71. let for_variable () =
  72. _test_instruction {|for $i = 0 to 5: msg i|} (message Warn)
  73. let for_step () =
  74. _test_instruction {|for i = 0 to 5 step '': msg i|} (message Warn)
  75. let test =
  76. ( "Typechecking",
  77. [
  78. Alcotest.test_case "Assign str to int" `Quick type_mismatch;
  79. Alcotest.test_case "$str = int" `Quick assign_int_str;
  80. Alcotest.test_case "Assign array" `Quick type_mismatch2;
  81. Alcotest.test_case "Conversion" `Quick type_conversion;
  82. Alcotest.test_case "Conversion'" `Quick type_conversion';
  83. Alcotest.test_case "Comparaison" `Quick type_comparaison;
  84. Alcotest.test_case "eq(str, int)" `Quick type_comparaison_eq;
  85. Alcotest.test_case "eq(int, str)" `Quick type_comparaison_eq';
  86. Alcotest.test_case "gte(str, int)" `Quick type_comparaison_gte;
  87. Alcotest.test_case "gte(int, str)" `Quick type_comparaison_gte';
  88. Alcotest.test_case "gt(str, int)" `Quick type_comparaison_gt;
  89. Alcotest.test_case "gt(int, str)" `Quick type_comparaison_gt';
  90. Alcotest.test_case "Comparaison Mismatch" `Quick type_comparaison_mismatch;
  91. Alcotest.test_case "Wrong predicate" `Quick wrong_predicate;
  92. Alcotest.test_case "+(int, str)" `Quick concat_text;
  93. Alcotest.test_case "str += int" `Quick increment_string;
  94. Alcotest.test_case "correct for" `Quick for_ok;
  95. Alcotest.test_case "for i = ''" `Quick for_start_string;
  96. Alcotest.test_case "for $i" `Quick for_variable;
  97. Alcotest.test_case "for … step ''" `Quick for_step;
  98. ] )