aboutsummaryrefslogtreecommitdiff
path: root/test/syntax_error.ml
diff options
context:
space:
mode:
authorChimrod <>2023-10-01 19:07:17 +0200
committerChimrod <>2023-10-01 19:07:17 +0200
commit622bbf897af29ec6f7d533f915b0170d3a9c899f (patch)
treef985dc2f40c8ca9938e93b75fb266fe2b12330ae /test/syntax_error.ml
parent2e41a214e4c2a2984ad3b2afa3d80178d227927f (diff)
Added some test for syntax errors
Diffstat (limited to 'test/syntax_error.ml')
-rw-r--r--test/syntax_error.ml85
1 files changed, 85 insertions, 0 deletions
diff --git a/test/syntax_error.ml b/test/syntax_error.ml
new file mode 100644
index 0000000..b907bc7
--- /dev/null
+++ b/test/syntax_error.ml
@@ -0,0 +1,85 @@
+module Parser = Qparser.Parser.Make (Qsp_syntax.Tree)
+module Ast = Qsp_syntax.Tree.Ast
+
+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 : Ast.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 :
+ (Ast.pos Syntax.location, Qsp_syntax.Report.t) result -> Ast.pos report =
+ function
+ | Ok _ -> failwith "No error"
+ | Error { level; loc; message } -> { level; loc; message }
+
+let _test_instruction : string -> Ast.pos report -> unit =
+ fun literal expected ->
+ let _location = Printf.sprintf {|# Location
+%s
+------- |} literal in
+ let actual = get_report @@ Syntax.parse _location and msg = literal in
+
+ Alcotest.(check' report ~msg ~expected ~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 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;
+ ] )