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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
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)
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)