Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "pseudodirectives" istexinfo/isdiary #276

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions inst/private/doctest_collect.m
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
target.name = what;
target.link = '';
target.depth = depth;
[target.docstring, target.error] = parse_texinfo(fileread(what));
[target.docstring, target.error, target.isdiary] = parse_texinfo(fileread(what));
target.istexinfo = true;
targets = [target];
else
target = struct();
Expand Down Expand Up @@ -169,6 +170,9 @@
continue;
end

% non-configurable pseudo-directives: set based on the target
directives = doctest_default_directives(directives, 'IS_TEXINFO', target.istexinfo);
directives = doctest_default_directives(directives, 'IS_DIARY', target.isdiary);
% run doctest
results = doctest_run(target.docstring, directives);

Expand Down Expand Up @@ -224,7 +228,8 @@
else
target.link = sprintf('<a href="matlab:editorservices.openAndGoToLine(''%s'', 1);">%s</a>', which(what), what);
end
[target.docstring, target.error] = extract_docstring(target.name);
[target.docstring, target.error, target.istexinfo, target.isdiary] = ...
extract_docstring(target.name);
end


Expand All @@ -243,7 +248,8 @@
target.link = sprintf('<a href="matlab:editorservices.openAndGoToLine(''%s'', 1);">%s</a>', which(what), what);
end
target.depth = depth;
[target.docstring, target.error] = extract_docstring(target.name);
[target.docstring, target.error, target.istexinfo, target.isdiary] = ...
extract_docstring(target.name);
targets = target;

% Next, add targets for all class methods
Expand All @@ -258,17 +264,21 @@
target.link = sprintf('<a href="matlab:editorservices.openAndGoToFunction(''%s'', ''%s'');">%s</a>', which(what), meths{i}, target.name);
end
target.depth = depth;
[target.docstring, target.error] = extract_docstring(target.name);
[target.docstring, target.error, target.istexinfo, target.isdiary] = ...
extract_docstring(target.name);
targets = [targets; target];
end
end


function [docstring, error] = extract_docstring(name)
function [docstring, error, istexinfo, isdiary] = extract_docstring(name)
istexinfo = false;
isdiary = true;
if is_octave()
[docstring, format] = get_help_text(name);
if strcmp(format, 'texinfo')
[docstring, error] = parse_texinfo(docstring);
[docstring, error, isdiary] = parse_texinfo(docstring);
istexinfo = true;
elseif strcmp(format, 'plain text')
error = '';
elseif strcmp(format, 'Not documented')
Expand All @@ -295,9 +305,11 @@
end


function [docstring, error] = parse_texinfo(str)
function [docstring, error, isdiary] = parse_texinfo(str)
docstring = '';
error = '';
% texinfo could have diary-style, and that might be useful to know
isdiary = false;

% no example blocks? not an error, but nothing to do
if (isempty(strfind(str, '@example')))
Expand Down Expand Up @@ -390,6 +402,7 @@
L = strsplit (T{i}, '\n');
L = regexprep (L, '^(\s*)(?:⇒|=>|⊣|-\|)', '$1', 'once', 'lineanchors');
T{i} = strjoin (L, '\n');
isdiary = true;
continue
end

Expand Down
8 changes: 8 additions & 0 deletions inst/private/doctest_default_directives.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

defaults.normalize_whitespace = true;
defaults.ellipsis = true;
% pseudo-directives: will be detected per target
defaults.is_texinfo = [];
defaults.is_diary = [];


if (nargin == 0)
d = defaults;
Expand All @@ -29,6 +33,10 @@
d.ellipsis = enable;
case 'NORMALIZE_WHITESPACE'
d.normalize_whitespace = enable;
case 'IS_TEXINFO'
d.is_texinfo = enable;
case 'IS_DIARY'
d.is_diary = enable;
otherwise
error('invalid directive "%s"', directive)
end
Expand Down
2 changes: 2 additions & 0 deletions inst/private/doctest_run.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
% set default options
tests(i).normalize_whitespace = defaults.normalize_whitespace;
tests(i).ellipsis = defaults.ellipsis;
tests(i).is_texinfo = defaults.is_texinfo;
tests(i).is_diary = defaults.is_diary;
tests(i).skip = {};
tests(i).xfail = {};

Expand Down