Skip to content
Snippets Groups Projects
Verified Commit 3c4079a8 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Change the tag triggering unit tests.

The unit tests are no more triggered by a comment on the first line (which is
kind of problematic for the help comand in Octave) but by a comment after the
last return statement.

Also English correction (unitary test -> unit test).
parent 343d9e13
Branches
Tags
No related merge requests found
...@@ -21,19 +21,24 @@ Contents: ...@@ -21,19 +21,24 @@ Contents:
for ``basedir``is the current directory. for ``basedir``is the current directory.
.. function:: is_unitary_test_available(fname) .. function:: is_unit_test_available(fname)
Returns ``1`` if unitary tests defined in matlab routine ``fname`` Returns ``true`` if unit tests defined in matlab routine ``fname``
have to be run, ``0`` otherwise. The mandatory input argument have to be run, ``false`` otherwise. The mandatory input argument
``fname`` is a string. The returned argument is set equal to one if ``fname`` is a string. The returned argument is set equal to ``true`` if
and only if the first line contains the following comment: and only if the file contains the following line:
.. code-block:: matlab .. code-block:: matlab
% --*-- Unitary tests --*-- return % --*-- Unit tests --*--
after the function header. after the core part of the routine, or
.. code-block:: matlab
end % classdef --*-- Unit tests --*--
at the end of the definition of a class.
.. function:: mtest(fname[, fpath]) .. function:: mtest(fname[, fpath])
...@@ -49,12 +54,12 @@ Contents: ...@@ -49,12 +54,12 @@ Contents:
- ``check`` (*integer*) Scalar equal to 0 if the test fails and 1 - ``check`` (*integer*) Scalar equal to 0 if the test fails and 1
otherwise. otherwise.
- ``info`` (*cell*) Describes the test results. Cell info has nn - ``info`` (*cell*) Describes the test results. Cell info has nn
rows and five columns. Each row correponds to a unitary test in rows and five columns. Each row correponds to a unit test in
fname, and the columns report the following informations: fname, and the columns report the following informations:
- Column 1 Name of the tested routine. - Column 1 Name of the tested routine.
- Column 2 Number of the unitary test. - Column 2 Number of the unit test.
- Column 3 Status of the unitary test (0 if the unitary test fails, 1 otherwise). - Column 3 Status of the unit test (0 if the unit test fails, 1 otherwise).
- Column 4 Details about the failure (vector of 0 and 1). - Column 4 Details about the failure (vector of 0 and 1).
- Column 5 Elapsed time in seconds (cpu time). - Column 5 Elapsed time in seconds (cpu time).
...@@ -84,26 +89,26 @@ Contents: ...@@ -84,26 +89,26 @@ Contents:
**REMARKS** **REMARKS**
1. The full path to the tested routine must be provided in ``fname``. 1. The full path to the tested routine must be provided in ``fname``.
.. function:: run_unitary_tests(listoffiles) .. function:: run_unit_tests(listoffiles)
Runs unitary tests defined in a collection of files. Runs unit tests defined in a collection of files.
**INPUTS** **INPUTS**
- ``listoffiles`` (*cell of strings*) The list of m files (with - ``listoffiles`` (*cell of strings*) The list of m files (with
path) where the unitary tests are written. This cell, with path) where the unit tests are written. This cell, with
\\(n\\) elements, is the output of :func:`get_directory_description` routine. \\(n\\) elements, is the output of :func:`get_directory_description` routine.
**OUTPUTS** **OUTPUTS**
- ``report`` (*cell*) Results of the unitary tests (\\(n\\) rows - ``report`` (*cell*) Results of the unit tests (\\(n\\) rows
and five columns). Each row stores the second output argument and five columns). Each row stores the second output argument
of :func:`mtest` routine (info). of :func:`mtest` routine (info).
- ``time`` (*double*) Current date and time as date vector (see - ``time`` (*double*) Current date and time as date vector (see
matlab's documentation for the ``clock`` function). matlab's documentation for the ``clock`` function).
.. function:: run_unitary_tests_in_directory(dirname[, savereport[, printreport, sendreport]) .. function:: run_unit_tests_in_directory(dirname[, savereport[, printreport, sendreport])
Runs all the unitary tests defined in a directory (and subfolders). Runs all the unit tests defined in a directory (and subfolders).
**INPUTS** **INPUTS**
- ``dirname`` (*string*) Path to the directory. - ``dirname`` (*string*) Path to the directory.
...@@ -112,7 +117,7 @@ Contents: ...@@ -112,7 +117,7 @@ Contents:
- ``sendreport`` (*string*) Email adress. If not empty generated report is sent by email. - ``sendreport`` (*string*) Email adress. If not empty generated report is sent by email.
**OUTPUTS** **OUTPUTS**
- ``report`` (*cell*) First output argument of :func:`run_unitary_test routine`. - ``report`` (*cell*) First output argument of :func:`run_unit_test routine`.
**REMARKS** **REMARKS**
1. Git needs to be available on the system, and it is assumed that the content of ``dirname`` is versionned with Git. 1. Git needs to be available on the system, and it is assumed that the content of ``dirname`` is versionned with Git.
...@@ -123,7 +128,7 @@ Contents: ...@@ -123,7 +128,7 @@ Contents:
Displays detailed report for the testsuite. Displays detailed report for the testsuite.
**INPUTS** **INPUTS**
- ``report`` (*cell*) Output of :func:`run_unitary_tests_in_directory`. - ``report`` (*cell*) Output of :func:`run_unit_tests_in_directory`.
...@@ -133,4 +138,3 @@ Indices and tables ...@@ -133,4 +138,3 @@ Indices and tables
* :ref:`genindex` * :ref:`genindex`
* :ref:`modindex` * :ref:`modindex`
* :ref:`search` * :ref:`search`
...@@ -7,14 +7,14 @@ ...@@ -7,14 +7,14 @@
% dtest - Runs unit test defined in a Matlab/Octave routine, by calling mtest, and display results. % dtest - Runs unit test defined in a Matlab/Octave routine, by calling mtest, and display results.
% get_directory_description - Lists recursively all the *.m files in a directory. % get_directory_description - Lists recursively all the *.m files in a directory.
% initialize_unit_tests_toolbox - Initialization of the path to the m-unit-tests/src folder. % initialize_unit_tests_toolbox - Initialization of the path to the m-unit-tests/src folder.
% is_unitary_test_available - Decides if unitary tests defined in a Matlab/Octave routine have to be run % is_unit_test_available - Decides if unit tests defined in a Matlab/Octave routine have to be run
% mtest - Extracts unit test sections from Matlab/Octave's routine, executes the tests and reports results. % mtest - Extracts unit test sections from Matlab/Octave's routine, executes the tests and reports results.
% run_unitary_tests - Runs unitary tests defined in a collection of files. % run_unit_tests - Runs unit tests defined in a collection of files.
% run_unitary_tests_in_directory - Runs all the unitary tests defined in a directory (and subfolders). % run_unit_tests_in_directory - Runs all the unit tests defined in a directory (and subfolders).
% %
% %
% Example: % Example:
% %
% >> addpath m-unit-tests % >> addpath m-unit-tests
% >> initialize_unit_test_toolbox % >> initialize_unit_test_toolbox
% >> run_unitary_tests_in_directory('dates/src/@dates') % >> run_unit_tests_in_directory('dates/src/@dates')
\ No newline at end of file
...@@ -10,7 +10,7 @@ function str = build_report_summary(reportfile, printonscreen, mailreport) ...@@ -10,7 +10,7 @@ function str = build_report_summary(reportfile, printonscreen, mailreport)
% OUTPUTS % OUTPUTS
% - str [string] Report summary. % - str [string] Report summary.
% Copyright © 2013-2018 Dynare Team % Copyright © 2013-2023 Dynare Team
% %
% This file is part of Dynare (m-unit-tests module). % This file is part of Dynare (m-unit-tests module).
% %
...@@ -57,7 +57,7 @@ gitlastcommithash = reportfilecontent.gitlastcommithash; ...@@ -57,7 +57,7 @@ gitlastcommithash = reportfilecontent.gitlastcommithash;
str = 'Hi,'; str = 'Hi,';
str = char(str,''); str = char(str,'');
str = char(str,'This is a summary report for the unitary tests in Dynare. Full report can be found at:'); str = char(str,'This is a summary report for the unit tests in Dynare. Full report can be found at:');
str = char(str,''); str = char(str,'');
str = char(str,''); str = char(str,'');
str = char(str,['http://www.dynare.org/stepan/dynare/tests/' reportfile]); str = char(str,['http://www.dynare.org/stepan/dynare/tests/' reportfile]);
...@@ -68,9 +68,9 @@ str = char(str,gitinfo(1,:)); ...@@ -68,9 +68,9 @@ str = char(str,gitinfo(1,:));
str = char(str,gitinfo(2,:)); str = char(str,gitinfo(2,:));
str = char(str,''); str = char(str,'');
str = char(str,''); str = char(str,'');
str = char(str,['===========================']); str = char(str,'========================');
str = char(str,'DYNARE/MATLAB UNITARY TESTS'); str = char(str,'DYNARE/MATLAB UNIT TESTS');
str = char(str,'==========================='); str = char(str,'========================');
str = char(str,['| TOTAL: ' int2str(size(reportcell,1))]); str = char(str,['| TOTAL: ' int2str(size(reportcell,1))]);
str = char(str,['| PASS: ' int2str(length(find(cell2mat(reportcell(:,3)))))]); str = char(str,['| PASS: ' int2str(length(find(cell2mat(reportcell(:,3)))))]);
str = char(str,['| FAIL: ' int2str(length(find(~cell2mat(reportcell(:,3)))))]); str = char(str,['| FAIL: ' int2str(length(find(~cell2mat(reportcell(:,3)))))]);
...@@ -105,5 +105,5 @@ if sendmail ...@@ -105,5 +105,5 @@ if sendmail
for i=2:size(str,1) for i=2:size(str,1)
STR = [STR 10 deblank(str(i,:)) ]; STR = [STR 10 deblank(str(i,:)) ];
end end
sendmail(mailto,'Dynare/Matlab unitary tests',STR); sendmail(mailto,'Dynare/Matlab unit tests',STR);
end end
...@@ -3,12 +3,12 @@ function display_report(r) ...@@ -3,12 +3,12 @@ function display_report(r)
% Display detailed report on screen. % Display detailed report on screen.
% %
% INPUTS % INPUTS
% - r [cell] Output of run_all_unitary_tests routine. % - r [cell] Output of run_all_unit_tests routine.
% %
% OUTPUTS % OUTPUTS
% none % none
% Copyright © 2018 Dynare Team % Copyright © 2018-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
......
function info = is_unitary_test_available(fun) function info = is_unit_test_available(mfile)
% Decides if unitary tests defined in a matlab routine (file) have to be run % Decides if unitary tests defined in a matlab routine (file) have to be run
% by checking the content of the first line. % by checking the content of the first line.
% %
% INPUTS % INPUTS
% - fun [string], name of the routine (with full relative path) % - mfile [string] name of the routine (with full relative path)
% %
% OUTPUTS % OUTPUTS
% - info [integer], scalar equal to 1 if unitary tests must be run, 0 otherwise. % - info [logical] scalar, equal to true if unit tests must be run, false otherwise.
% Copyright © 2013-2017 Dynare Team % Copyright © 2013-2023 Dynare Team
% %
% This file is part of Dynare (m-unit-tests module). % This file is part of Dynare (m-unit-tests module).
% %
...@@ -26,12 +26,16 @@ function info = is_unitary_test_available(fun) ...@@ -26,12 +26,16 @@ function info = is_unitary_test_available(fun)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>. % along with Dynare. If not, see <https://www.gnu.org/licenses/>.
info = 0; info = false;
fid = fopen(fun,'r'); filecontent = fileread(mfile);
first_line = fgetl(fid);
fclose(fid);
if strfind(first_line,'% --*-- Unitary tests --*--') if ~isempty(regexp(filecontent, 'return(\s*)\%(\s*)--\*--(\s*)Unit tests(\s*)--\*--','match'))
info = 1; info = true;
return
end
if ~isempty(regexp(filecontent, 'end(\s*)\%(\s*)classdef(\s*)--\*--(\s*)Unit tests(\s*)--\*--','match'))
info = true;
return
end end
...@@ -9,12 +9,12 @@ function [check, info] = mtest(fname, fpath) ...@@ -9,12 +9,12 @@ function [check, info] = mtest(fname, fpath)
% OUTPUTS % OUTPUTS
% - check [integer], scalar equal to 0 if the test fails and 0 otherwise % - check [integer], scalar equal to 0 if the test fails and 0 otherwise
% - info [cell], a cell describing the test results. Cell info has nn rows and % - info [cell], a cell describing the test results. Cell info has nn rows and
% five columns. Each row correponds to a unitary test in fname, and the % five columns. Each row correponds to a unit test in fname, and the
% columns report the following informations: % columns report the following informations:
% %
% Column 1 Name of the tested routine. % Column 1 Name of the tested routine.
% Column 2 Number of the unitary test. % Column 2 Number of the unit test.
% Column 3 Status of the unitary test (0 if the unitary test fails, 1 otherwise). % Column 3 Status of the unit test (0 if the unit test fails, 1 otherwise).
% Column 4 Details about the failure (vector of 0 and 1). % Column 4 Details about the failure (vector of 0 and 1).
% Column 5 Elapsed time in seconds. % Column 5 Elapsed time in seconds.
% %
...@@ -24,7 +24,7 @@ function [check, info] = mtest(fname, fpath) ...@@ -24,7 +24,7 @@ function [check, info] = mtest(fname, fpath)
% - If two input arguments are provided, fname is the base name of the targeted % - If two input arguments are provided, fname is the base name of the targeted
% matlab routine and fpath is the path to this routine. % matlab routine and fpath is the path to this routine.
% Copyright © 2013-2017 Dynare Team % Copyright © 2013-2023 Dynare Team
% %
% This file is part of Dynare (m-unit-tests module). % This file is part of Dynare (m-unit-tests module).
% %
......
function [report, time] = run_unitary_tests(listoffiles) function [report, time] = run_unit_tests(listoffiles)
% Runs unitary tests defined in a collection of files. % Runs unit tests defined in a collection of files.
% %
% INPUTS % INPUTS
% - listoffiles [cell], The list of m files (with path) where the unitary tests are written. % - listoffiles [cell], The list of m files (with path) where the unit tests are written.
% This cell, with n elements, is the output of get_directory_description routine. % This cell, with n elements, is the output of get_directory_description routine.
% %
% OUTPUTS % OUTPUTS
% - report [cell], Results of the unitary tests (n rows and 5 columns). Each row stores % - report [cell], Results of the unit tests (n rows and 5 columns). Each row stores
% the second output argument of mtest routine (info). % the second output argument of mtest routine (info).
% - time [double], Current date and time as date vector. % - time [double], Current date and time as date vector.
% %
% %
% See also get_directory_description, mtest % See also get_directory_description, mtest
% Copyright © 2013-2017 Dynare Team % Copyright © 2013-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -43,7 +43,7 @@ skipline() ...@@ -43,7 +43,7 @@ skipline()
for f=1:length(listoffiles) for f=1:length(listoffiles)
if isempty(strfind(listoffiles{f},'.#')) if isempty(strfind(listoffiles{f},'.#'))
if is_unitary_test_available(listoffiles{f}) if is_unit_test_available(listoffiles{f})
testcoverage(1) = testcoverage(1) + 1; testcoverage(1) = testcoverage(1) + 1;
[check, info] = mtest(listoffiles{f}); [check, info] = mtest(listoffiles{f});
r0 = sprintf('[%s/%s]',num2str(sum([info{:,3}])),num2str(size(info,1))); r0 = sprintf('[%s/%s]',num2str(sum([info{:,3}])),num2str(size(info,1)));
...@@ -52,7 +52,7 @@ for f=1:length(listoffiles) ...@@ -52,7 +52,7 @@ for f=1:length(listoffiles)
else else
r1 = 'FAILED'; r1 = 'FAILED';
end end
disp(sprintf('***** Unitary tests in %s %s %s!', listoffiles{f}, r0, r1)); disp(sprintf('***** Unit tests in %s %s %s!', listoffiles{f}, r0, r1));
report = [report; info]; report = [report; info];
else else
testcoverage(2) = testcoverage(2) + 1; testcoverage(2) = testcoverage(2) + 1;
......
function report = run_unitary_tests_in_directory(dirname, savereport, printreport, sendreport) function report = run_unit_tests_in_directory(dirname, savereport, printreport, sendreport)
% Runs all the unitary tests defined in a directory (and subfolders). % Runs all the unit tests defined in a directory (and subfolders).
% %
% INPUTS % INPUTS
% - dirname [string], name of the directory where % - dirname [string], name of the directory where
...@@ -9,14 +9,14 @@ function report = run_unitary_tests_in_directory(dirname, savereport, printrepor ...@@ -9,14 +9,14 @@ function report = run_unitary_tests_in_directory(dirname, savereport, printrepor
% - sendreport [string], email address. If not empty the report is sent by email. % - sendreport [string], email address. If not empty the report is sent by email.
% %
% OUTPUTS % OUTPUTS
% - report [cell], first output argument of run_unitary_test routine. % - report [cell], first output argument of run_unit_test routine.
% %
% REMARKS % REMARKS
% 1. Git needs to be available on the system and it is assumed that the content of dirname is versionned with Git. % 1. Git needs to be available on the system and it is assumed that the content of dirname is versionned with Git.
% %
% See also get_directory_description, run_unitary_tests, build_report_summary % See also get_directory_description, run_unit_tests, build_report_summary
% Copyright © 2013-2017 Dynare Team % Copyright © 2013-2023 Dynare Team
% %
% This file is part of Dynare (m-unit-tests module). % This file is part of Dynare (m-unit-tests module).
% %
...@@ -58,7 +58,7 @@ listoffiles = get_directory_description(dirname); ...@@ -58,7 +58,7 @@ listoffiles = get_directory_description(dirname);
diary(['report-' gitlastcommithash '.log'] ) diary(['report-' gitlastcommithash '.log'] )
str = sprintf('Unitary tests in %s', dirname); str = sprintf('Unit tests in %s', dirname);
lstr = length(str); lstr = length(str);
sstr = repmat('*', 1, lstr); sstr = repmat('*', 1, lstr);
skipline() skipline()
...@@ -66,7 +66,7 @@ disp(sstr) ...@@ -66,7 +66,7 @@ disp(sstr)
disp(str) disp(str)
disp(sstr) disp(sstr)
[report, time] = run_unitary_tests(listoffiles); [report, time] = run_unit_tests(listoffiles);
diary off diary off
if nargin>1 && savereport if nargin>1 && savereport
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment