OLD | NEW |
(Empty) | |
| 1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
| 2 # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt |
| 3 |
| 4 """Tests for coverage.py.""" |
| 5 |
| 6 import coverage |
| 7 from coverage import env |
| 8 from coverage.misc import CoverageException |
| 9 |
| 10 from tests.coveragetest import CoverageTest |
| 11 |
| 12 |
| 13 class TestCoverageTest(CoverageTest): |
| 14 """Make sure our complex self.check_coverage method works.""" |
| 15 |
| 16 def test_successful_coverage(self): |
| 17 # The simplest run possible. |
| 18 self.check_coverage("""\ |
| 19 a = 1 |
| 20 b = 2 |
| 21 """, |
| 22 [1,2] |
| 23 ) |
| 24 # You can provide a list of possible statement matches. |
| 25 self.check_coverage("""\ |
| 26 a = 1 |
| 27 b = 2 |
| 28 """, |
| 29 ([100], [1,2], [1723,47]), |
| 30 ) |
| 31 # You can specify missing lines. |
| 32 self.check_coverage("""\ |
| 33 a = 1 |
| 34 if a == 2: |
| 35 a = 3 |
| 36 """, |
| 37 [1,2,3], |
| 38 missing="3", |
| 39 ) |
| 40 # You can specify a list of possible missing lines. |
| 41 self.check_coverage("""\ |
| 42 a = 1 |
| 43 if a == 2: |
| 44 a = 3 |
| 45 """, |
| 46 [1,2,3], |
| 47 missing=("47-49", "3", "100,102") |
| 48 ) |
| 49 |
| 50 def test_failed_coverage(self): |
| 51 # If the lines are wrong, the message shows right and wrong. |
| 52 with self.assertRaisesRegex(AssertionError, r"\[1, 2] != \[1]"): |
| 53 self.check_coverage("""\ |
| 54 a = 1 |
| 55 b = 2 |
| 56 """, |
| 57 [1] |
| 58 ) |
| 59 # If the list of lines possibilities is wrong, the msg shows right. |
| 60 msg = r"None of the lines choices matched \[1, 2]" |
| 61 with self.assertRaisesRegex(AssertionError, msg): |
| 62 self.check_coverage("""\ |
| 63 a = 1 |
| 64 b = 2 |
| 65 """, |
| 66 ([1], [2]) |
| 67 ) |
| 68 # If the missing lines are wrong, the message shows right and wrong. |
| 69 with self.assertRaisesRegex(AssertionError, r"'3' != '37'"): |
| 70 self.check_coverage("""\ |
| 71 a = 1 |
| 72 if a == 2: |
| 73 a = 3 |
| 74 """, |
| 75 [1,2,3], |
| 76 missing="37", |
| 77 ) |
| 78 # If the missing lines possibilities are wrong, the msg shows right. |
| 79 msg = r"None of the missing choices matched '3'" |
| 80 with self.assertRaisesRegex(AssertionError, msg): |
| 81 self.check_coverage("""\ |
| 82 a = 1 |
| 83 if a == 2: |
| 84 a = 3 |
| 85 """, |
| 86 [1,2,3], |
| 87 missing=("37", "4-10"), |
| 88 ) |
| 89 |
| 90 |
| 91 class BasicCoverageTest(CoverageTest): |
| 92 """The simplest tests, for quick smoke testing of fundamental changes.""" |
| 93 |
| 94 def test_simple(self): |
| 95 self.check_coverage("""\ |
| 96 a = 1 |
| 97 b = 2 |
| 98 |
| 99 c = 4 |
| 100 # Nothing here |
| 101 d = 6 |
| 102 """, |
| 103 [1,2,4,6], report="4 0 100%") |
| 104 |
| 105 def test_indentation_wackiness(self): |
| 106 # Partial final lines are OK. |
| 107 self.check_coverage("""\ |
| 108 import sys |
| 109 if not sys.path: |
| 110 a = 1 |
| 111 """, |
| 112 [1,2,3], "3") |
| 113 |
| 114 def test_multiline_initializer(self): |
| 115 self.check_coverage("""\ |
| 116 d = { |
| 117 'foo': 1+2, |
| 118 'bar': (lambda x: x+1)(1), |
| 119 'baz': str(1), |
| 120 } |
| 121 |
| 122 e = { 'foo': 1, 'bar': 2 } |
| 123 """, |
| 124 [1,7], "") |
| 125 |
| 126 def test_list_comprehension(self): |
| 127 self.check_coverage("""\ |
| 128 l = [ |
| 129 2*i for i in range(10) |
| 130 if i > 5 |
| 131 ] |
| 132 assert l == [12, 14, 16, 18] |
| 133 """, |
| 134 [1,5], "") |
| 135 |
| 136 |
| 137 class SimpleStatementTest(CoverageTest): |
| 138 """Testing simple single-line statements.""" |
| 139 |
| 140 def test_expression(self): |
| 141 # Bare expressions as statements are tricky: some implementations |
| 142 # optimize some of them away. All implementations seem to count |
| 143 # the implicit return at the end as executable. |
| 144 self.check_coverage("""\ |
| 145 12 |
| 146 23 |
| 147 """, |
| 148 ([1,2],[2]), "") |
| 149 self.check_coverage("""\ |
| 150 12 |
| 151 23 |
| 152 a = 3 |
| 153 """, |
| 154 ([1,2,3],[3]), "") |
| 155 self.check_coverage("""\ |
| 156 1 + 2 |
| 157 1 + \\ |
| 158 2 |
| 159 """, |
| 160 ([1,2], [2]), "") |
| 161 self.check_coverage("""\ |
| 162 1 + 2 |
| 163 1 + \\ |
| 164 2 |
| 165 a = 4 |
| 166 """, |
| 167 ([1,2,4], [4]), "") |
| 168 |
| 169 def test_assert(self): |
| 170 self.check_coverage("""\ |
| 171 assert (1 + 2) |
| 172 assert (1 + |
| 173 2) |
| 174 assert (1 + 2), 'the universe is broken' |
| 175 assert (1 + |
| 176 2), \\ |
| 177 'something is amiss' |
| 178 """, |
| 179 [1,2,4,5], "") |
| 180 |
| 181 def test_assignment(self): |
| 182 # Simple variable assignment |
| 183 self.check_coverage("""\ |
| 184 a = (1 + 2) |
| 185 b = (1 + |
| 186 2) |
| 187 c = \\ |
| 188 1 |
| 189 """, |
| 190 [1,2,4], "") |
| 191 |
| 192 def test_assign_tuple(self): |
| 193 self.check_coverage("""\ |
| 194 a = 1 |
| 195 a,b,c = 7,8,9 |
| 196 assert a == 7 and b == 8 and c == 9 |
| 197 """, |
| 198 [1,2,3], "") |
| 199 |
| 200 def test_attribute_assignment(self): |
| 201 # Attribute assignment |
| 202 self.check_coverage("""\ |
| 203 class obj: pass |
| 204 o = obj() |
| 205 o.foo = (1 + 2) |
| 206 o.foo = (1 + |
| 207 2) |
| 208 o.foo = \\ |
| 209 1 |
| 210 """, |
| 211 [1,2,3,4,6], "") |
| 212 |
| 213 def test_list_of_attribute_assignment(self): |
| 214 self.check_coverage("""\ |
| 215 class obj: pass |
| 216 o = obj() |
| 217 o.a, o.b = (1 + 2), 3 |
| 218 o.a, o.b = (1 + |
| 219 2), (3 + |
| 220 4) |
| 221 o.a, o.b = \\ |
| 222 1, \\ |
| 223 2 |
| 224 """, |
| 225 [1,2,3,4,7], "") |
| 226 |
| 227 def test_augmented_assignment(self): |
| 228 self.check_coverage("""\ |
| 229 a = 1 |
| 230 a += 1 |
| 231 a += (1 + |
| 232 2) |
| 233 a += \\ |
| 234 1 |
| 235 """, |
| 236 [1,2,3,5], "") |
| 237 |
| 238 def test_triple_string_stuff(self): |
| 239 self.check_coverage("""\ |
| 240 a = ''' |
| 241 a multiline |
| 242 string. |
| 243 ''' |
| 244 b = ''' |
| 245 long expression |
| 246 ''' + ''' |
| 247 on many |
| 248 lines. |
| 249 ''' |
| 250 c = len(''' |
| 251 long expression |
| 252 ''' + |
| 253 ''' |
| 254 on many |
| 255 lines. |
| 256 ''') |
| 257 """, |
| 258 [1,5,11], "") |
| 259 |
| 260 def test_pass(self): |
| 261 # pass is tricky: if it's the only statement in a block, then it is |
| 262 # "executed". But if it is not the only statement, then it is not. |
| 263 self.check_coverage("""\ |
| 264 if 1==1: |
| 265 pass |
| 266 """, |
| 267 [1,2], "") |
| 268 self.check_coverage("""\ |
| 269 def foo(): |
| 270 pass |
| 271 foo() |
| 272 """, |
| 273 [1,2,3], "") |
| 274 self.check_coverage("""\ |
| 275 def foo(): |
| 276 "doc" |
| 277 pass |
| 278 foo() |
| 279 """, |
| 280 ([1,3,4], [1,4]), "") |
| 281 self.check_coverage("""\ |
| 282 class Foo: |
| 283 def foo(self): |
| 284 pass |
| 285 Foo().foo() |
| 286 """, |
| 287 [1,2,3,4], "") |
| 288 self.check_coverage("""\ |
| 289 class Foo: |
| 290 def foo(self): |
| 291 "Huh?" |
| 292 pass |
| 293 Foo().foo() |
| 294 """, |
| 295 ([1,2,4,5], [1,2,5]), "") |
| 296 |
| 297 def test_del(self): |
| 298 self.check_coverage("""\ |
| 299 d = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1 } |
| 300 del d['a'] |
| 301 del d[ |
| 302 'b' |
| 303 ] |
| 304 del d['c'], \\ |
| 305 d['d'], \\ |
| 306 d['e'] |
| 307 assert(len(d.keys()) == 0) |
| 308 """, |
| 309 [1,2,3,6,9], "") |
| 310 |
| 311 def test_print(self): |
| 312 if env.PY3: # Print statement is gone in Py3k. |
| 313 self.skip("No more print statement in Python 3.") |
| 314 |
| 315 self.check_coverage("""\ |
| 316 print "hello, world!" |
| 317 print ("hey: %d" % |
| 318 17) |
| 319 print "goodbye" |
| 320 print "hello, world!", |
| 321 print ("hey: %d" % |
| 322 17), |
| 323 print "goodbye", |
| 324 """, |
| 325 [1,2,4,5,6,8], "") |
| 326 |
| 327 def test_raise(self): |
| 328 self.check_coverage("""\ |
| 329 try: |
| 330 raise Exception( |
| 331 "hello %d" % |
| 332 17) |
| 333 except: |
| 334 pass |
| 335 """, |
| 336 [1,2,5,6], "") |
| 337 |
| 338 def test_return(self): |
| 339 self.check_coverage("""\ |
| 340 def fn(): |
| 341 a = 1 |
| 342 return a |
| 343 |
| 344 x = fn() |
| 345 assert(x == 1) |
| 346 """, |
| 347 [1,2,3,5,6], "") |
| 348 self.check_coverage("""\ |
| 349 def fn(): |
| 350 a = 1 |
| 351 return ( |
| 352 a + |
| 353 1) |
| 354 |
| 355 x = fn() |
| 356 assert(x == 2) |
| 357 """, |
| 358 [1,2,3,7,8], "") |
| 359 self.check_coverage("""\ |
| 360 def fn(): |
| 361 a = 1 |
| 362 return (a, |
| 363 a + 1, |
| 364 a + 2) |
| 365 |
| 366 x,y,z = fn() |
| 367 assert x == 1 and y == 2 and z == 3 |
| 368 """, |
| 369 [1,2,3,7,8], "") |
| 370 |
| 371 def test_yield(self): |
| 372 self.check_coverage("""\ |
| 373 def gen(): |
| 374 yield 1 |
| 375 yield (2+ |
| 376 3+ |
| 377 4) |
| 378 yield 1, \\ |
| 379 2 |
| 380 a,b,c = gen() |
| 381 assert a == 1 and b == 9 and c == (1,2) |
| 382 """, |
| 383 [1,2,3,6,8,9], "") |
| 384 |
| 385 def test_break(self): |
| 386 self.check_coverage("""\ |
| 387 for x in range(10): |
| 388 a = 2 + x |
| 389 break |
| 390 a = 4 |
| 391 assert a == 2 |
| 392 """, |
| 393 [1,2,3,4,5], "4") |
| 394 |
| 395 def test_continue(self): |
| 396 self.check_coverage("""\ |
| 397 for x in range(10): |
| 398 a = 2 + x |
| 399 continue |
| 400 a = 4 |
| 401 assert a == 11 |
| 402 """, |
| 403 [1,2,3,4,5], "4") |
| 404 |
| 405 if 0: # expected failure |
| 406 # Peephole optimization of jumps to jumps can mean that some statements |
| 407 # never hit the line tracer. The behavior is different in different |
| 408 # versions of Python, so don't run this test: |
| 409 def test_strange_unexecuted_continue(self): |
| 410 self.check_coverage("""\ |
| 411 a = b = c = 0 |
| 412 for n in range(100): |
| 413 if n % 2: |
| 414 if n % 4: |
| 415 a += 1 |
| 416 continue # <-- This line may not be hit. |
| 417 else: |
| 418 b += 1 |
| 419 c += 1 |
| 420 assert a == 50 and b == 50 and c == 50 |
| 421 |
| 422 a = b = c = 0 |
| 423 for n in range(100): |
| 424 if n % 2: |
| 425 if n % 3: |
| 426 a += 1 |
| 427 continue # <-- This line is always hit. |
| 428 else: |
| 429 b += 1 |
| 430 c += 1 |
| 431 assert a == 33 and b == 50 and c == 50 |
| 432 """, |
| 433 [1,2,3,4,5,6,8,9,10, 12,13,14,15,16,17,19,20,21], "") |
| 434 |
| 435 def test_import(self): |
| 436 self.check_coverage("""\ |
| 437 import string |
| 438 from sys import path |
| 439 a = 1 |
| 440 """, |
| 441 [1,2,3], "") |
| 442 self.check_coverage("""\ |
| 443 import string |
| 444 if 1 == 2: |
| 445 from sys import path |
| 446 a = 1 |
| 447 """, |
| 448 [1,2,3,4], "3") |
| 449 self.check_coverage("""\ |
| 450 import string, \\ |
| 451 os, \\ |
| 452 re |
| 453 from sys import path, \\ |
| 454 stdout |
| 455 a = 1 |
| 456 """, |
| 457 [1,4,6], "") |
| 458 self.check_coverage("""\ |
| 459 import sys, sys as s |
| 460 assert s.path == sys.path |
| 461 """, |
| 462 [1,2], "") |
| 463 self.check_coverage("""\ |
| 464 import sys, \\ |
| 465 sys as s |
| 466 assert s.path == sys.path |
| 467 """, |
| 468 [1,3], "") |
| 469 self.check_coverage("""\ |
| 470 from sys import path, \\ |
| 471 path as p |
| 472 assert p == path |
| 473 """, |
| 474 [1,3], "") |
| 475 self.check_coverage("""\ |
| 476 from sys import \\ |
| 477 * |
| 478 assert len(path) > 0 |
| 479 """, |
| 480 [1,3], "") |
| 481 |
| 482 def test_global(self): |
| 483 self.check_coverage("""\ |
| 484 g = h = i = 1 |
| 485 def fn(): |
| 486 global g |
| 487 global h, \\ |
| 488 i |
| 489 g = h = i = 2 |
| 490 fn() |
| 491 assert g == 2 and h == 2 and i == 2 |
| 492 """, |
| 493 [1,2,6,7,8], "") |
| 494 self.check_coverage("""\ |
| 495 g = h = i = 1 |
| 496 def fn(): |
| 497 global g; g = 2 |
| 498 fn() |
| 499 assert g == 2 and h == 1 and i == 1 |
| 500 """, |
| 501 [1,2,3,4,5], "") |
| 502 |
| 503 def test_exec(self): |
| 504 self.check_coverage("""\ |
| 505 a = b = c = 1 |
| 506 exec("a = 2") |
| 507 exec("b = " + |
| 508 "c = " + |
| 509 "2") |
| 510 assert a == 2 and b == 2 and c == 2 |
| 511 """, |
| 512 [1,2,3,6], "") |
| 513 self.check_coverage("""\ |
| 514 vars = {'a': 1, 'b': 1, 'c': 1} |
| 515 exec("a = 2", vars) |
| 516 exec("b = " + |
| 517 "c = " + |
| 518 "2", vars) |
| 519 assert vars['a'] == 2 and vars['b'] == 2 and vars['c'] == 2 |
| 520 """, |
| 521 [1,2,3,6], "") |
| 522 self.check_coverage("""\ |
| 523 globs = {} |
| 524 locs = {'a': 1, 'b': 1, 'c': 1} |
| 525 exec("a = 2", globs, locs) |
| 526 exec("b = " + |
| 527 "c = " + |
| 528 "2", globs, locs) |
| 529 assert locs['a'] == 2 and locs['b'] == 2 and locs['c'] == 2 |
| 530 """, |
| 531 [1,2,3,4,7], "") |
| 532 |
| 533 def test_extra_doc_string(self): |
| 534 self.check_coverage("""\ |
| 535 a = 1 |
| 536 "An extra docstring, should be a comment." |
| 537 b = 3 |
| 538 assert (a,b) == (1,3) |
| 539 """, |
| 540 [1,3,4], "") |
| 541 self.check_coverage("""\ |
| 542 a = 1 |
| 543 "An extra docstring, should be a comment." |
| 544 b = 3 |
| 545 123 # A number for some reason: ignored |
| 546 1+1 # An expression: executed. |
| 547 c = 6 |
| 548 assert (a,b,c) == (1,3,6) |
| 549 """, |
| 550 ([1,3,6,7], [1,3,5,6,7], [1,3,4,5,6,7]), "") |
| 551 |
| 552 |
| 553 class CompoundStatementTest(CoverageTest): |
| 554 """Testing coverage of multi-line compound statements.""" |
| 555 |
| 556 def test_statement_list(self): |
| 557 self.check_coverage("""\ |
| 558 a = 1; |
| 559 b = 2; c = 3 |
| 560 d = 4; e = 5; |
| 561 |
| 562 assert (a,b,c,d,e) == (1,2,3,4,5) |
| 563 """, |
| 564 [1,2,3,5], "") |
| 565 |
| 566 def test_if(self): |
| 567 self.check_coverage("""\ |
| 568 a = 1 |
| 569 if a == 1: |
| 570 x = 3 |
| 571 assert x == 3 |
| 572 if (a == |
| 573 1): |
| 574 x = 7 |
| 575 assert x == 7 |
| 576 """, |
| 577 [1,2,3,4,5,7,8], "") |
| 578 self.check_coverage("""\ |
| 579 a = 1 |
| 580 if a == 1: |
| 581 x = 3 |
| 582 else: |
| 583 y = 5 |
| 584 assert x == 3 |
| 585 """, |
| 586 [1,2,3,5,6], "5") |
| 587 self.check_coverage("""\ |
| 588 a = 1 |
| 589 if a != 1: |
| 590 x = 3 |
| 591 else: |
| 592 y = 5 |
| 593 assert y == 5 |
| 594 """, |
| 595 [1,2,3,5,6], "3") |
| 596 self.check_coverage("""\ |
| 597 a = 1; b = 2 |
| 598 if a == 1: |
| 599 if b == 2: |
| 600 x = 4 |
| 601 else: |
| 602 y = 6 |
| 603 else: |
| 604 z = 8 |
| 605 assert x == 4 |
| 606 """, |
| 607 [1,2,3,4,6,8,9], "6-8") |
| 608 |
| 609 def test_elif(self): |
| 610 self.check_coverage("""\ |
| 611 a = 1; b = 2; c = 3; |
| 612 if a == 1: |
| 613 x = 3 |
| 614 elif b == 2: |
| 615 y = 5 |
| 616 else: |
| 617 z = 7 |
| 618 assert x == 3 |
| 619 """, |
| 620 [1,2,3,4,5,7,8], "4-7", report="7 3 57% 4-7") |
| 621 self.check_coverage("""\ |
| 622 a = 1; b = 2; c = 3; |
| 623 if a != 1: |
| 624 x = 3 |
| 625 elif b == 2: |
| 626 y = 5 |
| 627 else: |
| 628 z = 7 |
| 629 assert y == 5 |
| 630 """, |
| 631 [1,2,3,4,5,7,8], "3, 7", report="7 2 71% 3, 7") |
| 632 self.check_coverage("""\ |
| 633 a = 1; b = 2; c = 3; |
| 634 if a != 1: |
| 635 x = 3 |
| 636 elif b != 2: |
| 637 y = 5 |
| 638 else: |
| 639 z = 7 |
| 640 assert z == 7 |
| 641 """, |
| 642 [1,2,3,4,5,7,8], "3, 5", report="7 2 71% 3, 5") |
| 643 |
| 644 def test_elif_no_else(self): |
| 645 self.check_coverage("""\ |
| 646 a = 1; b = 2; c = 3; |
| 647 if a == 1: |
| 648 x = 3 |
| 649 elif b == 2: |
| 650 y = 5 |
| 651 assert x == 3 |
| 652 """, |
| 653 [1,2,3,4,5,6], "4-5", report="6 2 67% 4-5") |
| 654 self.check_coverage("""\ |
| 655 a = 1; b = 2; c = 3; |
| 656 if a != 1: |
| 657 x = 3 |
| 658 elif b == 2: |
| 659 y = 5 |
| 660 assert y == 5 |
| 661 """, |
| 662 [1,2,3,4,5,6], "3", report="6 1 83% 3") |
| 663 |
| 664 def test_elif_bizarre(self): |
| 665 self.check_coverage("""\ |
| 666 def f(self): |
| 667 if self==1: |
| 668 x = 3 |
| 669 elif self.m('fred'): |
| 670 x = 5 |
| 671 elif (g==1) and (b==2): |
| 672 x = 7 |
| 673 elif self.m('fred')==True: |
| 674 x = 9 |
| 675 elif ((g==1) and (b==2))==True: |
| 676 x = 11 |
| 677 else: |
| 678 x = 13 |
| 679 """, |
| 680 [1,2,3,4,5,6,7,8,9,10,11,13], "2-13") |
| 681 |
| 682 def test_split_if(self): |
| 683 self.check_coverage("""\ |
| 684 a = 1; b = 2; c = 3; |
| 685 if \\ |
| 686 a == 1: |
| 687 x = 3 |
| 688 elif \\ |
| 689 b == 2: |
| 690 y = 5 |
| 691 else: |
| 692 z = 7 |
| 693 assert x == 3 |
| 694 """, |
| 695 [1,2,4,5,7,9,10], "5-9") |
| 696 self.check_coverage("""\ |
| 697 a = 1; b = 2; c = 3; |
| 698 if \\ |
| 699 a != 1: |
| 700 x = 3 |
| 701 elif \\ |
| 702 b == 2: |
| 703 y = 5 |
| 704 else: |
| 705 z = 7 |
| 706 assert y == 5 |
| 707 """, |
| 708 [1,2,4,5,7,9,10], "4, 9") |
| 709 self.check_coverage("""\ |
| 710 a = 1; b = 2; c = 3; |
| 711 if \\ |
| 712 a != 1: |
| 713 x = 3 |
| 714 elif \\ |
| 715 b != 2: |
| 716 y = 5 |
| 717 else: |
| 718 z = 7 |
| 719 assert z == 7 |
| 720 """, |
| 721 [1,2,4,5,7,9,10], "4, 7") |
| 722 |
| 723 def test_pathological_split_if(self): |
| 724 self.check_coverage("""\ |
| 725 a = 1; b = 2; c = 3; |
| 726 if ( |
| 727 a == 1 |
| 728 ): |
| 729 x = 3 |
| 730 elif ( |
| 731 b == 2 |
| 732 ): |
| 733 y = 5 |
| 734 else: |
| 735 z = 7 |
| 736 assert x == 3 |
| 737 """, |
| 738 [1,2,5,6,9,11,12], "6-11") |
| 739 self.check_coverage("""\ |
| 740 a = 1; b = 2; c = 3; |
| 741 if ( |
| 742 a != 1 |
| 743 ): |
| 744 x = 3 |
| 745 elif ( |
| 746 b == 2 |
| 747 ): |
| 748 y = 5 |
| 749 else: |
| 750 z = 7 |
| 751 assert y == 5 |
| 752 """, |
| 753 [1,2,5,6,9,11,12], "5, 11") |
| 754 self.check_coverage("""\ |
| 755 a = 1; b = 2; c = 3; |
| 756 if ( |
| 757 a != 1 |
| 758 ): |
| 759 x = 3 |
| 760 elif ( |
| 761 b != 2 |
| 762 ): |
| 763 y = 5 |
| 764 else: |
| 765 z = 7 |
| 766 assert z == 7 |
| 767 """, |
| 768 [1,2,5,6,9,11,12], "5, 9") |
| 769 |
| 770 def test_absurd_split_if(self): |
| 771 self.check_coverage("""\ |
| 772 a = 1; b = 2; c = 3; |
| 773 if a == 1 \\ |
| 774 : |
| 775 x = 3 |
| 776 elif b == 2 \\ |
| 777 : |
| 778 y = 5 |
| 779 else: |
| 780 z = 7 |
| 781 assert x == 3 |
| 782 """, |
| 783 [1,2,4,5,7,9,10], "5-9") |
| 784 self.check_coverage("""\ |
| 785 a = 1; b = 2; c = 3; |
| 786 if a != 1 \\ |
| 787 : |
| 788 x = 3 |
| 789 elif b == 2 \\ |
| 790 : |
| 791 y = 5 |
| 792 else: |
| 793 z = 7 |
| 794 assert y == 5 |
| 795 """, |
| 796 [1,2,4,5,7,9,10], "4, 9") |
| 797 self.check_coverage("""\ |
| 798 a = 1; b = 2; c = 3; |
| 799 if a != 1 \\ |
| 800 : |
| 801 x = 3 |
| 802 elif b != 2 \\ |
| 803 : |
| 804 y = 5 |
| 805 else: |
| 806 z = 7 |
| 807 assert z == 7 |
| 808 """, |
| 809 [1,2,4,5,7,9,10], "4, 7") |
| 810 |
| 811 def test_constant_if(self): |
| 812 self.check_coverage("""\ |
| 813 if 1: |
| 814 a = 2 |
| 815 assert a == 2 |
| 816 """, |
| 817 [2,3], "") |
| 818 |
| 819 def test_while(self): |
| 820 self.check_coverage("""\ |
| 821 a = 3; b = 0 |
| 822 while a: |
| 823 b += 1 |
| 824 a -= 1 |
| 825 assert a == 0 and b == 3 |
| 826 """, |
| 827 [1,2,3,4,5], "") |
| 828 self.check_coverage("""\ |
| 829 a = 3; b = 0 |
| 830 while a: |
| 831 b += 1 |
| 832 break |
| 833 b = 99 |
| 834 assert a == 3 and b == 1 |
| 835 """, |
| 836 [1,2,3,4,5,6], "5") |
| 837 |
| 838 def test_while_else(self): |
| 839 # Take the else branch. |
| 840 self.check_coverage("""\ |
| 841 a = 3; b = 0 |
| 842 while a: |
| 843 b += 1 |
| 844 a -= 1 |
| 845 else: |
| 846 b = 99 |
| 847 assert a == 0 and b == 99 |
| 848 """, |
| 849 [1,2,3,4,6,7], "") |
| 850 # Don't take the else branch. |
| 851 self.check_coverage("""\ |
| 852 a = 3; b = 0 |
| 853 while a: |
| 854 b += 1 |
| 855 a -= 1 |
| 856 break |
| 857 b = 123 |
| 858 else: |
| 859 b = 99 |
| 860 assert a == 2 and b == 1 |
| 861 """, |
| 862 [1,2,3,4,5,6,8,9], "6-8") |
| 863 |
| 864 def test_split_while(self): |
| 865 self.check_coverage("""\ |
| 866 a = 3; b = 0 |
| 867 while \\ |
| 868 a: |
| 869 b += 1 |
| 870 a -= 1 |
| 871 assert a == 0 and b == 3 |
| 872 """, |
| 873 [1,2,4,5,6], "") |
| 874 self.check_coverage("""\ |
| 875 a = 3; b = 0 |
| 876 while ( |
| 877 a |
| 878 ): |
| 879 b += 1 |
| 880 a -= 1 |
| 881 assert a == 0 and b == 3 |
| 882 """, |
| 883 [1,2,5,6,7], "") |
| 884 |
| 885 def test_for(self): |
| 886 self.check_coverage("""\ |
| 887 a = 0 |
| 888 for i in [1,2,3,4,5]: |
| 889 a += i |
| 890 assert a == 15 |
| 891 """, |
| 892 [1,2,3,4], "") |
| 893 self.check_coverage("""\ |
| 894 a = 0 |
| 895 for i in [1, |
| 896 2,3,4, |
| 897 5]: |
| 898 a += i |
| 899 assert a == 15 |
| 900 """, |
| 901 [1,2,5,6], "") |
| 902 self.check_coverage("""\ |
| 903 a = 0 |
| 904 for i in [1,2,3,4,5]: |
| 905 a += i |
| 906 break |
| 907 a = 99 |
| 908 assert a == 1 |
| 909 """, |
| 910 [1,2,3,4,5,6], "5") |
| 911 |
| 912 def test_for_else(self): |
| 913 self.check_coverage("""\ |
| 914 a = 0 |
| 915 for i in range(5): |
| 916 a += i+1 |
| 917 else: |
| 918 a = 99 |
| 919 assert a == 99 |
| 920 """, |
| 921 [1,2,3,5,6], "") |
| 922 self.check_coverage("""\ |
| 923 a = 0 |
| 924 for i in range(5): |
| 925 a += i+1 |
| 926 break |
| 927 a = 99 |
| 928 else: |
| 929 a = 123 |
| 930 assert a == 1 |
| 931 """, |
| 932 [1,2,3,4,5,7,8], "5-7") |
| 933 |
| 934 def test_split_for(self): |
| 935 self.check_coverage("""\ |
| 936 a = 0 |
| 937 for \\ |
| 938 i in [1,2,3,4,5]: |
| 939 a += i |
| 940 assert a == 15 |
| 941 """, |
| 942 [1,2,4,5], "") |
| 943 self.check_coverage("""\ |
| 944 a = 0 |
| 945 for \\ |
| 946 i in [1, |
| 947 2,3,4, |
| 948 5]: |
| 949 a += i |
| 950 assert a == 15 |
| 951 """, |
| 952 [1,2,6,7], "") |
| 953 |
| 954 def test_try_except(self): |
| 955 self.check_coverage("""\ |
| 956 a = 0 |
| 957 try: |
| 958 a = 1 |
| 959 except: |
| 960 a = 99 |
| 961 assert a == 1 |
| 962 """, |
| 963 [1,2,3,4,5,6], "4-5") |
| 964 self.check_coverage("""\ |
| 965 a = 0 |
| 966 try: |
| 967 a = 1 |
| 968 raise Exception("foo") |
| 969 except: |
| 970 a = 99 |
| 971 assert a == 99 |
| 972 """, |
| 973 [1,2,3,4,5,6,7], "") |
| 974 self.check_coverage("""\ |
| 975 a = 0 |
| 976 try: |
| 977 a = 1 |
| 978 raise Exception("foo") |
| 979 except ImportError: |
| 980 a = 99 |
| 981 except: |
| 982 a = 123 |
| 983 assert a == 123 |
| 984 """, |
| 985 [1,2,3,4,5,6,7,8,9], "6") |
| 986 self.check_coverage("""\ |
| 987 a = 0 |
| 988 try: |
| 989 a = 1 |
| 990 raise IOError("foo") |
| 991 except ImportError: |
| 992 a = 99 |
| 993 except IOError: |
| 994 a = 17 |
| 995 except: |
| 996 a = 123 |
| 997 assert a == 17 |
| 998 """, |
| 999 [1,2,3,4,5,6,7,8,9,10,11], "6, 9-10") |
| 1000 self.check_coverage("""\ |
| 1001 a = 0 |
| 1002 try: |
| 1003 a = 1 |
| 1004 except: |
| 1005 a = 99 |
| 1006 else: |
| 1007 a = 123 |
| 1008 assert a == 123 |
| 1009 """, |
| 1010 [1,2,3,4,5,7,8], "4-5") |
| 1011 self.check_coverage("""\ |
| 1012 a = 0 |
| 1013 try: |
| 1014 a = 1 |
| 1015 raise Exception("foo") |
| 1016 except: |
| 1017 a = 99 |
| 1018 else: |
| 1019 a = 123 |
| 1020 assert a == 99 |
| 1021 """, |
| 1022 [1,2,3,4,5,6,8,9], "8") |
| 1023 |
| 1024 def test_try_finally(self): |
| 1025 self.check_coverage("""\ |
| 1026 a = 0 |
| 1027 try: |
| 1028 a = 1 |
| 1029 finally: |
| 1030 a = 99 |
| 1031 assert a == 99 |
| 1032 """, |
| 1033 [1,2,3,5,6], "") |
| 1034 self.check_coverage("""\ |
| 1035 a = 0; b = 0 |
| 1036 try: |
| 1037 a = 1 |
| 1038 try: |
| 1039 raise Exception("foo") |
| 1040 finally: |
| 1041 b = 123 |
| 1042 except: |
| 1043 a = 99 |
| 1044 assert a == 99 and b == 123 |
| 1045 """, |
| 1046 [1,2,3,4,5,7,8,9,10], "") |
| 1047 |
| 1048 def test_function_def(self): |
| 1049 self.check_coverage("""\ |
| 1050 a = 99 |
| 1051 def foo(): |
| 1052 ''' docstring |
| 1053 ''' |
| 1054 return 1 |
| 1055 |
| 1056 a = foo() |
| 1057 assert a == 1 |
| 1058 """, |
| 1059 [1,2,5,7,8], "") |
| 1060 self.check_coverage("""\ |
| 1061 def foo( |
| 1062 a, |
| 1063 b |
| 1064 ): |
| 1065 ''' docstring |
| 1066 ''' |
| 1067 return a+b |
| 1068 |
| 1069 x = foo(17, 23) |
| 1070 assert x == 40 |
| 1071 """, |
| 1072 [1,7,9,10], "") |
| 1073 self.check_coverage("""\ |
| 1074 def foo( |
| 1075 a = (lambda x: x*2)(10), |
| 1076 b = ( |
| 1077 lambda x: |
| 1078 x+1 |
| 1079 )(1) |
| 1080 ): |
| 1081 ''' docstring |
| 1082 ''' |
| 1083 return a+b |
| 1084 |
| 1085 x = foo() |
| 1086 assert x == 22 |
| 1087 """, |
| 1088 [1,10,12,13], "") |
| 1089 |
| 1090 def test_class_def(self): |
| 1091 self.check_coverage("""\ |
| 1092 # A comment. |
| 1093 class theClass: |
| 1094 ''' the docstring. |
| 1095 Don't be fooled. |
| 1096 ''' |
| 1097 def __init__(self): |
| 1098 ''' Another docstring. ''' |
| 1099 self.a = 1 |
| 1100 |
| 1101 def foo(self): |
| 1102 return self.a |
| 1103 |
| 1104 x = theClass().foo() |
| 1105 assert x == 1 |
| 1106 """, |
| 1107 [2,6,8,10,11,13,14], "") |
| 1108 |
| 1109 |
| 1110 class ExcludeTest(CoverageTest): |
| 1111 """Tests of the exclusion feature to mark lines as not covered.""" |
| 1112 |
| 1113 def test_default(self): |
| 1114 # A number of forms of pragma comment are accepted. |
| 1115 self.check_coverage("""\ |
| 1116 a = 1 |
| 1117 b = 2 # pragma: no cover |
| 1118 c = 3 |
| 1119 d = 4 #pragma NOCOVER |
| 1120 e = 5 |
| 1121 f = 6#\tpragma:\tno cover |
| 1122 g = 7 |
| 1123 """, |
| 1124 [1,3,5,7] |
| 1125 ) |
| 1126 |
| 1127 def test_simple(self): |
| 1128 self.check_coverage("""\ |
| 1129 a = 1; b = 2 |
| 1130 |
| 1131 if 0: |
| 1132 a = 4 # -cc |
| 1133 """, |
| 1134 [1,3], "", excludes=['-cc']) |
| 1135 |
| 1136 def test_two_excludes(self): |
| 1137 self.check_coverage("""\ |
| 1138 a = 1; b = 2 |
| 1139 |
| 1140 if a == 99: |
| 1141 a = 4 # -cc |
| 1142 b = 5 |
| 1143 c = 6 # -xx |
| 1144 assert a == 1 and b == 2 |
| 1145 """, |
| 1146 [1,3,5,7], "5", excludes=['-cc', '-xx']) |
| 1147 |
| 1148 def test_excluding_if_suite(self): |
| 1149 self.check_coverage("""\ |
| 1150 a = 1; b = 2 |
| 1151 |
| 1152 if 0: |
| 1153 a = 4 |
| 1154 b = 5 |
| 1155 c = 6 |
| 1156 assert a == 1 and b == 2 |
| 1157 """, |
| 1158 [1,7], "", excludes=['if 0:']) |
| 1159 |
| 1160 def test_excluding_if_but_not_else_suite(self): |
| 1161 self.check_coverage("""\ |
| 1162 a = 1; b = 2 |
| 1163 |
| 1164 if 0: |
| 1165 a = 4 |
| 1166 b = 5 |
| 1167 c = 6 |
| 1168 else: |
| 1169 a = 8 |
| 1170 b = 9 |
| 1171 assert a == 8 and b == 9 |
| 1172 """, |
| 1173 [1,8,9,10], "", excludes=['if 0:']) |
| 1174 |
| 1175 def test_excluding_else_suite(self): |
| 1176 self.check_coverage("""\ |
| 1177 a = 1; b = 2 |
| 1178 |
| 1179 if 1==1: |
| 1180 a = 4 |
| 1181 b = 5 |
| 1182 c = 6 |
| 1183 else: #pragma: NO COVER |
| 1184 a = 8 |
| 1185 b = 9 |
| 1186 assert a == 4 and b == 5 and c == 6 |
| 1187 """, |
| 1188 [1,3,4,5,6,10], "", excludes=['#pragma: NO COVER']) |
| 1189 self.check_coverage("""\ |
| 1190 a = 1; b = 2 |
| 1191 |
| 1192 if 1==1: |
| 1193 a = 4 |
| 1194 b = 5 |
| 1195 c = 6 |
| 1196 |
| 1197 # Lots of comments to confuse the else handler. |
| 1198 # more. |
| 1199 |
| 1200 else: #pragma: NO COVER |
| 1201 |
| 1202 # Comments here too. |
| 1203 |
| 1204 a = 8 |
| 1205 b = 9 |
| 1206 assert a == 4 and b == 5 and c == 6 |
| 1207 """, |
| 1208 [1,3,4,5,6,17], "", excludes=['#pragma: NO COVER']) |
| 1209 |
| 1210 def test_excluding_elif_suites(self): |
| 1211 self.check_coverage("""\ |
| 1212 a = 1; b = 2 |
| 1213 |
| 1214 if 1==1: |
| 1215 a = 4 |
| 1216 b = 5 |
| 1217 c = 6 |
| 1218 elif 1==0: #pragma: NO COVER |
| 1219 a = 8 |
| 1220 b = 9 |
| 1221 else: |
| 1222 a = 11 |
| 1223 b = 12 |
| 1224 assert a == 4 and b == 5 and c == 6 |
| 1225 """, |
| 1226 [1,3,4,5,6,11,12,13], "11-12", excludes=['#pragma: NO COVER']) |
| 1227 |
| 1228 def test_excluding_oneline_if(self): |
| 1229 self.check_coverage("""\ |
| 1230 def foo(): |
| 1231 a = 2 |
| 1232 if 0: x = 3 # no cover |
| 1233 b = 4 |
| 1234 |
| 1235 foo() |
| 1236 """, |
| 1237 [1,2,4,6], "", excludes=["no cover"]) |
| 1238 |
| 1239 def test_excluding_a_colon_not_a_suite(self): |
| 1240 self.check_coverage("""\ |
| 1241 def foo(): |
| 1242 l = list(range(10)) |
| 1243 a = l[:3] # no cover |
| 1244 b = 4 |
| 1245 |
| 1246 foo() |
| 1247 """, |
| 1248 [1,2,4,6], "", excludes=["no cover"]) |
| 1249 |
| 1250 def test_excluding_for_suite(self): |
| 1251 self.check_coverage("""\ |
| 1252 a = 0 |
| 1253 for i in [1,2,3,4,5]: #pragma: NO COVER |
| 1254 a += i |
| 1255 assert a == 15 |
| 1256 """, |
| 1257 [1,4], "", excludes=['#pragma: NO COVER']) |
| 1258 self.check_coverage("""\ |
| 1259 a = 0 |
| 1260 for i in [1, |
| 1261 2,3,4, |
| 1262 5]: #pragma: NO COVER |
| 1263 a += i |
| 1264 assert a == 15 |
| 1265 """, |
| 1266 [1,6], "", excludes=['#pragma: NO COVER']) |
| 1267 self.check_coverage("""\ |
| 1268 a = 0 |
| 1269 for i in [1,2,3,4,5 |
| 1270 ]: #pragma: NO COVER |
| 1271 a += i |
| 1272 break |
| 1273 a = 99 |
| 1274 assert a == 1 |
| 1275 """, |
| 1276 [1,7], "", excludes=['#pragma: NO COVER']) |
| 1277 |
| 1278 def test_excluding_for_else(self): |
| 1279 self.check_coverage("""\ |
| 1280 a = 0 |
| 1281 for i in range(5): |
| 1282 a += i+1 |
| 1283 break |
| 1284 a = 99 |
| 1285 else: #pragma: NO COVER |
| 1286 a = 123 |
| 1287 assert a == 1 |
| 1288 """, |
| 1289 [1,2,3,4,5,8], "5", excludes=['#pragma: NO COVER']) |
| 1290 |
| 1291 def test_excluding_while(self): |
| 1292 self.check_coverage("""\ |
| 1293 a = 3; b = 0 |
| 1294 while a*b: #pragma: NO COVER |
| 1295 b += 1 |
| 1296 break |
| 1297 b = 99 |
| 1298 assert a == 3 and b == 0 |
| 1299 """, |
| 1300 [1,6], "", excludes=['#pragma: NO COVER']) |
| 1301 self.check_coverage("""\ |
| 1302 a = 3; b = 0 |
| 1303 while ( |
| 1304 a*b |
| 1305 ): #pragma: NO COVER |
| 1306 b += 1 |
| 1307 break |
| 1308 b = 99 |
| 1309 assert a == 3 and b == 0 |
| 1310 """, |
| 1311 [1,8], "", excludes=['#pragma: NO COVER']) |
| 1312 |
| 1313 def test_excluding_while_else(self): |
| 1314 self.check_coverage("""\ |
| 1315 a = 3; b = 0 |
| 1316 while a: |
| 1317 b += 1 |
| 1318 break |
| 1319 b = 99 |
| 1320 else: #pragma: NO COVER |
| 1321 b = 123 |
| 1322 assert a == 3 and b == 1 |
| 1323 """, |
| 1324 [1,2,3,4,5,8], "5", excludes=['#pragma: NO COVER']) |
| 1325 |
| 1326 def test_excluding_try_except(self): |
| 1327 self.check_coverage("""\ |
| 1328 a = 0 |
| 1329 try: |
| 1330 a = 1 |
| 1331 except: #pragma: NO COVER |
| 1332 a = 99 |
| 1333 assert a == 1 |
| 1334 """, |
| 1335 [1,2,3,6], "", excludes=['#pragma: NO COVER']) |
| 1336 self.check_coverage("""\ |
| 1337 a = 0 |
| 1338 try: |
| 1339 a = 1 |
| 1340 raise Exception("foo") |
| 1341 except: |
| 1342 a = 99 |
| 1343 assert a == 99 |
| 1344 """, |
| 1345 [1,2,3,4,5,6,7], "", excludes=['#pragma: NO COVER']) |
| 1346 self.check_coverage("""\ |
| 1347 a = 0 |
| 1348 try: |
| 1349 a = 1 |
| 1350 raise Exception("foo") |
| 1351 except ImportError: #pragma: NO COVER |
| 1352 a = 99 |
| 1353 except: |
| 1354 a = 123 |
| 1355 assert a == 123 |
| 1356 """, |
| 1357 [1,2,3,4,7,8,9], "", excludes=['#pragma: NO COVER']) |
| 1358 self.check_coverage("""\ |
| 1359 a = 0 |
| 1360 try: |
| 1361 a = 1 |
| 1362 except: #pragma: NO COVER |
| 1363 a = 99 |
| 1364 else: |
| 1365 a = 123 |
| 1366 assert a == 123 |
| 1367 """, |
| 1368 [1,2,3,7,8], "", excludes=['#pragma: NO COVER']) |
| 1369 self.check_coverage("""\ |
| 1370 a = 0 |
| 1371 try: |
| 1372 a = 1 |
| 1373 raise Exception("foo") |
| 1374 except: |
| 1375 a = 99 |
| 1376 else: #pragma: NO COVER |
| 1377 a = 123 |
| 1378 assert a == 99 |
| 1379 """, |
| 1380 [1,2,3,4,5,6,9], "", excludes=['#pragma: NO COVER']) |
| 1381 |
| 1382 def test_excluding_try_except_pass(self): |
| 1383 self.check_coverage("""\ |
| 1384 a = 0 |
| 1385 try: |
| 1386 a = 1 |
| 1387 except: #pragma: NO COVER |
| 1388 x = 2 |
| 1389 assert a == 1 |
| 1390 """, |
| 1391 [1,2,3,6], "", excludes=['#pragma: NO COVER']) |
| 1392 self.check_coverage("""\ |
| 1393 a = 0 |
| 1394 try: |
| 1395 a = 1 |
| 1396 raise Exception("foo") |
| 1397 except ImportError: #pragma: NO COVER |
| 1398 x = 2 |
| 1399 except: |
| 1400 a = 123 |
| 1401 assert a == 123 |
| 1402 """, |
| 1403 [1,2,3,4,7,8,9], "", excludes=['#pragma: NO COVER']) |
| 1404 self.check_coverage("""\ |
| 1405 a = 0 |
| 1406 try: |
| 1407 a = 1 |
| 1408 except: #pragma: NO COVER |
| 1409 x = 2 |
| 1410 else: |
| 1411 a = 123 |
| 1412 assert a == 123 |
| 1413 """, |
| 1414 [1,2,3,7,8], "", excludes=['#pragma: NO COVER']) |
| 1415 self.check_coverage("""\ |
| 1416 a = 0 |
| 1417 try: |
| 1418 a = 1 |
| 1419 raise Exception("foo") |
| 1420 except: |
| 1421 a = 99 |
| 1422 else: #pragma: NO COVER |
| 1423 x = 2 |
| 1424 assert a == 99 |
| 1425 """, |
| 1426 [1,2,3,4,5,6,9], "", excludes=['#pragma: NO COVER']) |
| 1427 |
| 1428 def test_excluding_if_pass(self): |
| 1429 # From a comment on the coverage.py page by Michael McNeil Forbes: |
| 1430 self.check_coverage("""\ |
| 1431 def f(): |
| 1432 if False: # pragma: no cover |
| 1433 pass # This line still reported as missing |
| 1434 if False: # pragma: no cover |
| 1435 x = 1 # Now it is skipped. |
| 1436 |
| 1437 f() |
| 1438 """, |
| 1439 [1,7], "", excludes=["no cover"]) |
| 1440 |
| 1441 def test_excluding_function(self): |
| 1442 self.check_coverage("""\ |
| 1443 def fn(foo): #pragma: NO COVER |
| 1444 a = 1 |
| 1445 b = 2 |
| 1446 c = 3 |
| 1447 |
| 1448 x = 1 |
| 1449 assert x == 1 |
| 1450 """, |
| 1451 [6,7], "", excludes=['#pragma: NO COVER']) |
| 1452 |
| 1453 def test_excluding_method(self): |
| 1454 self.check_coverage("""\ |
| 1455 class Fooey: |
| 1456 def __init__(self): |
| 1457 self.a = 1 |
| 1458 |
| 1459 def foo(self): #pragma: NO COVER |
| 1460 return self.a |
| 1461 |
| 1462 x = Fooey() |
| 1463 assert x.a == 1 |
| 1464 """, |
| 1465 [1,2,3,8,9], "", excludes=['#pragma: NO COVER']) |
| 1466 |
| 1467 def test_excluding_class(self): |
| 1468 self.check_coverage("""\ |
| 1469 class Fooey: #pragma: NO COVER |
| 1470 def __init__(self): |
| 1471 self.a = 1 |
| 1472 |
| 1473 def foo(self): |
| 1474 return self.a |
| 1475 |
| 1476 x = 1 |
| 1477 assert x == 1 |
| 1478 """, |
| 1479 [8,9], "", excludes=['#pragma: NO COVER']) |
| 1480 |
| 1481 |
| 1482 class Py24Test(CoverageTest): |
| 1483 """Tests of new syntax in Python 2.4.""" |
| 1484 |
| 1485 def test_function_decorators(self): |
| 1486 self.check_coverage("""\ |
| 1487 def require_int(func): |
| 1488 def wrapper(arg): |
| 1489 assert isinstance(arg, int) |
| 1490 return func(arg) |
| 1491 |
| 1492 return wrapper |
| 1493 |
| 1494 @require_int |
| 1495 def p1(arg): |
| 1496 return arg*2 |
| 1497 |
| 1498 assert p1(10) == 20 |
| 1499 """, |
| 1500 [1,2,3,4,6,8,10,12], "") |
| 1501 |
| 1502 def test_function_decorators_with_args(self): |
| 1503 self.check_coverage("""\ |
| 1504 def boost_by(extra): |
| 1505 def decorator(func): |
| 1506 def wrapper(arg): |
| 1507 return extra*func(arg) |
| 1508 return wrapper |
| 1509 return decorator |
| 1510 |
| 1511 @boost_by(10) |
| 1512 def boosted(arg): |
| 1513 return arg*2 |
| 1514 |
| 1515 assert boosted(10) == 200 |
| 1516 """, |
| 1517 [1,2,3,4,5,6,8,10,12], "") |
| 1518 |
| 1519 def test_double_function_decorators(self): |
| 1520 self.check_coverage("""\ |
| 1521 def require_int(func): |
| 1522 def wrapper(arg): |
| 1523 assert isinstance(arg, int) |
| 1524 return func(arg) |
| 1525 return wrapper |
| 1526 |
| 1527 def boost_by(extra): |
| 1528 def decorator(func): |
| 1529 def wrapper(arg): |
| 1530 return extra*func(arg) |
| 1531 return wrapper |
| 1532 return decorator |
| 1533 |
| 1534 @require_int |
| 1535 @boost_by(10) |
| 1536 def boosted1(arg): |
| 1537 return arg*2 |
| 1538 |
| 1539 assert boosted1(10) == 200 |
| 1540 |
| 1541 @boost_by(10) |
| 1542 @require_int |
| 1543 def boosted2(arg): |
| 1544 return arg*2 |
| 1545 |
| 1546 assert boosted2(10) == 200 |
| 1547 """, |
| 1548 ([1,2,3,4,5,7,8,9,10,11,12,14,15,17,19,21,22,24,26], |
| 1549 [1,2,3,4,5,7,8,9,10,11,12,14, 17,19,21, 24,26]), "") |
| 1550 |
| 1551 |
| 1552 class Py25Test(CoverageTest): |
| 1553 """Tests of new syntax in Python 2.5.""" |
| 1554 |
| 1555 def test_with_statement(self): |
| 1556 self.check_coverage("""\ |
| 1557 class Managed: |
| 1558 def __enter__(self): |
| 1559 desc = "enter" |
| 1560 |
| 1561 def __exit__(self, type, value, tb): |
| 1562 desc = "exit" |
| 1563 |
| 1564 m = Managed() |
| 1565 with m: |
| 1566 desc = "block1a" |
| 1567 desc = "block1b" |
| 1568 |
| 1569 try: |
| 1570 with m: |
| 1571 desc = "block2" |
| 1572 raise Exception("Boo!") |
| 1573 except: |
| 1574 desc = "caught" |
| 1575 """, |
| 1576 [1,2,3,5,6,8,9,10,11,13,14,15,16,17,18], "") |
| 1577 |
| 1578 def test_try_except_finally(self): |
| 1579 self.check_coverage("""\ |
| 1580 a = 0; b = 0 |
| 1581 try: |
| 1582 a = 1 |
| 1583 except: |
| 1584 a = 99 |
| 1585 finally: |
| 1586 b = 2 |
| 1587 assert a == 1 and b == 2 |
| 1588 """, |
| 1589 [1,2,3,4,5,7,8], "4-5") |
| 1590 self.check_coverage("""\ |
| 1591 a = 0; b = 0 |
| 1592 try: |
| 1593 a = 1 |
| 1594 raise Exception("foo") |
| 1595 except: |
| 1596 a = 99 |
| 1597 finally: |
| 1598 b = 2 |
| 1599 assert a == 99 and b == 2 |
| 1600 """, |
| 1601 [1,2,3,4,5,6,8,9], "") |
| 1602 self.check_coverage("""\ |
| 1603 a = 0; b = 0 |
| 1604 try: |
| 1605 a = 1 |
| 1606 raise Exception("foo") |
| 1607 except ImportError: |
| 1608 a = 99 |
| 1609 except: |
| 1610 a = 123 |
| 1611 finally: |
| 1612 b = 2 |
| 1613 assert a == 123 and b == 2 |
| 1614 """, |
| 1615 [1,2,3,4,5,6,7,8,10,11], "6") |
| 1616 self.check_coverage("""\ |
| 1617 a = 0; b = 0 |
| 1618 try: |
| 1619 a = 1 |
| 1620 raise IOError("foo") |
| 1621 except ImportError: |
| 1622 a = 99 |
| 1623 except IOError: |
| 1624 a = 17 |
| 1625 except: |
| 1626 a = 123 |
| 1627 finally: |
| 1628 b = 2 |
| 1629 assert a == 17 and b == 2 |
| 1630 """, |
| 1631 [1,2,3,4,5,6,7,8,9,10,12,13], "6, 9-10") |
| 1632 self.check_coverage("""\ |
| 1633 a = 0; b = 0 |
| 1634 try: |
| 1635 a = 1 |
| 1636 except: |
| 1637 a = 99 |
| 1638 else: |
| 1639 a = 123 |
| 1640 finally: |
| 1641 b = 2 |
| 1642 assert a == 123 and b == 2 |
| 1643 """, |
| 1644 [1,2,3,4,5,7,9,10], "4-5") |
| 1645 self.check_coverage("""\ |
| 1646 a = 0; b = 0 |
| 1647 try: |
| 1648 a = 1 |
| 1649 raise Exception("foo") |
| 1650 except: |
| 1651 a = 99 |
| 1652 else: |
| 1653 a = 123 |
| 1654 finally: |
| 1655 b = 2 |
| 1656 assert a == 99 and b == 2 |
| 1657 """, |
| 1658 [1,2,3,4,5,6,8,10,11], "8") |
| 1659 |
| 1660 |
| 1661 class ModuleTest(CoverageTest): |
| 1662 """Tests for the module-level behavior of the `coverage` module.""" |
| 1663 |
| 1664 run_in_temp_dir = False |
| 1665 |
| 1666 def test_not_singleton(self): |
| 1667 # You *can* create another coverage object. |
| 1668 coverage.Coverage() |
| 1669 coverage.Coverage() |
| 1670 |
| 1671 def test_old_name_and_new_name(self): |
| 1672 self.assertIs(coverage.coverage, coverage.Coverage) |
| 1673 |
| 1674 |
| 1675 class ReportingTest(CoverageTest): |
| 1676 """Tests of some reporting behavior.""" |
| 1677 |
| 1678 # We don't make any temporary files, but we need an empty directory to run |
| 1679 # the tests in. |
| 1680 no_files_in_temp_dir = True |
| 1681 |
| 1682 def test_no_data_to_report_on_annotate(self): |
| 1683 # Reporting with no data produces a nice message and no output |
| 1684 # directory. |
| 1685 with self.assertRaisesRegex(CoverageException, "No data to report."): |
| 1686 self.command_line("annotate -d ann") |
| 1687 self.assert_doesnt_exist("ann") |
| 1688 |
| 1689 def test_no_data_to_report_on_html(self): |
| 1690 # Reporting with no data produces a nice message and no output |
| 1691 # directory. |
| 1692 with self.assertRaisesRegex(CoverageException, "No data to report."): |
| 1693 self.command_line("html -d htmlcov") |
| 1694 self.assert_doesnt_exist("htmlcov") |
| 1695 |
| 1696 def test_no_data_to_report_on_xml(self): |
| 1697 # Reporting with no data produces a nice message. |
| 1698 with self.assertRaisesRegex(CoverageException, "No data to report."): |
| 1699 self.command_line("xml") |
| 1700 self.assert_doesnt_exist("coverage.xml") |
OLD | NEW |