diff options
-rw-r--r-- | test/coveragetest.py | 7 | ||||
-rw-r--r-- | test/test_parser.py | 33 |
2 files changed, 37 insertions, 3 deletions
diff --git a/test/coveragetest.py b/test/coveragetest.py index 53f0ef0b..1e2c2431 100644 --- a/test/coveragetest.py +++ b/test/coveragetest.py @@ -123,11 +123,12 @@ class CoverageTest(TestCase): """Return the data written to stderr during the test.""" return self.captured_stderr.getvalue() - def make_file(self, filename, text=""): + def make_file(self, filename, text="", newline=None): """Create a temp file. `filename` is the path to the file, including directories if desired, - and `text` is the content. + and `text` is the content. If `newline` is provided, it is a string + that will be used as the line endings in the created file. Returns the path to the file. @@ -135,6 +136,8 @@ class CoverageTest(TestCase): # Tests that call `make_file` should be run in a temp environment. assert self.run_in_temp_dir text = textwrap.dedent(text) + if newline: + text = text.replace("\n", newline) # Make sure the directories are available. dirs, _ = os.path.split(filename) diff --git a/test/test_parser.py b/test/test_parser.py index b398044d..c25611cf 100644 --- a/test/test_parser.py +++ b/test/test_parser.py @@ -16,7 +16,7 @@ class ParserTest(CoverageTest): def parse_source(self, text): """Parse `text` as source, and return the `CodeParser` used.""" text = textwrap.dedent(text) - cp = CodeParser(text, exclude="nocover") + cp = CodeParser(text=text, exclude="nocover") cp.parse_source() return cp @@ -94,3 +94,34 @@ class ParserTest(CoverageTest): b = 6 """) self.assertEqual(cp.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 `CodeParser` used.""" + cp = CodeParser(filename=filename, exclude="nocover") + cp.parse_source() + return cp + + 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) + cp = self.parse_file(fname) + self.assertEqual(cp.exit_counts(), counts) |