1. Thomas Smith
  2. Doctest for Matlab

Commits

thom...@gmail.com  committed f80406d

Add hgignore to ignore SVN directory
renamed files to pollute namespace less

  • Participants
  • Parent commits 50a175d
  • Branches default

Comments (0)

Files changed (7)

File .hgignore

View file
  • Ignore whitespace
+\.svn
+.*~

File compare.m

  • Ignore whitespace
-function match = compare(want, got)
-% Matches two strings together... they should be identical, except that the
-% first one can contain '***', which matches anything in the second string.
-%
-% But there are also some tricksy things that Matlab does to strings.  Such
-% as add hyperlinks to help.  This doctest tests that condition.
-%
-% >> disp('Hi there!  <a href="matlab:help help">foo</a>')
-% Hi there!  foo
-%
-%
-% They also sometimes backspace over things for no apparent reason.  This
-% doctest recreates that condition.
-%
-% >> sprintf('Hi there, there is no dot here: .\x08')
-% 
-% ans =
-% 
-% Hi there, there is no dot here: 
-%
-%
-% All of the doctests should pass, and they manipulate this function.
-%
-
-% This looks bad, like hardcoding for lower-case "a href"
-% and a double quote... but that's what MATLAB looks for too.
-got = regexprep(got, '<a +href=".*?>', '');
-got = regexprep(got, '</a>', '');
-
-% WHY do they need backspaces?  huh.
-got = regexprep(got, '.\x08', '');
-
-want = strtrim(want);
-got = strtrim(got);
-
-if isempty(want) && isempty(got)
-    match = 1;
-    return
-end
-
-want_escaped = regexptranslate('escape', want);
-want_re = regexprep(want_escaped, '(\\\*){3}', '.*');
-
-
-
-result = regexp(got, want_re, 'once');
-
-match = ~ isempty(result);
-
-end

File doctest.m

View file
  • Ignore whitespace
 % 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;
 
+
+%
+% Make a list of every method/function that we need to examine.
+%
+
 to_test = { func_or_class };
 
 theMethods = methods(func_or_class);
         sprintf('%s.%s', func_or_class, theMethods{I}) ];
 end
 
-% to_test
 
+
+%
+% Examine each function/method for a docstring, and run any examples in
+% that docstring
+%
+
+% Can't predict number of results beforehand, depends of number of examples
+% in each docstring.
 result = [];
 
 for I = 1:length(to_test)
-    result = [result, do_test(to_test{I})];
+    docstring = help(to_test{I});
+
+    this_result = doctest_run(docstring);
+    result = [result, this_result];
 end
     
 test_anything(result, verbose);
 
 end
 
-function result = do_test(func_name)
-docstring = help(func_name);
 
-result = run_doctests(docstring);
+function test_anything(results, verbose)
+% Prints out test results in the Test Anything Protocol format
+%
+% See http://testanything.org/
+%
 
+out = 1; % stdout
+
+fprintf(out, 'TAP version 13\n')
+fprintf(out, '1..%d\n', numel(results));
+for I = 1:length(results)
+    if results(I).pass
+        ok = 'ok';
+    else
+        ok = 'not ok';
+    end
+    
+    fprintf(out, '%s %d - "%s"\n', ok, I, results(I).source);
+    if verbose || ~ results(I).pass
+        fprintf(out, '    expected: %s\n', results(I).want);
+        fprintf(out, '    got     : %s\n', results(I).got);
+    end
 end
 
+
+end

File doctest_compare.m

View file
  • Ignore whitespace
+function match = doctest_compare(want, got)
+% Matches two strings together... they should be identical, except that the
+% first one can contain '***', which matches anything in the second string.
+%
+% But there are also some tricksy things that Matlab does to strings.  Such
+% as add hyperlinks to help.  This doctest tests that condition.
+%
+% >> disp('Hi there!  <a href="matlab:help help">foo</a>')
+% Hi there!  foo
+%
+%
+% They also sometimes backspace over things for no apparent reason.  This
+% doctest recreates that condition.
+%
+% >> sprintf('Hi there, there is no dot here: .\x08')
+% 
+% ans =
+% 
+% Hi there, there is no dot here: 
+%
+%
+% All of the doctests should pass, and they manipulate this function.
+%
+
+% This looks bad, like hardcoding for lower-case "a href"
+% and a double quote... but that's what MATLAB looks for too.
+got = regexprep(got, '<a +href=".*?>', '');
+got = regexprep(got, '</a>', '');
+
+% WHY do they need backspaces?  huh.
+got = regexprep(got, '.\x08', '');
+
+want = strtrim(want);
+got = strtrim(got);
+
+if isempty(want) && isempty(got)
+    match = 1;
+    return
+end
+
+want_escaped = regexptranslate('escape', want);
+want_re = regexprep(want_escaped, '(\\\*){3}', '.*');
+
+
+
+result = regexp(got, want_re, 'once');
+
+match = ~ isempty(result);
+
+end

File doctest_run.m

View file
  • Ignore whitespace
+function results = doctest_run(docstring)
+%
+% 
+% >> 1 + 2
+% ans =
+%      3
+%
+
+% loosely based on Python 2.6 doctest.py, line 510
+example_re = '(?m)(?-s)(?:^ *>> )(?<source>.*)\n(?<want>(?:(?:^ *$\n)?(?!\s*>>).*\w.*\n)*)';
+
+[matches] = regexp(docstring, example_re, 'names', 'warnings');
+
+results = [];
+
+for I = 1:length(matches)
+    
+    % ['<<' matches(I).want '>>']
+    try
+        got = evalc(matches(I).source);
+    catch exc
+        got = ['??? ' exc.message];
+    end
+    % ['{{' got '}}']
+    
+    want_unspaced = regexprep(matches(I).want, '\s+', ' ');
+    
+    got_unspaced = regexprep(got, '\s+', ' ');
+    
+
+    
+    results(I).source = matches(I).source;
+    results(I).want = strtrim(want_unspaced);
+    results(I).got = strtrim(got_unspaced);
+    results(I).pass = doctest_compare(want_unspaced, got_unspaced);
+    
+end
+
+

File run_doctests.m

  • Ignore whitespace
-function results = run_doctests(docstring)
-%
-% 
-% >> 1 + 2
-% ans =
-%      3
-%
-
-% loosely based on Python 2.6 doctest.py, line 510
-example_re = '(?m)(?-s)(?:^ *>> )(?<source>.*)\n(?<want>(?:(?:^ *$\n)?(?!\s*>>).*\w.*\n)*)';
-
-[matches] = regexp(docstring, example_re, 'names', 'warnings');
-
-results = [];
-
-for I = 1:length(matches)
-    
-    % ['<<' matches(I).want '>>']
-    try
-        got = evalc(matches(I).source);
-    catch exc
-        got = ['??? ' exc.message];
-    end
-    % ['{{' got '}}']
-    
-    want_unspaced = regexprep(matches(I).want, '\s+', ' ');
-    
-    got_unspaced = regexprep(got, '\s+', ' ');
-    
-
-    
-    results(I).source = matches(I).source;
-    results(I).want = strtrim(want_unspaced);
-    results(I).got = strtrim(got_unspaced);
-    results(I).pass = compare(want_unspaced, got_unspaced);
-    
-end
-
-

File test_anything.m

  • Ignore whitespace
-function test_anything(results, verbose)
-
-out = 1;
-
-fprintf(out, 'TAP version 13\n')
-fprintf(out, '1..%d\n', numel(results));
-for I = 1:length(results)
-    if results(I).pass
-        ok = 'ok';
-    else
-        ok = 'not ok';
-    end
-    
-    fprintf(out, '%s %d - "%s"\n', ok, I, results(I).source);
-    if verbose || ~ results(I).pass
-        fprintf(out, '    expected: %s\n', results(I).want);
-        fprintf(out, '    got     : %s\n', results(I).got);
-    end
-end