Commits

thom...@gmail.com  committed aaaa555

Restructure doctest_run so it doesn't require an external file

  • Participants
  • Parent commits f9a7338

Comments (0)

Files changed (2)

File doctest_run.m

 
 results = [];
 
-file_for_persisting = [ tempname() '.mat' ];
-
+all_outputs = DOCTEST__evalc({matches(:).source});
+  
 for I = 1:length(matches)
-
-    got = doctest_evalc_persist(matches(I).source, file_for_persisting);
-    
+  
+    got = all_outputs{I};
     want_unspaced = regexprep(matches(I).want, '\s+', ' ');
     
     got_unspaced = regexprep(got, '\s+', ' ');
     
 end
 
-delete(file_for_persisting);
+end
+
+
+
+function DOCTEST__results = DOCTEST__evalc(DOCTEST__examples_to_run)
+% I wish I had my very own namespace...
+% DOCTEST__
+
+DOCTEST__results = cell(size(DOCTEST__examples_to_run));
+
+for DOCTEST__I = 1:numel(DOCTEST__examples_to_run)
+    try
+        DOCTEST__results{DOCTEST__I} = evalc(DOCTEST__examples_to_run{DOCTEST__I});
+    catch DOCTEST__exception
+        DOCTEST__results{DOCTEST__I} = DOCTEST__format_exception(DOCTEST__exception);
+    end
+end
+
+
+
+% If we get excited, we could add this snippet
+%             % list created variables in this context
+%             %clear ans
+%             DOCTEST__vars = whos('-regexp', '^(?!DOCTEST__).*');   % java regex negative lookahead
+%             varargout{1} = { DOCTEST__vars.name };
+% 
+%             if nargout > 2
+%                 % return those variables
+%                 varargout{2} = cell(1,numel(DOCTEST__vars));
+%                 for DOCTEST__i=1:numel(DOCTEST__vars)
+%                     [~,varargout{2}{DOCTEST__i}] = evalc( DOCTEST__vars(DOCTEST__i).name );
+%                 end
+%             end
 
 end
 
+function formatted = DOCTEST__format_exception(ex)
 
-
-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')
+if strcmp(ex.stack(1).name, 'DOCTEST__evalc')
     % 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.

File doctest_run_examples.m

-function [DOCTEST__b varargout] = testContext(DOCTEST__src, DOCTEST__srcOutput)
-    % TESTCONTEXT   Executes the source code and tests for
-    %               equality against the expected output
-    %
-    %   Input:
-    %       DOCTEST__src       - source to execute, cellarry of statements
-    %       DOCTEST__srcOutput - output to expect, cellarray of output of each statement
-    %
-    %   Output:
-    %       DOCTEST__b         - true/false for success/failure of test
-    %                          note that the output is strtrim()'ed then strcmp()'ed
-    %       varargout{1}     - variable names assigned in this confined context
-    %       varargout{2}     - variable values assigned
-    %
-    %   Example 1:
-    %       source = { 'I = 5+33;' 'I' };
-    %       output = { [], ['I =' char(10) '    38'] };
-    %       b = testContext(source, output);
-    %
-    %   Example 2:
-    %       source = { 'I = 5+33; J = 2;' 'K = 1;' 'disp(I+J+K)' };
-    %       output = { [], [], '41' };
-    %       [b varNames varValues] = testContext(source, output);
-    %
-    %   See also: eval evalc
-    %
-
-    DOCTEST__b = true;
-
-    try
-        % for each statement
-        for DOCTEST__i=1:numel(DOCTEST__src)
-            % evaluate
-            DOCTEST__output = evalc( DOCTEST__src{DOCTEST__i} );
-            DOCTEST__output = strtrim(DOCTEST__output);            % trim whitespaces
-            % compare output
-            if ~isempty( DOCTEST__srcOutput{DOCTEST__i} )
-                if ~strcmp(DOCTEST__output,DOCTEST__srcOutput{DOCTEST__i})
-                    DOCTEST__b = false;
-                    return
-                end
-            end
-        end
-
-        if nargout > 1
-            % list created variables in this context
-            %clear ans
-            DOCTEST__vars = whos('-regexp', '^(?!DOCTEST__).*');   % java regex negative lookahead
-            varargout{1} = { DOCTEST__vars.name };
-
-            if nargout > 2
-                % return those variables
-                varargout{2} = cell(1,numel(DOCTEST__vars));
-                for DOCTEST__i=1:numel(DOCTEST__vars)
-                    [~,varargout{2}{DOCTEST__i}] = evalc( DOCTEST__vars(DOCTEST__i).name );
-                end
-            end
-        end
-
-    catch ME
-        warning(ME.identifier, ME.message)
-        DOCTEST__b = false;
-        varargout{1} = {};
-        varargout{2} = {};
-    end
-end