aboutsummaryrefslogtreecommitdiff
path: root/test/get_type.ml
blob: 56b46892161dd45816c90e5eb8df3e4a7181346b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module Get_type = Qsp_checks.Get_type
module T = Qsp_syntax.T

let _position = (Lexing.dummy_pos, Lexing.dummy_pos)

let type_of : Get_type.Expression.t Alcotest.testable =
  Alcotest.testable Get_type.Expression.pp Get_type.Expression.equal

let ctx = Qsp_syntax.S.{ f = (fun _ -> None) }

let add_number () =
  let actual =
    Get_type.Expression.boperator ~ctx _position T.Plus
      (Get_type.Expression.integer ~ctx _position "0")
      (Get_type.Expression.integer ~ctx _position "1")
  in
  let expected = Get_type.Expression.(Raw Integer) in
  let msg = "Adding integer" in
  Alcotest.(check' type_of ~msg ~expected ~actual)

let add_literal_number () =
  let actual =
    Get_type.Expression.boperator ~ctx _position T.Plus
      (Get_type.Expression.literal ~ctx _position [ T.Text "2" ])
      (Get_type.Expression.integer ~ctx _position "1")
  in
  let expected = Get_type.Expression.(Raw Integer) in
  let msg = "A string containing integer is considered as integer" in
  Alcotest.(check' type_of ~msg ~expected ~actual)

let concat_text () =
  let actual =
    Get_type.Expression.boperator ~ctx _position T.Plus
      (Get_type.Expression.literal ~ctx _position [ T.Text "a" ])
      (Get_type.Expression.integer ~ctx _position "1")
  in
  let expected = Get_type.Expression.(Raw String) in
  let msg = "Concatenate" in
  Alcotest.(check' type_of ~msg ~expected ~actual)

let literal_1 () =
  let actual =
    Get_type.Expression.literal ~ctx _position
      [ T.Expression (Get_type.Expression.Raw Integer) ]
  and expected = Get_type.Expression.(Raw NumericString) in
  let msg = "" in
  Alcotest.(check' type_of ~msg ~expected ~actual)

let literal_2 () =
  let actual =
    Get_type.Expression.literal ~ctx _position
      Get_type.Expression.[ T.Text "1"; T.Expression (Raw Integer) ]
  and expected = Get_type.Expression.(Raw NumericString) in
  let msg = "" in
  Alcotest.(check' type_of ~msg ~expected ~actual)

let literal_3 () =
  let actual =
    Get_type.Expression.literal ~ctx _position
      Get_type.Expression.[ T.Text "b"; T.Expression (Raw Integer) ]
  and expected = Get_type.Expression.(Raw String) in
  let msg = "" in
  Alcotest.(check' type_of ~msg ~expected ~actual)

let literal_4 () =
  let actual =
    Get_type.Expression.literal ~ctx _position
      Get_type.Expression.[ T.Expression (Variable Integer) ]
  and expected = Get_type.Expression.(Variable NumericString) in
  let msg = "" in
  Alcotest.(check' type_of ~msg ~expected ~actual)

let min () =
  let actual = Get_type.Expression.function_ ~ctx _position T.Min [] in
  let expected = Get_type.Expression.(Raw Bool) in
  let msg = "The function min without argument return a default value" in
  Alcotest.(check' type_of ~msg ~expected ~actual);

  let actual =
    Get_type.Expression.function_ ~ctx _position T.Min
      [ Get_type.Expression.literal ~ctx _position [] ]
  in
  let expected = Get_type.Expression.(Variable NumericString) in
  let msg =
    "The function min with a literal will take the literal as the name of an \
     array"
  in
  Alcotest.(check' type_of ~msg ~expected ~actual);

  let actual =
    Get_type.Expression.function_ ~ctx _position T.Min
      Get_type.Expression.
        [ integer ~ctx _position ""; integer ~ctx _position "" ]
  in
  let expected = Get_type.Expression.(Raw Integer) in
  let msg = "With two or more arguments, return the type of the first one" in
  Alcotest.(check' type_of ~msg ~expected ~actual)

let test =
  ( "Type expression",
    [
      Alcotest.test_case "int + int" `Quick add_number;
      Alcotest.test_case "'int' + int" `Quick add_literal_number;
      Alcotest.test_case "str + int" `Quick concat_text;
      Alcotest.test_case "<<int>>" `Quick literal_1;
      Alcotest.test_case "1<<int>>" `Quick literal_2;
      Alcotest.test_case "b<<int>>" `Quick literal_3;
      Alcotest.test_case "<<$int>>" `Quick literal_4;
      Alcotest.test_case "min" `Quick min;
    ] )