| 1 |
|
|---|
| 2 |
""" |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
import doctest |
|---|
| 14 |
import sys |
|---|
| 15 |
import os.path |
|---|
| 16 |
from optparse import OptionParser |
|---|
| 17 |
|
|---|
| 18 |
optionflags = doctest.IGNORE_EXCEPTION_DETAIL+doctest.NORMALIZE_WHITESPACE |
|---|
| 19 |
|
|---|
| 20 |
class MyOutputChecker(doctest.OutputChecker): |
|---|
| 21 |
""" |
|---|
| 22 |
Modification of doctest.OutputChecker to work better with the |
|---|
| 23 |
users' manual: |
|---|
| 24 |
* Often, we don't want to have the output that is printed |
|---|
| 25 |
by Python in the manual, as it just takes up space without adding any |
|---|
| 26 |
useful information. |
|---|
| 27 |
""" |
|---|
| 28 |
|
|---|
| 29 |
def __init__(self,strict): |
|---|
| 30 |
self.strict = strict |
|---|
| 31 |
|
|---|
| 32 |
def check_output(self, want, got, optionflags): |
|---|
| 33 |
if self.strict: |
|---|
| 34 |
return doctest.OutputChecker.check_output(self, want, got, optionflags) |
|---|
| 35 |
else: |
|---|
| 36 |
if want == '': |
|---|
| 37 |
return True |
|---|
| 38 |
else: |
|---|
| 39 |
try: |
|---|
| 40 |
int(want) and int(got) |
|---|
| 41 |
return True |
|---|
| 42 |
except ValueError: |
|---|
| 43 |
return doctest.OutputChecker.check_output(self, want, got, optionflags) |
|---|
| 44 |
|
|---|
| 45 |
def mytestfile(filename,globs,optionflags,strict=False): |
|---|
| 46 |
parser = doctest.DocTestParser() |
|---|
| 47 |
if globs is None: |
|---|
| 48 |
globs = {} |
|---|
| 49 |
else: |
|---|
| 50 |
globs = globs.copy() |
|---|
| 51 |
name = os.path.basename(filename) |
|---|
| 52 |
|
|---|
| 53 |
runner = doctest.DocTestRunner(checker=MyOutputChecker(strict=strict), optionflags=optionflags) |
|---|
| 54 |
|
|---|
| 55 |
s = open(filename).read() |
|---|
| 56 |
test = parser.get_doctest(s, globs, name, filename, 0) |
|---|
| 57 |
runner.run(test) |
|---|
| 58 |
runner.summarize() |
|---|
| 59 |
return runner.failures, runner.tries |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
if __name__ == "__main__": |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
parser = OptionParser(usage="usage: %prog [options] FILE") |
|---|
| 66 |
parser.add_option("--strict", action="store_true", dest="strict", default=False, |
|---|
| 67 |
help="Use the original doctest output checker, not the more lax local version.") |
|---|
| 68 |
|
|---|
| 69 |
(options, args) = parser.parse_args() |
|---|
| 70 |
if len(args) == 1: |
|---|
| 71 |
docfile = args[0] |
|---|
| 72 |
else: |
|---|
| 73 |
parser.print_help() |
|---|
| 74 |
sys.exit(1) |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
exec("from NeuroTools.%s import *" % docfile.replace('.txt','')) |
|---|
| 78 |
mytestfile(docfile, globs=globals(), optionflags=optionflags, strict=options.strict) |
|---|
| 79 |
|
|---|
| 80 |
sys.exit(0) |
|---|