"""A module that contains a base class that has helper methods for testing PyT."""
importunittest
importsys
importos
sys.path.insert(0, os.path.abspath('../pyt'))
frominterprocedural_cfgimportinterprocedural
fromast_helperimportgenerate_ast
frommodule_definitionsimportproject_definitions
classBaseTestCase(unittest.TestCase):
"""A base class that has helper methods for testing PyT."""
defassertInCfg(self, connections):
''' Assert that all connections in the connections list exists in the cfg,
as well as all connections not in the list do not exist
connections is a list of tuples where the node at index 0 of the tuple has to be in the new_constraintset of the node a index 1 of the tuple'''
forconnectioninconnections:
self.assertIn(self.cfg.nodes[connection[0]], self.cfg.nodes[connection[1]].outgoing, str(connection) +" expected to be connected")
self.assertIn(self.cfg.nodes[connection[1]], self.cfg.nodes[connection[0]].ingoing, str(connection) +" expected to be connected")
nodes=len(self.cfg.nodes)
forelementinrange(nodes):
forsetsinrange(nodes):
ifnot (element, sets) inconnections:
self.assertNotIn(self.cfg.nodes[element], self.cfg.nodes[sets].outgoing, "(%s <- %s)"% (element, sets) +" expected to be disconnected")
self.assertNotIn(self.cfg.nodes[sets], self.cfg.nodes[element].ingoing, "(%s <- %s)"% (sets, element) +" expected to be disconnected")
defassertConnected(self, node, successor):
'''Asserts that a node is connected to its successor.
This means that node has successor in its outgoing and
successor has node in its ingoing.'''
self.assertIn(successor, node.outgoing,
'\n%s was NOT found in the outgoing list of %s containing: '% (successor.label, node.label) +'['+', '.join([x.labelforxinnode.outgoing]) +']')
self.assertIn(node, successor.ingoing,
'\n%s was NOT found in the ingoing list of %s containing: '% (node.label, successor.label) +'['+', '.join([x.labelforxinsuccessor.ingoing]) +']')
defassertNotConnected(self, node, successor):
'''Asserts that a node is not connected to its successor.
This means that node does not the successor in its outgoing and
successor does not have the node in its ingoing.'''
self.assertNotIn(successor, node.outgoing,
'\n%s was mistakenly found in the outgoing list of %s containing: '% (successor.label, node.label) +'['+', '.join([x.labelforxinnode.outgoing]) +']')
self.assertNotIn(node, successor.ingoing,
'\n%s was mistakenly found in the ingoing list of %s containing: '% (node.label, successor.label) +'['+', '.join([x.labelforxinsuccessor.ingoing]) +']')
defassertLineNumber(self, node, line_number):
self.assertEqual(node.line_number, line_number)
defcfg_list_to_dict(self, list):
'''This method converts the CFG list to a dict, making it easier to find nodes to test.
This method assumes that no nodes in the code have the same label'''