Parcourir la source

Trying to generalise tests

 + Not working, problem with the length. Run
./test.native -q -e
   for more.
Leo il y a 9 ans
Parent
commit
94c2913be9
2 fichiers modifiés avec 31 ajouts et 12 suppressions
  1. 1 1
      src/list_rc.ml
  2. 30 11
      src/test/listrc_t.ml

+ 1 - 1
src/list_rc.ml

@@ -39,7 +39,7 @@ open Core.Std;;
 (* This modules contains function to list the content of the rc file *)
 (* This modules contains function to list the content of the rc file *)
 
 
 (* Characters to append to show a command was truncated *)
 (* Characters to append to show a command was truncated *)
-let trunc_indicator = "...";;
+let trunc_indicator = "....";;
 
 
 (* Truncate to elength, and add [trunc_indicator] after entries longer than
 (* Truncate to elength, and add [trunc_indicator] after entries longer than
  * elength or let them going through unmodified. *)
  * elength or let them going through unmodified. *)

+ 30 - 11
src/test/listrc_t.ml

@@ -41,26 +41,45 @@ open Core.Std;;
 (* Function truncate *)
 (* Function truncate *)
 let trunc (str, elength, expected) () =
 let trunc (str, elength, expected) () =
   let current = List_rc.truncate ~elength str in
   let current = List_rc.truncate ~elength str in
-  OUnit.assert_equal current expected
+  OUnit.assert_equal ~printer:(sprintf "%s") current expected
 ;;
 ;;
 
 
+(* TODO Generalise tests *)
+Random.self_init ();;
+(* Table of chars *)
+let table = [
+  'a' ; 'b' ; 'c' ; 'd' ; 'e' ; 'f' ; 'g' ; 'h' ; 'i' ; 'j' ; 'k' ; 'l' ; 'm' ;
+  'n' ; 'o' ; 'p' ; 'q' ; 'r' ; 's' ; 't' ; 'u' ; 'v' ; 'w' ; 'x' ; 'y' ; 'z' ;
+  '0' ; '1' ; '2' ; '3' ; '4' ; '5' ; '6' ; '7' ; '8' ; '9'
+];;
+(* Function to generate string for tests *)
+let gen_str len =
+  let rec gen_char () =
+    match Random.int 1_000_000 |> List.nth table with
+    | Some c -> c
+    | None -> gen_char ()
+  in
+  String.init len ~f:(fun _ -> gen_char ())
+;;
+(* Usefull value for the rest *)
+let ti = List_rc.trunc_indicator;;
+let til = String.length ti;;
+(* Some pseudo command to test *)
+let cright = gen_str (til + 6);;
+let cbit = "c" ^ gen_str til;;
+let ctoo = "c" ^ gen_str (til + 10);;
+
 let data = [
 let data = [
   ( ("", 4, ""), "Empty string" );
   ( ("", 4, ""), "Empty string" );
 
 
-  ( ("cmd1", 4, "cmd1"), "Right length" );
-  ( ("cmd11", 4, "c..."), "A bit longer" );
-  ( ("cmdddd1", 4, "c..."), "Much too long" );
+  ( (cright, til, cright), "Right length" );
+  ( (cbit, til, "c" ^ ti), "A bit longer" );
+  ( (ctoo, til, "c" ^ ti), "Much too long" );
 
 
-  ( ("cmd", 3, "cmd"), "Short command" );
-  ( ("cm", 3, "cm"), "Shorter command" );
-  ( ("c", 3, "c"), "Tiny command" );
+  ( ("c", til, "c"), "Tiny command" );
 
 
   ( ("cmd1", 0, "cmd1"), "Keep as-is" );
   ( ("cmd1", 0, "cmd1"), "Keep as-is" );
   ( ("cmd1", -5, "cmd1"), "Negative figure, keep as-is" );
   ( ("cmd1", -5, "cmd1"), "Negative figure, keep as-is" );
-  ( ("cmd1", (String.length List_rc.trunc_indicator), "cmd1"), "On the \
-  indicator, keep as-is" );
-  ( ("cmd1", (String.length List_rc.trunc_indicator) - 1, "cmd1"), "Under \
-  indicator, keep as-is" );
 ];;
 ];;
 
 
 let tests =
 let tests =