Anonymous avatar Anonymous committed 405a6fd

Better avoidance of polluting test namespace, better options for verbosity, better matching of expected error messages

Comments (0)

Files changed (2)

-function doctest(func_or_class)
+function doctest(func_or_class, varargin)
 % Run examples embedded in documentation
 %
 % doctest func_name
 % but this hasn't happened yet.  (See Issue #2 on the bitbucket site,
 % below)
 %
-
 %
 % The latest version from the original author, Thomas Smith, is available
 % at http://bitbucket.org/tgs/doctest-for-matlab/src
 
-% This is a dumb way to be verbose or not, sorry.
-verbose = 0;
+p = inputParser;
+p.addOptional('CreateLinks', true);
+p.addOptional('Verbose', false);
+p.parse(varargin{:});
+verbose = p.Results.Verbose;
+createLinks = p.Results.CreateLinks;
+
 
 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % Print the results
 %
 
-test_anything(result, verbose);
+test_anything(result, verbose, createLinks);
 
 
 end
 
 
-function test_anything(results, verbose)
+function test_anything(results, verbose, createLinks)
 % Prints out test results in the Test Anything Protocol format
 %
 % See http://testanything.org/
     end
     
     fprintf(out, '%s %d - "%s"\n', ok, I, results(I).source);
+%     results(I).pass
     if verbose || ~ results(I).pass
-        fprintf(out, '    in %s\n', results(I).link);
+        if createLinks
+            fprintf(out, '    in %s\n', results(I).link);
+        end
         fprintf(out, '    expected: %s\n', results(I).want);
         fprintf(out, '    got     : %s\n', results(I).got);
     end
 
 results = [];
 
+file_for_persisting = [ tempname() '.mat' ];
+
 for I = 1:length(matches)
-    
-    % ['<<' matches(I).want '>>']
-    try
-        got = evalc(matches(I).source);
-    catch exc
-        got = ['??? ' exc.message];
-    end
-    % ['{{' got '}}']
+
+    got = doctest_evalc_persist(matches(I).source, file_for_persisting);
     
     want_unspaced = regexprep(matches(I).want, '\s+', ' ');
     
     
 end
 
+delete(file_for_persisting);
 
+end
+
+
+
+function doctest_result = doctest_evalc_persist(doctest_example_to_run, doctest_file_to_save)
+% I wish I had my very own namespace...
+
+if exist(doctest_file_to_save, 'file')
+    load(doctest_file_to_save);
+end
+
+try
+    doctest_result = evalc(doctest_example_to_run);
+catch doctest_exception
+    doctest_result = doctest_format_exception(doctest_exception);
+end
+
+% to prevent SAVE from dying because it can't save anything
+abcdefghijklmnopqrstuvwxyz0123456789_doctest_bleah = 1;
+
+save(doctest_file_to_save, '-regexp', '^(?!doctest_).');
+
+end
+
+function formatted = doctest_format_exception(ex)
+
+if strcmp(ex.stack(1).name, 'doctest_evalc_persist')
+    % we don't want the report, we just want the message
+    % otherwise it'll talk about evalc, which is not what the user got on
+    % the command line.
+    formatted = ['??? ' ex.message];
+else
+    formatted = ['??? ' ex.getReport('basic')];
+end
+
+
+
+end
+
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.