Use meaningful names of test cases
[tiger.ml.git] / tiger / src / exe / tiger_tests.ml
index af707b0..e222599 100644 (file)
@@ -1,9 +1,10 @@
 module List = ListLabels
 
 let test_01 =
+  let name = "an array type and an array variable" in
   let code =
     " \
-    /* an array type and an array variable */ \
+    /* "^name^" */ \
     let \
       type arrtype = array of int \
       var arr1:arrtype := \
@@ -24,16 +25,17 @@ let test_01 =
       END
     ]
   in
-  ("test_01", code, tokens)
+  (name, code, tokens)
 
 let test_02 =
+  let name = "arr1 is valid since expression 0 is int = myint" in
   let code =
     " \
-    /* arr1 is valid since expression 0 is int = myint */ \
+    /* "^name^" */ \
     let \
       type myint = int \
       type arrtype = array of myint \
-      var arr1:arrtype :=
+      var arr1:arrtype := \
         arrtype [10] of 0 \
     in \
       arr1 \
@@ -52,12 +54,13 @@ let test_02 =
       END
     ]
   in
-  ("test_02", code, tokens)
+  (name, code, tokens)
 
 let test_03 =
+  let name = "a record type and a record variable" in
   let code =
     " \
-    /* a record type and a record variable */ \
+    /* "^name^" */ \
     let \
       type rectype = \
         { name : string \
@@ -92,14 +95,46 @@ let test_03 =
       END
     ]
   in
-  ("test_03", code, tokens)
+  (name, code, tokens)
+
+let test_04 =
+  let name = "define a recursive function" in
+  let code =
+    " \
+    /* "^name^" */ \
+    let \
+    \
+      /* calculate n! */ \
+      function nfactor(n: int): int = \
+        if n = 0  \
+        then 1 \
+        else n * nfactor(n-1) \
+    \
+    in \
+      nfactor(10) \
+    end \
+    "
+  in
+  let tokens =
+    let open Tiger.Parser.Token in
+    [ LET;
+        FUNCTION; ID "nfactor"; LPAREN; ID "n"; COLON; ID "int"; RPAREN; COLON; ID "int"; EQ;
+          IF; ID "n"; EQ; INT 0;
+          THEN; INT 1;
+          ELSE; ID "n"; TIMES; ID "nfactor"; LPAREN; ID "n"; MINUS; INT 1; RPAREN;
+      IN;
+        ID "nfactor"; LPAREN; INT 10; RPAREN;
+      END
+    ]
+  in
+  (name, code, tokens)
 
 let tokens_of_code code =
   let lexbuf = Lexing.from_string code in
   let rec tokens () =
     match Tiger.Lexer.token lexbuf with
-    | Tiger.Parser.Token.EOF -> []
-    | token -> token :: tokens ()
+    | None -> []
+    | Some token -> token :: tokens ()
   in
   tokens ()
 
@@ -107,6 +142,7 @@ let tests =
   [ test_01
   ; test_02
   ; test_03
+  ; test_04
   ]
 
 let () =
@@ -121,7 +157,7 @@ let () =
       print_endline "OK";
     with Assert_failure _ ->
       let tokens_to_string tokens =
-        String.concat "; " (List.map Tiger.Parser.Token.to_string tokens)
+        String.concat "; " (List.map ~f:Tiger.Parser.Token.to_string tokens)
       in
       printf
         "ERROR\n    Expected: %s\n    Emitted : %s\n\n"
This page took 0.026516 seconds and 4 git commands to generate.