diff --git a/doc/source/index.rst b/doc/source/index.rst index 8035223e302b36a6662a408c1cfca27a593dec54..e4933f148669414ea3de65bb12079eb51aa00991 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -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` - diff --git a/src/Contents.m b/src/Contents.m index e2ba145da94d9666c7a9c2634274f327f06f923e..5a9b3ee697673a7187bda0cf91ff3df97fe0b416 100644 --- a/src/Contents.m +++ b/src/Contents.m @@ -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') diff --git a/src/build_report_summary.m b/src/build_report_summary.m index 1f76cc3519879c59a401fb534c81238024ec0b9d..6c1f619c1b244b497d4c00cf31495793ade428be 100644 --- a/src/build_report_summary.m +++ b/src/build_report_summary.m @@ -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 diff --git a/src/display_report.m b/src/display_report.m index c23ab876ae4d739384dc4862340c7e98289386b5..8f8f09a4dbfa404e99278c3e9c973b9d69c84f9c 100644 --- a/src/display_report.m +++ b/src/display_report.m @@ -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 diff --git a/src/is_unitary_test_available.m b/src/is_unit_test_available.m similarity index 60% rename from src/is_unitary_test_available.m rename to src/is_unit_test_available.m index fc18b5dacd2a6ea0cb39b6ea0255b47c60b69dc0..8f62a5f3997ed06b39edd6a06de66d202eea4f98 100644 --- a/src/is_unitary_test_available.m +++ b/src/is_unit_test_available.m @@ -1,15 +1,15 @@ -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 diff --git a/src/mtest.m b/src/mtest.m index a666c51073b0a51275425c1545a069e898c5e30f..223de435d251a3da6ceba492f30610fc0859e8b9 100644 --- a/src/mtest.m +++ b/src/mtest.m @@ -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 diff --git a/src/run_unitary_tests.m b/src/run_unit_tests.m similarity index 82% rename from src/run_unitary_tests.m rename to src/run_unit_tests.m index 6f4920df12255f1b637125b4631253d84fc028bb..3d87f6f5e757664420b80619bbff4098f8f086dd 100644 --- a/src/run_unitary_tests.m +++ b/src/run_unit_tests.m @@ -1,20 +1,20 @@ -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; diff --git a/src/run_unitary_tests_in_directory.m b/src/run_unit_tests_in_directory.m similarity index 82% rename from src/run_unitary_tests_in_directory.m rename to src/run_unit_tests_in_directory.m index 9855345a3d741ed49f922d52ad91464b5d8df262..82322fd81c18a5378b5e0e9ed0f7d80701504cf2 100644 --- a/src/run_unitary_tests_in_directory.m +++ b/src/run_unit_tests_in_directory.m @@ -1,6 +1,6 @@ -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