summaryrefslogtreecommitdiff
path: root/tests/test_parser.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_parser.py')
-rw-r--r--tests/test_parser.py216
1 files changed, 99 insertions, 117 deletions
diff --git a/tests/test_parser.py b/tests/test_parser.py
index 9d3f9f67..6edb6d1a 100644
--- a/tests/test_parser.py
+++ b/tests/test_parser.py
@@ -5,6 +5,8 @@
import textwrap
+import pytest
+
from coverage import env
from coverage.misc import NotPython
from coverage.parser import PythonParser
@@ -40,9 +42,9 @@ class PythonParserTest(CoverageTest):
class Bar:
pass
""")
- self.assertEqual(parser.exit_counts(), {
+ assert parser.exit_counts() == {
2:1, 3:1, 4:2, 5:1, 7:1, 9:1, 10:1
- })
+ }
def test_generator_exit_counts(self):
# https://github.com/nedbat/coveragepy/issues/324
@@ -53,12 +55,12 @@ class PythonParserTest(CoverageTest):
list(gen([1,2,3]))
""")
- self.assertEqual(parser.exit_counts(), {
+ assert parser.exit_counts() == {
1:1, # def -> list
2:2, # for -> yield; for -> exit
3:2, # yield -> for; genexp exit
5:1, # list -> exit
- })
+ }
def test_try_except(self):
parser = self.parse_source("""\
@@ -72,9 +74,9 @@ class PythonParserTest(CoverageTest):
a = 8
b = 9
""")
- self.assertEqual(parser.exit_counts(), {
+ assert parser.exit_counts() == {
1: 1, 2:1, 3:2, 4:1, 5:2, 6:1, 7:1, 8:1, 9:1
- })
+ }
def test_excluded_classes(self):
parser = self.parse_source("""\
@@ -86,9 +88,9 @@ class PythonParserTest(CoverageTest):
class Bar:
pass
""")
- self.assertEqual(parser.exit_counts(), {
+ assert parser.exit_counts() == {
1:0, 2:1, 3:1
- })
+ }
def test_missing_branch_to_excluded_code(self):
parser = self.parse_source("""\
@@ -98,7 +100,7 @@ class PythonParserTest(CoverageTest):
a = 4
b = 5
""")
- self.assertEqual(parser.exit_counts(), { 1:1, 2:1, 5:1 })
+ assert parser.exit_counts() == { 1:1, 2:1, 5:1 }
parser = self.parse_source("""\
def foo():
if fooey:
@@ -107,7 +109,7 @@ class PythonParserTest(CoverageTest):
a = 5
b = 6
""")
- self.assertEqual(parser.exit_counts(), { 1:1, 2:2, 3:1, 5:1, 6:1 })
+ assert parser.exit_counts() == { 1:1, 2:2, 3:1, 5:1, 6:1 }
parser = self.parse_source("""\
def foo():
if fooey:
@@ -116,14 +118,14 @@ class PythonParserTest(CoverageTest):
a = 5
b = 6
""")
- self.assertEqual(parser.exit_counts(), { 1:1, 2:1, 3:1, 6:1 })
+ assert parser.exit_counts() == { 1:1, 2:1, 3:1, 6:1 }
def test_indentation_error(self):
msg = (
"Couldn't parse '<code>' as Python source: "
"'unindent does not match any outer indentation level' at line 3"
)
- with self.assertRaisesRegex(NotPython, msg):
+ with pytest.raises(NotPython, match=msg):
_ = self.parse_source("""\
0 spaces
2
@@ -132,7 +134,7 @@ class PythonParserTest(CoverageTest):
def test_token_error(self):
msg = "Couldn't parse '<code>' as Python source: 'EOF in multi-line string' at line 1"
- with self.assertRaisesRegex(NotPython, msg):
+ with pytest.raises(NotPython, match=msg):
_ = self.parse_source("""\
'''
""")
@@ -174,8 +176,8 @@ class PythonParserTest(CoverageTest):
raw_statements = {3, 4, 5, 6, 8, 9, 10, 13, 15, 16, 17, 20, 22, 23, 25, 26}
if env.PYBEHAVIOR.trace_decorated_def:
raw_statements.update([11, 19])
- self.assertEqual(parser.raw_statements, raw_statements)
- self.assertEqual(parser.statements, {8})
+ assert parser.raw_statements == raw_statements
+ assert parser.statements == {8}
def test_class_decorator_pragmas(self):
parser = self.parse_source("""\
@@ -188,8 +190,8 @@ class PythonParserTest(CoverageTest):
def __init__(self):
self.x = 8
""")
- self.assertEqual(parser.raw_statements, {1, 2, 3, 5, 6, 7, 8})
- self.assertEqual(parser.statements, {1, 2, 3})
+ assert parser.raw_statements == {1, 2, 3, 5, 6, 7, 8}
+ assert parser.statements == {1, 2, 3}
def test_empty_decorated_function(self):
parser = self.parse_source("""\
@@ -219,9 +221,9 @@ class PythonParserTest(CoverageTest):
expected_arcs.update(set(arcz_to_arcs("-46 6-4")))
expected_exits.update({6: 1})
- self.assertEqual(expected_statements, parser.statements)
- self.assertEqual(expected_arcs, parser.arcs())
- self.assertEqual(expected_exits, parser.exit_counts())
+ assert expected_statements == parser.statements
+ assert expected_arcs == parser.arcs()
+ assert expected_exits == parser.exit_counts()
class ParserMissingArcDescriptionTest(CoverageTest):
@@ -252,31 +254,24 @@ class ParserMissingArcDescriptionTest(CoverageTest):
thing(12)
more_stuff(13)
""")
- self.assertEqual(
- parser.missing_arc_description(1, 2),
- "line 1 didn't jump to line 2, because the condition on line 1 was never true"
- )
- self.assertEqual(
- parser.missing_arc_description(1, 3),
- "line 1 didn't jump to line 3, because the condition on line 1 was never false"
+ expected = "line 1 didn't jump to line 2, because the condition on line 1 was never true"
+ assert expected == parser.missing_arc_description(1, 2)
+ expected = "line 1 didn't jump to line 3, because the condition on line 1 was never false"
+ assert expected == parser.missing_arc_description(1, 3)
+ expected = (
+ "line 6 didn't return from function 'func5', " +
+ "because the loop on line 6 didn't complete"
)
- self.assertEqual(
- parser.missing_arc_description(6, -5),
- "line 6 didn't return from function 'func5', "
- "because the loop on line 6 didn't complete"
- )
- self.assertEqual(
- parser.missing_arc_description(6, 7),
- "line 6 didn't jump to line 7, because the loop on line 6 never started"
- )
- self.assertEqual(
- parser.missing_arc_description(11, 12),
- "line 11 didn't jump to line 12, because the condition on line 11 was never true"
- )
- self.assertEqual(
- parser.missing_arc_description(11, 13),
- "line 11 didn't jump to line 13, because the condition on line 11 was never false"
+ assert expected == parser.missing_arc_description(6, -5)
+ expected = "line 6 didn't jump to line 7, because the loop on line 6 never started"
+ assert expected == parser.missing_arc_description(6, 7)
+ expected = "line 11 didn't jump to line 12, because the condition on line 11 was never true"
+ assert expected == parser.missing_arc_description(11, 12)
+ expected = (
+ "line 11 didn't jump to line 13, " +
+ "because the condition on line 11 was never false"
)
+ assert expected == parser.missing_arc_description(11, 13)
def test_missing_arc_descriptions_for_small_callables(self):
parser = self.parse_text(u"""\
@@ -288,22 +283,14 @@ class ParserMissingArcDescriptionTest(CoverageTest):
]
x = 7
""")
- self.assertEqual(
- parser.missing_arc_description(2, -2),
- "line 2 didn't finish the lambda on line 2"
- )
- self.assertEqual(
- parser.missing_arc_description(3, -3),
- "line 3 didn't finish the generator expression on line 3"
- )
- self.assertEqual(
- parser.missing_arc_description(4, -4),
- "line 4 didn't finish the dictionary comprehension on line 4"
- )
- self.assertEqual(
- parser.missing_arc_description(5, -5),
- "line 5 didn't finish the set comprehension on line 5"
- )
+ expected = "line 2 didn't finish the lambda on line 2"
+ assert expected == parser.missing_arc_description(2, -2)
+ expected = "line 3 didn't finish the generator expression on line 3"
+ assert expected == parser.missing_arc_description(3, -3)
+ expected = "line 4 didn't finish the dictionary comprehension on line 4"
+ assert expected == parser.missing_arc_description(4, -4)
+ expected = "line 5 didn't finish the set comprehension on line 5"
+ assert expected == parser.missing_arc_description(5, -5)
def test_missing_arc_descriptions_for_exceptions(self):
parser = self.parse_text(u"""\
@@ -314,14 +301,16 @@ class ParserMissingArcDescriptionTest(CoverageTest):
except ValueError:
print("yikes")
""")
- self.assertEqual(
- parser.missing_arc_description(3, 4),
- "line 3 didn't jump to line 4, because the exception caught by line 3 didn't happen"
+ expected = (
+ "line 3 didn't jump to line 4, " +
+ "because the exception caught by line 3 didn't happen"
)
- self.assertEqual(
- parser.missing_arc_description(5, 6),
- "line 5 didn't jump to line 6, because the exception caught by line 5 didn't happen"
+ assert expected == parser.missing_arc_description(3, 4)
+ expected = (
+ "line 5 didn't jump to line 6, " +
+ "because the exception caught by line 5 didn't happen"
)
+ assert expected == parser.missing_arc_description(5, 6)
def test_missing_arc_descriptions_for_finally(self):
parser = self.parse_text(u"""\
@@ -346,56 +335,56 @@ class ParserMissingArcDescriptionTest(CoverageTest):
that_thing(19)
""")
if env.PYBEHAVIOR.finally_jumps_back:
- self.assertEqual(
- parser.missing_arc_description(18, 5),
- "line 18 didn't jump to line 5, because the break on line 5 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(5, 19),
- "line 5 didn't jump to line 19, because the break on line 5 wasn't executed"
+ expected = "line 18 didn't jump to line 5, because the break on line 5 wasn't executed"
+ assert expected == parser.missing_arc_description(18, 5)
+ expected = "line 5 didn't jump to line 19, because the break on line 5 wasn't executed"
+ assert expected == parser.missing_arc_description(5, 19)
+ expected = (
+ "line 18 didn't jump to line 10, " +
+ "because the continue on line 10 wasn't executed"
)
- self.assertEqual(
- parser.missing_arc_description(18, 10),
- "line 18 didn't jump to line 10, because the continue on line 10 wasn't executed"
+ assert expected == parser.missing_arc_description(18, 10)
+ expected = (
+ "line 10 didn't jump to line 2, " +
+ "because the continue on line 10 wasn't executed"
)
- self.assertEqual(
- parser.missing_arc_description(10, 2),
- "line 10 didn't jump to line 2, because the continue on line 10 wasn't executed"
+ assert expected == parser.missing_arc_description(10, 2)
+ expected = (
+ "line 18 didn't jump to line 14, " +
+ "because the return on line 14 wasn't executed"
)
- self.assertEqual(
- parser.missing_arc_description(18, 14),
- "line 18 didn't jump to line 14, because the return on line 14 wasn't executed"
+ assert expected == parser.missing_arc_description(18, 14)
+ expected = (
+ "line 14 didn't return from function 'function', " +
+ "because the return on line 14 wasn't executed"
)
- self.assertEqual(
- parser.missing_arc_description(14, -1),
- "line 14 didn't return from function 'function', "
- "because the return on line 14 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(18, -1),
- "line 18 didn't except from function 'function', "
- "because the raise on line 16 wasn't executed"
+ assert expected == parser.missing_arc_description(14, -1)
+ expected = (
+ "line 18 didn't except from function 'function', " +
+ "because the raise on line 16 wasn't executed"
)
+ assert expected == parser.missing_arc_description(18, -1)
else:
- self.assertEqual(
- parser.missing_arc_description(18, 19),
- "line 18 didn't jump to line 19, because the break on line 5 wasn't executed"
+ expected = (
+ "line 18 didn't jump to line 19, " +
+ "because the break on line 5 wasn't executed"
)
- self.assertEqual(
- parser.missing_arc_description(18, 2),
- "line 18 didn't jump to line 2, "
- "because the continue on line 10 wasn't executed"
- " or "
+ assert expected == parser.missing_arc_description(18, 19)
+ expected = (
+ "line 18 didn't jump to line 2, " +
+ "because the continue on line 10 wasn't executed" +
+ " or " +
"the continue on line 12 wasn't executed"
)
- self.assertEqual(
- parser.missing_arc_description(18, -1),
- "line 18 didn't except from function 'function', "
- "because the raise on line 16 wasn't executed"
- " or "
- "line 18 didn't return from function 'function', "
+ assert expected == parser.missing_arc_description(18, 2)
+ expected = (
+ "line 18 didn't except from function 'function', " +
+ "because the raise on line 16 wasn't executed" +
+ " or " +
+ "line 18 didn't return from function 'function', " +
"because the return on line 14 wasn't executed"
)
+ assert expected == parser.missing_arc_description(18, -1)
def test_missing_arc_descriptions_bug460(self):
parser = self.parse_text(u"""\
@@ -406,10 +395,7 @@ class ParserMissingArcDescriptionTest(CoverageTest):
}
x = 6
""")
- self.assertEqual(
- parser.missing_arc_description(2, -3),
- "line 3 didn't finish the lambda on line 3",
- )
+ assert parser.missing_arc_description(2, -3) == "line 3 didn't finish the lambda on line 3"
class ParserFileTest(CoverageTest):
@@ -440,18 +426,14 @@ class ParserFileTest(CoverageTest):
fname = fname + ".py"
self.make_file(fname, text, newline=newline)
parser = self.parse_file(fname)
- self.assertEqual(
- parser.exit_counts(),
- counts,
- "Wrong for %r" % fname
- )
+ assert parser.exit_counts() == counts, "Wrong for %r" % fname
def test_encoding(self):
self.make_file("encoded.py", """\
coverage = "\xe7\xf6v\xear\xe3g\xe9"
""")
parser = self.parse_file("encoded.py")
- self.assertEqual(parser.exit_counts(), {1: 1})
+ assert parser.exit_counts() == {1: 1}
def test_missing_line_ending(self):
# Test that the set of statements is the same even if a final
@@ -466,7 +448,7 @@ class ParserFileTest(CoverageTest):
""")
parser = self.parse_file("normal.py")
- self.assertEqual(parser.statements, {1})
+ assert parser.statements == {1}
self.make_file("abrupt.py", """\
out, err = subprocess.Popen(
@@ -476,7 +458,7 @@ class ParserFileTest(CoverageTest):
# Double-check that some test helper wasn't being helpful.
with open("abrupt.py") as f:
- self.assertEqual(f.read()[-1], ")")
+ assert f.read()[-1] == ")"
parser = self.parse_file("abrupt.py")
- self.assertEqual(parser.statements, {1})
+ assert parser.statements == {1}