forked from nedbat/coveragepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_parser.py
131 lines (114 loc) · 3.81 KB
/
test_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""Tests for Coverage.py's code parsing."""
import textwrap
from tests.coveragetest import CoverageTest
from coverage.parser import PythonParser
class PythonParserTest(CoverageTest):
"""Tests for Coverage.py's Python code parsing."""
run_in_temp_dir = False
def parse_source(self, text):
"""Parse `text` as source, and return the `PythonParser` used."""
text = textwrap.dedent(text)
parser = PythonParser(text=text, exclude="nocover")
parser.parse_source()
return parser
def test_exit_counts(self):
parser = self.parse_source("""\
# check some basic branch counting
class Foo:
def foo(self, a):
if a:
return 5
else:
return 7
class Bar:
pass
""")
self.assertEqual(parser.exit_counts(), {
2:1, 3:1, 4:2, 5:1, 7:1, 9:1, 10:1
})
def test_try_except(self):
parser = self.parse_source("""\
try:
a = 2
except ValueError:
a = 4
except ZeroDivideError:
a = 6
except:
a = 8
b = 9
""")
self.assertEqual(parser.exit_counts(), {
1: 1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1
})
def test_excluded_classes(self):
parser = self.parse_source("""\
class Foo:
def __init__(self):
pass
if 0: # nocover
class Bar:
pass
""")
self.assertEqual(parser.exit_counts(), {
1:0, 2:1, 3:1
})
def test_missing_branch_to_excluded_code(self):
parser = self.parse_source("""\
if fooey:
a = 2
else: # nocover
a = 4
b = 5
""")
self.assertEqual(parser.exit_counts(), { 1:1, 2:1, 5:1 })
parser = self.parse_source("""\
def foo():
if fooey:
a = 3
else:
a = 5
b = 6
""")
self.assertEqual(parser.exit_counts(), { 1:1, 2:2, 3:1, 5:1, 6:1 })
parser = self.parse_source("""\
def foo():
if fooey:
a = 3
else: # nocover
a = 5
b = 6
""")
self.assertEqual(parser.exit_counts(), { 1:1, 2:1, 3:1, 6:1 })
class ParserFileTest(CoverageTest):
"""Tests for Coverage.py's code parsing from files."""
def parse_file(self, filename):
"""Parse `text` as source, and return the `PythonParser` used."""
parser = PythonParser(filename=filename, exclude="nocover")
parser.parse_source()
return parser
def test_line_endings(self):
text = """\
# check some basic branch counting
class Foo:
def foo(self, a):
if a:
return 5
else:
return 7
class Bar:
pass
"""
counts = { 2:1, 3:1, 4:2, 5:1, 7:1, 9:1, 10:1 }
name_endings = (("unix", "\n"), ("dos", "\r\n"), ("mac", "\r"))
for fname, newline in name_endings:
fname = fname + ".py"
self.make_file(fname, text, newline=newline)
parser = self.parse_file(fname)
self.assertEqual(parser.exit_counts(), counts)
def test_encoding(self):
self.make_file("encoded.py", """\
coverage = "\xe7\xf6v\xear\xe3g\xe9"
""")
parser = self.parse_file("encoded.py")
parser.exit_counts() # TODO: This value should be tested!