Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: tools/telemetry/third_party/coverage/tests/test_arcs.py

Issue 1366913004: Add coverage Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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's arc measurement."""
5
6 from tests.coveragetest import CoverageTest
7
8 import coverage
9 from coverage import env
10 from coverage.files import abs_file
11
12
13 class SimpleArcTest(CoverageTest):
14 """Tests for coverage.py's arc measurement."""
15
16 def test_simple_sequence(self):
17 self.check_coverage("""\
18 a = 1
19 b = 2
20 """,
21 arcz=".1 12 2.")
22 self.check_coverage("""\
23 a = 1
24
25 b = 3
26 """,
27 arcz=".1 13 3.")
28 self.check_coverage("""\
29
30 a = 2
31 b = 3
32
33 c = 5
34 """,
35 arcz=".2 23 35 5-2")
36
37 def test_function_def(self):
38 self.check_coverage("""\
39 def foo():
40 a = 2
41
42 foo()
43 """,
44 arcz=".1 .2 14 2. 4.")
45
46 def test_if(self):
47 self.check_coverage("""\
48 a = 1
49 if len([]) == 0:
50 a = 3
51 assert a == 3
52 """,
53 arcz=".1 12 23 24 34 4.", arcz_missing="24")
54 self.check_coverage("""\
55 a = 1
56 if len([]) == 1:
57 a = 3
58 assert a == 1
59 """,
60 arcz=".1 12 23 24 34 4.", arcz_missing="23 34")
61
62 def test_if_else(self):
63 self.check_coverage("""\
64 if len([]) == 0:
65 a = 2
66 else:
67 a = 4
68 assert a == 2
69 """,
70 arcz=".1 12 25 14 45 5.", arcz_missing="14 45")
71 self.check_coverage("""\
72 if len([]) == 1:
73 a = 2
74 else:
75 a = 4
76 assert a == 4
77 """,
78 arcz=".1 12 25 14 45 5.", arcz_missing="12 25")
79
80 def test_compact_if(self):
81 self.check_coverage("""\
82 a = 1
83 if len([]) == 0: a = 2
84 assert a == 2
85 """,
86 arcz=".1 12 23 3.", arcz_missing="")
87 self.check_coverage("""\
88 def fn(x):
89 if x % 2: return True
90 return False
91 a = fn(1)
92 assert a == True
93 """,
94 arcz=".1 14 45 5. .2 2. 23 3.", arcz_missing="23 3.")
95
96 def test_multiline(self):
97 self.check_coverage("""\
98 a = (
99 2 +
100 3
101 )
102 b = \\
103 6
104 """,
105 arcz=".1 15 5-2", arcz_missing="")
106
107 def test_if_return(self):
108 self.check_coverage("""\
109 def if_ret(a):
110 if a:
111 return 3
112 b = 4
113 return 5
114 x = if_ret(0) + if_ret(1)
115 assert x == 8
116 """,
117 arcz=".1 16 67 7. .2 23 24 3. 45 5.", arcz_missing=""
118 )
119
120 def test_dont_confuse_exit_and_else(self):
121 self.check_coverage("""\
122 def foo():
123 if foo:
124 a = 3
125 else:
126 a = 5
127 return a
128 assert foo() == 3 # 7
129 """,
130 arcz=".1 17 7. .2 23 36 25 56 6.", arcz_missing="25 56"
131 )
132 self.check_coverage("""\
133 def foo():
134 if foo:
135 a = 3
136 else:
137 a = 5
138 foo() # 6
139 """,
140 arcz=".1 16 6. .2 23 3. 25 5.", arcz_missing="25 5."
141 )
142
143 if 0: # expected failure
144 def test_unused_lambdas_are_confusing_bug_90(self):
145 self.check_coverage("""\
146 a = 1
147 fn = lambda x: x
148 b = 3
149 """,
150 arcz=".1 12 .2 2-2 23 3."
151 )
152
153
154 class WithTest(CoverageTest):
155 """Arc-measuring tests involving context managers."""
156
157 def test_with(self):
158 self.check_coverage("""\
159 def example():
160 with open("test", "w") as f: # exit
161 f.write("")
162 return 1
163
164 example()
165 """,
166 arcz=".1 .2 23 34 4. 16 6."
167 )
168
169 def test_bug_146(self):
170 # https://bitbucket.org/ned/coveragepy/issue/146
171 self.check_coverage("""\
172 for i in range(2):
173 with open("test", "w") as f:
174 print(3)
175 print(4)
176 print(5)
177 """,
178 arcz=".1 12 23 34 41 15 5."
179 )
180
181
182 class LoopArcTest(CoverageTest):
183 """Arc-measuring tests involving loops."""
184
185 def test_loop(self):
186 self.check_coverage("""\
187 for i in range(10):
188 a = i
189 assert a == 9
190 """,
191 arcz=".1 12 21 13 3.", arcz_missing="")
192 self.check_coverage("""\
193 a = -1
194 for i in range(0):
195 a = i
196 assert a == -1
197 """,
198 arcz=".1 12 23 32 24 4.", arcz_missing="23 32")
199
200 def test_nested_loop(self):
201 self.check_coverage("""\
202 for i in range(3):
203 for j in range(3):
204 a = i + j
205 assert a == 4
206 """,
207 arcz=".1 12 23 32 21 14 4.", arcz_missing="")
208
209 def test_break(self):
210 self.check_coverage("""\
211 for i in range(10):
212 a = i
213 break # 3
214 a = 99
215 assert a == 0 # 5
216 """,
217 arcz=".1 12 23 35 15 41 5.", arcz_missing="15 41")
218
219 def test_continue(self):
220 self.check_coverage("""\
221 for i in range(10):
222 a = i
223 continue # 3
224 a = 99
225 assert a == 9 # 5
226 """,
227 arcz=".1 12 23 31 15 41 5.", arcz_missing="41")
228
229 def test_nested_breaks(self):
230 self.check_coverage("""\
231 for i in range(3):
232 for j in range(3):
233 a = i + j
234 break # 4
235 if i == 2:
236 break
237 assert a == 2 and i == 2 # 7
238 """,
239 arcz=".1 12 23 34 45 25 56 51 67 17 7.", arcz_missing="17 25")
240
241 def test_while_true(self):
242 # With "while 1", the loop knows it's constant.
243 self.check_coverage("""\
244 a, i = 1, 0
245 while 1:
246 if i >= 3:
247 a = 4
248 break
249 i += 1
250 assert a == 4 and i == 3
251 """,
252 arcz=".1 12 23 34 45 36 63 57 7.",
253 )
254 # With "while True", 2.x thinks it's computation, 3.x thinks it's
255 # constant.
256 if env.PY3:
257 arcz = ".1 12 23 34 45 36 63 57 7."
258 else:
259 arcz = ".1 12 23 27 34 45 36 62 57 7."
260 self.check_coverage("""\
261 a, i = 1, 0
262 while True:
263 if i >= 3:
264 a = 4
265 break
266 i += 1
267 assert a == 4 and i == 3
268 """,
269 arcz=arcz,
270 )
271
272 def test_for_if_else_for(self):
273 self.check_coverage("""\
274 def branches_2(l):
275 if l:
276 for e in l:
277 a = 4
278 else:
279 a = 6
280
281 def branches_3(l):
282 for x in l:
283 if x:
284 for e in l:
285 a = 12
286 else:
287 a = 14
288
289 branches_2([0,1])
290 branches_3([0,1])
291 """,
292 arcz=
293 ".1 18 8G GH H. "
294 ".2 23 34 43 26 3. 6. "
295 ".9 9A 9-8 AB BC CB B9 AE E9",
296 arcz_missing="26 6."
297 )
298
299 def test_for_else(self):
300 self.check_coverage("""\
301 def forelse(seq):
302 for n in seq:
303 if n > 5:
304 break
305 else:
306 print('None of the values were greater than 5')
307 print('Done')
308 forelse([1,2])
309 forelse([1,6])
310 """,
311 arcz=".1 .2 23 32 34 47 26 67 7. 18 89 9."
312 )
313
314 def test_confusing_for_loop_bug_175(self):
315 if env.PY3:
316 # Py3 counts the list comp as a separate code object.
317 arcz = ".1 .2 2-2 12 23 34 45 53 3."
318 else:
319 arcz = ".1 12 23 34 45 53 3."
320 self.check_coverage("""\
321 o = [(1,2), (3,4)]
322 o = [a for a in o]
323 for tup in o:
324 x = tup[0]
325 y = tup[1]
326 """,
327 arcz=arcz, arcz_missing="", arcz_unpredicted="")
328 if env.PY3:
329 arcz = ".1 12 .2 2-2 23 34 42 2."
330 else:
331 arcz = ".1 12 23 34 42 2."
332 self.check_coverage("""\
333 o = [(1,2), (3,4)]
334 for tup in [a for a in o]:
335 x = tup[0]
336 y = tup[1]
337 """,
338 arcz=arcz, arcz_missing="", arcz_unpredicted="")
339
340
341 class ExceptionArcTest(CoverageTest):
342 """Arc-measuring tests involving exception handling."""
343
344 def test_try_except(self):
345 self.check_coverage("""\
346 a, b = 1, 1
347 try:
348 a = 3
349 except:
350 b = 5
351 assert a == 3 and b == 1
352 """,
353 arcz=".1 12 23 36 45 56 6.", arcz_missing="45 56")
354 self.check_coverage("""\
355 a, b = 1, 1
356 try:
357 a = 3
358 raise Exception("Yikes!")
359 a = 5
360 except:
361 b = 7
362 assert a == 3 and b == 7
363 """,
364 arcz=".1 12 23 34 58 67 78 8.",
365 arcz_missing="58", arcz_unpredicted="46")
366
367 def test_hidden_raise(self):
368 self.check_coverage("""\
369 a, b = 1, 1
370 def oops(x):
371 if x % 2: raise Exception("odd")
372 try:
373 a = 5
374 oops(1)
375 a = 7
376 except:
377 b = 9
378 assert a == 5 and b == 9
379 """,
380 arcz=".1 12 .3 3-2 24 45 56 67 7A 89 9A A.",
381 arcz_missing="67 7A", arcz_unpredicted="68")
382
383 def test_except_with_type(self):
384 self.check_coverage("""\
385 a, b = 1, 1
386 def oops(x):
387 if x % 2: raise ValueError("odd")
388 def try_it(x):
389 try:
390 a = 6
391 oops(x)
392 a = 8
393 except ValueError:
394 b = 10
395 return a
396 assert try_it(0) == 8 # C
397 assert try_it(1) == 6 # D
398 """,
399 arcz=".1 12 .3 3-2 24 4C CD D. .5 56 67 78 8B 9A AB B-4",
400 arcz_missing="",
401 arcz_unpredicted="79")
402
403 def test_try_finally(self):
404 self.check_coverage("""\
405 a, c = 1, 1
406 try:
407 a = 3
408 finally:
409 c = 5
410 assert a == 3 and c == 5
411 """,
412 arcz=".1 12 23 35 56 6.", arcz_missing="")
413 self.check_coverage("""\
414 a, c, d = 1, 1, 1
415 try:
416 try:
417 a = 4
418 finally:
419 c = 6
420 except:
421 d = 8
422 assert a == 4 and c == 6 and d == 1 # 9
423 """,
424 arcz=".1 12 23 34 46 67 78 89 69 9.",
425 arcz_missing="67 78 89", arcz_unpredicted="")
426 self.check_coverage("""\
427 a, c, d = 1, 1, 1
428 try:
429 try:
430 a = 4
431 raise Exception("Yikes!")
432 a = 6
433 finally:
434 c = 8
435 except:
436 d = 10 # A
437 assert a == 4 and c == 8 and d == 10 # B
438 """,
439 arcz=".1 12 23 34 45 68 89 8B 9A AB B.",
440 arcz_missing="68 8B", arcz_unpredicted="58")
441
442 def test_finally_in_loop(self):
443 self.check_coverage("""\
444 a, c, d, i = 1, 1, 1, 99
445 try:
446 for i in range(5):
447 try:
448 a = 5
449 if i > 0:
450 raise Exception("Yikes!")
451 a = 8
452 finally:
453 c = 10
454 except:
455 d = 12 # C
456 assert a == 5 and c == 10 and d == 12 # D
457 """,
458 arcz=".1 12 23 34 3D 45 56 67 68 8A A3 AB BC CD D.",
459 arcz_missing="3D", arcz_unpredicted="7A")
460 self.check_coverage("""\
461 a, c, d, i = 1, 1, 1, 99
462 try:
463 for i in range(5):
464 try:
465 a = 5
466 if i > 10:
467 raise Exception("Yikes!")
468 a = 8
469 finally:
470 c = 10
471 except:
472 d = 12 # C
473 assert a == 8 and c == 10 and d == 1 # D
474 """,
475 arcz=".1 12 23 34 3D 45 56 67 68 8A A3 AB BC CD D.",
476 arcz_missing="67 AB BC CD", arcz_unpredicted="")
477
478
479 def test_break_in_finally(self):
480 self.check_coverage("""\
481 a, c, d, i = 1, 1, 1, 99
482 try:
483 for i in range(5):
484 try:
485 a = 5
486 if i > 0:
487 break
488 a = 8
489 finally:
490 c = 10
491 except:
492 d = 12 # C
493 assert a == 5 and c == 10 and d == 1 # D
494 """,
495 arcz=".1 12 23 34 3D 45 56 67 68 7A 8A A3 AB BC CD D.",
496 arcz_missing="3D AB BC CD", arcz_unpredicted="AD")
497
498 def test_finally_in_loop_bug_92(self):
499 self.check_coverage("""\
500 for i in range(5):
501 try:
502 j = 3
503 finally:
504 f = 5
505 g = 6
506 h = 7
507 """,
508 arcz=".1 12 23 35 56 61 17 7.",
509 arcz_missing="", arcz_unpredicted="")
510
511 # "except Exception as e" is crucial here.
512 def test_bug_212(self):
513 # Run this test only on Py2 for now. I hope to fix it on Py3
514 # eventually...
515 if env.PY3:
516 self.skip("This doesn't work on Python 3")
517 self.check_coverage("""\
518 def b(exc):
519 try:
520 while 1:
521 raise Exception(exc) # 4
522 except Exception as e:
523 if exc != 'expected':
524 raise
525 q = 8
526
527 b('expected')
528 try:
529 b('unexpected') # C
530 except:
531 pass
532 """,
533 arcz=".1 .2 1A 23 34 56 67 68 8. AB BC C. DE E.",
534 arcz_missing="C.", arcz_unpredicted="45 7. CD")
535
536 def test_except_finally(self):
537 self.check_coverage("""\
538 a, b, c = 1, 1, 1
539 try:
540 a = 3
541 except:
542 b = 5
543 finally:
544 c = 7
545 assert a == 3 and b == 1 and c == 7
546 """,
547 arcz=".1 12 23 45 37 57 78 8.", arcz_missing="45 57")
548 self.check_coverage("""\
549 a, b, c = 1, 1, 1
550 def oops(x):
551 if x % 2: raise Exception("odd")
552 try:
553 a = 5
554 oops(1)
555 a = 7
556 except:
557 b = 9
558 finally:
559 c = 11
560 assert a == 5 and b == 9 and c == 11
561 """,
562 arcz=".1 12 .3 3-2 24 45 56 67 7B 89 9B BC C.",
563 arcz_missing="67 7B", arcz_unpredicted="68")
564
565
566 class YieldTest(CoverageTest):
567 """Arc tests for generators."""
568
569 def test_yield_in_loop(self):
570 self.check_coverage("""\
571 def gen(inp):
572 for n in inp:
573 yield n
574
575 list(gen([1,2,3]))
576 """,
577 arcz=".1 .2 23 2. 32 15 5.",
578 arcz_missing="",
579 arcz_unpredicted="")
580
581 def test_padded_yield_in_loop(self):
582 self.check_coverage("""\
583 def gen(inp):
584 i = 2
585 for n in inp:
586 i = 4
587 yield n
588 i = 6
589 i = 7
590
591 list(gen([1,2,3]))
592 """,
593 arcz=".1 19 9. .2 23 34 45 56 63 37 7.",
594 arcz_missing="",
595 arcz_unpredicted="")
596
597 def test_bug_308(self):
598 self.check_coverage("""\
599 def run():
600 for i in range(10):
601 yield lambda: i
602
603 for f in run():
604 print(f())
605 """,
606 arcz=".1 15 56 65 5. .2 23 32 2. .3 3-3",
607 arcz_missing="",
608 arcz_unpredicted="")
609
610 self.check_coverage("""\
611 def run():
612 yield lambda: 100
613 for i in range(10):
614 yield lambda: i
615
616 for f in run():
617 print(f())
618 """,
619 arcz=".1 16 67 76 6. .2 23 34 43 3. 2-2 .4 4-4",
620 arcz_missing="",
621 arcz_unpredicted="")
622
623 self.check_coverage("""\
624 def run():
625 yield lambda: 100 # no branch miss
626
627 for f in run():
628 print(f())
629 """,
630 arcz=".1 14 45 54 4. .2 2. 2-2",
631 arcz_missing="",
632 arcz_unpredicted="")
633
634 def test_bug_324(self):
635 # This code is tricky: the list() call pulls all the values from gen(),
636 # but each of them is a generator itself that is never iterated. As a
637 # result, the generator expression on line 3 is never entered or run.
638 self.check_coverage("""\
639 def gen(inp):
640 for n in inp:
641 yield (i * 2 for i in range(n))
642
643 list(gen([1,2,3]))
644 """,
645 arcz=
646 ".1 15 5. " # The module level
647 ".2 23 32 2. " # The gen() function
648 ".3 3-3", # The generator expression
649 arcz_missing=".3 3-3",
650 arcz_unpredicted="")
651
652 def test_coroutines(self):
653 self.check_coverage("""\
654 def double_inputs():
655 while [1]: # avoid compiler differences
656 x = yield
657 x *= 2
658 yield x
659
660 gen = double_inputs()
661 next(gen)
662 print(gen.send(10))
663 next(gen)
664 print(gen.send(6))
665 """,
666 arcz=
667 ".1 17 78 89 9A AB B. "
668 ".2 23 34 45 52 2.",
669 arcz_missing="2.",
670 arcz_unpredicted="")
671 self.assertEqual(self.stdout(), "20\n12\n")
672
673
674 class MiscArcTest(CoverageTest):
675 """Miscellaneous arc-measuring tests."""
676
677 def test_dict_literal(self):
678 if env.PYVERSION < (3, 5):
679 arcz = ".1 19 9."
680 else:
681 # Python 3.5 changed how dict literals are constructed.
682 arcz = ".1 19 9-2"
683 self.check_coverage("""\
684 d = {
685 'a': 2,
686 'b': 3,
687 'c': {
688 'd': 5,
689 'e': 6,
690 }
691 }
692 assert d
693 """,
694 arcz=arcz)
695
696 def test_pathologically_long_code_object(self):
697 # https://bitbucket.org/ned/coveragepy/issue/359
698 # The structure of this file is such that an EXTENDED_ARG byte code is
699 # needed to encode the jump at the end. We weren't interpreting those
700 # opcodes.
701 code = """\
702 data = [
703 """ + "".join("""\
704 [{i}, {i}, {i}, {i}, {i}, {i}, {i}, {i}, {i}, {i}],
705 """.format(i=i) for i in range(2000)
706 ) + """\
707 ]
708
709 if __name__ == "__main__":
710 print(len(data))
711 """
712 self.check_coverage(
713 code,
714 arcs=[(-1, 1), (1, 2004), (2004, -2), (2004, 2005), (2005, -2)],
715 )
716
717
718 class ExcludeTest(CoverageTest):
719 """Tests of exclusions to indicate known partial branches."""
720
721 def test_default(self):
722 # A number of forms of pragma comment are accepted.
723 self.check_coverage("""\
724 a = 1
725 if a: #pragma: no branch
726 b = 3
727 c = 4
728 if c: # pragma NOBRANCH
729 d = 6
730 e = 7
731 if e:#\tpragma:\tno branch
732 f = 9
733 """,
734 [1,2,3,4,5,6,7,8,9],
735 arcz=".1 12 23 24 34 45 56 57 67 78 89 9. 8.", arcz_missing="")
736
737 def test_custom_pragmas(self):
738 self.check_coverage("""\
739 a = 1
740 while a: # [only some]
741 c = 3
742 break
743 assert c == 5-2
744 """,
745 [1,2,3,4,5],
746 partials=["only some"],
747 arcz=".1 12 23 34 45 25 5.", arcz_missing="")
748
749
750 class LineDataTest(CoverageTest):
751 """Tests that line_data gives us what we expect."""
752
753 def test_branch(self):
754 cov = coverage.Coverage(branch=True)
755
756 self.make_file("fun1.py", """\
757 def fun1(x):
758 if x == 1:
759 return
760
761 fun1(3)
762 """)
763
764 self.start_import_stop(cov, "fun1")
765
766 data = cov.get_data()
767 fun1_lines = data.lines(abs_file("fun1.py"))
768 self.assertCountEqual(fun1_lines, [1, 2, 5])
OLDNEW
« no previous file with comments | « tools/telemetry/third_party/coverage/tests/test_api.py ('k') | tools/telemetry/third_party/coverage/tests/test_backward.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698