aboutsummaryrefslogtreecommitdiff
path: root/test/syntax_error.ml
blob: 66f442d776bc7cca5e434d310bc824bb2b2c1ebc (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
module Ast = Qsp_syntax.Tree.Ast
module S = Qsp_syntax.S

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

type 'a report = { level : Qsp_syntax.Report.level; loc : 'a; message : string }
[@@deriving eq, show]

let report : S.pos report Alcotest.testable =
  let equal = equal_report (fun _ _ -> true) in
  let pp = pp_report (fun formater _ -> Format.fprintf formater "_position") in
  Alcotest.testable pp equal

let get_report :
    (S.pos Syntax.location, Qsp_syntax.Report.t) result -> S.pos report =
  function
  | Ok _ -> failwith "No error"
  | Error { level; loc; message } -> { level; loc; message }

let _test_instruction :
    ?k:(S.pos report -> unit) -> string -> S.pos report -> unit =
 fun ?k literal expected ->
  let _location = Printf.sprintf {|# Location
%s
------- |} literal in
  let actual = get_report @@ Syntax.parse _location and msg = literal in

  let () = Alcotest.(check' report ~msg ~expected ~actual) in
  match k with None -> () | Some f -> f actual

let else_column () =
  _test_instruction
    {|IF 1:
      0
      ELSE:
      1
      END|}
    {
      level = Error;
      loc = _position;
      message = "Unexpected operator after `ELSE`";
    }

let elseif_no_column () =
  _test_instruction
    {|IF 1:
      0
      ELSEIF 0
      1
      END|}
    {
      level = Error;
      loc = _position;
      message =
        "The `ELIF` expression does not end properly. A `:` is expected before \
         any instruction.";
    }

let unclosed_paren () =
  _test_instruction
    {|(1
    |}
    {
      level = Error;
      loc = _position;
      message = "Unexpected '('. Did you forgot a function before ?";
    }

let act_no_column () =
  _test_instruction
    {|ACT 1
      0
      END|}
    {
      level = Error;
      loc = _position;
      message = "Invalid `ACT` label. You probably missed a ':'";
    }

let missing_ampersand () =
  let result =
    {
      level = Error;
      loc = _position;
      message = "Missing separator between instructions";
    }
  in
  let () = _test_instruction "b = 1 a = 2" result
  and () = _test_instruction "let b = 1 a = 2" result
  and () = _test_instruction "set b = 1 a = 2" result in
  ()

let unclose_comment () =
  _test_instruction {| ! that's it|}
    { level = Error; loc = _position; message = "Unclosed text" }

let syntax_error () =
  _test_instruction {|*clr $ cla|}
    { level = Error; loc = _position; message = "Unexpected character \"\"" }

let missing_operand () =
  let result =
    { level = Error; loc = _position; message = "Missing operand" }
  in
  let () = _test_instruction {|if  and other: 1|} result
  and () = _test_instruction {|a =  and other: 1|} result in
  ()

let unknow_function () =
  _test_instruction "a = ran(1, 2)"
    { level = Error; loc = _position; message = "Unexpected expression here." }

let inline_elif () =
  _test_instruction {|
		if a = 1:
		elseif a = 1: a = 1
  end|}
    {
      level = Error;
      loc = _position;
      message = "Mix between `IF` block and inline `ELIF`";
    }

let unclosed_block () =
  _test_instruction {|
if $ARGS[0] = 'arg':
	act'action':
end|}
    {
      level = Error;
      loc = _position;
      message =
        "Unclosed `IF` block. Another block ends before the `END` instruction.";
    };

  _test_instruction
    {|
IF 1:
ELSE
    0
      |}
    {
      level = Error;
      loc = _position;
      message =
        "Unclosed `ELSE` block. Another block ends before the `END` \
         instruction.";
    };
  _test_instruction
    {|
IF 1:
ELSEIF 0:
    0
      |}
    {
      level = Error;
      loc = _position;
      message =
        "Unclosed `ELIF` block. Another block ends before the `END` \
         instruction.";
    }

let comment_as_operator () =
  let result =
    {
      level = Error;
      loc = _position;
      message = "Missing separator between instructions";
    }
  in
  _test_instruction "gs 'a', 'b' ! target" result;
  _test_instruction "gs 'a' ! target" result

let missing_comparable () =
  let result =
    {
      level = Error;
      loc = _position;
      message = "Missing boolean after operator";
    }
  in
  _test_instruction "1 and >= 0" result;
  _test_instruction "1 >= and 0" result;
  _test_instruction "1 > and 0" result;
  _test_instruction "1 < and 0" result;
  _test_instruction "1 or >= 0" result;
  _test_instruction "1 >= or 0" result;
  _test_instruction "1 <= or 0" result;
  _test_instruction "1 = or 0" result

(** This code looks like a new location, but is actualy invalid. 
    The application should report the old location.
 *)
let location_change () =
  let result =
    {
      level = Error;
      loc = _position;
      message = "Missing boolean after operator";
    }
  in
  _test_instruction "1 and >= # invalid" result ~k:(fun actual ->
      let actual = (fst actual.loc).Lexing.pos_fname in

      Alcotest.(
        check' ~msg:"The location name is not valid" string ~actual
          ~expected:"Location"))

let misplaced_if () =
  _test_instruction {|
if $ARGS[0] = 'arg':
  0	
end if|}
    {
      level = Error;
      loc = _position;
      message = "Unexpected instruction after `IF` `END` block.";
    };
  _test_instruction {|
act 'arg':
  0	
end if|}
    {
      level = Error;
      loc = _position;
      message = "Unexpected instruction after `ACT` `END` block.";
    }

(* The location name *)

let unclosed_paren2 () =
  _test_instruction {|
iif(1,0,0 + iif(1, 1, 2)
|}
    { level = Error; loc = _position; message = "Unclosed `(`" }

let test =
  ( "Syntax Errors",
    [
      Alcotest.test_case "else:" `Quick else_column;
      Alcotest.test_case "elseif" `Quick elseif_no_column;
      Alcotest.test_case "(1" `Quick unclosed_paren;
      Alcotest.test_case "act 1" `Quick act_no_column;
      Alcotest.test_case "no &" `Quick missing_ampersand;
      Alcotest.test_case "unclose_comment" `Quick unclose_comment;
      Alcotest.test_case "Syntax error $" `Quick syntax_error;
      Alcotest.test_case "Missing operand" `Quick missing_operand;
      Alcotest.test_case "Unknown function" `Quick unknow_function;
      Alcotest.test_case "Inline elif" `Quick inline_elif;
      Alcotest.test_case "Unclosed block" `Quick unclosed_block;
      Alcotest.test_case "Unclosed block" `Quick comment_as_operator;
      Alcotest.test_case "Missing comparable" `Quick missing_comparable;
      Alcotest.test_case "Location change" `Quick location_change;
      Alcotest.test_case "Misplaced if" `Quick misplaced_if;
      Alcotest.test_case "(()" `Quick unclosed_paren2;
    ] )