Skip to content

Commit

Permalink
try methods(foo) to determine if foo is an object
Browse files Browse the repository at this point in the history
Use a try-catch block with the methods call.  If it has methods it
must be a class.  This allows refactoring the object identification
code a bit.  Fixes a few bugs.  Fixes #199.  Fixes #200.  Fixes #211.

Related to Issue #196.
  • Loading branch information
cbm755 committed Mar 23, 2019
1 parent c31d2e5 commit b93e75d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
41 changes: 22 additions & 19 deletions inst/private/doctest_collect.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,36 @@
elseif (exist (what) == 3) % .oct/.mex
[~, what, ~] = fileparts (what); % strip extension if present
type = 'function'; % then access like any function
elseif (exist(what, 'file') && ~exist(what, 'dir')) || exist(what, 'builtin')
if (exist(['@' what], 'dir'))
% special case, e.g., @logical is class, logical is builtin
type = 'class';
else
type = 'unknown';
end

%% Let's see if its a class by checking if methods returns
% What about classdef in oct file above? Should we do this even if
% type is 'octfile'?
if (strcmp (type, 'unknown'))
if (~ isempty (what) && strcmp (what(1), '@'))
temp = what(2:end);
else
type = 'function';
temp = what;
end
elseif (strcmp(what(1), '@'))
% comes after 'file' above for "doctest @class/method"
type = 'class';
elseif (exist(what, 'dir'))
type = 'dir';
elseif exist(what) == 2 || exist(what) == 103
% Notes:
% * exist('@class', 'dir') only works if pwd is the parent of
% '@class', having it in the path is not sufficient.
% * Return 2 on Octave 3.8 and 103 on Octave 4.
type = 'class';
else
% classdef classes are not detected by any of the above
try
temp = methods(what);
temp = methods(temp);
type = 'class';
catch
type = 'unknown';
end
end

if (strcmp (type, 'unknown'))
if (exist(what, 'dir'))
type = 'dir';
elseif (exist(what, 'file') || exist(what, 'builtin'))
type = 'function';
else
type = 'unknown';
end
end
else % Matlab
if (strcmp(what(1), '@')) && ~isempty(methods(what(2:end)))
% covers "doctest @class", but not "doctest @class/method"
Expand Down
11 changes: 4 additions & 7 deletions test/bist.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ function bist()

%!assert (~ doctest ('there_is_no_such_file', '-quiet'))

%!assert (~ doctest ({'doctest', 'there_is_no_such_file'}, '-quiet'))
%!assert (~ doctest ('@there_is_no_such_class', '-quiet'))

%!error
%! % TODO: maybe this should be EXTRACTION_ERROR, not raise an error...
%! doctest @there_is_no_such_class -quiet
%!assert (~ doctest ({'doctest', 'there_is_no_such_file'}, '-quiet'))

%!test
%! [n, t] = doctest ('doctest', '-quiet');
Expand Down Expand Up @@ -67,8 +65,7 @@ function bist()
%! assert (nump == 5 && numt == 5)
%! assert (summ.num_targets == 4)

%!xtest
%! % Currently cannot even run
%!test
%! % https://github.com/catch22/octave-doctest/issues/199
%! [nump, numt, summ] = doctest ('@classdef_infile/disp');
%! assert (nump >= 0)
Expand All @@ -88,7 +85,7 @@ function bist()
%! assert (nump == 4 && numt == 4)
%! assert (summ.num_targets == 3)

%!xtest
%!test
%! % monkey-patching methods to existing builtin-objects
%! [nump, numt, summ1] = doctest ('logical');
%! % First, there is (at least) the "logical" builtin
Expand Down

0 comments on commit b93e75d

Please sign in to comment.