Skip to content

Commit

Permalink
Merge pull request #202 from catch22/quiet
Browse files Browse the repository at this point in the history
WIP: Add a hidden "-quiet" directive
  • Loading branch information
cbm755 authored Mar 19, 2019
2 parents 8abc9a4 + b5f3c30 commit 55a8f08
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 36 deletions.
43 changes: 29 additions & 14 deletions inst/doctest.m
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@

% input parsing for options and directives
recursive = true;
if (nargout < 3)
verbose = true;
else
verbose = false;
end
directives = doctest_default_directives();
for i = 1:(nargin-1)
assert(ischar(varargin{i}))
Expand All @@ -281,6 +286,14 @@
case 'nonrecursive'
assert(strcmp(pm, '-'))
recursive = false;
case 'quiet'
% currently not mentioned in help text
assert(strcmp(pm, '-'))
verbose = false;
case 'verbose'
% currently not mentioned in help text
assert(strcmp(pm, '-'))
verbose = true;
otherwise
assert(strcmp(pm, '+') || strcmp(pm, '-'))
warning('Doctest:deprecated', ...
Expand All @@ -298,9 +311,9 @@
% get terminal color codes
[color_ok, color_err, color_warn, reset] = doctest_colors(fid);

% print banner
fprintf(fid, 'Doctest v0.6.1+: this is Free Software without warranty, see source.\n\n');

if (verbose)
fprintf(fid, 'Doctest v0.6.1+: this is Free Software without warranty, see source.\n\n');
end

summary = struct();
summary.num_targets = 0;
Expand Down Expand Up @@ -331,7 +344,7 @@
% Collect and run tests
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:numel(what)
summary = doctest_collect(what{i}, directives, summary, recursive, 0, fid);
summary = doctest_collect(what{i}, directives, summary, recursive, verbose, 0, fid);
end


Expand All @@ -343,18 +356,20 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Report summary
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(fid, '\nSummary:\n\n');
if (summary.num_tests_passed == summary.num_tests)
fprintf(fid, [' ' color_ok 'PASS %4d/%-4d' reset '\n\n'], summary.num_tests_passed, summary.num_tests);
else
fprintf(fid, [' ' color_err 'FAIL %4d/%-4d' reset '\n\n'], summary.num_tests - summary.num_tests_passed, summary.num_tests);
end
if (verbose)
fprintf(fid, '\nSummary:\n\n');
if (summary.num_tests_passed == summary.num_tests)
fprintf(fid, [' ' color_ok 'PASS %4d/%-4d' reset '\n\n'], summary.num_tests_passed, summary.num_tests);
else
fprintf(fid, [' ' color_err 'FAIL %4d/%-4d' reset '\n\n'], summary.num_tests - summary.num_tests_passed, summary.num_tests);
end

fprintf(fid, '%d/%d targets passed, %d without tests', summary.num_targets_passed, summary.num_targets, summary.num_targets_without_tests);
if summary.num_targets_with_extraction_errors > 0
fprintf(fid, [', ' color_err '%d with extraction errors' reset], summary.num_targets_with_extraction_errors);
fprintf(fid, '%d/%d targets passed, %d without tests', summary.num_targets_passed, summary.num_targets, summary.num_targets_without_tests);
if summary.num_targets_with_extraction_errors > 0
fprintf(fid, [', ' color_err '%d with extraction errors' reset], summary.num_targets_with_extraction_errors);
end
fprintf(fid, '.\n\n');
end
fprintf(fid, '.\n\n');

if nargout == 1
varargout = {summary.num_targets_passed == summary.num_targets};
Expand Down
52 changes: 30 additions & 22 deletions inst/private/doctest_collect.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function summary = doctest_collect(what, directives, summary, recursive, depth, fid)
function summary = doctest_collect(what, directives, summary, recursive, verbose, depth, fid)
%DOCTEST_COLLECT Find and run doctests.
%
% The parameter WHAT is the name of a class, directory, function or filename:
Expand All @@ -10,7 +10,7 @@
%%
% Copyright (c) 2010 Thomas Grenfell Smith
% Copyright (c) 2015 Michael Walter
% Copyright (c) 2015-2018 Colin B. Macdonald
% Copyright (c) 2015-2019 Colin B. Macdonald
% Copyright (c) 2015 Oliver Heimlich
% Copyright (C) 2018 Mike Miller
% This is Free Software, BSD-3-Clause, see doctest.m for details.
Expand Down Expand Up @@ -94,7 +94,9 @@
else
slashchar = filesep();
end
fprintf(fid, '%s%s%s\n', spaces, what, slashchar);
if (verbose)
fprintf(fid, '%s%s%s\n', spaces, what, slashchar);
end
end
oldcwd = chdir(what);
files = dir('.');
Expand All @@ -121,7 +123,7 @@
continue
end
end
summary = doctest_collect(f, directives, summary, recursive, depth + 1, fid);
summary = doctest_collect(f, directives, summary, recursive, verbose, depth + 1, fid);
end
chdir(oldcwd);
return
Expand Down Expand Up @@ -174,13 +176,17 @@
target = targets(i);
spaces = repmat(' ', 1, 2*target.depth);
dots = repmat('.', 1, 55 - numel(target.name) - 2*target.depth);
fprintf(fid, '%s%s %s ', spaces, target.name, dots);
if (verbose)
fprintf(fid, '%s%s %s ', spaces, target.name, dots);
end

% extraction error?
if target.error
summary.num_targets_with_extraction_errors = summary.num_targets_with_extraction_errors + 1;
fprintf(fid, [color_err 'EXTRACTION ERROR' reset '\n\n']);
fprintf(fid, ' %s\n\n', target.error);
if (verbose)
fprintf(fid, [color_err 'EXTRACTION ERROR' reset '\n\n']);
fprintf(fid, ' %s\n\n', target.error);
end
continue;
end

Expand All @@ -206,22 +212,24 @@
summary.num_targets_without_tests = summary.num_targets_without_tests + 1;
end

% pretty print outcome
if num_tests == 0
fprintf(fid, 'NO TESTS\n');
elseif num_tests_passed == num_tests
fprintf(fid, [color_ok 'PASS %4d/%-4d' reset '\n'], num_tests_passed, num_tests);
else
fprintf(fid, [color_err 'FAIL %4d/%-4d' reset '\n\n'], num_tests - num_tests_passed, num_tests);
for j = 1:num_tests
if ~results(j).passed
fprintf(fid, ' >> %s\n\n', results(j).source);
fprintf(fid, [ ' expected: ' '%s' '\n' ], results(j).want);
fprintf(fid, [ ' got : ' color_err '%s' reset '\n' ], results(j).got);
if results(j).xfail
fprintf(fid, ' expected failure, but test succeeded!');
if (verbose)
% pretty print outcome
if num_tests == 0
fprintf(fid, 'NO TESTS\n');
elseif num_tests_passed == num_tests
fprintf(fid, [color_ok 'PASS %4d/%-4d' reset '\n'], num_tests_passed, num_tests);
else
fprintf(fid, [color_err 'FAIL %4d/%-4d' reset '\n\n'], num_tests - num_tests_passed, num_tests);
for j = 1:num_tests
if ~results(j).passed
fprintf(fid, ' >> %s\n\n', results(j).source);
fprintf(fid, [ ' expected: ' '%s' '\n' ], results(j).want);
fprintf(fid, [ ' got : ' color_err '%s' reset '\n' ], results(j).got);
if results(j).xfail
fprintf(fid, ' expected failure, but test succeeded!');
end
fprintf(fid, '\n');
end
fprintf(fid, '\n');
end
end
end
Expand Down

0 comments on commit 55a8f08

Please sign in to comment.