def test_serialize_ModuleMetadata(self): from instrumental.metadata import ModuleMetadata from instrumental.constructs import BooleanDecision from instrumental.constructs import LogicalOr node = ast.BoolOp(op=ast.Or(), values=[ast.Name(id="a"), ast.Name(id="b")], lineno=4) decision = BooleanDecision('somemodule', '4.1', node, []) or_ = LogicalOr('somemodule', '4.2', node, []) md = ModuleMetadata('somemodule', '', []) md.lines = {1: False, 2: False, 4: True} md.constructs = {'4.1': decision, '4.2': or_} serializer = self._makeOne() actual = serializer.dump(md) expected = """ModuleMetadata 1:0,2:0,4:1 BooleanDecision|somemodule|4.1|BoolOp'4'Or'Name{a}'Name{b}||False:;True: LogicalOr|somemodule|4.2|BoolOp'4'Or'Name{a}'Name{b}||0:;1:;2: """ assert actual == expected, (actual, expected)
def test_encode_ModuleMetadata(self): from instrumental.storage import ObjectEncoder as OE node = ast.BoolOp(op=ast.Or(), values=[ast.Name(id='a'), ast.Name(id='b')], lineno=4) construct = LogicalOr('somemodule', '4.2', node, []) construct.conditions = {0: set(), 1: set(['X']), 2: set()} metadata = ModuleMetadata('somemodule', 'somesource', []) metadata.lines = {1: False, 2: False, 4: True} metadata.constructs = {'4.2': construct} result = OE().encode(metadata) expected_construct = {'__python_class__': 'LogicalOr', 'modulename': 'somemodule', 'label': '4.2', 'node': {'__python_class__': 'BoolOp', 'op': {'__python_class__': 'Or'}, 'values': [{'__python_class__': 'Name', 'id': 'a'}, {'__python_class__': 'Name', 'id': 'b'}], 'lineno': 4, }, 'conditions': {0: [], 1: ['X'], 2: []}, } expected = {'__python_class__': 'ModuleMetadata', 'modulename': 'somemodule', 'source': 'somesource', 'lines': {1: False, 2: False, 4: True}, 'constructs': {'4.2': expected_construct}, } assert result == expected, (result, expected)
def _makeOne(self, selector='3.1', pragmas=[]): node = ast.BoolOp(values=[ast.Name(id='a'), ast.Name(id='b')], op=ast.Or(), col_offset=12, lineno=3) construct = LogicalOr('somemodule', selector, node, pragmas) return construct
def test_construct_with_literal(self): recorder = ExecutionRecorder.get() node = ast.BoolOp(op=ast.Or(), values=[ast.Name(id="foo"), ast.Str(s='""')], lineno=1, col_offset=0) recorder.add_BoolOp('somemodule', '1.1', node, [], None)
def test_BoolOp_2_pin_or_without_literals(self): from astkit import ast node = ast.BoolOp( op=ast.Or(), values=[ ast.Name(id='a'), ast.Name(id='b'), ], ) expected = set([True, False]) yield self._test_node, node, expected
def test_BoolOp_3_pin_or_with_1_literal_True(self): from astkit import ast node = ast.BoolOp( op=ast.Or(), values=[ ast.Name(id='a'), ast.Name(id='True'), ast.Name(id='c'), ], ) expected = set([True]) yield self._test_node, node, expected
def test_serialize_LogicalBoolean_Or(self): from instrumental.constructs import LogicalOr node = ast.BoolOp(op=ast.Or(), values=[ast.Name(id="a"), ast.Name(id="b")], lineno=4) construct = LogicalOr('somemodule', '4.2', node, []) serializer = self._makeOne() actual = serializer.dump(construct) expected = "LogicalOr|somemodule|4.2|BoolOp'4'Or'Name{a}'Name{b}||0:;1:;2:" assert actual == expected, (actual, expected)
def test_roundtrip(self): from instrumental.storage import JSONSerializer node = ast.BoolOp(op=ast.Or(), values=[ast.Name(id='a'), ast.Name(id='b')], lineno=4) construct = LogicalOr('somemodule', '4.2', node, []) construct.conditions = {0: set(), 1: set(['X']), 2: set([UnreachableCondition])} metadata = ModuleMetadata('somemodule', 'somesource', []) metadata.lines = {1: False, 2: False, 4: True} metadata.constructs = {'4.2': construct} recorder = ExecutionRecorder() recorder.add_metadata(metadata) f = StringIO() JSONSerializer.dump(recorder, f) f.seek(0) got_recorder = JSONSerializer.load(f) got_metadata = got_recorder.metadata['somemodule'] assert got_metadata.modulename == 'somemodule' assert got_metadata.source == 'somesource' assert got_metadata.lines == {1: False, 2: False, 4: True},( got_metadata.lines) got_construct = got_metadata.constructs['4.2'] assert got_construct.modulename == 'somemodule' assert got_construct.label == '4.2' assert got_construct.conditions == {0: set(), 1: set(['X']), 2: set([UnreachableCondition])} got_node = got_construct.node assert isinstance(got_node.op, ast.Or) assert isinstance(got_node.values[0], ast.Name) assert got_node.values[0].id == 'a' assert isinstance(got_node.values[1], ast.Name) assert got_node.values[1].id == 'b' assert got_node.lineno == 4
def test_roundtrip(self): from instrumental.storage import ResultStore node = ast.BoolOp(op=ast.Or(), values=[ast.Name(id='a'), ast.Name(id='b')], lineno=4) construct = LogicalOr('somemodule', '4.2', node, []) construct.conditions = {0: set(), 1: set(['X']), 2: set([UnreachableCondition])} metadata = ModuleMetadata('somemodule', 'somesource', []) metadata.lines = {1: False, 2: False, 4: True} metadata.constructs = {'4.2': construct} recorder = ExecutionRecorder() recorder.add_metadata(metadata) store = self._makeOne('.', 'testing', None) store.save(recorder) got_recorder = store.load() os.remove(store.filename) got_metadata = got_recorder.metadata['somemodule'] assert got_metadata.modulename == 'somemodule' assert got_metadata.source == 'somesource' assert got_metadata.lines == {1: False, 2: False, 4: True},( got_metadata.lines) got_construct = got_metadata.constructs['4.2'] assert got_construct.modulename == 'somemodule' assert got_construct.label == '4.2' assert got_construct.conditions == {0: set(), 1: set(['X']), 2: set([UnreachableCondition])} got_node = got_construct.node assert isinstance(got_node.op, ast.Or) assert isinstance(got_node.values[0], ast.Name) assert got_node.values[0].id == 'a' assert isinstance(got_node.values[1], ast.Name) assert got_node.values[1].id == 'b' assert got_node.lineno == 4
def test_encode_LogicalOr(self): from instrumental.storage import ObjectEncoder as OE node = ast.BoolOp(op=ast.Or(), values=[ast.Name(id='a'), ast.Name(id='b')], lineno=4) construct = LogicalOr('somemodule', '4.2', node, []) construct.conditions = {0: set(), 1: set(['X']), 2: set()} result = OE().encode(construct) expected = {'__python_class__': 'LogicalOr', 'modulename': 'somemodule', 'label': '4.2', 'node': {'__python_class__': 'BoolOp', 'op': {'__python_class__': 'Or'}, 'values': [{'__python_class__': 'Name', 'id': 'a'}, {'__python_class__': 'Name', 'id': 'b'}], 'lineno': 4, }, 'conditions': {0: [], 1: ['X'], 2: []}, } assert result == expected
def _makeOr(self, *conditions): node = self._makeNode(ast.Or(), conditions) return LogicalOr('somemodule', '3.1', node, {})