* Bug #10415 fixed - Remove a session line in history causes the removal 15/10615/4
Simon Marchetto [Tue, 26 Feb 2013 11:08:57 +0000 (12:08 +0100)]
                     of the session node in history browser

+ removelinehistory unit tests

Change-Id: I23741ebee565ede41d87a3fb3d788cb080391bb9

scilab/CHANGES_5.4.X
scilab/modules/history_manager/src/cpp/HistoryManager.cpp
scilab/modules/history_manager/src/cpp/HistoryManager.hxx
scilab/modules/history_manager/tests/nonreg_tests/bug_10415.tst [new file with mode: 0644]
scilab/modules/history_manager/tests/unit_tests/removelinehistory.dia.ref [new file with mode: 0644]
scilab/modules/history_manager/tests/unit_tests/removelinehistory.tst [new file with mode: 0644]

index 5640e27..e695d77 100644 (file)
@@ -230,6 +230,9 @@ Bug fixes
 * Bug #9305 fixed - In the optimisation help page, new chapter "Least Squares functions"
                     created.
 
+* Bug #10415 fixed - Remove a session line in history causes the removal
+                     of the session node in history browser
+
 * Bug #10606 fixed - unix_g did not return stdout in somes cases.
 
 * Bug #10661 fixed - xmltopdf & xmltopdf were failing in the binary version.
index 23b2fb8..d09aa4e 100644 (file)
@@ -272,7 +272,9 @@ BOOL setDefaultFilenameScilabHistory(void)
 void resetScilabHistory(void)
 {
     if (ScilabHistory)
+    {
         ScilabHistory->reset();
+    }
 }
 
 /*------------------------------------------------------------------------*/
@@ -301,7 +303,9 @@ int getSizeAllLinesOfScilabHistory(void)
     char **lines = NULL;
 
     if (ScilabHistory)
+    {
         lines = ScilabHistory->getAllLines(&nbElements);
+    }
 
     freeArrayOfString(lines, nbElements);
 
@@ -314,7 +318,9 @@ char *getLastLineInScilabHistory(void)
     char *line = NULL;
 
     if (ScilabHistory)
+    {
         line = ScilabHistory->getLastLine();
+    }
     return line;
 }
 
@@ -324,7 +330,9 @@ char *getPreviousLineInScilabHistory(void)
     char *line = NULL;
 
     if (ScilabHistory)
+    {
         line = ScilabHistory->getPreviousLine();
+    }
     return line;
 }
 
@@ -334,7 +342,9 @@ char *getNextLineInScilabHistory(void)
     char *line = NULL;
 
     if (ScilabHistory)
+    {
         line = ScilabHistory->getNextLine();
+    }
     return line;
 }
 
@@ -344,7 +354,9 @@ int getNumberOfLinesInScilabHistory(void)
     int val = 0;
 
     if (ScilabHistory)
+    {
         val = ScilabHistory->getNumberOfLines();
+    }
     return val;
 }
 
@@ -352,14 +364,18 @@ int getNumberOfLinesInScilabHistory(void)
 void setSaveConsecutiveDuplicateLinesInScilabHistory(BOOL doit)
 {
     if (ScilabHistory)
+    {
         ScilabHistory->setSaveConsecutiveDuplicateLines(doit);
+    }
 }
 
 /*------------------------------------------------------------------------*/
 BOOL getSaveConsecutiveDuplicateLinesInScilabHistory(void)
 {
     if (ScilabHistory)
+    {
         return ScilabHistory->getSaveConsecutiveDuplicateLines();
+    }
     return FALSE;
 }
 
@@ -367,7 +383,9 @@ BOOL getSaveConsecutiveDuplicateLinesInScilabHistory(void)
 void setAfterHowManyLinesScilabHistoryIsSaved(int num)
 {
     if (ScilabHistory)
+    {
         ScilabHistory->setAfterHowManyLinesHistoryIsSaved(num);
+    }
 }
 
 /*------------------------------------------------------------------------*/
@@ -376,7 +394,9 @@ int getAfterHowManyLinesScilabHistoryIsSaved(void)
     int val = 0;
 
     if (ScilabHistory)
+    {
         val = ScilabHistory->getAfterHowManyLinesHistoryIsSaved();
+    }
     return val;
 }
 
@@ -384,7 +404,9 @@ int getAfterHowManyLinesScilabHistoryIsSaved(void)
 char *getNthLineInScilabHistory(int N)
 {
     if (ScilabHistory)
+    {
         return ScilabHistory->getNthLine(N);
+    }
     return NULL;
 }
 
@@ -404,7 +426,9 @@ int getSizeScilabHistory(void)
     int val = 0;
 
     if (ScilabHistory)
+    {
         val = ScilabHistory->getNumberOfLines() - 1;
+    }
     return val;
 }
 
@@ -412,14 +436,20 @@ int getSizeScilabHistory(void)
 BOOL setSizeMaxScilabHistory(int nbLinesMax)
 {
     BOOL bOK = FALSE;
-    if (ScilabHistory) bOK = ScilabHistory->setNumberOfLinesMax(nbLinesMax);
+    if (ScilabHistory)
+    {
+        bOK = ScilabHistory->setNumberOfLinesMax(nbLinesMax);
+    }
     return bOK;
 }
 /*------------------------------------------------------------------------*/
 int getSizeMaxScilabHistory(void)
 {
     int val = 0;
-    if (ScilabHistory) val = ScilabHistory->getNumberOfLinesMax();
+    if (ScilabHistory)
+    {
+        val = ScilabHistory->getNumberOfLinesMax();
+    }
     return val;
 }
 /*------------------------------------------------------------------------*/
@@ -510,7 +540,9 @@ BOOL HistoryManager::appendLines(char **lines, int nbrlines)
     for (i = 0; i < nbrlines; i++)
     {
         if ((lines[i] == NULL) || (!appendLine(lines[i])))
+        {
             bOK = FALSE;
+        }
     }
     return bOK;
 }
@@ -659,7 +691,9 @@ char **HistoryManager::getAllLines(int *numberoflines)
     *numberoflines = 0;
 
     if (CommandsList.empty())
+    {
         return lines;
+    }
     else
     {
         list < CommandLine >::iterator it_commands;
@@ -730,7 +764,9 @@ char *HistoryManager::getNthLine(int N)
     char *line = NULL;
 
     if (N < 0)
+    {
         N = getNumberOfLines() + N;
+    }
 
     if ((N >= 0) && (N <= getNumberOfLines()))
     {
@@ -758,34 +794,40 @@ char *HistoryManager::getNthLine(int N)
 /*--------------------------------------------------------------------------*/
 BOOL HistoryManager::deleteNthLine(int N)
 {
-    BOOL bOK = FALSE;
-
     if ((N >= 0) && (N <= getNumberOfLines()))
     {
         int i = 0;
 
-        list < CommandLine >::iterator it_commands;
-        for (it_commands = CommandsList.begin(); it_commands != CommandsList.end(); ++it_commands)
+        list<CommandLine>::iterator it_start, it_end;
+        for (it_start = CommandsList.begin(); it_start != CommandsList.end(); ++it_start)
         {
             if (i == N)
             {
-                if (it_commands != CommandsList.end())
+                it_end = it_start;
+                it_end++;
+                if (isBeginningSessionLine(*it_start))
                 {
-                    std::string str;
-                    str.erase();
-                    CommandsList.erase(it_commands);
-                    // After a remove , we update search
-                    my_search.setHistory(CommandsList);
-                    my_search.setToken(str);
-
-                    CommandHistoryDeleteLine(N);
-                    return TRUE;
+                    for (; it_end != CommandsList.end(); ++it_end)
+                    {
+                        if (isBeginningSessionLine(*it_end))
+                        {
+                            break;
+                        }
+                    }
                 }
+
+                CommandsList.erase(it_start, it_end);
+
+                my_search.setHistory(CommandsList);
+                my_search.setToken(std::string());
+
+                CommandHistoryDeleteLine(N);
+                return TRUE;
             }
             i++;
         }
     }
-    return bOK;
+    return FALSE;
 }
 
 /*--------------------------------------------------------------------------*/
@@ -850,7 +892,9 @@ BOOL HistoryManager::setToken(char *token)
 {
     std::string Token;
     if (token)
+    {
         Token.assign(token);
+    }
     my_search.setHistory(CommandsList);
     return my_search.setToken(Token);
 }
@@ -875,20 +919,25 @@ BOOL HistoryManager::resetToken(void)
     return my_search.reset();
 }
 
+BOOL HistoryManager::isBeginningSessionLine(CommandLine& line)
+{
+    std::string str = line.get();
+    return isBeginningSessionLine(str.c_str());
+}
+
 /*--------------------------------------------------------------------------*/
-BOOL HistoryManager::isBeginningSessionLine(char *line)
+BOOL HistoryManager::isBeginningSessionLine(const char *line)
 {
     if (line)
     {
-        if (strlen(line) > strlen(SESSION_PRAGMA_BEGIN) + strlen(SESSION_PRAGMA_END))
-        {
-#define STR_LEN_MAX 64
-            char str_start[STR_LEN_MAX];
-            char str_end[STR_LEN_MAX];
+        size_t len_begin = strlen(SESSION_PRAGMA_BEGIN);
+        size_t len_end = strlen(SESSION_PRAGMA_END);
+        size_t len_line = strlen(line);
 
-            strncpy(str_start, line, (int)strlen(SESSION_PRAGMA_BEGIN));
-            strncpy(str_end, &line[strlen(line) - strlen(SESSION_PRAGMA_END)], (int)strlen(SESSION_PRAGMA_END));
-            if ((strcmp(str_start, SESSION_PRAGMA_BEGIN) == 0) && (strcmp(str_end, SESSION_PRAGMA_END) == 0))
+        if (len_line > len_begin + len_end)
+        {
+            if ((strncmp(line, SESSION_PRAGMA_BEGIN, len_begin) == 0) &&
+                    (strncmp(line + len_line - len_end, SESSION_PRAGMA_END, len_end) == 0))
             {
                 return TRUE;
             }
@@ -897,6 +946,8 @@ BOOL HistoryManager::isBeginningSessionLine(char *line)
     return FALSE;
 }
 
+
+
 /*--------------------------------------------------------------------------*/
 void HistoryManager::fixHistorySession(void)
 {
index eaaa573..61a57e8 100644 (file)
@@ -55,7 +55,7 @@ public:
     * @param size of the array of string
     * @return TRUE or FALSE
     */
-    BOOL appendLines(char **lines,int nbrlines);
+    BOOL appendLines(char **lines, int nbrlines);
 
     /**
     * Display history
@@ -226,10 +226,16 @@ private:
     int numberoflinesbeforehistoryissaved;
 
     /**
-    * search if line is a beginning of a session
+    * indicates if command line is a beginning of a session
     * @return TRUE or FALSE
     */
-    BOOL isBeginningSessionLine(char *line);
+    BOOL isBeginningSessionLine(CommandLine& line);
+
+    /**
+    * indicates if a string has the pattern "beginning of a session"
+    * @return TRUE or FALSE
+    */
+    BOOL isBeginningSessionLine(const char *line);
 
     /**
     * add as first line  beginning session info
diff --git a/scilab/modules/history_manager/tests/nonreg_tests/bug_10415.tst b/scilab/modules/history_manager/tests/nonreg_tests/bug_10415.tst
new file mode 100644 (file)
index 0000000..d25172c
--- /dev/null
@@ -0,0 +1,27 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- Non-regression test for bug 10415 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=10415
+//
+// <-- Short Description -->
+// The removal of an history session line does not remove
+// all the children command lines
+
+resethistory();
+
+// a session line will be added at the front of this history
+addhistory("lineA");
+addhistory("lineB");
+addhistory("lineC");
+
+// we check the removal of current session line remove all history
+removelinehistory(0);
+h = gethistory();
+assert_checkequal(h, []);
diff --git a/scilab/modules/history_manager/tests/unit_tests/removelinehistory.dia.ref b/scilab/modules/history_manager/tests/unit_tests/removelinehistory.dia.ref
new file mode 100644 (file)
index 0000000..7d26a2a
--- /dev/null
@@ -0,0 +1,57 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - DIGITEO
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+function [] = check_history(expected)
+  actual = gethistory();
+  // ignore first line of history which indicates the current session
+  actual(1,1) = []
+  assert_checkequal(actual, expected);
+endfunction
+// checking removing simple lines
+resethistory()
+// warning: a session line will be added at the front of this history
+addhistory("lineA");
+addhistory("lineB");
+addhistory("lineC");
+addhistory("lineD");
+removelinehistory(1);
+check_history(['lineB';'lineC';'lineD']);
+removelinehistory(2);
+h = gethistory();
+check_history(['lineB';'lineD']);
+removelinehistory(2);
+h = gethistory();
+check_history(['lineB']);
+removelinehistory(1);
+check_history([]);
+// check removing of session lines
+resethistory()
+// warning: a session line will be added at the front of this history
+expected_history = ["// -- 26/02/2013 10:00:00 -- //";
+                    "lineA";
+                    "lineB";
+                    "// -- 26/02/2013 11:00:00 -- //";
+                    "lineC";
+                    "// -- 26/02/2013 12:00:00 -- //";
+                    "// -- 26/02/2013 13:00:00 -- //"];
+addhistory(expected_history);
+removelinehistory(1);
+expected_history(1:3,1) = [];
+check_history(expected_history);
+removelinehistory(3);
+expected_history(3,1) = [];
+check_history(expected_history);
+removelinehistory(3);
+expected_history(3,1) = [];
+check_history(expected_history);
+removelinehistory(1);
+expected_history(1:2,1) = [];
+check_history(expected_history);
+// check error messages
+resethistory()
+ierr = execstr("removelinehistory(10)", "errcatch");
+assert_checkequal(ierr, 999);
+
diff --git a/scilab/modules/history_manager/tests/unit_tests/removelinehistory.tst b/scilab/modules/history_manager/tests/unit_tests/removelinehistory.tst
new file mode 100644 (file)
index 0000000..c240947
--- /dev/null
@@ -0,0 +1,72 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+function [] = check_history(expected)
+  actual = gethistory();
+  // ignore first line of history which indicates the current session
+  actual(1,1) = []
+  assert_checkequal(actual, expected);
+endfunction
+
+// checking removing simple lines
+
+resethistory()
+// warning: a session line will be added at the front of this history
+addhistory("lineA");
+addhistory("lineB");
+addhistory("lineC");
+addhistory("lineD");
+
+removelinehistory(1);
+check_history(['lineB';'lineC';'lineD']);
+
+removelinehistory(2);
+h = gethistory();
+check_history(['lineB';'lineD']);
+
+removelinehistory(2);
+h = gethistory();
+check_history(['lineB']);
+
+removelinehistory(1);
+check_history([]);
+
+// check removing of session lines
+
+resethistory()
+// warning: a session line will be added at the front of this history
+expected_history = ["// -- 26/02/2013 10:00:00 -- //";
+                    "lineA";
+                    "lineB";
+                    "// -- 26/02/2013 11:00:00 -- //";
+                    "lineC";
+                    "// -- 26/02/2013 12:00:00 -- //";
+                    "// -- 26/02/2013 13:00:00 -- //"];
+addhistory(expected_history);
+
+removelinehistory(1);
+expected_history(1:3,1) = [];
+check_history(expected_history);
+
+removelinehistory(3);
+expected_history(3,1) = [];
+check_history(expected_history);
+
+removelinehistory(3);
+expected_history(3,1) = [];
+check_history(expected_history);
+
+removelinehistory(1);
+expected_history(1:2,1) = [];
+check_history(expected_history);
+
+// check error messages
+
+resethistory()
+ierr = execstr("removelinehistory(10)", "errcatch");
+assert_checkequal(ierr, 999);
+