-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_snippets.py
executable file
·64 lines (55 loc) · 2.11 KB
/
test_snippets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""
Python snippet tester for Markdown manuscripts
This tester executes doctests in the Markdown file(s) passed as arguments.
Precede tests that need to be skipped with a comment like this:
<!-- test:skip (description of why it's being skipped) -->
This comment should not be indented, and a blank line after it is required
to preserve Markdown parsing. No closing marker is needed; the blank line
after the code block is sufficient. Skipped test lines are printed to the
console.
Author: Paul Bissex <pb@e-scribe.com>
License: MIT
Project: http://withdjango.com/
"""
import doctest
import sys
def remove_skipped_examples(lines):
"""Remove examples preceded by <!-- test:skip -->"""
cleaned_text = skipped_text = ""
for num, line in enumerate(lines):
if line.startswith("<!-- test:skip"):
skipped_text += "\nSkipping @ line %s: %s\n" % (num, line[14:-4])
line = lines.next() # consume leading blank line
line = lines.next() # get first line of test
while line.strip():
skipped_text += line.rstrip() + "\n"
line = lines.next()
line = ""
cleaned_text += line
skipped_text += "\n"
return cleaned_text, skipped_text
def checkfiles(files):
"""Check doctest examples embedded in provided files"""
for f in files:
lines = iter(open(f).readlines())
cleaned_text, skipped_text = remove_skipped_examples(lines)
parser = doctest.DocTestParser()
test = parser.get_doctest(cleaned_text, {}, f, f, 1)
if len(test.examples):
print "\nTesting %s" % f
if skipped_text:
print skipped_text
runner = doctest.DocTestRunner(optionflags=doctest.NORMALIZE_WHITESPACE +
doctest.ELLIPSIS)
runner.run(test)
print
runner.summarize(verbose=True)
else:
print "\nNo tests in %s" % f
if __name__ == "__main__":
files = sys.argv[1:]
if files:
checkfiles(files)
else:
print "Please provide one or more filenames."