Ejemplo n.º 1
0
 def test_missing_finish_node_source(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH))
     expression.add_node(3, Node(NodeType.ERROR, 'error'))
     expression.add_edge(1, 3)
     with self.assertRaises(RuntimeError):
         validator.check_invalid_connections(expression)
Ejemplo n.º 2
0
 def test_unnecessary_ground_node_value(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH))
     expression.add_node(3, Node(NodeType.GROUND, 'ground'))
     expression.add_edge(1, 2)
     with self.assertRaises(RuntimeError):
         validator.check_unnecessary_node_values(expression)
Ejemplo n.º 3
0
 def test_missing_operation_node_value(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH))
     expression.add_node(3, Node(NodeType.OPERATION))
     expression.add_edge(1, 2)
     expression.add_edge(2, 3)
     with self.assertRaises(RuntimeError):
         validator.check_missing_node_values(expression)
Ejemplo n.º 4
0
 def test_missing_token_node_target(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH))
     expression.add_node(3, Node(NodeType.TOKEN, 'token'))
     expression.add_edge(1, 2)
     expression.add_edge(1, 3)
     with self.assertRaises(RuntimeError):
         validator.check_invalid_connections(expression)
Ejemplo n.º 5
0
 def test_multiple_ground_nodes(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH))
     expression.add_node(3, Node(NodeType.GROUND))
     expression.add_node(4, Node(NodeType.GROUND))
     expression.add_edge(1, 2)
     with self.assertRaises(RuntimeError):
         validator.check_ground_nodes(expression)
Ejemplo n.º 6
0
 def test_missing_router_node_source(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH))
     expression.add_node(3, Node(NodeType.ROUTER, 'router'))
     expression.add_edge(1, 2)
     expression.add_edge(3, 2)
     with self.assertRaises(RuntimeError):
         validator.check_invalid_connections(expression)
Ejemplo n.º 7
0
 def test_missing_operation_node_source(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH))
     expression.add_node(3, Node(NodeType.OPERATION, 'operation'))
     expression.add_edge(1, 2)
     expression.add_edge(3, 2)
     with self.assertRaises(RuntimeError):
         validator.check_invalid_connections(expression)
Ejemplo n.º 8
0
 def test_unnecessary_connection_node_value(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.CONNECTION, 'connection'))
     expression.add_node(3, Node(NodeType.FINISH))
     expression.add_edge(1, 2)
     expression.add_edge(2, 3)
     with self.assertRaises(RuntimeError):
         validator.check_unnecessary_node_values(expression)
Ejemplo n.º 9
0
 def test_invalid_ground_node_source(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH))
     expression.add_node(3, Node(NodeType.GROUND))
     expression.add_edge(1, 2)
     expression.add_edge(1, 3)
     expression.add_edge(3, 2)
     with self.assertRaises(RuntimeError):
         validator.check_invalid_connections(expression)
Ejemplo n.º 10
0
 def test_missing_finish_node(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.INFO, 'validation test'))
     expression.add_edge(1, 2)
     with self.assertRaises(RuntimeError):
         validator.check_finish_node(expression)
Ejemplo n.º 11
0
 def test_unnecessary_finish_node_value(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH, 'finish'))
     expression.add_edge(1, 2)
     with self.assertRaises(RuntimeError):
         validator.check_unnecessary_node_values(expression)
Ejemplo n.º 12
0
def load_expressions(path):
    """
    Load expressions from a grammar file.
    :param path: the path of the file
    :return: dictionary of expression objects
    """
    expressions = {}
    expression_index = 0
    if not os.path.isfile(path):
        raise ValueError('Invalid grammar file path "{}"!'.format(path))
    with open(path, 'r') as grammar_file:
        expression_name = ''
        for line in grammar_file:
            words = list(filter(None, line.split(' ')))
            if len(words) > 0:
                if words[0] == 'expression':
                    expression_name = get_string_content(line)
                    expressions[expression_name] = Expression(expression_index)
                    expression_index += 1
                elif len(words) >= 5:
                    node_id = int(words[0])
                    node_type = words[1]
                    node_value = get_string_content(line)
                    node = Node(node_type, node_value)
                    expressions[expression_name].add_node(node_id, node)
                elif len(words) == 2:
                    source_id = int(words[0])
                    target_id = int(words[1])
                    expressions[expression_name].add_edge(source_id, target_id)
    return expressions
Ejemplo n.º 13
0
 def test_referenced_expressions(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.FINISH))
     expression.add_node(3, Node(NodeType.EXPRESSION, 'expression'))
     expression.add_edge(1, 2)
     expression.add_edge(1, 3)
     expression.add_edge(3, 2)
     grammar = Grammar()
     grammar.add_expression('sample', expression)
     with self.assertRaises(RuntimeError):
         validator.check_referenced_expressions(grammar)
Ejemplo n.º 14
0
 def test_multiple_start_node(self):
     expression = Expression()
     expression.add_node(1, Node(NodeType.START))
     expression.add_node(2, Node(NodeType.START))
     expression.add_node(3, Node(NodeType.INFO, 'validation test'))
     expression.add_node(4, Node(NodeType.FINISH))
     expression.add_edge(1, 3)
     expression.add_edge(2, 3)
     expression.add_edge(3, 4)
     with self.assertRaises(RuntimeError):
         validator.check_start_node(expression)