def test_Module_with_docstring_and_future_import(self): from instrumental.instrument import CoverageAnnotator from instrumental.metadata import MetadataGatheringVisitor from instrumental.pragmas import PragmaFinder source = """'a docstring am I' from __future__ import with_statement if u: return u else: return 'else' """ module = ast.parse(source) pragmas = PragmaFinder().find_pragmas(source) config = DummyConfig() metadata = MetadataGatheringVisitor.analyze(config, 'somemodule', source, pragmas) self.recorder.add_metadata(metadata) transformer = CoverageAnnotator(config, 'somemodule', self.recorder) inst_module = transformer.visit(module) assert isinstance(inst_module.body[1], ast.ImportFrom) assert module.body[1].module == '__future__' assert isinstance(module.body[1].names[0], ast.alias) assert module.body[1].names[0].name == 'with_statement' self._assert_recorder_setup(inst_module, 2)
def test_monkeypatch(self): import imp from instrumental.instrument import AnnotatorFactory from instrumental.monkey import monkeypatch_imp original_load_module = imp.load_module config = DummyConfig() monkeypatch_imp([], [], AnnotatorFactory(config, None)) assert original_load_module != imp.load_module
def test_gather_constructs__ifexp_simple_decision(self): def test_module(): a = 3 b = 0 if a == 4 else 1 module, source = load_module(test_module) pragmas = self._get_pragmas(source) metadata = self._make_one().analyze(DummyConfig(), 'modname', module, pragmas) assert "2.1" in metadata.constructs, metadata.constructs decision = metadata.constructs["2.1"] assert isinstance(decision, constructs.BooleanDecision)
def test_gather_lines__with_pragmas(self): def test_module(): a = 3 if a == 4: b = 0 # pragma: no cover else: b = 1 module, source = load_module(test_module) pragmas = self._get_pragmas(source) metadata = self._make_one().analyze(DummyConfig(), 'modname', module, pragmas) assert set([1, 2, 5]) == set(metadata.lines), set(metadata.lines)
def test_visit_Assert(self): def test_module(): assert a or b module, source = load_module(test_module) pragmas = self._get_pragmas(source) metadata = self._make_one().analyze(DummyConfig(), 'modname', module, pragmas) assert 2 == len(metadata.constructs) decision = metadata.constructs["1.1"] assert isinstance(decision, constructs.BooleanDecision) boolop = metadata.constructs["1.2"] assert isinstance(boolop, constructs.LogicalOr)
def test_visit_Or_unreachable_False(self): def test_module(): c = a or True module, source = load_module(test_module) pragmas = self._get_pragmas(source) metadata = self._make_one().analyze(DummyConfig(), 'modname', module, pragmas) assert 2 == len(metadata.constructs), len(metadata.constructs) decision = metadata.constructs["1.1"] assert list(decision.conditions[False])[0] == constructs.UnreachableCondition assert isinstance(decision, constructs.BooleanDecision) boolop = metadata.constructs["1.2"] assert isinstance(boolop, constructs.LogicalOr)
def _load_and_compile_module(self, module_func): module, source = load_module(module_func) from instrumental.pragmas import PragmaFinder pragmas = PragmaFinder().find_pragmas(source) from instrumental.metadata import MetadataGatheringVisitor config = DummyConfig() self.recorder.add_metadata( MetadataGatheringVisitor.analyze(config, module_func.__name__, source, pragmas)) # self.recorder.add_source(module_func.__name__, source) transformer = CoverageAnnotator(config, module_func.__name__, self.recorder) inst_module = transformer.visit(module) sys.stdout.write(renderer.render(inst_module) + "\n") code = compile(inst_module, '<string>', 'exec') return code
def test_gather_constructs__multiple_ifexps(self): def test_module(): a = 3 b, c = 0 if a == 4 else 1, 2 if a == 2 or a == 3 else 3 module, source = load_module(test_module) pragmas = self._get_pragmas(source) metadata = self._make_one().analyze(DummyConfig(), 'modname', module, pragmas) assert 6 == len(metadata.constructs), metadata.constructs decision = metadata.constructs["2.1"] assert isinstance(decision, constructs.BooleanDecision) comparison = metadata.constructs["2.2"] assert isinstance(comparison, constructs.Comparison) decision = metadata.constructs["2.3"] assert isinstance(decision, constructs.BooleanDecision) decision = metadata.constructs["2.4"] assert isinstance(decision, constructs.LogicalOr) comparison = metadata.constructs["2.5"] assert isinstance(comparison, constructs.Comparison) comparison = metadata.constructs["2.6"] assert isinstance(comparison, constructs.Comparison)
def test_nonempty(self): statements = {'instrumental': {1: True, 2: True, 4: False}, 'instrumental.tests': {1: False, 3:True, 5:False}, } _1_1 = DummyConstruct('instrumental', True, {True: set(['X']), False: set(['X']), }) _1_2 = DummyConstruct('instrumental', False, {0: set(['X']), 1: set(), 2: set(), }) _3_1 = DummyConstruct('intrumental.test', True, {True: set(['X']), False: set(['X']), }) _3_2 = DummyConstruct('instrumental.test', False, {0: set(['X']), 1: set(), 2: set(), }) conditions = {'instrumental': {'1.1': _1_1, '1.2': _1_2, }, 'instrumental.tests': {'3.1': _3_1, '3.2': _3_2, } } s = summary.ExecutionSummary(conditions, statements, DummyConfig()) packages = s.packages assert 'instrumental' in packages
def setup(self): self.config = DummyConfig() ExecutionRecorder.reset() ExecutionRecorder.get().start()
def _makeOne(self): from instrumental.metadata import MetadataGatheringVisitor metadata = MetadataGatheringVisitor.analyze(DummyConfig(), self.modulename, self.source, {6: set()}) return metadata.constructs[self.label]
def setup(self): from instrumental.recorder import ExecutionRecorder ExecutionRecorder.reset() self._pre_test_modules = sys.modules.keys() self.config = DummyConfig()