get_type.ml 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. module Get_type = Qsp_syntax.Get_type
  2. module T = Qsp_syntax.T
  3. let _position = (Lexing.dummy_pos, Lexing.dummy_pos)
  4. let type_of : Get_type.t Alcotest.testable =
  5. Alcotest.testable Get_type.pp Get_type.equal
  6. let add_number () =
  7. let actual =
  8. Get_type.boperator _position T.Plus
  9. (Get_type.integer _position "0")
  10. (Get_type.integer _position "1")
  11. in
  12. let expected = Get_type.(Raw Integer) in
  13. let msg = "Adding integer" in
  14. Alcotest.(check' type_of ~msg ~expected ~actual)
  15. let add_literal_number () =
  16. let actual =
  17. Get_type.boperator _position T.Plus
  18. (Get_type.literal _position [ T.Text "2" ])
  19. (Get_type.integer _position "1")
  20. in
  21. let expected = Get_type.(Raw Integer) in
  22. let msg = "A string containing integer is considered as integer" in
  23. Alcotest.(check' type_of ~msg ~expected ~actual)
  24. let concat_text () =
  25. let actual =
  26. Get_type.boperator _position T.Plus
  27. (Get_type.literal _position [ T.Text "a" ])
  28. (Get_type.integer _position "1")
  29. in
  30. let expected = Get_type.(Raw String) in
  31. let msg = "Concatenate" in
  32. Alcotest.(check' type_of ~msg ~expected ~actual)
  33. let literal_1 () =
  34. let actual =
  35. Get_type.literal _position [ T.Expression (Get_type.Raw Integer) ]
  36. and expected = Get_type.(Raw NumericString) in
  37. let msg = "" in
  38. Alcotest.(check' type_of ~msg ~expected ~actual)
  39. let literal_2 () =
  40. let actual =
  41. Get_type.literal _position
  42. Get_type.[ T.Text "1"; T.Expression (Raw Integer) ]
  43. and expected = Get_type.(Raw NumericString) in
  44. let msg = "" in
  45. Alcotest.(check' type_of ~msg ~expected ~actual)
  46. let literal_3 () =
  47. let actual =
  48. Get_type.literal _position
  49. Get_type.[ T.Text "b"; T.Expression (Raw Integer) ]
  50. and expected = Get_type.(Raw String) in
  51. let msg = "" in
  52. Alcotest.(check' type_of ~msg ~expected ~actual)
  53. let literal_4 () =
  54. let actual =
  55. Get_type.literal _position [ T.Expression (Get_type.Variable Integer) ]
  56. and expected = Get_type.(Variable NumericString) in
  57. let msg = "" in
  58. Alcotest.(check' type_of ~msg ~expected ~actual)
  59. let test =
  60. ( "Type expression",
  61. [
  62. Alcotest.test_case "int + int" `Quick add_number;
  63. Alcotest.test_case "'int' + int" `Quick add_literal_number;
  64. Alcotest.test_case "str + int" `Quick concat_text;
  65. Alcotest.test_case "<<int>>" `Quick literal_1;
  66. Alcotest.test_case "1<<int>>" `Quick literal_2;
  67. Alcotest.test_case "b<<int>>" `Quick literal_3;
  68. Alcotest.test_case "<<$int>>" `Quick literal_4;
  69. ] )