Parcourir la source

More accurate representation of the state of OcLaunch

 + Define type, almost like None and Some, but adding an empty state, to
 allow to behave more precisely, for instance for help messages.
 + Adapt tests to new type, on semantic purpose.
Leo il y a 9 ans
Parent
commit
3226d00701
3 fichiers modifiés avec 34 ajouts et 18 suppressions
  1. 7 2
      src/default.ml
  2. 16 5
      src/exec_cmd.ml
  3. 11 11
      src/test/exec_t.ml

+ 7 - 2
src/default.ml

@@ -55,13 +55,18 @@ let run ~rc cmd_number =
       (* Execute each item (one by one) in config file *)
       Exec_cmd.what_next ~tmp
       |> function
-      | None -> (* If no command was found, all has been launched *)
+      | Exec_cmd.Empty -> (* Nothing in RC file *)
+        Messages.ok "There is nothing in your RC file!";
+        Messages.tips "You can add entries with 'edit' or 'add' subcommand.";
+        Lock.remove ()
+      | Exec_cmd.Finish -> (* If no command was found, all has been launched *)
         Messages.ok "All has been launched!";
         Messages.tips "You can reset with 'reset-all' subcommand";
         Lock.remove ()
-      | Some cmd_to_exec -> Exec_cmd.execute cmd_to_exec;
+      | Exec_cmd.A cmd_to_exec -> Exec_cmd.execute cmd_to_exec;
     end
   | Some num -> begin
+      (* Run given (num) item *)
       File_com.num_cmd2cmd ~rc num
       |> function
       | None -> Messages.warning "Your number is out of bound"

+ 16 - 5
src/exec_cmd.ml

@@ -36,6 +36,13 @@
 
 open Core.Std;;
 
+(* Type to give a more accurate representation of the program *)
+type state =
+  | Empty (* Empty rc file *)
+  | Finish (* everything was launched *)
+  | A of string (* There is this command to launch *)
+;;
+
 (* Function allowing to set the title of the current terminal windows
  * XXX Maybe better in some lib *)
 let set_title new_title =
@@ -51,11 +58,15 @@ let set_title new_title =
 let less_launched (log : (string * int) list) =
   let open Option in
   let max = Const.default_launch in (* Number of launch, maximum *)
-  (* Return smallest, n is the smaller key *)
-  let entries_by_number = List.Assoc.inverse log in
-  List.min_elt ~cmp:(fun (n,_) (n',_) -> Int.compare n n') entries_by_number
-  |> fun smallest ->
-  bind smallest (fun (min, cmd) -> some_if (min < max) cmd)
+  (* Return smallest, n is the smaller key, if there are some entries *)
+  match log with
+  | [] -> Empty
+  | _ ->
+    let entries_by_number = List.Assoc.inverse log in
+    List.min_elt ~cmp:(fun (n,_) (n',_) -> Int.compare n n') entries_by_number
+    |> fun smallest ->
+    bind smallest (fun (min, cmd) -> some_if (min < max) cmd)
+    |> function None -> Finish | Some entry -> A entry
 ;;
 
 (* Function to get the number corresponding to the next command to launch (less

+ 11 - 11
src/test/exec_t.ml

@@ -58,7 +58,7 @@ let common_data =
   [
     ( [ ( "cmd1", 4 ) ; ( "cmd2", 0 ) ], "Canonical case 1" );
     ( [ ( "cmd1", 0 ) ; ( "cmd2", 5 ) ], "Canonical case 2" );
-    ( [], "Empty list" );
+    ( [], "Empty RC file" );
     ( [ ( "cmd1", 0 ) ; ( "cmd2", 3 ) ; ( "cmd3", 4 )  ; ( "cmd4", 5 ) ], "Canonical case 3" );
     ( [ ( "cmd1", 0 ) ; ( "cmd2", 4 ) ; ( "cmd3", 4 )  ; ( "cmd5", 5 ) ],
       "Twice the same number, with others" );
@@ -81,16 +81,16 @@ let add_solutions data expected =
 (* Data customized for the tests *)
 let ll_data =
   add_solutions common_data
-    [
-      Some "cmd2";
-      Some "cmd1";
-      None;
-      Some "cmd1";
-      Some "cmd1";
-      None;
-      None;
-      Some "cmd1";
-      Some "cmd3"
+    Exec_cmd.[
+      A "cmd2";
+      A "cmd1";
+      Empty;
+      A "cmd1";
+      A "cmd1";
+      Finish;
+      Finish;
+      A "cmd1";
+      A "cmd3"
     ]
 ;;
 let ll_data2 =