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

Side by Side Diff: tools/telemetry/third_party/coverage/lab/branches.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 # Demonstrate some issues with coverage.py branch testing.
5
6 def my_function(x):
7 """This isn't real code, just snippets..."""
8
9 # An infinite loop is structurally still a branch: it can next execute the
10 # first line of the loop, or the first line after the loop. But
11 # "while True" will never jump to the line after the loop, so the line
12 # is shown as a partial branch:
13
14 i = 0
15 while True:
16 print "In while True"
17 if i > 0:
18 break
19 i += 1
20 print "Left the True loop"
21
22 # Notice that "while 1" also has this problem. Even though the compiler
23 # knows there's no computation at the top of the loop, it's still expressed
24 # in byte code as a branch with two possibilities.
25
26 i = 0
27 while 1:
28 print "In while 1"
29 if i > 0:
30 break
31 i += 1
32 print "Left the 1 loop"
33
34 # Coverage.py lets developers exclude lines that they know will not be
35 # executed. So far, the branch coverage doesn't use all that information
36 # when deciding which lines are partially executed.
37 #
38 # Here, even though the else line is explicitly marked as never executed,
39 # the if line complains that it never branched to the else:
40
41 if x < 1000:
42 # This branch is always taken
43 print "x is reasonable"
44 else: # pragma: nocover
45 print "this never happens"
46
47 # try-except structures are complex branches. An except clause with a
48 # type is a three-way branch: there could be no exception, there could be
49 # a matching exception, and there could be a non-matching exception.
50 #
51 # Here we run the code twice: once with no exception, and once with a
52 # matching exception. The "except" line is marked as partial because we
53 # never executed its third case: a non-matching exception.
54
55 for y in (1, 2):
56 try:
57 if y % 2:
58 raise ValueError("y is odd!")
59 except ValueError:
60 print "y must have been odd"
61 print "done with y"
62 print "done with 1, 2"
63
64 # Another except clause, but this time all three cases are executed. No
65 # partial lines are shown:
66
67 for y in (0, 1, 2):
68 try:
69 if y % 2:
70 raise ValueError("y is odd!")
71 if y == 0:
72 raise Exception("zero!")
73 except ValueError:
74 print "y must have been odd"
75 except:
76 print "y is something else"
77 print "done with y"
78 print "done with 0, 1, 2"
79
80
81 my_function(1)
OLDNEW
« no previous file with comments | « tools/telemetry/third_party/coverage/igor.py ('k') | tools/telemetry/third_party/coverage/lab/cover-plugin.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698