示例#1
0
 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)
示例#2
0
    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)
示例#3
0
    def test_conditions_are_ignored(self):
        import re
        from astkit import ast
        from instrumental.constructs import LogicalAnd
        from instrumental.pragmas import no_cond
        from instrumental.pragmas import PragmaNoCondition
        node = ast.BoolOp(values=[ast.Name(id="x"),
                                  ast.Name(id="y")],
                          op=ast.And(),
                          lineno=17,
                          col_offset=1)
        match = re.match(no_cond, 'no cond(T F,F *)')
        pragma = PragmaNoCondition(match)
        construct = LogicalAnd('<string>', '17.1', node, set([pragma]))
        assert '(x and y)' == construct.source
        assert 3 == construct.number_of_conditions(False)
        assert "T T" == construct.description(0)
        assert "F *" == construct.description(1)
        assert "T F" == construct.description(2)

        # T T
        construct.record(True, 0, '*')
        construct.record(True, 1, '*')

        assert not construct.conditions_missed(False)
        assert construct.conditions[0] == set(['*'])
        assert construct.conditions[1] == set(['P'])
        assert construct.conditions[2] == set(['P'])
示例#4
0
 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
示例#5
0
 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
示例#6
0
 def test_IfExp_with_literal_orelse_falsy(self):
     from astkit import ast
     test = ast.Name(id='False')
     body = ast.Name(id='b')
     orelse = ast.Num(n=0)
     node = ast.IfExp(test=test, body=body, orelse=orelse)
     expected = set([False])
     yield self._test_node, node, expected
示例#7
0
 def test_IfExp_without_literals(self):
     from astkit import ast
     test = ast.Name(id='a')
     body = ast.Name(id='b')
     orelse = ast.Name(id='c')
     node = ast.IfExp(test=test, body=body, orelse=orelse)
     expected = set([True, False])
     yield self._test_node, node, expected
示例#8
0
 def _makeOne(self, selector='3.1', pragmas={}):
     node = ast.Compare(left=ast.Name(id='a'),
                        ops=[ast.Eq()],
                        comparators=[ast.Name(id='b')],
                        col_offset=12,
                        lineno=3)
     construct = Comparison('somemodule', selector, node, pragmas)
     return construct
示例#9
0
 def test_IfExp_with_literal_body_truthy(self):
     from astkit import ast
     test = ast.Name(id='True')
     body = ast.Num(n=4)
     orelse = ast.Name(id='c')
     node = ast.IfExp(test=test, body=body, orelse=orelse)
     expected = set([True])
     yield self._test_node, node, expected
示例#10
0
 def test_BoolOp_2_pin_and_without_literals(self):
     from astkit import ast
     node = ast.BoolOp(
         op=ast.And(),
         values=[
             ast.Name(id='a'),
             ast.Name(id='b'),
         ],
     )
     expected = set([True, False])
     yield self._test_node, node, expected
示例#11
0
 def test_BoolOp_3_pin_or_with_1_literal_False(self):
     from astkit import ast
     node = ast.BoolOp(
         op=ast.Or(),
         values=[
             ast.Name(id='a'),
             ast.Name(id='False'),
             ast.Name(id='c'),
         ],
     )
     expected = set([True, False])
     yield self._test_node, node, expected
示例#12
0
    def test_serialize_BooleanDecision(self):
        from instrumental.constructs import BooleanDecision

        node = ast.BoolOp(op=ast.And(),
                          values=[ast.Name(id="a"),
                                  ast.Name(id="b")],
                          lineno=4)
        construct = BooleanDecision('somemodule', '4.2', node, [])

        serializer = self._makeOne()
        actual = serializer.dump(construct)
        expected = "BooleanDecision|somemodule|4.2|BoolOp'4'And'Name{a}'Name{b}||False:;True:"
        assert actual == expected, (actual, expected)
示例#13
0
    def test_serialize_LogicalBoolean_And(self):
        from instrumental.constructs import LogicalAnd

        node = ast.BoolOp(op=ast.And(),
                          values=[ast.Name(id="a"),
                                  ast.Name(id="b")],
                          lineno=4)
        construct = LogicalAnd('somemodule', '4.2', node, [])

        serializer = self._makeOne()
        actual = serializer.dump(construct)
        expected = "LogicalAnd|somemodule|4.2|BoolOp'4'And'Name{a}'Name{b}||0:;1:;2:"
        assert actual == expected, (actual, expected)
示例#14
0
 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
示例#15
0
 def test_List_truthy(self):
     from astkit import ast
     node = ast.List(elts=[
         ast.Name(id='False'),
     ])
     expected = set([True])
     yield self._test_node, node, expected
示例#16
0
 def get_recorder_call():
     kall = ast.Call()
     kall.func = ast.Attribute(value=ast.Name(id="_xxx_recorder_xxx_",
                                              ctx=ast.Load()),
                               attr="record",
                               ctx=ast.Load())
     kall.keywords = []
     return kall
示例#17
0
 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)
示例#18
0
 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
示例#19
0
 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,
                       }
示例#20
0
 def test_add_a_non_BoolOp(self):
     recorder = ExecutionRecorder.get()
     node = ast.BoolOp(op=4,
                       values=[ast.Name(id="foo"),
                               ast.Str(s='""')],
                       lineno=1,
                       col_offset=0)
     try:
         recorder.add_BoolOp('somemodule', node, [], None)
     except TypeError as exc:
         assert "BoolOp" in str(exc), exc
示例#21
0
 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
示例#22
0
 def get_statement_recorder_call(modulename, lineno):
     kall = ast.Call()
     kall.func = ast.Attribute(value=ast.Name(id="_xxx_recorder_xxx_",
                                              ctx=ast.Load()),
                               attr="record_statement",
                               ctx=ast.Load())
     kall.args = [
         ast.Str(s=modulename),
         ast.Num(n=lineno),
     ]
     kall.keywords = []
     kall_stmt = ast.Expr(value=kall)
     return kall_stmt
示例#23
0
    def test_serialize_Comparison(self):
        from instrumental.constructs import Comparison

        node = ast.Compare(left=ast.Name(id="a"),
                           ops=[ast.NotEq()],
                           comparators=[ast.Str(s="foobar")],
                           lineno=4)
        construct = Comparison('somemodule', '4.2', node, [])

        serializer = self._makeOne()
        actual = serializer.dump(construct)
        expected = "Comparison|somemodule|4.2|Compare'4'Name{a};NotEq;Str{Zm9vYmFy}||False:;True:"
        assert actual == expected, (actual, expected)
示例#24
0
 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)
示例#25
0
    def test_conditions_are_ignored_for_decision(self):
        import re
        from astkit import ast
        from instrumental.constructs import BooleanDecision
        from instrumental.pragmas import no_cond
        from instrumental.pragmas import PragmaNoCondition
        node = ast.Name(id="x", lineno=17, col_offset=1)
        match = re.match(no_cond, 'no cond(T)')
        pragma = PragmaNoCondition(match)
        construct = BooleanDecision('<string>', '17.1', node, set([pragma]))
        assert 'x' == construct.source
        assert 1 == construct.number_of_conditions(False)
        assert "T" == construct.description(True)
        assert "F" == construct.description(False)

        # T T
        construct.record(False, '*')

        assert not construct.conditions_missed(False)
        assert construct.conditions[True] == set(['P'])
        assert construct.conditions[False] == set(['*'])
示例#26
0
 def test_Or_with_False_third_pin(self):
     yield self._test, [2], self._makeOr, (ast.Name(id='a'),
                                           ast.Name(id='b'),
                                           ast.Name(id='False'))
示例#27
0
 def test_Or_with_True_third_pin(self):
     yield self._test, [3], self._makeOr, (ast.Name(id='a'),
                                           ast.Name(id='b'),
                                           ast.Name(id='True'))
示例#28
0
 def test_Or_with_False_second_pin(self):
     yield self._test, [1], self._makeOr, (ast.Name(id='a'),
                                           ast.Name(id='False'),
                                           ast.Name(id='c'))
示例#29
0
 def test_Or_with_True_second_pin(self):
     yield self._test, [], self._makeOr, (ast.Name(id='a'),
                                          ast.Name(id='True'),
                                          ast.Name(id='c'))
示例#30
0
 def test_Or_with_False_first_pin(self):
     yield self._test, [0], self._makeOr, (ast.Name(id='False'),
                                           ast.Name(id='b'),
                                           ast.Name(id='c'))