Commits

ccorrodi  committed 534d75d

test wheter generated and given database match

The testsuite checks whether the lines that are no comments in the generated
database match with the lines of the input database. For this, a
"pretty_print_lines" function is introduced in ocamlmake.mli (which should be
changed later). Also, ast.mli contains additional mappings.

  • Participants
  • Parent commits 82f23a0
  • Branches develop

Comments (0)

Files changed (5)

 
 type dependency = {
   next : dependency;
+  name : string;
+  stem : string;
+  (* TODO: in dep.h, it is declared as struct file *)
+  file : string;
+  changed : int;
+  ignore_mtime : bool;
+  staticpattern : bool;
+  need_2nd_expansion : bool;
+  dontcare : bool;
 }
 
 type rule = {
 (** Dependency. TODO. *)
 type dependency = {
   next : dependency;
+  name : string;
+  stem : string;
+  file : string;
+  changed : int;
+  ignore_mtime : bool;
+  staticpattern : bool;
+  need_2nd_expansion : bool;
+  dontcare : bool;
 }
 
 (** Represent a rule definition. *)

File src/ocamlmake.ml

       Printf.printf "line: %d, cnum: %d, tok: \"%s\"\n" line cnum tok;
       raise exn
     end
+
+let pretty_print_lines (a : A.ast) : string list =
+  []

File src/ocamlmake.mli

 (** Parse the database from given input file input and return the generated
     AST.*)
 val main : in_channel -> Ast.ast
+
+(** Pretty print a given ast and return a string array. *)
+val pretty_print_lines : Ast.ast -> string list

File test/unitTests.ml

 module O = Ocamlmake
+module P = Pervasives
 open OUnit
 
-let test_makefile _ =
-  ignore (O.main (open_in "test/ocaml-make.mkdb"));
-  assert_equal 0 0
+(** Read lines from given input channel and return as a string list (without
+    newline characters). *)
+let read_lines (input : in_channel) : string list =
+  let lines = ref [] in
+  try
+    while true do
+      lines := input_line input :: !lines
+    done;
+    []
+  with End_of_file ->
+    close_in input;
+    List.rev !lines
+
+let rec pp (ls : string list) : string =
+  List.fold_right (fun a b -> a ^ "\n" ^ b) ls ""
+
+let startswith (re : string) (s : string) : bool =
+  Str.string_match (Str.regexp re) s 0
+
+let iscomment (s : string) : bool =
+  startswith "^\\(#\\|$\\)" s
+
+let rec has_non_comments (ls : string list) : bool =
+  match ls with
+  | l :: ls ->
+      if not (iscomment l) then
+        true
+      else
+        has_non_comments ls
+  | [] ->
+      false
+
+(** Return true, if original contains all lines of output in that order, and
+    original contains no extra non-comment lines. *)
+let rec equal (output : string list) (original : string list) : bool =
+  match output, original with
+  | x :: xs, _ when (iscomment x) ->
+      equal xs original
+  | _, y :: ys when (iscomment y) ->
+      equal output ys
+  | x :: xs, y :: ys ->
+      if x = y then
+        equal xs ys
+      else begin
+        false
+      end
+  | [], [] ->
+      true
+  | [], ys ->
+      has_non_comments ys
+  | xs, [] ->
+      has_non_comments xs
+
+(** Compare the given databases (values) and print to the given formatter info
+    about where the data did not match. *)
+let rec diff (fmt : Format.formatter) (values : 'a * 'a) : unit =
+  let ocamlmake_line = ref 1 in
+  let orig_line = ref 1 in
+  let rec helper (values : 'a * 'a) : unit =
+    match values with
+    | x :: xs, y :: ys ->
+        if (iscomment x) then begin
+          incr orig_line;
+          Printf.printf "orig line now: %d\n" !orig_line;
+          helper (xs, y :: ys)
+        end
+        else if (iscomment y) then begin
+          incr ocamlmake_line;
+          helper (x :: xs, ys)
+        end
+        else if x <> y then begin
+          Format.fprintf fmt
+            "Error on output line %d (original db file line %d)\n"
+            !ocamlmake_line !orig_line;
+          Format.fprintf fmt "Expected:\n%s\nGot:\n%s\n" x y;
+        end
+        else begin
+          incr ocamlmake_line;
+          incr orig_line;
+          helper (xs, ys)
+        end
+    | _, _ ->
+        ()
+  in
+  helper values
+
+let test_ocaml_make_prettyprint _ =
+  let original_lines = read_lines (open_in "test/ocaml-make.mkdb") in
+  let ast = O.main (open_in "test/ocaml-make.mkdb") in
+  let ocamlmake_lines = O.pretty_print_lines ast in
+  assert_equal
+    ~cmp:equal
+    ~pp_diff:diff
+    original_lines ocamlmake_lines
 
 let suite = "OUnit Example" >:::
   [
-    "test_main" >:: test_makefile;
+    "test_ocaml-make_prettyprint" >:: test_ocaml_make_prettyprint;
   ]
 
-let _ = run_test_tt suite
+let _ =
+  run_test_tt suite;