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
No related branches found
No related tags found
No related merge requests found
......@@ -21,19 +21,24 @@ Contents:
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``
have to be run, ``0`` otherwise. The mandatory input argument
``fname`` is a string. The returned argument is set equal to one if
and only if the first line contains the following comment:
Returns ``true`` if unit tests defined in matlab routine ``fname``
have to be run, ``false`` otherwise. The mandatory input argument
``fname`` is a string. The returned argument is set equal to ``true`` if
and only if the file contains the following line:
.. code-block:: matlab
% --*-- Unitary tests --*--
after the function header.
return % --*-- Unit tests --*--
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])
......@@ -49,12 +54,12 @@ Contents:
- ``check`` (*integer*) Scalar equal to 0 if the test fails and 1
otherwise.
- ``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:
- Column 1 Name of the tested routine.
- Column 2 Number of the unitary test.
- Column 3 Status of the unitary test (0 if the unitary test fails, 1 otherwise).
- Column 2 Number of the unit test.
- 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 5 Elapsed time in seconds (cpu time).
......@@ -84,26 +89,26 @@ Contents:
**REMARKS**
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**
- ``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.
**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
of :func:`mtest` routine (info).
- ``time`` (*double*) Current date and time as date vector (see
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**
- ``dirname`` (*string*) Path to the directory.
......@@ -112,7 +117,7 @@ Contents:
- ``sendreport`` (*string*) Email adress. If not empty generated report is sent by email.
**OUTPUTS**
- ``report`` (*cell*) First output argument of :func:`run_unitary_test routine`.
- ``report`` (*cell*) First output argument of :func:`run_unit_test routine`.
**REMARKS**
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:
Displays detailed report for the testsuite.
**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
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
......@@ -7,14 +7,14 @@
% 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.
% 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.
% run_unitary_tests - Runs unitary 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 - Runs unit tests defined in a collection of files.
% run_unit_tests_in_directory - Runs all the unit tests defined in a directory (and subfolders).
%
%
% Example:
%
% >> addpath m-unit-tests
% >> initialize_unit_test_toolbox
% >> run_unitary_tests_in_directory('dates/src/@dates')
\ No newline at end of file
% >> run_unit_tests_in_directory('dates/src/@dates')
......@@ -10,7 +10,7 @@ function str = build_report_summary(reportfile, printonscreen, mailreport)
% OUTPUTS
% - str [string] Report summary.
% Copyright © 2013-2018 Dynare Team
% Copyright © 2013-2023 Dynare Team
%
% This file is part of Dynare (m-unit-tests module).
%
......@@ -57,7 +57,7 @@ gitlastcommithash = reportfilecontent.gitlastcommithash;
str = 'Hi,';
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,['http://www.dynare.org/stepan/dynare/tests/' reportfile]);
......@@ -68,9 +68,9 @@ str = char(str,gitinfo(1,:));
str = char(str,gitinfo(2,:));
str = char(str,'');
str = char(str,'');
str = char(str,['===========================']);
str = char(str,'DYNARE/MATLAB UNITARY TESTS');
str = char(str,'===========================');
str = char(str,'========================');
str = char(str,'DYNARE/MATLAB UNIT TESTS');
str = char(str,'========================');
str = char(str,['| TOTAL: ' int2str(size(reportcell,1))]);
str = char(str,['| PASS: ' int2str(length(find(cell2mat(reportcell(:,3)))))]);
str = char(str,['| FAIL: ' int2str(length(find(~cell2mat(reportcell(:,3)))))]);
......@@ -105,5 +105,5 @@ if sendmail
for i=2:size(str,1)
STR = [STR 10 deblank(str(i,:)) ];
end
sendmail(mailto,'Dynare/Matlab unitary tests',STR);
end
\ No newline at end of file
sendmail(mailto,'Dynare/Matlab unit tests',STR);
end
......@@ -3,12 +3,12 @@ function display_report(r)
% Display detailed report on screen.
%
% INPUTS
% - r [cell] Output of run_all_unitary_tests routine.
% - r [cell] Output of run_all_unit_tests routine.
%
% OUTPUTS
% none
% Copyright © 2018 Dynare Team
% Copyright © 2018-2023 Dynare Team
%
% This file is part of Dynare.
%
......@@ -41,4 +41,4 @@ end
Results = [char(r(:,1)), repmat(' Unit test number ', n, 1), num2str(cell2mat(r(:,2))), repmat(' ', n, 1), char(R) repmat(' [ ', n,1), num2str(cell2mat(r(:,5))), repmat(' ]', n,1)];
Results
\ No newline at end of file
Results
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
% by checking the content of the first line.
%
% INPUTS
% - fun [string], name of the routine (with full relative path)
% - mfile [string] name of the routine (with full relative path)
%
% 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).
%
......@@ -26,12 +26,16 @@ function info = is_unitary_test_available(fun)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
info = 0;
info = false;
fid = fopen(fun,'r');
first_line = fgetl(fid);
fclose(fid);
filecontent = fileread(mfile);
if strfind(first_line,'% --*-- Unitary tests --*--')
info = 1;
end
\ No newline at end of file
if ~isempty(regexp(filecontent, 'return(\s*)\%(\s*)--\*--(\s*)Unit tests(\s*)--\*--','match'))
info = true;
return
end
if ~isempty(regexp(filecontent, 'end(\s*)\%(\s*)classdef(\s*)--\*--(\s*)Unit tests(\s*)--\*--','match'))
info = true;
return
end
......@@ -9,12 +9,12 @@ function [check, info] = mtest(fname, fpath)
% OUTPUTS
% - 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
% 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:
%
% Column 1 Name of the tested routine.
% Column 2 Number of the unitary test.
% Column 3 Status of the unitary test (0 if the unitary test fails, 1 otherwise).
% Column 2 Number of the unit test.
% 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 5 Elapsed time in seconds.
%
......@@ -24,7 +24,7 @@ function [check, info] = mtest(fname, fpath)
% - If two input arguments are provided, fname is the base name of the targeted
% 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).
%
......@@ -165,4 +165,4 @@ end
function file = append_extension_if_needed(file)
if ~isequal(file(end-1:end),'.m')
file = [file '.m'];
end
\ No newline at end of file
end
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
% - 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.
%
% 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).
% - time [double], Current date and time as date vector.
%
%
% See also get_directory_description, mtest
% Copyright © 2013-2017 Dynare Team
% Copyright © 2013-2023 Dynare Team
%
% This file is part of Dynare.
%
......@@ -43,7 +43,7 @@ skipline()
for f=1:length(listoffiles)
if isempty(strfind(listoffiles{f},'.#'))
if is_unitary_test_available(listoffiles{f})
if is_unit_test_available(listoffiles{f})
testcoverage(1) = testcoverage(1) + 1;
[check, info] = mtest(listoffiles{f});
r0 = sprintf('[%s/%s]',num2str(sum([info{:,3}])),num2str(size(info,1)));
......@@ -52,7 +52,7 @@ for f=1:length(listoffiles)
else
r1 = 'FAILED';
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];
else
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
% - dirname [string], name of the directory where
......@@ -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.
%
% OUTPUTS
% - report [cell], first output argument of run_unitary_test routine.
% - report [cell], first output argument of run_unit_test routine.
%
% REMARKS
% 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).
%
......@@ -58,7 +58,7 @@ listoffiles = get_directory_description(dirname);
diary(['report-' gitlastcommithash '.log'] )
str = sprintf('Unitary tests in %s', dirname);
str = sprintf('Unit tests in %s', dirname);
lstr = length(str);
sstr = repmat('*', 1, lstr);
skipline()
......@@ -66,7 +66,7 @@ disp(sstr)
disp(str)
disp(sstr)
[report, time] = run_unitary_tests(listoffiles);
[report, time] = run_unit_tests(listoffiles);
diary off
if nargin>1 && savereport
......@@ -78,4 +78,4 @@ if nargin>2
sendreport = '';
end
build_report_summary(['report-' gitlastcommithash '.mat'], printreport, sendreport);
end
\ No newline at end of file
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment