Version 0.2 :
Pierre Marechal [Thu, 8 Nov 2007 16:47:25 +0000 (16:47 +0000)]
 - Add options ( third argument );
 - Fix some problems

scilab/modules/development_tools/macros/unit_test_run.sci

index da14211..1bc926e 100644 (file)
@@ -14,83 +14,113 @@ function unit_test_run(varargin)
        
        global test_list;
        global test_count;
+       global displayed_txt;
+       
+       global check_ref;
+       global create_ref;
+       
+       check_ref          = %T;
+       create_ref         = %F;
        
        test_count         = 0;
        test_passed_count  = 0;
        test_failed_count  = 0;
        test_skipped_count = 0;
        
-       select rhs
-               case 0
-                       // No input argument
-                       // unit_test_run()
-                       // => Launch each test of each module
-                       
-                       module_list = getmodules();
-                       for k=1:size(module_list,'*')
-                               unit_test_add_module(module_list(k));
-                       end
-                       
-                       break
+       displayed_txt      = '';
+       
+       // =======================================================
+       // Gestion des tests à lancer
+       // =======================================================
+       
+       if (rhs == 0) | ((rhs == 1) & (varargin(1)==[])) then
                
-               case 1
-                       
-                       // One input argument
-                       // unit_test_run(<module_name>)
-                       // unit_test_run([<module_name_1>,<module_name_2>])
-                       
-                       // varargin(1) = [<module_name_1>,<module_name_2>]
-                       
-                       module_mat = varargin(1);
-                       
-                       [nl,nc] = size(module_mat);
-                       
-                       for i=1:nl
-                               for j=1:nc
-                                       if( with_module(module_mat(i,j)) ) then
-                                               unit_test_add_module(module_mat(i,j));
-                                       else
-                                               error(sprintf(gettext("%s is not an installed module"),module_mat(i,j)));
-                                       end
+               // No input argument
+               // unit_test_run()
+               // unit_test_run([])
+               // => Launch each test of each module
+               
+               module_list = getmodules();
+               for k=1:size(module_list,'*')
+                       unit_test_add_module(module_list(k));
+               end
+       
+       elseif (rhs == 1) ..
+                               | ((rhs == 2) & (varargin(2)==[])) ..
+                               | ((rhs == 3) & (varargin(2)==[])) then
+               
+               // One input argument
+               // unit_test_run(<module_name>)
+               // unit_test_run([<module_name_1>,<module_name_2>])
+               
+               // varargin(1) = [<module_name_1>,<module_name_2>]
+               
+               module_mat = varargin(1);
+               
+               [nl,nc] = size(module_mat);
+               
+               // unit_test_run([<module_name_1>,<module_name_2>])
+               
+               for i=1:nl
+                       for j=1:nc
+                               if( with_module(module_mat(i,j)) ) then
+                                       unit_test_add_module(module_mat(i,j));
+                               else
+                                       error(sprintf(gettext("%s is not an installed module"),module_mat(i,j)));
                                end
                        end
-                       
-                       break
-                       
-               case 2
-                       
-                       // Two input arguments
-                       // unit_test_run(<module_name>,<test_name>)
-                       // unit_test_run(<module_name>,[<test_name_1>,<test_name_2>] )
-                       
-                       // varargin(1) = <module_name> ==> string 1x1
-                       // varargin(2) = <test_name_1> ==> mat nl x nc
-                       
-                       module   = varargin(1);
-                       test_mat = varargin(2);
-                       
-                       if (size(module) <> [1,1]) & (size(module) <> [1,1]) then
-                               error(gettext("Input argument sizes are not valid"));
-                       end
-                       
-                       [nl,nc] = size(test_mat);
-                       
-                       for i=1:nl
-                               for j=1:nc
-                                       if( fileinfo(SCI+"/modules/"+module+"/unit_tests/"+test_mat(i,j)+".tst") <> [] ) then
-                                               unit_test_add_onetest(module,test_mat(i,j));
-                                       else
-                                               error(sprintf(gettext("The test ""%s"" is not available from the ""%s"" module"),test_mat(i,j),module));
-                                       end
+               end
+               
+       elseif (rhs == 2) | (rhs == 3) then
+               
+               // Two input arguments
+               // unit_test_run(<module_name>,<test_name>)
+               // unit_test_run(<module_name>,[<test_name_1>,<test_name_2>] )
+               
+               // varargin(1) = <module_name> ==> string 1x1
+               // varargin(2) = <test_name_1> ==> mat nl x nc
+               
+               module   = varargin(1);
+               test_mat = varargin(2);
+               
+               if (size(module) <> [1,1]) & (size(module) <> [1,1]) then
+                       error(gettext("Input argument sizes are not valid"));
+               end
+               
+               [nl,nc] = size(test_mat);
+               
+               for i=1:nl
+                       for j=1:nc
+                               if( fileinfo(SCI+"/modules/"+module+"/unit_tests/"+test_mat(i,j)+".tst") <> [] ) then
+                                       unit_test_add_onetest(module,test_mat(i,j));
+                               else
+                                       error(sprintf(gettext("The test ""%s"" is not available from the ""%s"" module"),test_mat(i,j),module));
                                end
                        end
-                       
-                       break
-                       
-               else
-                       error(gettext('Number of parameters incorrect.'));
+               end
+       else
+               error(gettext('Number of parameters incorrect.'));
        end
-
+       
+       // =======================================================
+       // Gestion des options
+       // =======================================================
+       
+       if rhs == 3 then
+               
+               option_mat =  varargin(3);
+               
+               if grep(option_mat,"no_check_ref") <> [] then
+                       check_ref  = %F;
+               end
+               
+               if grep(option_mat,"create_ref") <> [] then
+                       create_ref = %T;
+                       check_ref  = %F;
+               end
+               
+       end
+       
        // Test launch
        
        printf("\n");
@@ -104,10 +134,12 @@ function unit_test_run(varargin)
                end
                
                [status_id,status_msg] = unit_test_run_onetest(test_list(i,1),test_list(i,2));
-               printf("%s\n",status_msg);
+               printf("%s \n",status_msg);
                
                if status_id == 0 then
                        test_passed_count = test_passed_count + 1;
+               elseif status_id == 10 then
+                       test_skipped_count = test_skipped_count + 1;
                elseif status_id > 0 then
                        test_failed_count = test_failed_count + 1;
                end
@@ -115,15 +147,17 @@ function unit_test_run(varargin)
        
        // Summary
        
-       test_passed_percent = test_passed_count / test_count * 100;
-       test_failed_percent = test_failed_count / test_count * 100;
+       test_passed_percent  = test_passed_count / test_count * 100;
+       test_skipped_percent = test_skipped_count / test_count * 100;
+       test_failed_percent  = test_failed_count / test_count * 100;
 
        printf("\n");
        printf("\t---------------------------------------------------------------------------------------\n");
        printf("\tSummary\n\n");
-       printf("\ttests                     %4d - 100 %% \n",test_count);
-       printf("\tpassed                    %4d - %3d %% \n",test_passed_count,test_passed_percent);
-       printf("\tfailed                    %4d - %3d %% \n",test_failed_count,test_failed_percent);
+       printf("\ttests                     %4d - 100 percent \n",test_count);
+       printf("\tpassed                    %4d - %3d percent \n",test_passed_count,test_passed_percent);
+       printf("\tfailed                    %4d - %3d percent \n",test_failed_count,test_failed_percent);
+       printf("\tskipped                   %4d - %3d percent \n",test_failed_count,test_failed_percent);
        printf("\t---------------------------------------------------------------------------------------\n");
        
 endfunction
@@ -181,37 +215,83 @@ endfunction
 
 function [status_id,status_msg] = unit_test_run_onetest(module,test)
        
+       global check_ref;
+       global create_ref;
+       
+       status_id  = 0 ;
+       status_msg = "passed" ;
+       
+       [status_id,status_msg] = unit_test_run_checkerror(module,test);
+       
+       if check_ref | create_ref then
+               // Check ref or create ref
+               if status_id == 0 then
+                       [status_id,status_msg] = unit_test_run_proc_ref(module,test);
+               end
+       end
+       
+       return;
+       
+endfunction
+
+//-----------------------------------------------------------------------------
+// Pierre MARECHAL
+// Scilab team
+// Copyright INRIA
+// Date : 8 novembre 2007
+//
+// => Check the test
+//-----------------------------------------------------------------------------
+
+function [status_id,status_msg] = unit_test_run_checkerror(module,test)
+       
        status_id  = 0 ;
        status_msg = "passed" ;
        
        // Some definitions
        
        tstfile     = pathconvert(SCI+"/modules/"+module+"/unit_tests/"+test+".tst",%f,%f);
-       diafile     = pathconvert(SCI+"/modules/"+module+"/unit_tests/"+test+".dia",%f,%f);
-       reffile     = pathconvert(SCI+"/modules/"+module+"/unit_tests/"+test+".dia.ref",%f,%f);
        
-       tmp_tstfile = pathconvert(TMPDIR+"/"+test+".tst",%f,%f);
-       tmp_diafile = pathconvert(TMPDIR+"/"+test+".dia",%f,%f);
-       tmp_resfile = pathconvert(TMPDIR+"/"+test+".res",%f,%f);
-       tmp_errfile = pathconvert(TMPDIR+"/"+test+".err",%f,%f);
+       tmp_tstfile = pathconvert(TMPDIR+"/"+test+"_1.tst",%f,%f);
+       tmp_diafile = pathconvert(TMPDIR+"/"+test+"_1.dia",%f,%f);
+       tmp_resfile = pathconvert(TMPDIR+"/"+test+"_1.res",%f,%f);
+       tmp_errfile = pathconvert(TMPDIR+"/"+test+"_1.err",%f,%f);
        
-       // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
-       // printf("tstfile = %s\n",tstfile);
-       // printf("diafile = %s\n",diafile);
-       // printf("tmp_tstfile = %s\n",tmp_tstfile);
-       // printf("tmp_diafile = %s\n",tmp_diafile);
-       // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
+       // Remove the previous tmp files
+       if fileinfo(tmp_tstfile) <> [] then
+               deletefile(tmp_tstfile)
+       end
+       
+       if fileinfo(tmp_diafile) <> [] then
+               deletefile(tmp_diafile)
+       end
+       
+       if fileinfo(tmp_resfile) <> [] then
+               deletefile(tmp_resfile)
+       end
+       
+       if fileinfo(tmp_errfile) <> [] then
+               deletefile(tmp_errfile)
+       end
        
        //Reset standard globals
        rand('seed',0);
        rand('uniform');
-
-       // Do some modification in  tst file
        
+       // Get the tst file
        txt = mgetl(tstfile);
+       
+       // Check if it's an interactive test
+       if grep(txt,"<-- INTERACTIVE TEST -->")<>[] then
+               status_msg = "skipped : interactive test";
+               status_id  = 10;
+               return;
+       end
+       
+       // Do some modification in tst file
        txt = strsubst(txt,'pause,end','bugmes();quit;end');
        txt = strsubst(txt,'-->','@#>'); //to avoid suppression of input --> with prompts
-       txt = strsubst(txt,'halt()','');
+       txt = strsubst(txt,'halt();','');
        
        // Header : the same for each test
        
@@ -222,15 +302,14 @@ function [status_id,status_msg] = unit_test_run_onetest(module,test)
                                "lines(0);" ;
                                "deff(''[]=bugmes()'',''write(%io(2),''''error on test'''')'');" ;
                                "predef(''all'');" ;
-                               "tmpdirToPrint = msprintf(''TMPDIR1=''''%s''''\n'',TMPDIR);" ;
                                "try";
                                "diary(''"+tmp_diafile+"'');";
-                               "write(%io(2),tmpdirToPrint);";
                                "// <-- HEADER END -->"];
                                
        tail = [        "// <-- FOOTER START -->";
                                "catch";
-                               "       printf(""Error on the test script file"")";
+                               "       errmsg = ""<--""+""Error on the test script file""+""-->""";
+                               "       printf(""%s"",errmsg)";
                                "end";
                                "diary(0);";
                                "exit;";
@@ -243,25 +322,20 @@ function [status_id,status_msg] = unit_test_run_onetest(module,test)
        // and save it in a temporary file
        mputl(txt,tmp_tstfile);
        
-       // Delete previous dia file
-       if fileinfo(diafile) <> [] then
-               deletefile(diafile)
-       end
-       
        // Build the command to launch
        if MSDOS then
-               unit_test_cmd = """"+SCI+"\bin\scilex.exe"+""""+" -nw -args -nouserstartup -f "+tmp_tstfile+" 1>NUL";
+               unit_test_cmd = "( """+SCI+"\bin\scilex.exe"+""""+" -nw -nb -args -nouserstartup -f """+tmp_tstfile+""" > """+tmp_resfile+""" ) 2> """+tmp_errfile+"""";
        else
                unit_test_cmd = "( "+SCI+"/bin/scilab -nw -nb -args -nouserstartup -f "+tmp_tstfile+" > "+tmp_resfile+" ) 2> "+tmp_errfile;
        end
        
        // Launch the test exec
        
-       //printf("%s\n",unit_test_cmd);
        host(unit_test_cmd);
-
+       
        // First Check
        tmp_errfile_info = fileinfo(tmp_errfile);
+       
        if ( (tmp_errfile_info <> []) & (tmp_errfile_info(1)<>0) ) then
                status_msg = "failed : error_output not empty"
                status_id  = 5;
@@ -271,24 +345,15 @@ function [status_id,status_msg] = unit_test_run_onetest(module,test)
        //  Do some modification in  dia file
        dia = mgetl(tmp_diafile);
        
-       // To get TMPDIR line
-       tmpdir1_line = grep(dia,"TMPDIR1");
-       execstr(dia(tmpdir1_line));
-       
-       // Remove Header and Footer
-       
-       body_start = grep(dia,"// <-- HEADER END -->");
-       
-       if body_start<>[] then
-               dia(1:body_start(1)) = [];
+       //Check for execution errors
+       if grep(dia,"<--Error on the test script file-->")<>[] then
+               status_msg = "failed : premature end of the test script";
+               status_id = 3;
+               return;
        end
        
-       body_end   = grep(dia,"// <-- FOOTER START -->");
-       
-       if body_end<>[] then
-               [dia_nl,dia_nc] = size(dia);
-               dia(body_end(1):dia_nl) = [];
-       end
+       // Remove Header and Footer
+       dia = remove_headers(dia);
        
        //Check for execution errors
        
@@ -304,12 +369,127 @@ function [status_id,status_msg] = unit_test_run_onetest(module,test)
                return;
        end
        
-       if grep(dia,"Error on the test script file")<>[] then
-               status_msg = "failed : premature end of the test script";
-               status_id = 3;
-               return;
+       return;
+       
+endfunction
+
+//-----------------------------------------------------------------------------
+// Pierre MARECHAL
+// Scilab team
+// Copyright INRIA
+// Date : 28 oct. 2007
+//
+// => Check ref or generate ref
+//-----------------------------------------------------------------------------
+
+function [status_id,status_msg] = unit_test_run_proc_ref(module,test)
+       
+       global create_ref;
+       global check_ref;
+       
+       status_id  = 0 ;
+       status_msg = "passed" ;
+       
+       // Some definitions
+       
+       tstfile     = pathconvert(SCI+"/modules/"+module+"/unit_tests/"+test+".tst",%f,%f);
+       diafile     = pathconvert(SCI+"/modules/"+module+"/unit_tests/"+test+".dia",%f,%f);
+       reffile     = pathconvert(SCI+"/modules/"+module+"/unit_tests/"+test+".dia.ref",%f,%f);
+       
+       tmp_tstfile = pathconvert(TMPDIR+"/"+test+"_2.tst",%f,%f);
+       tmp_diafile = pathconvert(TMPDIR+"/"+test+"_2.dia",%f,%f);
+       tmp_resfile = pathconvert(TMPDIR+"/"+test+"_2.res",%f,%f);
+       tmp_errfile = pathconvert(TMPDIR+"/"+test+"_2.err",%f,%f);
+       
+       // On test l'existence de la ref si besoin
+       
+       if check_ref then
+               if fileinfo(reffile) == [] then
+                       status_msg = "failed : the ref file doesn''t exist";
+                       status_id  = 5;
+                       return;
+               end
+       end
+       
+       // Remove the previous tmp files
+       if fileinfo(tmp_tstfile) <> [] then
+               deletefile(tmp_tstfile)
+       end
+       
+       if fileinfo(tmp_diafile) <> [] then
+               deletefile(tmp_diafile)
+       end
+       
+       if fileinfo(tmp_resfile) <> [] then
+               deletefile(tmp_resfile)
+       end
+       
+       if fileinfo(tmp_errfile) <> [] then
+               deletefile(tmp_errfile)
+       end
+       
+       //Reset standard globals
+       rand('seed',0);
+       rand('uniform');
+
+       // Do some modification in  tst file
+       
+       txt = mgetl(tstfile);
+       txt = strsubst(txt,'pause,end','bugmes();quit;end');
+       txt = strsubst(txt,'-->','@#>'); //to avoid suppression of input --> with prompts
+       txt = strsubst(txt,'halt()','');
+       
+       // Header : the same for each test
+       
+       head = [        "// <-- HEADER START -->";
+                               "mode(3);" ;
+                               "clear;" ;
+                               "lines(28,72);";
+                               "lines(0);" ;
+                               "deff(''[]=bugmes()'',''write(%io(2),''''error on test'''')'');" ;
+                               "predef(''all'');" ;
+                               "tmpdirToPrint = msprintf(''TMPDIR1=''''%s''''\n'',TMPDIR);" ;
+                               "diary(''"+tmp_diafile+"'');";
+                               "write(%io(2),tmpdirToPrint);";
+                               "// <-- HEADER END -->"];
+                               
+       tail = [        "// <-- FOOTER START -->";
+                               "diary(0);";
+                               "exit;";
+                               "// <-- FOOTER END -->"]
+       
+       txt = [head;
+               txt;
+               tail];
+       
+       // and save it in a temporary file
+       mputl(txt,tmp_tstfile);
+       
+       // Delete previous dia file
+       if fileinfo(diafile) <> [] then
+               deletefile(diafile)
+       end
+       
+       // Build the command to launch
+       if MSDOS then
+               unit_test_cmd = "( """+SCI+"\bin\scilex.exe"+""""+" -nw -nb -args -nouserstartup -f """+tmp_tstfile+""" > """+tmp_resfile+""" ) 2> """+tmp_errfile+"""";
+       else
+               unit_test_cmd = "( "+SCI+"/bin/scilab -nw -nb -args -nouserstartup -f "+tmp_tstfile+" > "+tmp_resfile+" ) 2> "+tmp_errfile;
        end
        
+       // Launch the test exec
+       host(unit_test_cmd);
+       
+       //  Do some modification in  dia file
+       dia = mgetl(tmp_diafile);
+       
+       // To get TMPDIR line
+       tmpdir1_line = grep(dia,"TMPDIR1");
+       execstr(dia(tmpdir1_line));
+       
+       // Remove Header and Footer
+       dia = remove_headers(dia);
+       
        //  Do some modification in  dia file
        dia(grep(dia,"exec("))                     = [];
        dia(grep(dia,"write(%io(2),tmpdirToPrint"))= [];
@@ -331,33 +511,88 @@ function [status_id,status_msg] = unit_test_run_onetest(module,test)
 
        //not to change the ref files
        dia=strsubst(dia,'bugmes();return','bugmes();quit');
-       
-       // write down the resulting dia file
-       mputl(dia,diafile)
-       
-       //Check for diff with the .ref file
-       
-       [u,ierr] = mopen(reffile,'r');
-       if ierr== 0 then //ref file exists
+
+       if create_ref then
+               
+               // Delete previous .dia.ref file
+               if fileinfo(reffile) <> [] then
+                       deletefile(reffile)
+               end
+               
+               mputl(dia,reffile);
+               
+               status_msg = "passed : ref created";
+               status_id  = 20;
+               
+               return;
+               
+       else
                
-               ref=mgetl(u);
-               mclose(u)
+               // write down the resulting dia file
+               mputl(dia,diafile)
                
-               // suppress blank (diff -nw)
-               dia=strsubst(dia,' ','')
-               ref=strsubst(ref,' ','')
+               //Check for diff with the .ref file
                
-               if or(ref<>dia) then
-                       if MSDOS then
-                               status_msg = "failed : dia and ref are not equal";
-                               status_id = 4;
-                       else
-                               status_msg = "failed : dia and ref are not equal";
-                               status_id = 4;
+               [u,ierr] = mopen(reffile,'r');
+               if ierr== 0 then //ref file exists
+                       
+                       ref=mgetl(u);
+                       mclose(u)
+                       
+                       // suppress blank (diff -nw)
+                       
+                       dia = strsubst(dia,' ','')
+                       ref = strsubst(ref,' ','')
+                       
+                       dia(find(dia=='')) = [];
+                       ref(find(ref=='')) = [];
+                       
+                       dia(find(dia=='')) = [];
+                       ref(find(ref=='')) = [];
+                       
+                       if or(ref<>dia) then
+                               if MSDOS then
+                                       status_msg = "failed : dia and ref are not equal";
+                                       status_id = 4;
+                               else
+                                       status_msg = "failed : dia and ref are not equal";
+                                       status_id = 4;
+                               end
                        end
+               else
+                       error(sprintf(gettext("The ref file (%s) doesn''t exist"),reffile));
                end
-       else
-               error(sprintf(gettext("The ref file (%s) doesn''t exist"),reffile));
+       end
+       
+       return;
+       
+endfunction
+
+//-----------------------------------------------------------------------------
+// Pierre MARECHAL
+// Scilab team
+// Copyright INRIA
+// Date : 8 novembre 2007
+//
+// => remove header from the diary txt
+//
+//-----------------------------------------------------------------------------
+
+function dia_out = remove_headers(dia_in)
+       
+       dia_out = dia_in;
+       
+       body_start = grep(dia_out,"// <-- HEADER END -->");
+       
+       if body_start<>[] then
+               dia_out(1:body_start(1)) = [];
+       end
+       
+       body_end   = grep(dia_out,"// <-- FOOTER START -->");
+       
+       if body_end<>[] then
+               [dia_nl,dia_nc] = size(dia);
+               dia_out(body_end(1):dia_nl) = [];
        end
        
        return;