gnu-octave / octave-doctest

Doctests for Octave/Matlab
https://gnu-octave.github.io/packages/doctest/
BSD 3-Clause "New" or "Revised" License
16 stars 4 forks source link

Doctest does not work properly with namespaces #230

Open skycaptain opened 4 years ago

skycaptain commented 4 years ago

Calling doctest directly on a file works fine:

>> doctest src/matlab/+myPackage/getVersion.m
Doctest v0.7.0+: this is Free Software without warranty, see source.

src/matlab/+myPackage/getVersion.m ...................... PASS    2/2   

Summary:

   PASS    2/2   

1/1 targets passed, 0 without tests.

However, when being called on a parent folder doctest suddenly does not find the tests:

>> doctest src/matlab/+myPackage -nonrecursive
Doctest v0.7.0+: this is Free Software without warranty, see source.

src/matlab/+myPackage/
  getVersion.m ......................................... NO TESTS

Summary:

   PASS    0/0   

1/1 targets passed, 1 without tests.

I'm using Matlab and the latest octave-doctest from master:

MATLAB Version: 9.7.0.1190202 (R2019b)
Operating System: Mac OS X  Version: 10.14.6 Build: 18G95 
skycaptain commented 4 years ago

I expected this to be the case although, the issue is the namespacing of Matlab, which is used here. In the end, doctest calls help on each file. However, when being called on a file its path (src/matlab/+myPackage/getVersion.m) gets passed to extract_docstring; when being called on a folder, only the function name (getVersion) is being passed. When using namespaces help expects the full namespace path, i.e. myPackage.getVersion. Also, stupid help does not raise an error when being called on an unknown function; it just returns an empty char array.

cbm755 commented 4 years ago

Nice sleuthing! As far as I know, no one has thought about how doctest interacts with +namespace directories.

Namespaces are also now supported on Octave as well so probably there are related or even the same bugs in Octave.

I think doctest +apkg should test

  1. +apkg/Contents.m (help apkg)
  2. everything in +apkg/.
  3. perhaps not +apkg/+subpkg, perhaps depending on -nonrecursive flag.
  4. perhaps not +pkg/private (?)
  5. doctest apkg does not precisely need to be the same as doctest +apkg: this is like #196 and #197.

I'm not sure about what should happen with +apkg/subdir/...: I don't see anything about that in Matlab's docs. Maybe we should just traverse whatever Matlab gives us from what('+apkg') and not mess around with dir +apkg?

cbm755 commented 4 years ago

I'd be happy to review PRs about this. My rough thoughts:

  1. there could be type = 'package' detected at the top of doctest_collect.
  2. maybe there could be another block a bit like this one:
    % Deal with directories
    if (strcmp(type, 'dir'))