def test_encode_Comparison(self): from instrumental.storage import ObjectEncoder as OE node = ast.Compare(left=ast.Name(id='a'), ops=[ast.Eq()], comparators=[ast.Num(n=4)], lineno=4) construct = Comparison('somemodule', '4.2', node, []) construct.conditions = {False: set(), True: set(['X'])} result = OE().encode(construct) expected = {'__python_class__': 'Comparison', 'modulename': 'somemodule', 'label': '4.2', 'node': {'__python_class__': 'Compare', 'left': {'__python_class__': 'Name', 'id': 'a'}, 'ops': [{'__python_class__': 'Eq'}], 'comparators': [{'__python_class__': 'Num', 'n': 4}], 'lineno': 4, }, 'conditions': {0: [], 1: ['X']}, } assert result == 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 test_encode_LogicalAnd(self): from instrumental.storage import ObjectEncoder as OE node = ast.BoolOp(op=ast.And(), values=[ast.Name(id='a'), ast.Name(id='b')], lineno=4) construct = LogicalAnd('somemodule', '4.2', node, []) construct.conditions = {0: set(), 1: set(['X']), 2: set([UnreachableCondition])} result = OE().encode(construct) expected = {'__python_class__': 'LogicalAnd', 'modulename': 'somemodule', 'label': '4.2', 'node': {'__python_class__': 'BoolOp', 'op': {'__python_class__': 'And'}, 'values': [{'__python_class__': 'Name', 'id': 'a'}, {'__python_class__': 'Name', 'id': 'b'}], 'lineno': 4, }, 'conditions': {0: [], 1: ['X'], 2: ['__unreachable__']}, } assert result == expected
def test_encode_Node_flat(self): from instrumental.storage import ObjectEncoder as OE node = ast.Name(id="var", lineno=4, offset=14) result = OE().encode_Node(node) assert result == {'__python_class__': 'Name', 'id': 'var', 'lineno': 4, 'offset': 14, }
def test_encode_Node_with_list_attribute(self): from instrumental.storage import ObjectEncoder as OE node = ast.Compare(left=ast.Name(id='a'), ops=[ast.Eq()], comparators=[ast.Num(n=4)]) result = OE().encode_Node(node) expected = {'__python_class__': 'Compare', 'left': {'__python_class__': 'Name', 'id': 'a'}, 'ops': [{'__python_class__': 'Eq'}], 'comparators': [{'__python_class__': 'Num', 'n': 4}], } assert result == expected, (result, expected)