Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 1 | # Module doctest. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2 | # Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org). |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 3 | # Major enhancements and refactoring by: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 4 | # Jim Fulton |
| 5 | # Edward Loper |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 6 | |
| 7 | # Provided as-is; use at your own risk; no warranty; no promises; enjoy! |
| 8 | |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 9 | r"""Module doctest -- a framework for running examples in docstrings. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 10 | |
| 11 | NORMAL USAGE |
| 12 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 13 | In simplest use, end each module M to be tested with: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 14 | |
| 15 | def _test(): |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 16 | import doctest |
| 17 | return doctest.testmod() |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 18 | |
| 19 | if __name__ == "__main__": |
| 20 | _test() |
| 21 | |
| 22 | Then running the module as a script will cause the examples in the |
| 23 | docstrings to get executed and verified: |
| 24 | |
| 25 | python M.py |
| 26 | |
| 27 | This won't display anything unless an example fails, in which case the |
| 28 | failing example(s) and the cause(s) of the failure(s) are printed to stdout |
| 29 | (why not stderr? because stderr is a lame hack <0.2 wink>), and the final |
| 30 | line of output is "Test failed.". |
| 31 | |
| 32 | Run it with the -v switch instead: |
| 33 | |
| 34 | python M.py -v |
| 35 | |
| 36 | and a detailed report of all examples tried is printed to stdout, along |
| 37 | with assorted summaries at the end. |
| 38 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 39 | You can force verbose mode by passing "verbose=True" to testmod, or prohibit |
| 40 | it by passing "verbose=False". In either of those cases, sys.argv is not |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 41 | examined by testmod. |
| 42 | |
| 43 | In any case, testmod returns a 2-tuple of ints (f, t), where f is the |
| 44 | number of docstring examples that failed and t is the total number of |
| 45 | docstring examples attempted. |
| 46 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 47 | There are a variety of other ways to run doctests, including integration |
| 48 | with the unittest framework, and support for running non-Python text |
| 49 | files containing doctests. There are also many ways to override parts |
| 50 | of doctest's default behaviors. See the Library Reference Manual for |
| 51 | details. |
| 52 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 53 | |
| 54 | WHICH DOCSTRINGS ARE EXAMINED? |
| 55 | |
| 56 | + M.__doc__. |
| 57 | |
| 58 | + f.__doc__ for all functions f in M.__dict__.values(), except those |
Raymond Hettinger | 71adf7e | 2003-07-16 19:25:22 +0000 | [diff] [blame] | 59 | defined in other modules. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 60 | |
Raymond Hettinger | 71adf7e | 2003-07-16 19:25:22 +0000 | [diff] [blame] | 61 | + C.__doc__ for all classes C in M.__dict__.values(), except those |
| 62 | defined in other modules. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 63 | |
| 64 | + If M.__test__ exists and "is true", it must be a dict, and |
| 65 | each entry maps a (string) name to a function object, class object, or |
| 66 | string. Function and class object docstrings found from M.__test__ |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 67 | are searched, and strings are searched directly as if they were docstrings. |
| 68 | In output, a key K in M.__test__ appears with name |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 69 | <name of M>.__test__.K |
| 70 | |
| 71 | Any classes found are recursively searched similarly, to test docstrings in |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 72 | their contained methods and nested classes. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 73 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 74 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 75 | WHAT'S THE EXECUTION CONTEXT? |
| 76 | |
| 77 | By default, each time testmod finds a docstring to test, it uses a *copy* |
| 78 | of M's globals (so that running tests on a module doesn't change the |
| 79 | module's real globals, and so that one test in M can't leave behind crumbs |
| 80 | that accidentally allow another test to work). This means examples can |
| 81 | freely use any names defined at top-level in M. It also means that sloppy |
| 82 | imports (see above) can cause examples in external docstrings to use |
| 83 | globals inappropriate for them. |
| 84 | |
| 85 | You can force use of your own dict as the execution context by passing |
| 86 | "globs=your_dict" to testmod instead. Presumably this would be a copy of |
| 87 | M.__dict__ merged with the globals from other imported modules. |
| 88 | |
| 89 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 90 | WHAT ABOUT EXCEPTIONS? |
| 91 | |
| 92 | No problem, as long as the only output generated by the example is the |
| 93 | traceback itself. For example: |
| 94 | |
Tim Peters | 60e23f4 | 2001-02-14 00:43:21 +0000 | [diff] [blame] | 95 | >>> [1, 2, 3].remove(42) |
Tim Peters | ea4f931 | 2001-02-13 20:54:42 +0000 | [diff] [blame] | 96 | Traceback (most recent call last): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 97 | File "<stdin>", line 1, in ? |
Tim Peters | 60e23f4 | 2001-02-14 00:43:21 +0000 | [diff] [blame] | 98 | ValueError: list.remove(x): x not in list |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 99 | >>> |
| 100 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 101 | Note that only the exception type and value are compared. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 102 | |
| 103 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 104 | SO WHAT DOES A DOCTEST EXAMPLE LOOK LIKE ALREADY!? |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 105 | |
| 106 | Oh ya. It's easy! In most cases a copy-and-paste of an interactive |
| 107 | console session works fine -- just make sure the leading whitespace is |
| 108 | rigidly consistent (you can mix tabs and spaces if you're too lazy to do it |
| 109 | right, but doctest is not in the business of guessing what you think a tab |
| 110 | means). |
| 111 | |
| 112 | >>> # comments are ignored |
| 113 | >>> x = 12 |
| 114 | >>> x |
| 115 | 12 |
| 116 | >>> if x == 13: |
| 117 | ... print "yes" |
| 118 | ... else: |
| 119 | ... print "no" |
| 120 | ... print "NO" |
| 121 | ... print "NO!!!" |
| 122 | ... |
| 123 | no |
| 124 | NO |
| 125 | NO!!! |
| 126 | >>> |
| 127 | |
| 128 | Any expected output must immediately follow the final ">>>" or "..." line |
| 129 | containing the code, and the expected output (if any) extends to the next |
| 130 | ">>>" or all-whitespace line. That's it. |
| 131 | |
| 132 | Bummers: |
| 133 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 134 | + Output to stdout is captured, but not output to stderr (exception |
| 135 | tracebacks are captured via a different means). |
| 136 | |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 137 | + If you continue a line via backslashing in an interactive session, |
| 138 | or for any other reason use a backslash, you should use a raw |
| 139 | docstring, which will preserve your backslahses exactly as you type |
| 140 | them: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 141 | |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 142 | >>> def f(x): |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 143 | ... r'''Backslashes in a raw docstring: m\n''' |
| 144 | >>> print f.__doc__ |
| 145 | Backslashes in a raw docstring: m\n |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 146 | |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 147 | Otherwise, the backslash will be interpreted as part of the string. |
| 148 | E.g., the "\n" above would be interpreted as a newline character. |
| 149 | Alternatively, you can double each backslash in the doctest version |
| 150 | (and not use a raw string): |
| 151 | |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 152 | >>> def f(x): |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 153 | ... '''Backslashes in a raw docstring: m\\n''' |
| 154 | >>> print f.__doc__ |
| 155 | Backslashes in a raw docstring: m\n |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 156 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 157 | The starting column doesn't matter: |
| 158 | |
| 159 | >>> assert "Easy!" |
| 160 | >>> import math |
| 161 | >>> math.floor(1.9) |
| 162 | 1.0 |
| 163 | |
| 164 | and as many leading whitespace characters are stripped from the expected |
| 165 | output as appeared in the initial ">>>" line that triggered it. |
| 166 | |
| 167 | If you execute this very file, the examples above will be found and |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 168 | executed. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 169 | """ |
| 170 | |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 171 | __all__ = [ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 172 | 'is_private', |
| 173 | 'Example', |
| 174 | 'DocTest', |
| 175 | 'DocTestFinder', |
| 176 | 'DocTestRunner', |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 177 | 'testmod', |
| 178 | 'run_docstring_examples', |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 179 | 'Tester', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 180 | 'DocTestCase', |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 181 | 'DocTestSuite', |
| 182 | 'testsource', |
| 183 | 'debug', |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 184 | # 'master', |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 185 | ] |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 186 | |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 187 | import __future__ |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 188 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 189 | import sys, traceback, inspect, linecache, os, re, types |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 190 | import unittest, difflib, pdb, tempfile |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 191 | import warnings |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 192 | from StringIO import StringIO |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 193 | |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 194 | real_pdb_set_trace = pdb.set_trace |
| 195 | |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 196 | # Option constants. |
| 197 | DONT_ACCEPT_TRUE_FOR_1 = 1 << 0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 198 | DONT_ACCEPT_BLANKLINE = 1 << 1 |
| 199 | NORMALIZE_WHITESPACE = 1 << 2 |
| 200 | ELLIPSIS = 1 << 3 |
| 201 | UNIFIED_DIFF = 1 << 4 |
| 202 | CONTEXT_DIFF = 1 << 5 |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 203 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 204 | OPTIONFLAGS_BY_NAME = { |
| 205 | 'DONT_ACCEPT_TRUE_FOR_1': DONT_ACCEPT_TRUE_FOR_1, |
| 206 | 'DONT_ACCEPT_BLANKLINE': DONT_ACCEPT_BLANKLINE, |
| 207 | 'NORMALIZE_WHITESPACE': NORMALIZE_WHITESPACE, |
| 208 | 'ELLIPSIS': ELLIPSIS, |
| 209 | 'UNIFIED_DIFF': UNIFIED_DIFF, |
| 210 | 'CONTEXT_DIFF': CONTEXT_DIFF, |
| 211 | } |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 212 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 213 | # Special string markers for use in `want` strings: |
| 214 | BLANKLINE_MARKER = '<BLANKLINE>' |
| 215 | ELLIPSIS_MARKER = '...' |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 216 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 217 | |
| 218 | # There are 4 basic classes: |
| 219 | # - Example: a <source, want> pair, plus an intra-docstring line number. |
| 220 | # - DocTest: a collection of examples, parsed from a docstring, plus |
| 221 | # info about where the docstring came from (name, filename, lineno). |
| 222 | # - DocTestFinder: extracts DocTests from a given object's docstring and |
| 223 | # its contained objects' docstrings. |
| 224 | # - DocTestRunner: runs DocTest cases, and accumulates statistics. |
| 225 | # |
| 226 | # So the basic picture is: |
| 227 | # |
| 228 | # list of: |
| 229 | # +------+ +---------+ +-------+ |
| 230 | # |object| --DocTestFinder-> | DocTest | --DocTestRunner-> |results| |
| 231 | # +------+ +---------+ +-------+ |
| 232 | # | Example | |
| 233 | # | ... | |
| 234 | # | Example | |
| 235 | # +---------+ |
| 236 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 237 | ###################################################################### |
| 238 | ## Table of Contents |
| 239 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 240 | # 1. Utility Functions |
| 241 | # 2. Example & DocTest -- store test cases |
| 242 | # 3. DocTest Parser -- extracts examples from strings |
| 243 | # 4. DocTest Finder -- extracts test cases from objects |
| 244 | # 5. DocTest Runner -- runs test cases |
| 245 | # 6. Test Functions -- convenient wrappers for testing |
| 246 | # 7. Tester Class -- for backwards compatibility |
| 247 | # 8. Unittest Support |
| 248 | # 9. Debugging Support |
| 249 | # 10. Example Usage |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 250 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 251 | ###################################################################### |
| 252 | ## 1. Utility Functions |
| 253 | ###################################################################### |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 254 | |
| 255 | def is_private(prefix, base): |
| 256 | """prefix, base -> true iff name prefix + "." + base is "private". |
| 257 | |
| 258 | Prefix may be an empty string, and base does not contain a period. |
| 259 | Prefix is ignored (although functions you write conforming to this |
| 260 | protocol may make use of it). |
| 261 | Return true iff base begins with an (at least one) underscore, but |
| 262 | does not both begin and end with (at least) two underscores. |
| 263 | |
Tim Peters | bafb1fe | 2004-08-08 01:52:57 +0000 | [diff] [blame] | 264 | >>> warnings.filterwarnings("ignore", "is_private", DeprecationWarning, |
| 265 | ... "doctest", 0) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 266 | >>> is_private("a.b", "my_func") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 267 | False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 268 | >>> is_private("____", "_my_func") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 269 | True |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 270 | >>> is_private("someclass", "__init__") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 271 | False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 272 | >>> is_private("sometypo", "__init_") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 273 | True |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 274 | >>> is_private("x.y.z", "_") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 275 | True |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 276 | >>> is_private("_x.y.z", "__") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 277 | False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 278 | >>> is_private("", "") # senseless but consistent |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 279 | False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 280 | """ |
Tim Peters | bafb1fe | 2004-08-08 01:52:57 +0000 | [diff] [blame] | 281 | warnings.warn("is_private is deprecated; it wasn't useful; " |
| 282 | "examine DocTestFinder.find() lists instead", |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 283 | DeprecationWarning, stacklevel=2) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 284 | return base[:1] == "_" and not base[:2] == "__" == base[-2:] |
| 285 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 286 | def _extract_future_flags(globs): |
| 287 | """ |
| 288 | Return the compiler-flags associated with the future features that |
| 289 | have been imported into the given namespace (globs). |
| 290 | """ |
| 291 | flags = 0 |
| 292 | for fname in __future__.all_feature_names: |
| 293 | feature = globs.get(fname, None) |
| 294 | if feature is getattr(__future__, fname): |
| 295 | flags |= feature.compiler_flag |
| 296 | return flags |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 297 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 298 | def _normalize_module(module, depth=2): |
| 299 | """ |
| 300 | Return the module specified by `module`. In particular: |
| 301 | - If `module` is a module, then return module. |
| 302 | - If `module` is a string, then import and return the |
| 303 | module with that name. |
| 304 | - If `module` is None, then return the calling module. |
| 305 | The calling module is assumed to be the module of |
| 306 | the stack frame at the given depth in the call stack. |
| 307 | """ |
| 308 | if inspect.ismodule(module): |
| 309 | return module |
| 310 | elif isinstance(module, (str, unicode)): |
| 311 | return __import__(module, globals(), locals(), ["*"]) |
| 312 | elif module is None: |
| 313 | return sys.modules[sys._getframe(depth).f_globals['__name__']] |
| 314 | else: |
| 315 | raise TypeError("Expected a module, string, or None") |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 316 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 317 | def _tag_msg(tag, msg, indent=' '): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 318 | """ |
| 319 | Return a string that displays a tag-and-message pair nicely, |
| 320 | keeping the tag and its message on the same line when that |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 321 | makes sense. If the message is displayed on separate lines, |
| 322 | then `indent` is added to the beginning of each line. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 323 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 324 | # If the message doesn't end in a newline, then add one. |
| 325 | if msg[-1:] != '\n': |
| 326 | msg += '\n' |
| 327 | # If the message is short enough, and contains no internal |
| 328 | # newlines, then display it on the same line as the tag. |
| 329 | # Otherwise, display the tag on its own line. |
| 330 | if (len(tag) + len(msg) < 75 and |
| 331 | msg.find('\n', 0, len(msg)-1) == -1): |
| 332 | return '%s: %s' % (tag, msg) |
| 333 | else: |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 334 | msg = '\n'.join([indent+l for l in msg[:-1].split('\n')]) |
| 335 | return '%s:\n%s\n' % (tag, msg) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 336 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 337 | # Override some StringIO methods. |
| 338 | class _SpoofOut(StringIO): |
| 339 | def getvalue(self): |
| 340 | result = StringIO.getvalue(self) |
| 341 | # If anything at all was written, make sure there's a trailing |
| 342 | # newline. There's no way for the expected output to indicate |
| 343 | # that a trailing newline is missing. |
| 344 | if result and not result.endswith("\n"): |
| 345 | result += "\n" |
| 346 | # Prevent softspace from screwing up the next test case, in |
| 347 | # case they used print with a trailing comma in an example. |
| 348 | if hasattr(self, "softspace"): |
| 349 | del self.softspace |
| 350 | return result |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 351 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 352 | def truncate(self, size=None): |
| 353 | StringIO.truncate(self, size) |
| 354 | if hasattr(self, "softspace"): |
| 355 | del self.softspace |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 356 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 357 | ###################################################################### |
| 358 | ## 2. Example & DocTest |
| 359 | ###################################################################### |
| 360 | ## - An "example" is a <source, want> pair, where "source" is a |
| 361 | ## fragment of source code, and "want" is the expected output for |
| 362 | ## "source." The Example class also includes information about |
| 363 | ## where the example was extracted from. |
| 364 | ## |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 365 | ## - A "doctest" is a collection of examples, typically extracted from |
| 366 | ## a string (such as an object's docstring). The DocTest class also |
| 367 | ## includes information about where the string was extracted from. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 368 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 369 | class Example: |
| 370 | """ |
| 371 | A single doctest example, consisting of source code and expected |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 372 | output. `Example` defines the following attributes: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 373 | |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 374 | - source: A single Python statement, always ending with a newline. |
| 375 | The constructor adds a newline if needed. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 376 | |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 377 | - want: The expected output from running the source code (either |
| 378 | from stdout, or a traceback in case of exception). `want` ends |
| 379 | with a newline unless it's empty, in which case it's an empty |
| 380 | string. The constructor adds a newline if needed. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 381 | |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 382 | - lineno: The line number within the DocTest string containing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 383 | this Example where the Example begins. This line number is |
| 384 | zero-based, with respect to the beginning of the DocTest. |
| 385 | """ |
| 386 | def __init__(self, source, want, lineno): |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 387 | # Normalize inputs. |
| 388 | if not source.endswith('\n'): |
| 389 | source += '\n' |
| 390 | if want and not want.endswith('\n'): |
| 391 | want += '\n' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 392 | # Store properties. |
| 393 | self.source = source |
| 394 | self.want = want |
| 395 | self.lineno = lineno |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 396 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 397 | class DocTest: |
| 398 | """ |
| 399 | A collection of doctest examples that should be run in a single |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 400 | namespace. Each `DocTest` defines the following attributes: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 401 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 402 | - examples: the list of examples. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 403 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 404 | - globs: The namespace (aka globals) that the examples should |
| 405 | be run in. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 406 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 407 | - name: A name identifying the DocTest (typically, the name of |
| 408 | the object whose docstring this DocTest was extracted from). |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 409 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 410 | - filename: The name of the file that this DocTest was extracted |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 411 | from, or `None` if the filename is unknown. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 412 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 413 | - lineno: The line number within filename where this DocTest |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 414 | begins, or `None` if the line number is unavailable. This |
| 415 | line number is zero-based, with respect to the beginning of |
| 416 | the file. |
| 417 | |
| 418 | - docstring: The string that the examples were extracted from, |
| 419 | or `None` if the string is unavailable. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 420 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 421 | def __init__(self, examples, globs, name, filename, lineno, docstring): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 422 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 423 | Create a new DocTest containing the given examples. The |
| 424 | DocTest's globals are initialized with a copy of `globs`. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 425 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 426 | assert not isinstance(examples, basestring), \ |
| 427 | "DocTest no longer accepts str; use DocTestParser instead" |
| 428 | self.examples = examples |
| 429 | self.docstring = docstring |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 430 | self.globs = globs.copy() |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 431 | self.name = name |
| 432 | self.filename = filename |
| 433 | self.lineno = lineno |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 434 | |
| 435 | def __repr__(self): |
| 436 | if len(self.examples) == 0: |
| 437 | examples = 'no examples' |
| 438 | elif len(self.examples) == 1: |
| 439 | examples = '1 example' |
| 440 | else: |
| 441 | examples = '%d examples' % len(self.examples) |
| 442 | return ('<DocTest %s from %s:%s (%s)>' % |
| 443 | (self.name, self.filename, self.lineno, examples)) |
| 444 | |
| 445 | |
| 446 | # This lets us sort tests by name: |
| 447 | def __cmp__(self, other): |
| 448 | if not isinstance(other, DocTest): |
| 449 | return -1 |
| 450 | return cmp((self.name, self.filename, self.lineno, id(self)), |
| 451 | (other.name, other.filename, other.lineno, id(other))) |
| 452 | |
| 453 | ###################################################################### |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 454 | ## 2. DocTestParser |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 455 | ###################################################################### |
| 456 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 457 | class DocTestParser: |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 458 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 459 | A class used to parse strings containing doctest examples. |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 460 | """ |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 461 | _EXAMPLE_RE = re.compile(r''' |
Tim Peters | d40a92b | 2004-08-09 03:28:45 +0000 | [diff] [blame] | 462 | # Source consists of a PS1 line followed by zero or more PS2 lines. |
| 463 | (?P<source> |
| 464 | (?:^(?P<indent> [ ]*) >>> .*) # PS1 line |
| 465 | (?:\n [ ]* \.\.\. .*)*) # PS2 lines |
| 466 | \n? |
| 467 | # Want consists of any non-blank lines that do not start with PS1. |
| 468 | (?P<want> (?:(?![ ]*$) # Not a blank line |
| 469 | (?![ ]*>>>) # Not a line starting with PS1 |
| 470 | .*$\n? # But any other line |
| 471 | )*) |
| 472 | ''', re.MULTILINE | re.VERBOSE) |
| 473 | _IS_BLANK_OR_COMMENT = re.compile('^[ ]*(#.*)?$').match |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 474 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 475 | def get_doctest(self, string, globs, name, filename, lineno): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 476 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 477 | Extract all doctest examples from the given string, and |
| 478 | collect them into a `DocTest` object. |
| 479 | |
| 480 | `globs`, `name`, `filename`, and `lineno` are attributes for |
| 481 | the new `DocTest` object. See the documentation for `DocTest` |
| 482 | for more information. |
| 483 | """ |
| 484 | return DocTest(self.get_examples(string, name), globs, |
| 485 | name, filename, lineno, string) |
| 486 | |
| 487 | def get_examples(self, string, name='<string>'): |
| 488 | """ |
| 489 | Extract all doctest examples from the given string, and return |
| 490 | them as a list of `Example` objects. Line numbers are |
| 491 | 0-based, because it's most common in doctests that nothing |
| 492 | interesting appears on the same line as opening triple-quote, |
| 493 | and so the first interesting line is called \"line 1\" then. |
| 494 | |
| 495 | The optional argument `name` is a name identifying this |
| 496 | string, and is only used for error messages. |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 497 | |
| 498 | >>> text = ''' |
| 499 | ... >>> x, y = 2, 3 # no output expected |
| 500 | ... >>> if 1: |
| 501 | ... ... print x |
| 502 | ... ... print y |
| 503 | ... 2 |
| 504 | ... 3 |
| 505 | ... |
| 506 | ... Some text. |
| 507 | ... >>> x+y |
| 508 | ... 5 |
| 509 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 510 | >>> for x in DocTestParser().get_examples(text): |
Edward Loper | 78b58f3 | 2004-08-09 02:56:02 +0000 | [diff] [blame] | 511 | ... print (x.source, x.want, x.lineno) |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 512 | ('x, y = 2, 3 # no output expected\\n', '', 1) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 513 | ('if 1:\\n print x\\n print y\\n', '2\\n3\\n', 2) |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 514 | ('x+y\\n', '5\\n', 9) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 515 | """ |
| 516 | examples = [] |
| 517 | charno, lineno = 0, 0 |
| 518 | # Find all doctest examples in the string: |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 519 | for m in self._EXAMPLE_RE.finditer(string.expandtabs()): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 520 | # Update lineno (lines before this example) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 521 | lineno += string.count('\n', charno, m.start()) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 522 | |
| 523 | # Extract source/want from the regexp match. |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 524 | (source, want) = self._parse_example(m, name, lineno) |
Tim Peters | d40a92b | 2004-08-09 03:28:45 +0000 | [diff] [blame] | 525 | if self._IS_BLANK_OR_COMMENT(source): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 526 | continue |
Edward Loper | 78b58f3 | 2004-08-09 02:56:02 +0000 | [diff] [blame] | 527 | examples.append( Example(source, want, lineno) ) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 528 | |
| 529 | # Update lineno (lines inside this example) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 530 | lineno += string.count('\n', m.start(), m.end()) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 531 | # Update charno. |
| 532 | charno = m.end() |
| 533 | return examples |
| 534 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 535 | def get_program(self, string, name="<string>"): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 536 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 537 | Return an executable program from the given string, as a string. |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 538 | |
| 539 | The format of this isn't rigidly defined. In general, doctest |
| 540 | examples become the executable statements in the result, and |
| 541 | their expected outputs become comments, preceded by an \"#Expected:\" |
| 542 | comment. Everything else (text, comments, everything not part of |
| 543 | a doctest test) is also placed in comments. |
| 544 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 545 | The optional argument `name` is a name identifying this |
| 546 | string, and is only used for error messages. |
| 547 | |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 548 | >>> text = ''' |
| 549 | ... >>> x, y = 2, 3 # no output expected |
| 550 | ... >>> if 1: |
| 551 | ... ... print x |
| 552 | ... ... print y |
| 553 | ... 2 |
| 554 | ... 3 |
| 555 | ... |
| 556 | ... Some text. |
| 557 | ... >>> x+y |
| 558 | ... 5 |
| 559 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 560 | >>> print DocTestParser().get_program(text) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 561 | x, y = 2, 3 # no output expected |
| 562 | if 1: |
| 563 | print x |
| 564 | print y |
| 565 | # Expected: |
| 566 | # 2 |
| 567 | # 3 |
| 568 | # |
| 569 | # Some text. |
| 570 | x+y |
| 571 | # Expected: |
| 572 | # 5 |
| 573 | """ |
| 574 | output = [] |
| 575 | charnum, lineno = 0, 0 |
| 576 | # Find all doctest examples in the string: |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 577 | for m in self._EXAMPLE_RE.finditer(string.expandtabs()): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 578 | # Add any text before this example, as a comment. |
| 579 | if m.start() > charnum: |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 580 | lines = string[charnum:m.start()-1].split('\n') |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 581 | output.extend([self._comment_line(l) for l in lines]) |
| 582 | lineno += len(lines) |
| 583 | |
| 584 | # Extract source/want from the regexp match. |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 585 | (source, want) = self._parse_example(m, name, lineno, False) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 586 | # Display the source |
| 587 | output.append(source) |
| 588 | # Display the expected output, if any |
| 589 | if want: |
| 590 | output.append('# Expected:') |
| 591 | output.extend(['# '+l for l in want.split('\n')]) |
| 592 | |
| 593 | # Update the line number & char number. |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 594 | lineno += string.count('\n', m.start(), m.end()) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 595 | charnum = m.end() |
| 596 | # Add any remaining text, as comments. |
| 597 | output.extend([self._comment_line(l) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 598 | for l in string[charnum:].split('\n')]) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 599 | # Trim junk on both ends. |
| 600 | while output and output[-1] == '#': |
| 601 | output.pop() |
| 602 | while output and output[0] == '#': |
| 603 | output.pop(0) |
| 604 | # Combine the output, and return it. |
| 605 | return '\n'.join(output) |
| 606 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 607 | def _parse_example(self, m, name, lineno, add_newlines=True): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 608 | # Get the example's indentation level. |
| 609 | indent = len(m.group('indent')) |
| 610 | |
| 611 | # Divide source into lines; check that they're properly |
| 612 | # indented; and then strip their indentation & prompts. |
| 613 | source_lines = m.group('source').split('\n') |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 614 | self._check_prompt_blank(source_lines, indent, name, lineno) |
| 615 | self._check_prefix(source_lines[1:], ' '*indent+'.', name, lineno) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 616 | source = '\n'.join([sl[indent+4:] for sl in source_lines]) |
| 617 | if len(source_lines) > 1 and add_newlines: |
| 618 | source += '\n' |
| 619 | |
| 620 | # Divide want into lines; check that it's properly |
| 621 | # indented; and then strip the indentation. |
| 622 | want_lines = m.group('want').rstrip().split('\n') |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 623 | self._check_prefix(want_lines, ' '*indent, name, |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 624 | lineno+len(source_lines)) |
| 625 | want = '\n'.join([wl[indent:] for wl in want_lines]) |
| 626 | if len(want) > 0 and add_newlines: |
| 627 | want += '\n' |
| 628 | |
| 629 | return source, want |
| 630 | |
| 631 | def _comment_line(self, line): |
| 632 | line = line.rstrip() |
Tim Peters | dd0e475 | 2004-08-09 03:31:56 +0000 | [diff] [blame] | 633 | if line: |
| 634 | return '# '+line |
| 635 | else: |
| 636 | return '#' |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 637 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 638 | def _check_prompt_blank(self, lines, indent, name, lineno): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 639 | for i, line in enumerate(lines): |
| 640 | if len(line) >= indent+4 and line[indent+3] != ' ': |
| 641 | raise ValueError('line %r of the docstring for %s ' |
| 642 | 'lacks blank after %s: %r' % |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 643 | (lineno+i+1, name, |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 644 | line[indent:indent+3], line)) |
| 645 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 646 | def _check_prefix(self, lines, prefix, name, lineno): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 647 | for i, line in enumerate(lines): |
| 648 | if line and not line.startswith(prefix): |
| 649 | raise ValueError('line %r of the docstring for %s has ' |
| 650 | 'inconsistent leading whitespace: %r' % |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 651 | (lineno+i+1, name, line)) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 652 | |
| 653 | |
| 654 | ###################################################################### |
| 655 | ## 4. DocTest Finder |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 656 | ###################################################################### |
| 657 | |
| 658 | class DocTestFinder: |
| 659 | """ |
| 660 | A class used to extract the DocTests that are relevant to a given |
| 661 | object, from its docstring and the docstrings of its contained |
| 662 | objects. Doctests can currently be extracted from the following |
| 663 | object types: modules, functions, classes, methods, staticmethods, |
| 664 | classmethods, and properties. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 665 | """ |
| 666 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 667 | def __init__(self, verbose=False, parser=DocTestParser(), |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 668 | recurse=True, _namefilter=None): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 669 | """ |
| 670 | Create a new doctest finder. |
| 671 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 672 | The optional argument `parser` specifies a class or |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 673 | function that should be used to create new DocTest objects (or |
Tim Peters | 161c963 | 2004-08-08 03:38:33 +0000 | [diff] [blame] | 674 | objects that implement the same interface as DocTest). The |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 675 | signature for this factory function should match the signature |
| 676 | of the DocTest constructor. |
| 677 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 678 | If the optional argument `recurse` is false, then `find` will |
| 679 | only examine the given object, and not any contained objects. |
| 680 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 681 | self._parser = parser |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 682 | self._verbose = verbose |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 683 | self._recurse = recurse |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 684 | # _namefilter is undocumented, and exists only for temporary backward- |
| 685 | # compatibility support of testmod's deprecated isprivate mess. |
| 686 | self._namefilter = _namefilter |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 687 | |
| 688 | def find(self, obj, name=None, module=None, globs=None, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 689 | extraglobs=None): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 690 | """ |
| 691 | Return a list of the DocTests that are defined by the given |
| 692 | object's docstring, or by any of its contained objects' |
| 693 | docstrings. |
| 694 | |
| 695 | The optional parameter `module` is the module that contains |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 696 | the given object. If the module is not specified or is None, then |
| 697 | the test finder will attempt to automatically determine the |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 698 | correct module. The object's module is used: |
| 699 | |
| 700 | - As a default namespace, if `globs` is not specified. |
| 701 | - To prevent the DocTestFinder from extracting DocTests |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 702 | from objects that are imported from other modules. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 703 | - To find the name of the file containing the object. |
| 704 | - To help find the line number of the object within its |
| 705 | file. |
| 706 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 707 | Contained objects whose module does not match `module` are ignored. |
| 708 | |
| 709 | If `module` is False, no attempt to find the module will be made. |
| 710 | This is obscure, of use mostly in tests: if `module` is False, or |
| 711 | is None but cannot be found automatically, then all objects are |
| 712 | considered to belong to the (non-existent) module, so all contained |
| 713 | objects will (recursively) be searched for doctests. |
| 714 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 715 | The globals for each DocTest is formed by combining `globs` |
| 716 | and `extraglobs` (bindings in `extraglobs` override bindings |
| 717 | in `globs`). A new copy of the globals dictionary is created |
| 718 | for each DocTest. If `globs` is not specified, then it |
| 719 | defaults to the module's `__dict__`, if specified, or {} |
| 720 | otherwise. If `extraglobs` is not specified, then it defaults |
| 721 | to {}. |
| 722 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 723 | """ |
| 724 | # If name was not specified, then extract it from the object. |
| 725 | if name is None: |
| 726 | name = getattr(obj, '__name__', None) |
| 727 | if name is None: |
| 728 | raise ValueError("DocTestFinder.find: name must be given " |
| 729 | "when obj.__name__ doesn't exist: %r" % |
| 730 | (type(obj),)) |
| 731 | |
| 732 | # Find the module that contains the given object (if obj is |
| 733 | # a module, then module=obj.). Note: this may fail, in which |
| 734 | # case module will be None. |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 735 | if module is False: |
| 736 | module = None |
| 737 | elif module is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 738 | module = inspect.getmodule(obj) |
| 739 | |
| 740 | # Read the module's source code. This is used by |
| 741 | # DocTestFinder._find_lineno to find the line number for a |
| 742 | # given object's docstring. |
| 743 | try: |
| 744 | file = inspect.getsourcefile(obj) or inspect.getfile(obj) |
| 745 | source_lines = linecache.getlines(file) |
| 746 | if not source_lines: |
| 747 | source_lines = None |
| 748 | except TypeError: |
| 749 | source_lines = None |
| 750 | |
| 751 | # Initialize globals, and merge in extraglobs. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 752 | if globs is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 753 | if module is None: |
| 754 | globs = {} |
| 755 | else: |
| 756 | globs = module.__dict__.copy() |
| 757 | else: |
| 758 | globs = globs.copy() |
| 759 | if extraglobs is not None: |
| 760 | globs.update(extraglobs) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 761 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 762 | # Recursively expore `obj`, extracting DocTests. |
| 763 | tests = [] |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 764 | self._find(tests, obj, name, module, source_lines, globs, {}) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 765 | return tests |
| 766 | |
| 767 | def _filter(self, obj, prefix, base): |
| 768 | """ |
| 769 | Return true if the given object should not be examined. |
| 770 | """ |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 771 | return (self._namefilter is not None and |
| 772 | self._namefilter(prefix, base)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 773 | |
| 774 | def _from_module(self, module, object): |
| 775 | """ |
| 776 | Return true if the given object is defined in the given |
| 777 | module. |
| 778 | """ |
| 779 | if module is None: |
| 780 | return True |
| 781 | elif inspect.isfunction(object): |
| 782 | return module.__dict__ is object.func_globals |
| 783 | elif inspect.isclass(object): |
| 784 | return module.__name__ == object.__module__ |
| 785 | elif inspect.getmodule(object) is not None: |
| 786 | return module is inspect.getmodule(object) |
| 787 | elif hasattr(object, '__module__'): |
| 788 | return module.__name__ == object.__module__ |
| 789 | elif isinstance(object, property): |
| 790 | return True # [XX] no way not be sure. |
| 791 | else: |
| 792 | raise ValueError("object must be a class or function") |
| 793 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 794 | def _find(self, tests, obj, name, module, source_lines, globs, seen): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 795 | """ |
| 796 | Find tests for the given object and any contained objects, and |
| 797 | add them to `tests`. |
| 798 | """ |
| 799 | if self._verbose: |
| 800 | print 'Finding tests in %s' % name |
| 801 | |
| 802 | # If we've already processed this object, then ignore it. |
| 803 | if id(obj) in seen: |
| 804 | return |
| 805 | seen[id(obj)] = 1 |
| 806 | |
| 807 | # Find a test for this object, and add it to the list of tests. |
| 808 | test = self._get_test(obj, name, module, globs, source_lines) |
| 809 | if test is not None: |
| 810 | tests.append(test) |
| 811 | |
| 812 | # Look for tests in a module's contained objects. |
| 813 | if inspect.ismodule(obj) and self._recurse: |
| 814 | for valname, val in obj.__dict__.items(): |
| 815 | # Check if this contained object should be ignored. |
| 816 | if self._filter(val, name, valname): |
| 817 | continue |
| 818 | valname = '%s.%s' % (name, valname) |
| 819 | # Recurse to functions & classes. |
| 820 | if ((inspect.isfunction(val) or inspect.isclass(val)) and |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 821 | self._from_module(module, val)): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 822 | self._find(tests, val, valname, module, source_lines, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 823 | globs, seen) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 824 | |
| 825 | # Look for tests in a module's __test__ dictionary. |
| 826 | if inspect.ismodule(obj) and self._recurse: |
| 827 | for valname, val in getattr(obj, '__test__', {}).items(): |
| 828 | if not isinstance(valname, basestring): |
| 829 | raise ValueError("DocTestFinder.find: __test__ keys " |
| 830 | "must be strings: %r" % |
| 831 | (type(valname),)) |
| 832 | if not (inspect.isfunction(val) or inspect.isclass(val) or |
| 833 | inspect.ismethod(val) or inspect.ismodule(val) or |
| 834 | isinstance(val, basestring)): |
| 835 | raise ValueError("DocTestFinder.find: __test__ values " |
| 836 | "must be strings, functions, methods, " |
| 837 | "classes, or modules: %r" % |
| 838 | (type(val),)) |
| 839 | valname = '%s.%s' % (name, valname) |
| 840 | self._find(tests, val, valname, module, source_lines, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 841 | globs, seen) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 842 | |
| 843 | # Look for tests in a class's contained objects. |
| 844 | if inspect.isclass(obj) and self._recurse: |
| 845 | for valname, val in obj.__dict__.items(): |
| 846 | # Check if this contained object should be ignored. |
| 847 | if self._filter(val, name, valname): |
| 848 | continue |
| 849 | # Special handling for staticmethod/classmethod. |
| 850 | if isinstance(val, staticmethod): |
| 851 | val = getattr(obj, valname) |
| 852 | if isinstance(val, classmethod): |
| 853 | val = getattr(obj, valname).im_func |
| 854 | |
| 855 | # Recurse to methods, properties, and nested classes. |
| 856 | if ((inspect.isfunction(val) or inspect.isclass(val) or |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 857 | isinstance(val, property)) and |
| 858 | self._from_module(module, val)): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 859 | valname = '%s.%s' % (name, valname) |
| 860 | self._find(tests, val, valname, module, source_lines, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 861 | globs, seen) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 862 | |
| 863 | def _get_test(self, obj, name, module, globs, source_lines): |
| 864 | """ |
| 865 | Return a DocTest for the given object, if it defines a docstring; |
| 866 | otherwise, return None. |
| 867 | """ |
| 868 | # Extract the object's docstring. If it doesn't have one, |
| 869 | # then return None (no test for this object). |
| 870 | if isinstance(obj, basestring): |
| 871 | docstring = obj |
| 872 | else: |
| 873 | try: |
| 874 | if obj.__doc__ is None: |
| 875 | return None |
| 876 | docstring = str(obj.__doc__) |
| 877 | except (TypeError, AttributeError): |
| 878 | return None |
| 879 | |
| 880 | # Don't bother if the docstring is empty. |
| 881 | if not docstring: |
| 882 | return None |
| 883 | |
| 884 | # Find the docstring's location in the file. |
| 885 | lineno = self._find_lineno(obj, source_lines) |
| 886 | |
| 887 | # Return a DocTest for this object. |
| 888 | if module is None: |
| 889 | filename = None |
| 890 | else: |
| 891 | filename = getattr(module, '__file__', module.__name__) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 892 | return self._parser.get_doctest(docstring, globs, name, |
| 893 | filename, lineno) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 894 | |
| 895 | def _find_lineno(self, obj, source_lines): |
| 896 | """ |
| 897 | Return a line number of the given object's docstring. Note: |
| 898 | this method assumes that the object has a docstring. |
| 899 | """ |
| 900 | lineno = None |
| 901 | |
| 902 | # Find the line number for modules. |
| 903 | if inspect.ismodule(obj): |
| 904 | lineno = 0 |
| 905 | |
| 906 | # Find the line number for classes. |
| 907 | # Note: this could be fooled if a class is defined multiple |
| 908 | # times in a single file. |
| 909 | if inspect.isclass(obj): |
| 910 | if source_lines is None: |
| 911 | return None |
| 912 | pat = re.compile(r'^\s*class\s*%s\b' % |
| 913 | getattr(obj, '__name__', '-')) |
| 914 | for i, line in enumerate(source_lines): |
| 915 | if pat.match(line): |
| 916 | lineno = i |
| 917 | break |
| 918 | |
| 919 | # Find the line number for functions & methods. |
| 920 | if inspect.ismethod(obj): obj = obj.im_func |
| 921 | if inspect.isfunction(obj): obj = obj.func_code |
| 922 | if inspect.istraceback(obj): obj = obj.tb_frame |
| 923 | if inspect.isframe(obj): obj = obj.f_code |
| 924 | if inspect.iscode(obj): |
| 925 | lineno = getattr(obj, 'co_firstlineno', None)-1 |
| 926 | |
| 927 | # Find the line number where the docstring starts. Assume |
| 928 | # that it's the first line that begins with a quote mark. |
| 929 | # Note: this could be fooled by a multiline function |
| 930 | # signature, where a continuation line begins with a quote |
| 931 | # mark. |
| 932 | if lineno is not None: |
| 933 | if source_lines is None: |
| 934 | return lineno+1 |
| 935 | pat = re.compile('(^|.*:)\s*\w*("|\')') |
| 936 | for lineno in range(lineno, len(source_lines)): |
| 937 | if pat.match(source_lines[lineno]): |
| 938 | return lineno |
| 939 | |
| 940 | # We couldn't find the line number. |
| 941 | return None |
| 942 | |
| 943 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 944 | ## 5. DocTest Runner |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 945 | ###################################################################### |
| 946 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 947 | class DocTestRunner: |
| 948 | """ |
| 949 | A class used to run DocTest test cases, and accumulate statistics. |
| 950 | The `run` method is used to process a single DocTest case. It |
| 951 | returns a tuple `(f, t)`, where `t` is the number of test cases |
| 952 | tried, and `f` is the number of test cases that failed. |
| 953 | |
| 954 | >>> tests = DocTestFinder().find(_TestClass) |
| 955 | >>> runner = DocTestRunner(verbose=False) |
| 956 | >>> for test in tests: |
| 957 | ... print runner.run(test) |
| 958 | (0, 2) |
| 959 | (0, 1) |
| 960 | (0, 2) |
| 961 | (0, 2) |
| 962 | |
| 963 | The `summarize` method prints a summary of all the test cases that |
| 964 | have been run by the runner, and returns an aggregated `(f, t)` |
| 965 | tuple: |
| 966 | |
| 967 | >>> runner.summarize(verbose=1) |
| 968 | 4 items passed all tests: |
| 969 | 2 tests in _TestClass |
| 970 | 2 tests in _TestClass.__init__ |
| 971 | 2 tests in _TestClass.get |
| 972 | 1 tests in _TestClass.square |
| 973 | 7 tests in 4 items. |
| 974 | 7 passed and 0 failed. |
| 975 | Test passed. |
| 976 | (0, 7) |
| 977 | |
| 978 | The aggregated number of tried examples and failed examples is |
| 979 | also available via the `tries` and `failures` attributes: |
| 980 | |
| 981 | >>> runner.tries |
| 982 | 7 |
| 983 | >>> runner.failures |
| 984 | 0 |
| 985 | |
| 986 | The comparison between expected outputs and actual outputs is done |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 987 | by an `OutputChecker`. This comparison may be customized with a |
| 988 | number of option flags; see the documentation for `testmod` for |
| 989 | more information. If the option flags are insufficient, then the |
| 990 | comparison may also be customized by passing a subclass of |
| 991 | `OutputChecker` to the constructor. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 992 | |
| 993 | The test runner's display output can be controlled in two ways. |
| 994 | First, an output function (`out) can be passed to |
| 995 | `TestRunner.run`; this function will be called with strings that |
| 996 | should be displayed. It defaults to `sys.stdout.write`. If |
| 997 | capturing the output is not sufficient, then the display output |
| 998 | can be also customized by subclassing DocTestRunner, and |
| 999 | overriding the methods `report_start`, `report_success`, |
| 1000 | `report_unexpected_exception`, and `report_failure`. |
| 1001 | """ |
| 1002 | # This divider string is used to separate failure messages, and to |
| 1003 | # separate sections of the summary. |
| 1004 | DIVIDER = "*" * 70 |
| 1005 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1006 | def __init__(self, checker=None, verbose=None, optionflags=0): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1007 | """ |
| 1008 | Create a new test runner. |
| 1009 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1010 | Optional keyword arg `checker` is the `OutputChecker` that |
| 1011 | should be used to compare the expected outputs and actual |
| 1012 | outputs of doctest examples. |
| 1013 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1014 | Optional keyword arg 'verbose' prints lots of stuff if true, |
| 1015 | only failures if false; by default, it's true iff '-v' is in |
| 1016 | sys.argv. |
| 1017 | |
| 1018 | Optional argument `optionflags` can be used to control how the |
| 1019 | test runner compares expected output to actual output, and how |
| 1020 | it displays failures. See the documentation for `testmod` for |
| 1021 | more information. |
| 1022 | """ |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1023 | self._checker = checker or OutputChecker() |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1024 | if verbose is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1025 | verbose = '-v' in sys.argv |
| 1026 | self._verbose = verbose |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1027 | self.optionflags = optionflags |
| 1028 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1029 | # Keep track of the examples we've run. |
| 1030 | self.tries = 0 |
| 1031 | self.failures = 0 |
| 1032 | self._name2ft = {} |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1033 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1034 | # Create a fake output target for capturing doctest output. |
| 1035 | self._fakeout = _SpoofOut() |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 1036 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1037 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1038 | # Reporting methods |
| 1039 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 1040 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1041 | def report_start(self, out, test, example): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1042 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1043 | Report that the test runner is about to process the given |
| 1044 | example. (Only displays a message if verbose=True) |
| 1045 | """ |
| 1046 | if self._verbose: |
| 1047 | out(_tag_msg("Trying", example.source) + |
| 1048 | _tag_msg("Expecting", example.want or "nothing")) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1049 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1050 | def report_success(self, out, test, example, got): |
| 1051 | """ |
| 1052 | Report that the given example ran successfully. (Only |
| 1053 | displays a message if verbose=True) |
| 1054 | """ |
| 1055 | if self._verbose: |
| 1056 | out("ok\n") |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1057 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1058 | def report_failure(self, out, test, example, got): |
| 1059 | """ |
| 1060 | Report that the given example failed. |
| 1061 | """ |
| 1062 | # Print an error message. |
| 1063 | out(self.__failure_header(test, example) + |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1064 | self._checker.output_difference(example.want, got, |
| 1065 | self.optionflags)) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1066 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1067 | def report_unexpected_exception(self, out, test, example, exc_info): |
| 1068 | """ |
| 1069 | Report that the given example raised an unexpected exception. |
| 1070 | """ |
| 1071 | # Get a traceback message. |
| 1072 | excout = StringIO() |
| 1073 | exc_type, exc_val, exc_tb = exc_info |
| 1074 | traceback.print_exception(exc_type, exc_val, exc_tb, file=excout) |
| 1075 | exception_tb = excout.getvalue() |
| 1076 | # Print an error message. |
| 1077 | out(self.__failure_header(test, example) + |
| 1078 | _tag_msg("Exception raised", exception_tb)) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1079 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1080 | def __failure_header(self, test, example): |
| 1081 | s = (self.DIVIDER + "\n" + |
| 1082 | _tag_msg("Failure in example", example.source)) |
| 1083 | if test.filename is None: |
| 1084 | # [XX] I'm not putting +1 here, to give the same output |
| 1085 | # as the old version. But I think it *should* go here. |
| 1086 | return s + ("from line #%s of %s\n" % |
| 1087 | (example.lineno, test.name)) |
| 1088 | elif test.lineno is None: |
| 1089 | return s + ("from line #%s of %s in %s\n" % |
| 1090 | (example.lineno+1, test.name, test.filename)) |
| 1091 | else: |
| 1092 | lineno = test.lineno+example.lineno+1 |
| 1093 | return s + ("from line #%s of %s (%s)\n" % |
| 1094 | (lineno, test.filename, test.name)) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1095 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1096 | #///////////////////////////////////////////////////////////////// |
| 1097 | # DocTest Running |
| 1098 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1099 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1100 | # A regular expression for handling `want` strings that contain |
| 1101 | # expected exceptions. It divides `want` into two pieces: the |
| 1102 | # pre-exception output (`out`) and the exception message (`exc`), |
| 1103 | # as generated by traceback.format_exception_only(). (I assume |
| 1104 | # that the exception_only message is the first non-indented line |
| 1105 | # starting with word characters after the "Traceback ...".) |
| 1106 | _EXCEPTION_RE = re.compile(('^(?P<out>.*)' |
| 1107 | '^(?P<hdr>Traceback \((?:%s|%s)\):)\s*$.*?' |
| 1108 | '^(?P<exc>\w+.*)') % |
| 1109 | ('most recent call last', 'innermost last'), |
| 1110 | re.MULTILINE | re.DOTALL) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1111 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1112 | _OPTION_DIRECTIVE_RE = re.compile('\s*doctest:\s*(?P<flags>[^#\n]*)') |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1113 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1114 | def __handle_directive(self, example): |
| 1115 | """ |
| 1116 | Check if the given example is actually a directive to doctest |
| 1117 | (to turn an optionflag on or off); and if it is, then handle |
| 1118 | the directive. |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1119 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1120 | Return true iff the example is actually a directive (and so |
| 1121 | should not be executed). |
Tim Peters | 4a9ac4a | 2001-10-02 22:47:08 +0000 | [diff] [blame] | 1122 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1123 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1124 | m = self._OPTION_DIRECTIVE_RE.match(example.source) |
| 1125 | if m is None: |
| 1126 | return False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1127 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1128 | for flag in m.group('flags').upper().split(): |
| 1129 | if (flag[:1] not in '+-' or |
| 1130 | flag[1:] not in OPTIONFLAGS_BY_NAME): |
| 1131 | raise ValueError('Bad doctest option directive: '+flag) |
| 1132 | if flag[0] == '+': |
| 1133 | self.optionflags |= OPTIONFLAGS_BY_NAME[flag[1:]] |
| 1134 | else: |
| 1135 | self.optionflags &= ~OPTIONFLAGS_BY_NAME[flag[1:]] |
| 1136 | return True |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1137 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1138 | def __run(self, test, compileflags, out): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1139 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1140 | Run the examples in `test`. Write the outcome of each example |
| 1141 | with one of the `DocTestRunner.report_*` methods, using the |
| 1142 | writer function `out`. `compileflags` is the set of compiler |
| 1143 | flags that should be used to execute examples. Return a tuple |
| 1144 | `(f, t)`, where `t` is the number of examples tried, and `f` |
| 1145 | is the number of examples that failed. The examples are run |
| 1146 | in the namespace `test.globs`. |
| 1147 | """ |
| 1148 | # Keep track of the number of failures and tries. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1149 | failures = tries = 0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1150 | |
| 1151 | # Save the option flags (since option directives can be used |
| 1152 | # to modify them). |
| 1153 | original_optionflags = self.optionflags |
| 1154 | |
| 1155 | # Process each example. |
| 1156 | for example in test.examples: |
| 1157 | # Check if it's an option directive. If it is, then handle |
| 1158 | # it, and go on to the next example. |
| 1159 | if self.__handle_directive(example): |
| 1160 | continue |
| 1161 | |
| 1162 | # Record that we started this example. |
| 1163 | tries += 1 |
| 1164 | self.report_start(out, test, example) |
| 1165 | |
| 1166 | # Run the example in the given context (globs), and record |
| 1167 | # any exception that gets raised. (But don't intercept |
| 1168 | # keyboard interrupts.) |
| 1169 | try: |
Tim Peters | 208ca70 | 2004-08-09 04:12:36 +0000 | [diff] [blame] | 1170 | # Don't blink! This is where the user's code gets run. |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 1171 | exec compile(example.source, "<string>", "single", |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1172 | compileflags, 1) in test.globs |
| 1173 | exception = None |
| 1174 | except KeyboardInterrupt: |
| 1175 | raise |
| 1176 | except: |
| 1177 | exception = sys.exc_info() |
| 1178 | |
Tim Peters | 208ca70 | 2004-08-09 04:12:36 +0000 | [diff] [blame] | 1179 | got = self._fakeout.getvalue() # the actual output |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1180 | self._fakeout.truncate(0) |
| 1181 | |
| 1182 | # If the example executed without raising any exceptions, |
| 1183 | # then verify its output and report its outcome. |
| 1184 | if exception is None: |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1185 | if self._checker.check_output(example.want, got, |
| 1186 | self.optionflags): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1187 | self.report_success(out, test, example, got) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1188 | else: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1189 | self.report_failure(out, test, example, got) |
| 1190 | failures += 1 |
| 1191 | |
| 1192 | # If the example raised an exception, then check if it was |
| 1193 | # expected. |
| 1194 | else: |
| 1195 | exc_info = sys.exc_info() |
| 1196 | exc_msg = traceback.format_exception_only(*exc_info[:2])[-1] |
| 1197 | |
| 1198 | # Search the `want` string for an exception. If we don't |
| 1199 | # find one, then report an unexpected exception. |
| 1200 | m = self._EXCEPTION_RE.match(example.want) |
| 1201 | if m is None: |
| 1202 | self.report_unexpected_exception(out, test, example, |
| 1203 | exc_info) |
| 1204 | failures += 1 |
| 1205 | else: |
| 1206 | exc_hdr = m.group('hdr')+'\n' # Exception header |
| 1207 | # The test passes iff the pre-exception output and |
| 1208 | # the exception description match the values given |
| 1209 | # in `want`. |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1210 | if (self._checker.check_output(m.group('out'), got, |
| 1211 | self.optionflags) and |
| 1212 | self._checker.check_output(m.group('exc'), exc_msg, |
| 1213 | self.optionflags)): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1214 | # Is +exc_msg the right thing here?? |
| 1215 | self.report_success(out, test, example, |
| 1216 | got+exc_hdr+exc_msg) |
| 1217 | else: |
| 1218 | self.report_failure(out, test, example, |
| 1219 | got+exc_hdr+exc_msg) |
| 1220 | failures += 1 |
| 1221 | |
| 1222 | # Restore the option flags (in case they were modified) |
| 1223 | self.optionflags = original_optionflags |
| 1224 | |
| 1225 | # Record and return the number of failures and tries. |
| 1226 | self.__record_outcome(test, failures, tries) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1227 | return failures, tries |
| 1228 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1229 | def __record_outcome(self, test, f, t): |
| 1230 | """ |
| 1231 | Record the fact that the given DocTest (`test`) generated `f` |
| 1232 | failures out of `t` tried examples. |
| 1233 | """ |
| 1234 | f2, t2 = self._name2ft.get(test.name, (0,0)) |
| 1235 | self._name2ft[test.name] = (f+f2, t+t2) |
| 1236 | self.failures += f |
| 1237 | self.tries += t |
| 1238 | |
| 1239 | def run(self, test, compileflags=None, out=None, clear_globs=True): |
| 1240 | """ |
| 1241 | Run the examples in `test`, and display the results using the |
| 1242 | writer function `out`. |
| 1243 | |
| 1244 | The examples are run in the namespace `test.globs`. If |
| 1245 | `clear_globs` is true (the default), then this namespace will |
| 1246 | be cleared after the test runs, to help with garbage |
| 1247 | collection. If you would like to examine the namespace after |
| 1248 | the test completes, then use `clear_globs=False`. |
| 1249 | |
| 1250 | `compileflags` gives the set of flags that should be used by |
| 1251 | the Python compiler when running the examples. If not |
| 1252 | specified, then it will default to the set of future-import |
| 1253 | flags that apply to `globs`. |
| 1254 | |
| 1255 | The output of each example is checked using |
| 1256 | `DocTestRunner.check_output`, and the results are formatted by |
| 1257 | the `DocTestRunner.report_*` methods. |
| 1258 | """ |
| 1259 | if compileflags is None: |
| 1260 | compileflags = _extract_future_flags(test.globs) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1261 | |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1262 | save_stdout = sys.stdout |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1263 | if out is None: |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1264 | out = save_stdout.write |
| 1265 | sys.stdout = self._fakeout |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1266 | |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1267 | # Patch pdb.set_trace to restore sys.stdout, so that interactive |
| 1268 | # debugging output is visible (not still redirected to self._fakeout). |
| 1269 | # Note that we run "the real" pdb.set_trace (captured at doctest |
| 1270 | # import time) in our replacement. Because the current run() may |
| 1271 | # run another doctest (and so on), the current pdb.set_trace may be |
| 1272 | # our set_trace function, which changes sys.stdout. If we called |
| 1273 | # a chain of those, we wouldn't be left with the save_stdout |
| 1274 | # *this* run() invocation wants. |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1275 | def set_trace(): |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1276 | sys.stdout = save_stdout |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1277 | real_pdb_set_trace() |
| 1278 | |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1279 | save_set_trace = pdb.set_trace |
| 1280 | pdb.set_trace = set_trace |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1281 | try: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1282 | return self.__run(test, compileflags, out) |
| 1283 | finally: |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1284 | sys.stdout = save_stdout |
| 1285 | pdb.set_trace = save_set_trace |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1286 | if clear_globs: |
| 1287 | test.globs.clear() |
| 1288 | |
| 1289 | #///////////////////////////////////////////////////////////////// |
| 1290 | # Summarization |
| 1291 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1292 | def summarize(self, verbose=None): |
| 1293 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1294 | Print a summary of all the test cases that have been run by |
| 1295 | this DocTestRunner, and return a tuple `(f, t)`, where `f` is |
| 1296 | the total number of failed examples, and `t` is the total |
| 1297 | number of tried examples. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1298 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1299 | The optional `verbose` argument controls how detailed the |
| 1300 | summary is. If the verbosity is not specified, then the |
| 1301 | DocTestRunner's verbosity is used. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1302 | """ |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1303 | if verbose is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1304 | verbose = self._verbose |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1305 | notests = [] |
| 1306 | passed = [] |
| 1307 | failed = [] |
| 1308 | totalt = totalf = 0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1309 | for x in self._name2ft.items(): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1310 | name, (f, t) = x |
| 1311 | assert f <= t |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1312 | totalt += t |
| 1313 | totalf += f |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1314 | if t == 0: |
| 1315 | notests.append(name) |
| 1316 | elif f == 0: |
| 1317 | passed.append( (name, t) ) |
| 1318 | else: |
| 1319 | failed.append(x) |
| 1320 | if verbose: |
| 1321 | if notests: |
| 1322 | print len(notests), "items had no tests:" |
| 1323 | notests.sort() |
| 1324 | for thing in notests: |
| 1325 | print " ", thing |
| 1326 | if passed: |
| 1327 | print len(passed), "items passed all tests:" |
| 1328 | passed.sort() |
| 1329 | for thing, count in passed: |
| 1330 | print " %3d tests in %s" % (count, thing) |
| 1331 | if failed: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1332 | print self.DIVIDER |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1333 | print len(failed), "items had failures:" |
| 1334 | failed.sort() |
| 1335 | for thing, (f, t) in failed: |
| 1336 | print " %3d of %3d in %s" % (f, t, thing) |
| 1337 | if verbose: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1338 | print totalt, "tests in", len(self._name2ft), "items." |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1339 | print totalt - totalf, "passed and", totalf, "failed." |
| 1340 | if totalf: |
| 1341 | print "***Test Failed***", totalf, "failures." |
| 1342 | elif verbose: |
| 1343 | print "Test passed." |
| 1344 | return totalf, totalt |
| 1345 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1346 | class OutputChecker: |
| 1347 | """ |
| 1348 | A class used to check the whether the actual output from a doctest |
| 1349 | example matches the expected output. `OutputChecker` defines two |
| 1350 | methods: `check_output`, which compares a given pair of outputs, |
| 1351 | and returns true if they match; and `output_difference`, which |
| 1352 | returns a string describing the differences between two outputs. |
| 1353 | """ |
| 1354 | def check_output(self, want, got, optionflags): |
| 1355 | """ |
| 1356 | Return True iff the actual output (`got`) matches the expected |
| 1357 | output (`want`). These strings are always considered to match |
| 1358 | if they are identical; but depending on what option flags the |
| 1359 | test runner is using, several non-exact match types are also |
| 1360 | possible. See the documentation for `TestRunner` for more |
| 1361 | information about option flags. |
| 1362 | """ |
| 1363 | # Handle the common case first, for efficiency: |
| 1364 | # if they're string-identical, always return true. |
| 1365 | if got == want: |
| 1366 | return True |
| 1367 | |
| 1368 | # The values True and False replaced 1 and 0 as the return |
| 1369 | # value for boolean comparisons in Python 2.3. |
| 1370 | if not (optionflags & DONT_ACCEPT_TRUE_FOR_1): |
| 1371 | if (got,want) == ("True\n", "1\n"): |
| 1372 | return True |
| 1373 | if (got,want) == ("False\n", "0\n"): |
| 1374 | return True |
| 1375 | |
| 1376 | # <BLANKLINE> can be used as a special sequence to signify a |
| 1377 | # blank line, unless the DONT_ACCEPT_BLANKLINE flag is used. |
| 1378 | if not (optionflags & DONT_ACCEPT_BLANKLINE): |
| 1379 | # Replace <BLANKLINE> in want with a blank line. |
| 1380 | want = re.sub('(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER), |
| 1381 | '', want) |
| 1382 | # If a line in got contains only spaces, then remove the |
| 1383 | # spaces. |
| 1384 | got = re.sub('(?m)^\s*?$', '', got) |
| 1385 | if got == want: |
| 1386 | return True |
| 1387 | |
| 1388 | # This flag causes doctest to ignore any differences in the |
| 1389 | # contents of whitespace strings. Note that this can be used |
| 1390 | # in conjunction with the ELLISPIS flag. |
| 1391 | if (optionflags & NORMALIZE_WHITESPACE): |
| 1392 | got = ' '.join(got.split()) |
| 1393 | want = ' '.join(want.split()) |
| 1394 | if got == want: |
| 1395 | return True |
| 1396 | |
| 1397 | # The ELLIPSIS flag says to let the sequence "..." in `want` |
| 1398 | # match any substring in `got`. We implement this by |
| 1399 | # transforming `want` into a regular expression. |
| 1400 | if (optionflags & ELLIPSIS): |
| 1401 | # Escape any special regexp characters |
| 1402 | want_re = re.escape(want) |
| 1403 | # Replace ellipsis markers ('...') with .* |
| 1404 | want_re = want_re.replace(re.escape(ELLIPSIS_MARKER), '.*') |
| 1405 | # Require that it matches the entire string; and set the |
| 1406 | # re.DOTALL flag (with '(?s)'). |
| 1407 | want_re = '(?s)^%s$' % want_re |
| 1408 | # Check if the `want_re` regexp matches got. |
| 1409 | if re.match(want_re, got): |
| 1410 | return True |
| 1411 | |
| 1412 | # We didn't find any match; return false. |
| 1413 | return False |
| 1414 | |
| 1415 | def output_difference(self, want, got, optionflags): |
| 1416 | """ |
| 1417 | Return a string describing the differences between the |
| 1418 | expected output (`want`) and the actual output (`got`). |
| 1419 | """ |
| 1420 | # If <BLANKLINE>s are being used, then replace <BLANKLINE> |
| 1421 | # with blank lines in the expected output string. |
| 1422 | if not (optionflags & DONT_ACCEPT_BLANKLINE): |
| 1423 | want = re.sub('(?m)^%s$' % re.escape(BLANKLINE_MARKER), '', want) |
| 1424 | |
| 1425 | # Check if we should use diff. Don't use diff if the actual |
| 1426 | # or expected outputs are too short, or if the expected output |
| 1427 | # contains an ellipsis marker. |
| 1428 | if ((optionflags & (UNIFIED_DIFF | CONTEXT_DIFF)) and |
| 1429 | want.count('\n') > 2 and got.count('\n') > 2 and |
| 1430 | not (optionflags & ELLIPSIS and '...' in want)): |
| 1431 | # Split want & got into lines. |
| 1432 | want_lines = [l+'\n' for l in want.split('\n')] |
| 1433 | got_lines = [l+'\n' for l in got.split('\n')] |
| 1434 | # Use difflib to find their differences. |
| 1435 | if optionflags & UNIFIED_DIFF: |
| 1436 | diff = difflib.unified_diff(want_lines, got_lines, n=2, |
| 1437 | fromfile='Expected', tofile='Got') |
| 1438 | kind = 'unified' |
| 1439 | elif optionflags & CONTEXT_DIFF: |
| 1440 | diff = difflib.context_diff(want_lines, got_lines, n=2, |
| 1441 | fromfile='Expected', tofile='Got') |
| 1442 | kind = 'context' |
| 1443 | else: |
| 1444 | assert 0, 'Bad diff option' |
| 1445 | # Remove trailing whitespace on diff output. |
| 1446 | diff = [line.rstrip() + '\n' for line in diff] |
| 1447 | return _tag_msg("Differences (" + kind + " diff)", |
| 1448 | ''.join(diff)) |
| 1449 | |
| 1450 | # If we're not using diff, then simply list the expected |
| 1451 | # output followed by the actual output. |
| 1452 | return (_tag_msg("Expected", want or "Nothing") + |
| 1453 | _tag_msg("Got", got)) |
| 1454 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1455 | class DocTestFailure(Exception): |
| 1456 | """A DocTest example has failed in debugging mode. |
| 1457 | |
| 1458 | The exception instance has variables: |
| 1459 | |
| 1460 | - test: the DocTest object being run |
| 1461 | |
| 1462 | - excample: the Example object that failed |
| 1463 | |
| 1464 | - got: the actual output |
| 1465 | """ |
| 1466 | def __init__(self, test, example, got): |
| 1467 | self.test = test |
| 1468 | self.example = example |
| 1469 | self.got = got |
| 1470 | |
| 1471 | def __str__(self): |
| 1472 | return str(self.test) |
| 1473 | |
| 1474 | class UnexpectedException(Exception): |
| 1475 | """A DocTest example has encountered an unexpected exception |
| 1476 | |
| 1477 | The exception instance has variables: |
| 1478 | |
| 1479 | - test: the DocTest object being run |
| 1480 | |
| 1481 | - excample: the Example object that failed |
| 1482 | |
| 1483 | - exc_info: the exception info |
| 1484 | """ |
| 1485 | def __init__(self, test, example, exc_info): |
| 1486 | self.test = test |
| 1487 | self.example = example |
| 1488 | self.exc_info = exc_info |
| 1489 | |
| 1490 | def __str__(self): |
| 1491 | return str(self.test) |
Tim Peters | d1b7827 | 2004-08-07 06:03:09 +0000 | [diff] [blame] | 1492 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1493 | class DebugRunner(DocTestRunner): |
| 1494 | r"""Run doc tests but raise an exception as soon as there is a failure. |
| 1495 | |
| 1496 | If an unexpected exception occurs, an UnexpectedException is raised. |
| 1497 | It contains the test, the example, and the original exception: |
| 1498 | |
| 1499 | >>> runner = DebugRunner(verbose=False) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1500 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
| 1501 | ... {}, 'foo', 'foo.py', 0) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1502 | >>> try: |
| 1503 | ... runner.run(test) |
| 1504 | ... except UnexpectedException, failure: |
| 1505 | ... pass |
| 1506 | |
| 1507 | >>> failure.test is test |
| 1508 | True |
| 1509 | |
| 1510 | >>> failure.example.want |
| 1511 | '42\n' |
| 1512 | |
| 1513 | >>> exc_info = failure.exc_info |
| 1514 | >>> raise exc_info[0], exc_info[1], exc_info[2] |
| 1515 | Traceback (most recent call last): |
| 1516 | ... |
| 1517 | KeyError |
| 1518 | |
| 1519 | We wrap the original exception to give the calling application |
| 1520 | access to the test and example information. |
| 1521 | |
| 1522 | If the output doesn't match, then a DocTestFailure is raised: |
| 1523 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1524 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1525 | ... >>> x = 1 |
| 1526 | ... >>> x |
| 1527 | ... 2 |
| 1528 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1529 | |
| 1530 | >>> try: |
| 1531 | ... runner.run(test) |
| 1532 | ... except DocTestFailure, failure: |
| 1533 | ... pass |
| 1534 | |
| 1535 | DocTestFailure objects provide access to the test: |
| 1536 | |
| 1537 | >>> failure.test is test |
| 1538 | True |
| 1539 | |
| 1540 | As well as to the example: |
| 1541 | |
| 1542 | >>> failure.example.want |
| 1543 | '2\n' |
| 1544 | |
| 1545 | and the actual output: |
| 1546 | |
| 1547 | >>> failure.got |
| 1548 | '1\n' |
| 1549 | |
| 1550 | If a failure or error occurs, the globals are left intact: |
| 1551 | |
| 1552 | >>> del test.globs['__builtins__'] |
| 1553 | >>> test.globs |
| 1554 | {'x': 1} |
| 1555 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1556 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1557 | ... >>> x = 2 |
| 1558 | ... >>> raise KeyError |
| 1559 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1560 | |
| 1561 | >>> runner.run(test) |
| 1562 | Traceback (most recent call last): |
| 1563 | ... |
| 1564 | UnexpectedException: <DocTest foo from foo.py:0 (2 examples)> |
Tim Peters | d1b7827 | 2004-08-07 06:03:09 +0000 | [diff] [blame] | 1565 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1566 | >>> del test.globs['__builtins__'] |
| 1567 | >>> test.globs |
| 1568 | {'x': 2} |
| 1569 | |
| 1570 | But the globals are cleared if there is no error: |
| 1571 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1572 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1573 | ... >>> x = 2 |
| 1574 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1575 | |
| 1576 | >>> runner.run(test) |
| 1577 | (0, 1) |
| 1578 | |
| 1579 | >>> test.globs |
| 1580 | {} |
| 1581 | |
| 1582 | """ |
| 1583 | |
| 1584 | def run(self, test, compileflags=None, out=None, clear_globs=True): |
| 1585 | r = DocTestRunner.run(self, test, compileflags, out, False) |
| 1586 | if clear_globs: |
| 1587 | test.globs.clear() |
| 1588 | return r |
| 1589 | |
| 1590 | def report_unexpected_exception(self, out, test, example, exc_info): |
| 1591 | raise UnexpectedException(test, example, exc_info) |
| 1592 | |
| 1593 | def report_failure(self, out, test, example, got): |
| 1594 | raise DocTestFailure(test, example, got) |
| 1595 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1596 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 1597 | ## 6. Test Functions |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1598 | ###################################################################### |
| 1599 | # These should be backwards compatible. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1600 | |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1601 | def testmod(m=None, name=None, globs=None, verbose=None, isprivate=None, |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1602 | report=True, optionflags=0, extraglobs=None, |
| 1603 | raise_on_error=False): |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1604 | """m=None, name=None, globs=None, verbose=None, isprivate=None, |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1605 | report=True, optionflags=0, extraglobs=None |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1606 | |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1607 | Test examples in docstrings in functions and classes reachable |
| 1608 | from module m (or the current module if m is not supplied), starting |
Raymond Hettinger | 71adf7e | 2003-07-16 19:25:22 +0000 | [diff] [blame] | 1609 | with m.__doc__. Unless isprivate is specified, private names |
| 1610 | are not skipped. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1611 | |
| 1612 | Also test examples reachable from dict m.__test__ if it exists and is |
| 1613 | not None. m.__dict__ maps names to functions, classes and strings; |
| 1614 | function and class docstrings are tested even if the name is private; |
| 1615 | strings are tested directly, as if they were docstrings. |
| 1616 | |
| 1617 | Return (#failures, #tests). |
| 1618 | |
| 1619 | See doctest.__doc__ for an overview. |
| 1620 | |
| 1621 | Optional keyword arg "name" gives the name of the module; by default |
| 1622 | use m.__name__. |
| 1623 | |
| 1624 | Optional keyword arg "globs" gives a dict to be used as the globals |
| 1625 | when executing examples; by default, use m.__dict__. A copy of this |
| 1626 | dict is actually used for each docstring, so that each docstring's |
| 1627 | examples start with a clean slate. |
| 1628 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1629 | Optional keyword arg "extraglobs" gives a dictionary that should be |
| 1630 | merged into the globals that are used to execute examples. By |
| 1631 | default, no extra globals are used. This is new in 2.4. |
| 1632 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1633 | Optional keyword arg "verbose" prints lots of stuff if true, prints |
| 1634 | only failures if false; by default, it's true iff "-v" is in sys.argv. |
| 1635 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1636 | Optional keyword arg "report" prints a summary at the end when true, |
| 1637 | else prints nothing at the end. In verbose mode, the summary is |
| 1638 | detailed, else very brief (in fact, empty if all tests passed). |
| 1639 | |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1640 | Optional keyword arg "optionflags" or's together module constants, |
| 1641 | and defaults to 0. This is new in 2.3. Possible values: |
| 1642 | |
| 1643 | DONT_ACCEPT_TRUE_FOR_1 |
| 1644 | By default, if an expected output block contains just "1", |
| 1645 | an actual output block containing just "True" is considered |
| 1646 | to be a match, and similarly for "0" versus "False". When |
| 1647 | DONT_ACCEPT_TRUE_FOR_1 is specified, neither substitution |
| 1648 | is allowed. |
| 1649 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1650 | DONT_ACCEPT_BLANKLINE |
| 1651 | By default, if an expected output block contains a line |
| 1652 | containing only the string "<BLANKLINE>", then that line |
| 1653 | will match a blank line in the actual output. When |
| 1654 | DONT_ACCEPT_BLANKLINE is specified, this substitution is |
| 1655 | not allowed. |
| 1656 | |
| 1657 | NORMALIZE_WHITESPACE |
| 1658 | When NORMALIZE_WHITESPACE is specified, all sequences of |
| 1659 | whitespace are treated as equal. I.e., any sequence of |
| 1660 | whitespace within the expected output will match any |
| 1661 | sequence of whitespace within the actual output. |
| 1662 | |
| 1663 | ELLIPSIS |
| 1664 | When ELLIPSIS is specified, then an ellipsis marker |
| 1665 | ("...") in the expected output can match any substring in |
| 1666 | the actual output. |
| 1667 | |
| 1668 | UNIFIED_DIFF |
| 1669 | When UNIFIED_DIFF is specified, failures that involve |
| 1670 | multi-line expected and actual outputs will be displayed |
| 1671 | using a unified diff. |
| 1672 | |
| 1673 | CONTEXT_DIFF |
| 1674 | When CONTEXT_DIFF is specified, failures that involve |
| 1675 | multi-line expected and actual outputs will be displayed |
| 1676 | using a context diff. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1677 | |
| 1678 | Optional keyword arg "raise_on_error" raises an exception on the |
| 1679 | first unexpected exception or failure. This allows failures to be |
| 1680 | post-mortem debugged. |
| 1681 | |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 1682 | Deprecated in Python 2.4: |
| 1683 | Optional keyword arg "isprivate" specifies a function used to |
| 1684 | determine whether a name is private. The default function is |
| 1685 | treat all functions as public. Optionally, "isprivate" can be |
| 1686 | set to doctest.is_private to skip over functions marked as private |
| 1687 | using the underscore naming convention; see its docs for details. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1688 | """ |
| 1689 | |
| 1690 | """ [XX] This is no longer true: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1691 | Advanced tomfoolery: testmod runs methods of a local instance of |
| 1692 | class doctest.Tester, then merges the results into (or creates) |
| 1693 | global Tester instance doctest.master. Methods of doctest.master |
| 1694 | can be called directly too, if you want to do something unusual. |
| 1695 | Passing report=0 to testmod is especially useful then, to delay |
| 1696 | displaying a summary. Invoke doctest.master.summarize(verbose) |
| 1697 | when you're done fiddling. |
| 1698 | """ |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 1699 | if isprivate is not None: |
| 1700 | warnings.warn("the isprivate argument is deprecated; " |
| 1701 | "examine DocTestFinder.find() lists instead", |
| 1702 | DeprecationWarning) |
| 1703 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1704 | # If no module was given, then use __main__. |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1705 | if m is None: |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1706 | # DWA - m will still be None if this wasn't invoked from the command |
| 1707 | # line, in which case the following TypeError is about as good an error |
| 1708 | # as we should expect |
| 1709 | m = sys.modules.get('__main__') |
| 1710 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1711 | # Check that we were actually given a module. |
| 1712 | if not inspect.ismodule(m): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1713 | raise TypeError("testmod: module required; %r" % (m,)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1714 | |
| 1715 | # If no name was given, then use the module's name. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1716 | if name is None: |
| 1717 | name = m.__name__ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1718 | |
| 1719 | # Find, parse, and run all tests in the given module. |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 1720 | finder = DocTestFinder(_namefilter=isprivate) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1721 | |
| 1722 | if raise_on_error: |
| 1723 | runner = DebugRunner(verbose=verbose, optionflags=optionflags) |
| 1724 | else: |
| 1725 | runner = DocTestRunner(verbose=verbose, optionflags=optionflags) |
| 1726 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1727 | for test in finder.find(m, name, globs=globs, extraglobs=extraglobs): |
| 1728 | runner.run(test) |
| 1729 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1730 | if report: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1731 | runner.summarize() |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1732 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1733 | return runner.failures, runner.tries |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1734 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1735 | def run_docstring_examples(f, globs, verbose=False, name="NoName", |
| 1736 | compileflags=None, optionflags=0): |
| 1737 | """ |
| 1738 | Test examples in the given object's docstring (`f`), using `globs` |
| 1739 | as globals. Optional argument `name` is used in failure messages. |
| 1740 | If the optional argument `verbose` is true, then generate output |
| 1741 | even if there are no failures. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1742 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1743 | `compileflags` gives the set of flags that should be used by the |
| 1744 | Python compiler when running the examples. If not specified, then |
| 1745 | it will default to the set of future-import flags that apply to |
| 1746 | `globs`. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1747 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1748 | Optional keyword arg `optionflags` specifies options for the |
| 1749 | testing and output. See the documentation for `testmod` for more |
| 1750 | information. |
| 1751 | """ |
| 1752 | # Find, parse, and run all tests in the given module. |
| 1753 | finder = DocTestFinder(verbose=verbose, recurse=False) |
| 1754 | runner = DocTestRunner(verbose=verbose, optionflags=optionflags) |
| 1755 | for test in finder.find(f, name, globs=globs): |
| 1756 | runner.run(test, compileflags=compileflags) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1757 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1758 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 1759 | ## 7. Tester |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1760 | ###################################################################### |
| 1761 | # This is provided only for backwards compatibility. It's not |
| 1762 | # actually used in any way. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1763 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1764 | class Tester: |
| 1765 | def __init__(self, mod=None, globs=None, verbose=None, |
| 1766 | isprivate=None, optionflags=0): |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 1767 | |
| 1768 | warnings.warn("class Tester is deprecated; " |
| 1769 | "use class doctest.DocTestRunner instead", |
| 1770 | DeprecationWarning, stacklevel=2) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1771 | if mod is None and globs is None: |
| 1772 | raise TypeError("Tester.__init__: must specify mod or globs") |
| 1773 | if mod is not None and not _ismodule(mod): |
| 1774 | raise TypeError("Tester.__init__: mod must be a module; %r" % |
| 1775 | (mod,)) |
| 1776 | if globs is None: |
| 1777 | globs = mod.__dict__ |
| 1778 | self.globs = globs |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1779 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1780 | self.verbose = verbose |
| 1781 | self.isprivate = isprivate |
| 1782 | self.optionflags = optionflags |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 1783 | self.testfinder = DocTestFinder(_namefilter=isprivate) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1784 | self.testrunner = DocTestRunner(verbose=verbose, |
| 1785 | optionflags=optionflags) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1786 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1787 | def runstring(self, s, name): |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1788 | test = DocTestParser().get_doctest(s, self.globs, name, None, None) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1789 | if self.verbose: |
| 1790 | print "Running string", name |
| 1791 | (f,t) = self.testrunner.run(test) |
| 1792 | if self.verbose: |
| 1793 | print f, "of", t, "examples failed in string", name |
| 1794 | return (f,t) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1795 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1796 | def rundoc(self, object, name=None, module=None): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1797 | f = t = 0 |
| 1798 | tests = self.testfinder.find(object, name, module=module, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1799 | globs=self.globs) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1800 | for test in tests: |
| 1801 | (f2, t2) = self.testrunner.run(test) |
| 1802 | (f,t) = (f+f2, t+t2) |
| 1803 | return (f,t) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1804 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1805 | def rundict(self, d, name, module=None): |
| 1806 | import new |
| 1807 | m = new.module(name) |
| 1808 | m.__dict__.update(d) |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1809 | if module is None: |
| 1810 | module = False |
| 1811 | return self.rundoc(m, name, module) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1812 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1813 | def run__test__(self, d, name): |
| 1814 | import new |
| 1815 | m = new.module(name) |
| 1816 | m.__test__ = d |
| 1817 | return self.rundoc(m, name, module) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1818 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1819 | def summarize(self, verbose=None): |
| 1820 | return self.testrunner.summarize(verbose) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1821 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1822 | def merge(self, other): |
| 1823 | d = self.testrunner._name2ft |
| 1824 | for name, (f, t) in other.testrunner._name2ft.items(): |
| 1825 | if name in d: |
| 1826 | print "*** Tester.merge: '" + name + "' in both" \ |
| 1827 | " testers; summing outcomes." |
| 1828 | f2, t2 = d[name] |
| 1829 | f = f + f2 |
| 1830 | t = t + t2 |
| 1831 | d[name] = f, t |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1832 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1833 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 1834 | ## 8. Unittest Support |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1835 | ###################################################################### |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1836 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1837 | class DocTestCase(unittest.TestCase): |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1838 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1839 | def __init__(self, test, optionflags=0, setUp=None, tearDown=None, |
| 1840 | checker=None): |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1841 | unittest.TestCase.__init__(self) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1842 | self._dt_optionflags = optionflags |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1843 | self._dt_checker = checker |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1844 | self._dt_test = test |
| 1845 | self._dt_setUp = setUp |
| 1846 | self._dt_tearDown = tearDown |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1847 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1848 | def setUp(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1849 | if self._dt_setUp is not None: |
| 1850 | self._dt_setUp() |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1851 | |
| 1852 | def tearDown(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1853 | if self._dt_tearDown is not None: |
| 1854 | self._dt_tearDown() |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1855 | |
| 1856 | def runTest(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1857 | test = self._dt_test |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1858 | old = sys.stdout |
| 1859 | new = StringIO() |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1860 | runner = DocTestRunner(optionflags=self._dt_optionflags, |
| 1861 | checker=self._dt_checker, verbose=False) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1862 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1863 | try: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1864 | runner.DIVIDER = "-"*70 |
| 1865 | failures, tries = runner.run(test, out=new.write) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1866 | finally: |
| 1867 | sys.stdout = old |
| 1868 | |
| 1869 | if failures: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1870 | raise self.failureException(self.format_failure(new.getvalue())) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1871 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1872 | def format_failure(self, err): |
| 1873 | test = self._dt_test |
| 1874 | if test.lineno is None: |
| 1875 | lineno = 'unknown line number' |
| 1876 | else: |
| 1877 | lineno = 'line %s' % test.lineno |
| 1878 | lname = '.'.join(test.name.split('.')[-1:]) |
| 1879 | return ('Failed doctest test for %s\n' |
| 1880 | ' File "%s", line %s, in %s\n\n%s' |
| 1881 | % (test.name, test.filename, lineno, lname, err) |
| 1882 | ) |
| 1883 | |
| 1884 | def debug(self): |
| 1885 | r"""Run the test case without results and without catching exceptions |
| 1886 | |
| 1887 | The unit test framework includes a debug method on test cases |
| 1888 | and test suites to support post-mortem debugging. The test code |
| 1889 | is run in such a way that errors are not caught. This way a |
| 1890 | caller can catch the errors and initiate post-mortem debugging. |
| 1891 | |
| 1892 | The DocTestCase provides a debug method that raises |
| 1893 | UnexpectedException errors if there is an unexepcted |
| 1894 | exception: |
| 1895 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1896 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1897 | ... {}, 'foo', 'foo.py', 0) |
| 1898 | >>> case = DocTestCase(test) |
| 1899 | >>> try: |
| 1900 | ... case.debug() |
| 1901 | ... except UnexpectedException, failure: |
| 1902 | ... pass |
| 1903 | |
| 1904 | The UnexpectedException contains the test, the example, and |
| 1905 | the original exception: |
| 1906 | |
| 1907 | >>> failure.test is test |
| 1908 | True |
| 1909 | |
| 1910 | >>> failure.example.want |
| 1911 | '42\n' |
| 1912 | |
| 1913 | >>> exc_info = failure.exc_info |
| 1914 | >>> raise exc_info[0], exc_info[1], exc_info[2] |
| 1915 | Traceback (most recent call last): |
| 1916 | ... |
| 1917 | KeyError |
| 1918 | |
| 1919 | If the output doesn't match, then a DocTestFailure is raised: |
| 1920 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1921 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1922 | ... >>> x = 1 |
| 1923 | ... >>> x |
| 1924 | ... 2 |
| 1925 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1926 | >>> case = DocTestCase(test) |
| 1927 | |
| 1928 | >>> try: |
| 1929 | ... case.debug() |
| 1930 | ... except DocTestFailure, failure: |
| 1931 | ... pass |
| 1932 | |
| 1933 | DocTestFailure objects provide access to the test: |
| 1934 | |
| 1935 | >>> failure.test is test |
| 1936 | True |
| 1937 | |
| 1938 | As well as to the example: |
| 1939 | |
| 1940 | >>> failure.example.want |
| 1941 | '2\n' |
| 1942 | |
| 1943 | and the actual output: |
| 1944 | |
| 1945 | >>> failure.got |
| 1946 | '1\n' |
| 1947 | |
| 1948 | """ |
| 1949 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1950 | runner = DebugRunner(optionflags=self._dt_optionflags, |
| 1951 | checker=self._dt_checker, verbose=False) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1952 | runner.run(self._dt_test, out=nooutput) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1953 | |
| 1954 | def id(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1955 | return self._dt_test.name |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1956 | |
| 1957 | def __repr__(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1958 | name = self._dt_test.name.split('.') |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1959 | return "%s (%s)" % (name[-1], '.'.join(name[:-1])) |
| 1960 | |
| 1961 | __str__ = __repr__ |
| 1962 | |
| 1963 | def shortDescription(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1964 | return "Doctest: " + self._dt_test.name |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1965 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1966 | def nooutput(*args): |
| 1967 | pass |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1968 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1969 | def DocTestSuite(module=None, globs=None, extraglobs=None, |
| 1970 | optionflags=0, test_finder=None, |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1971 | setUp=lambda: None, tearDown=lambda: None, |
| 1972 | checker=None): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1973 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1974 | Convert doctest tests for a mudule to a unittest test suite. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1975 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1976 | This converts each documentation string in a module that |
| 1977 | contains doctest tests to a unittest test case. If any of the |
| 1978 | tests in a doc string fail, then the test case fails. An exception |
| 1979 | is raised showing the name of the file containing the test and a |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1980 | (sometimes approximate) line number. |
| 1981 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1982 | The `module` argument provides the module to be tested. The argument |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1983 | can be either a module or a module name. |
| 1984 | |
| 1985 | If no argument is given, the calling module is used. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1986 | """ |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1987 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1988 | if test_finder is None: |
| 1989 | test_finder = DocTestFinder() |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1990 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1991 | module = _normalize_module(module) |
| 1992 | tests = test_finder.find(module, globs=globs, extraglobs=extraglobs) |
| 1993 | if globs is None: |
| 1994 | globs = module.__dict__ |
| 1995 | if not tests: # [XX] why do we want to do this? |
| 1996 | raise ValueError(module, "has no tests") |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1997 | |
| 1998 | tests.sort() |
| 1999 | suite = unittest.TestSuite() |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2000 | for test in tests: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2001 | if len(test.examples) == 0: |
| 2002 | continue |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2003 | if not test.filename: |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2004 | filename = module.__file__ |
| 2005 | if filename.endswith(".pyc"): |
| 2006 | filename = filename[:-1] |
| 2007 | elif filename.endswith(".pyo"): |
| 2008 | filename = filename[:-1] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2009 | test.filename = filename |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 2010 | suite.addTest(DocTestCase(test, optionflags, setUp, tearDown, |
| 2011 | checker)) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2012 | |
| 2013 | return suite |
| 2014 | |
| 2015 | class DocFileCase(DocTestCase): |
| 2016 | |
| 2017 | def id(self): |
| 2018 | return '_'.join(self._dt_test.name.split('.')) |
| 2019 | |
| 2020 | def __repr__(self): |
| 2021 | return self._dt_test.filename |
| 2022 | __str__ = __repr__ |
| 2023 | |
| 2024 | def format_failure(self, err): |
| 2025 | return ('Failed doctest test for %s\n File "%s", line 0\n\n%s' |
| 2026 | % (self._dt_test.name, self._dt_test.filename, err) |
| 2027 | ) |
| 2028 | |
| 2029 | def DocFileTest(path, package=None, globs=None, |
| 2030 | setUp=None, tearDown=None, |
| 2031 | optionflags=0): |
| 2032 | package = _normalize_module(package) |
| 2033 | name = path.split('/')[-1] |
| 2034 | dir = os.path.split(package.__file__)[0] |
| 2035 | path = os.path.join(dir, *(path.split('/'))) |
| 2036 | doc = open(path).read() |
| 2037 | |
| 2038 | if globs is None: |
| 2039 | globs = {} |
| 2040 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 2041 | test = DocTestParser().get_doctest(doc, globs, name, path, 0) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2042 | |
| 2043 | return DocFileCase(test, optionflags, setUp, tearDown) |
| 2044 | |
| 2045 | def DocFileSuite(*paths, **kw): |
| 2046 | """Creates a suite of doctest files. |
| 2047 | |
| 2048 | One or more text file paths are given as strings. These should |
| 2049 | use "/" characters to separate path segments. Paths are relative |
| 2050 | to the directory of the calling module, or relative to the package |
| 2051 | passed as a keyword argument. |
| 2052 | |
| 2053 | A number of options may be provided as keyword arguments: |
| 2054 | |
| 2055 | package |
| 2056 | The name of a Python package. Text-file paths will be |
| 2057 | interpreted relative to the directory containing this package. |
| 2058 | The package may be supplied as a package object or as a dotted |
| 2059 | package name. |
| 2060 | |
| 2061 | setUp |
| 2062 | The name of a set-up function. This is called before running the |
| 2063 | tests in each file. |
| 2064 | |
| 2065 | tearDown |
| 2066 | The name of a tear-down function. This is called after running the |
| 2067 | tests in each file. |
| 2068 | |
| 2069 | globs |
| 2070 | A dictionary containing initial global variables for the tests. |
| 2071 | """ |
| 2072 | suite = unittest.TestSuite() |
| 2073 | |
| 2074 | # We do this here so that _normalize_module is called at the right |
| 2075 | # level. If it were called in DocFileTest, then this function |
| 2076 | # would be the caller and we might guess the package incorrectly. |
| 2077 | kw['package'] = _normalize_module(kw.get('package')) |
| 2078 | |
| 2079 | for path in paths: |
| 2080 | suite.addTest(DocFileTest(path, **kw)) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2081 | |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2082 | return suite |
| 2083 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2084 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 2085 | ## 9. Debugging Support |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2086 | ###################################################################### |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2087 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2088 | def script_from_examples(s): |
| 2089 | r"""Extract script from text with examples. |
| 2090 | |
| 2091 | Converts text with examples to a Python script. Example input is |
| 2092 | converted to regular code. Example output and all other words |
| 2093 | are converted to comments: |
| 2094 | |
| 2095 | >>> text = ''' |
| 2096 | ... Here are examples of simple math. |
| 2097 | ... |
| 2098 | ... Python has super accurate integer addition |
| 2099 | ... |
| 2100 | ... >>> 2 + 2 |
| 2101 | ... 5 |
| 2102 | ... |
| 2103 | ... And very friendly error messages: |
| 2104 | ... |
| 2105 | ... >>> 1/0 |
| 2106 | ... To Infinity |
| 2107 | ... And |
| 2108 | ... Beyond |
| 2109 | ... |
| 2110 | ... You can use logic if you want: |
| 2111 | ... |
| 2112 | ... >>> if 0: |
| 2113 | ... ... blah |
| 2114 | ... ... blah |
| 2115 | ... ... |
| 2116 | ... |
| 2117 | ... Ho hum |
| 2118 | ... ''' |
| 2119 | |
| 2120 | >>> print script_from_examples(text) |
| 2121 | # Here are examples of simple math. |
| 2122 | # |
| 2123 | # Python has super accurate integer addition |
| 2124 | # |
| 2125 | 2 + 2 |
| 2126 | # Expected: |
| 2127 | # 5 |
| 2128 | # |
| 2129 | # And very friendly error messages: |
| 2130 | # |
| 2131 | 1/0 |
| 2132 | # Expected: |
| 2133 | # To Infinity |
| 2134 | # And |
| 2135 | # Beyond |
| 2136 | # |
| 2137 | # You can use logic if you want: |
| 2138 | # |
| 2139 | if 0: |
| 2140 | blah |
| 2141 | blah |
| 2142 | <BLANKLINE> |
| 2143 | # |
| 2144 | # Ho hum |
| 2145 | """ |
| 2146 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 2147 | return DocTestParser().get_program(s) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2148 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2149 | def _want_comment(example): |
| 2150 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2151 | Return a comment containing the expected output for the given example. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2152 | """ |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2153 | # Return the expected output, if any |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2154 | want = example.want |
| 2155 | if want: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2156 | if want[-1] == '\n': |
| 2157 | want = want[:-1] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2158 | want = "\n# ".join(want.split("\n")) |
| 2159 | want = "\n# Expected:\n# %s" % want |
| 2160 | return want |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2161 | |
| 2162 | def testsource(module, name): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2163 | """Extract the test sources from a doctest docstring as a script. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2164 | |
| 2165 | Provide the module (or dotted name of the module) containing the |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2166 | test to be debugged and the name (within the module) of the object |
| 2167 | with the doc string with tests to be debugged. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2168 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2169 | module = _normalize_module(module) |
| 2170 | tests = DocTestFinder().find(module) |
| 2171 | test = [t for t in tests if t.name == name] |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2172 | if not test: |
| 2173 | raise ValueError(name, "not found in tests") |
| 2174 | test = test[0] |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2175 | testsrc = script_from_examples(test.docstring) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2176 | return testsrc |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2177 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2178 | def debug_src(src, pm=False, globs=None): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2179 | """Debug a single doctest docstring, in argument `src`'""" |
| 2180 | testsrc = script_from_examples(src) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2181 | debug_script(testsrc, pm, globs) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2182 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2183 | def debug_script(src, pm=False, globs=None): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2184 | "Debug a test script. `src` is the script, as a string." |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2185 | import pdb |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2186 | |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2187 | srcfilename = tempfile.mktemp("doctestdebug.py") |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2188 | f = open(srcfilename, 'w') |
| 2189 | f.write(src) |
| 2190 | f.close() |
| 2191 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2192 | if globs: |
| 2193 | globs = globs.copy() |
| 2194 | else: |
| 2195 | globs = {} |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2196 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2197 | if pm: |
| 2198 | try: |
| 2199 | execfile(srcfilename, globs, globs) |
| 2200 | except: |
| 2201 | print sys.exc_info()[1] |
| 2202 | pdb.post_mortem(sys.exc_info()[2]) |
| 2203 | else: |
| 2204 | # Note that %r is vital here. '%s' instead can, e.g., cause |
| 2205 | # backslashes to get treated as metacharacters on Windows. |
| 2206 | pdb.run("execfile(%r)" % srcfilename, globs, globs) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2207 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2208 | def debug(module, name, pm=False): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2209 | """Debug a single doctest docstring. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2210 | |
| 2211 | Provide the module (or dotted name of the module) containing the |
| 2212 | test to be debugged and the name (within the module) of the object |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2213 | with the docstring with tests to be debugged. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2214 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2215 | module = _normalize_module(module) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2216 | testsrc = testsource(module, name) |
| 2217 | debug_script(testsrc, pm, module.__dict__) |
| 2218 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2219 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 2220 | ## 10. Example Usage |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2221 | ###################################################################### |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2222 | class _TestClass: |
| 2223 | """ |
| 2224 | A pointless class, for sanity-checking of docstring testing. |
| 2225 | |
| 2226 | Methods: |
| 2227 | square() |
| 2228 | get() |
| 2229 | |
| 2230 | >>> _TestClass(13).get() + _TestClass(-12).get() |
| 2231 | 1 |
| 2232 | >>> hex(_TestClass(13).square().get()) |
| 2233 | '0xa9' |
| 2234 | """ |
| 2235 | |
| 2236 | def __init__(self, val): |
| 2237 | """val -> _TestClass object with associated value val. |
| 2238 | |
| 2239 | >>> t = _TestClass(123) |
| 2240 | >>> print t.get() |
| 2241 | 123 |
| 2242 | """ |
| 2243 | |
| 2244 | self.val = val |
| 2245 | |
| 2246 | def square(self): |
| 2247 | """square() -> square TestClass's associated value |
| 2248 | |
| 2249 | >>> _TestClass(13).square().get() |
| 2250 | 169 |
| 2251 | """ |
| 2252 | |
| 2253 | self.val = self.val ** 2 |
| 2254 | return self |
| 2255 | |
| 2256 | def get(self): |
| 2257 | """get() -> return TestClass's associated value. |
| 2258 | |
| 2259 | >>> x = _TestClass(-42) |
| 2260 | >>> print x.get() |
| 2261 | -42 |
| 2262 | """ |
| 2263 | |
| 2264 | return self.val |
| 2265 | |
| 2266 | __test__ = {"_TestClass": _TestClass, |
| 2267 | "string": r""" |
| 2268 | Example of a string object, searched as-is. |
| 2269 | >>> x = 1; y = 2 |
| 2270 | >>> x + y, x * y |
| 2271 | (3, 2) |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 2272 | """, |
| 2273 | "bool-int equivalence": r""" |
| 2274 | In 2.2, boolean expressions displayed |
| 2275 | 0 or 1. By default, we still accept |
| 2276 | them. This can be disabled by passing |
| 2277 | DONT_ACCEPT_TRUE_FOR_1 to the new |
| 2278 | optionflags argument. |
| 2279 | >>> 4 == 4 |
| 2280 | 1 |
| 2281 | >>> 4 == 4 |
| 2282 | True |
| 2283 | >>> 4 > 4 |
| 2284 | 0 |
| 2285 | >>> 4 > 4 |
| 2286 | False |
| 2287 | """, |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2288 | "blank lines": r""" |
| 2289 | Blank lines can be marked with <BLANKLINE>: |
| 2290 | >>> print 'foo\n\nbar\n' |
| 2291 | foo |
| 2292 | <BLANKLINE> |
| 2293 | bar |
| 2294 | <BLANKLINE> |
| 2295 | """, |
| 2296 | } |
| 2297 | # "ellipsis": r""" |
| 2298 | # If the ellipsis flag is used, then '...' can be used to |
| 2299 | # elide substrings in the desired output: |
| 2300 | # >>> print range(1000) |
| 2301 | # [0, 1, 2, ..., 999] |
| 2302 | # """, |
| 2303 | # "whitespace normalization": r""" |
| 2304 | # If the whitespace normalization flag is used, then |
| 2305 | # differences in whitespace are ignored. |
| 2306 | # >>> print range(30) |
| 2307 | # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 2308 | # 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
| 2309 | # 27, 28, 29] |
| 2310 | # """, |
| 2311 | # } |
| 2312 | |
| 2313 | def test1(): r""" |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 2314 | >>> warnings.filterwarnings("ignore", "class Tester", DeprecationWarning, |
| 2315 | ... "doctest", 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2316 | >>> from doctest import Tester |
| 2317 | >>> t = Tester(globs={'x': 42}, verbose=0) |
| 2318 | >>> t.runstring(r''' |
| 2319 | ... >>> x = x * 2 |
| 2320 | ... >>> print x |
| 2321 | ... 42 |
| 2322 | ... ''', 'XYZ') |
| 2323 | ********************************************************************** |
| 2324 | Failure in example: print x |
| 2325 | from line #2 of XYZ |
| 2326 | Expected: 42 |
| 2327 | Got: 84 |
| 2328 | (1, 2) |
| 2329 | >>> t.runstring(">>> x = x * 2\n>>> print x\n84\n", 'example2') |
| 2330 | (0, 2) |
| 2331 | >>> t.summarize() |
| 2332 | ********************************************************************** |
| 2333 | 1 items had failures: |
| 2334 | 1 of 2 in XYZ |
| 2335 | ***Test Failed*** 1 failures. |
| 2336 | (1, 4) |
| 2337 | >>> t.summarize(verbose=1) |
| 2338 | 1 items passed all tests: |
| 2339 | 2 tests in example2 |
| 2340 | ********************************************************************** |
| 2341 | 1 items had failures: |
| 2342 | 1 of 2 in XYZ |
| 2343 | 4 tests in 2 items. |
| 2344 | 3 passed and 1 failed. |
| 2345 | ***Test Failed*** 1 failures. |
| 2346 | (1, 4) |
| 2347 | """ |
| 2348 | |
| 2349 | def test2(): r""" |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 2350 | >>> warnings.filterwarnings("ignore", "class Tester", |
| 2351 | ... DeprecationWarning, "doctest", 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2352 | >>> t = Tester(globs={}, verbose=1) |
| 2353 | >>> test = r''' |
| 2354 | ... # just an example |
| 2355 | ... >>> x = 1 + 2 |
| 2356 | ... >>> x |
| 2357 | ... 3 |
| 2358 | ... ''' |
| 2359 | >>> t.runstring(test, "Example") |
| 2360 | Running string Example |
| 2361 | Trying: x = 1 + 2 |
| 2362 | Expecting: nothing |
| 2363 | ok |
| 2364 | Trying: x |
| 2365 | Expecting: 3 |
| 2366 | ok |
| 2367 | 0 of 2 examples failed in string Example |
| 2368 | (0, 2) |
| 2369 | """ |
| 2370 | def test3(): r""" |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 2371 | >>> warnings.filterwarnings("ignore", "class Tester", |
| 2372 | ... DeprecationWarning, "doctest", 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2373 | >>> t = Tester(globs={}, verbose=0) |
| 2374 | >>> def _f(): |
| 2375 | ... '''Trivial docstring example. |
| 2376 | ... >>> assert 2 == 2 |
| 2377 | ... ''' |
| 2378 | ... return 32 |
| 2379 | ... |
| 2380 | >>> t.rundoc(_f) # expect 0 failures in 1 example |
| 2381 | (0, 1) |
| 2382 | """ |
| 2383 | def test4(): """ |
| 2384 | >>> import new |
| 2385 | >>> m1 = new.module('_m1') |
| 2386 | >>> m2 = new.module('_m2') |
| 2387 | >>> test_data = \""" |
| 2388 | ... def _f(): |
| 2389 | ... '''>>> assert 1 == 1 |
| 2390 | ... ''' |
| 2391 | ... def g(): |
| 2392 | ... '''>>> assert 2 != 1 |
| 2393 | ... ''' |
| 2394 | ... class H: |
| 2395 | ... '''>>> assert 2 > 1 |
| 2396 | ... ''' |
| 2397 | ... def bar(self): |
| 2398 | ... '''>>> assert 1 < 2 |
| 2399 | ... ''' |
| 2400 | ... \""" |
| 2401 | >>> exec test_data in m1.__dict__ |
| 2402 | >>> exec test_data in m2.__dict__ |
| 2403 | >>> m1.__dict__.update({"f2": m2._f, "g2": m2.g, "h2": m2.H}) |
| 2404 | |
| 2405 | Tests that objects outside m1 are excluded: |
| 2406 | |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 2407 | >>> warnings.filterwarnings("ignore", "class Tester", |
| 2408 | ... DeprecationWarning, "doctest", 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2409 | >>> t = Tester(globs={}, verbose=0) |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 2410 | >>> t.rundict(m1.__dict__, "rundict_test", m1) # f2 and g2 and h2 skipped |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2411 | (0, 4) |
| 2412 | |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 2413 | Once more, not excluding stuff outside m1: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2414 | |
| 2415 | >>> t = Tester(globs={}, verbose=0) |
| 2416 | >>> t.rundict(m1.__dict__, "rundict_test_pvt") # None are skipped. |
| 2417 | (0, 8) |
| 2418 | |
| 2419 | The exclusion of objects from outside the designated module is |
| 2420 | meant to be invoked automagically by testmod. |
| 2421 | |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 2422 | >>> testmod(m1, verbose=False) |
| 2423 | (0, 4) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2424 | """ |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2425 | |
| 2426 | def _test(): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2427 | #import doctest |
| 2428 | #doctest.testmod(doctest, verbose=False, |
| 2429 | # optionflags=ELLIPSIS | NORMALIZE_WHITESPACE | |
| 2430 | # UNIFIED_DIFF) |
| 2431 | #print '~'*70 |
| 2432 | r = unittest.TextTestRunner() |
| 2433 | r.run(DocTestSuite()) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2434 | |
| 2435 | if __name__ == "__main__": |
| 2436 | _test() |