diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-11-27 19:28:02 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-11-27 19:28:02 -0500 |
commit | 82d7b2bec95db01265eb68707e807d89bb0b1677 (patch) | |
tree | 1dfd923a8bd2baa69527154713039ce6d85b4848 /tests/plugin2.py | |
parent | 0d8e1f7783171c9c4f666f5b759322c5f27ebeb0 (diff) | |
download | python-coveragepy-git-82d7b2bec95db01265eb68707e807d89bb0b1677.tar.gz |
Finished the plugin2 test of a dynamic tracing plugin.
Diffstat (limited to 'tests/plugin2.py')
-rw-r--r-- | tests/plugin2.py | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/tests/plugin2.py b/tests/plugin2.py index 1fa66cb2..4fb3d057 100644 --- a/tests/plugin2.py +++ b/tests/plugin2.py @@ -1,24 +1,48 @@ """A plugin for test_plugins.py to import.""" import coverage +from coverage.parser import CodeParser + class Plugin(coverage.CoveragePlugin): def file_tracer(self, filename): if "render.py" in filename: - return RenderFileTracer(filename) + return RenderFileTracer() + + def file_reporter(self, filename): + return FileReporter(filename) class RenderFileTracer(coverage.plugin.FileTracer): - def __init__(self, filename): - pass + """A FileTracer using information from the caller.""" def has_dynamic_source_filename(self): return True def dynamic_source_filename(self, filename, frame): - filename = "fake%d.html" % frame.f_lineno - print("dynamic filename: %r" % filename) - return filename + if frame.f_code.co_name != "render": + return None + return frame.f_locals['filename'] def line_number_range(self, frame): - return 17,19 + lineno = frame.f_locals['linenum'] + return lineno,lineno+1 + + +class FileReporter(coverage.plugin.FileReporter): + # TODO: Why do I have to make a FileReporter just to make a CodeParser?? + def __init__(self, filename): + self.filename = filename + + def get_parser(self, exclude=None): + # Goofy test arrangement: claim that the file has as many lines as the + # number in its name. + num = self.filename.split(".")[0].split("_")[1] + return PluginParser(int(num)) + +class PluginParser(CodeParser): + def __init__(self, num_lines): + self.num_lines = num_lines + + def parse_source(self): + return set(range(1, self.num_lines+1)), set([]) |