• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Utility used for unit test."""
2from typing import Dict, Sequence
3
4
5class MockNode:
6  """Mock node class."""
7
8  def __init__(self, attrs: Dict[str, str],
9               child_attrs: Sequence[Dict[str, str]] = ({},),
10               is_child: bool = False):
11    self.attributes = attrs
12    self.childs = [
13        MockNode(attrs, is_child=True) for attrs in child_attrs if attrs]
14
15    self.is_child = is_child
16    if 'bounds' not in self.attributes:
17      self.attributes['bounds'] = '[0,0][384,384]'
18
19  def __str__(self):
20    xml_str_elements = []
21    if not self.is_child:
22      xml_str_elements.append(
23          "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>")
24      xml_str_elements.append('<hierarchy rotation="0">')
25
26    xml_str_elements.append('<node index="0"')
27    for attr_key, attr_val in self.attributes.items():
28      xml_str_elements.append(f' {attr_key}="{attr_val}"')
29    xml_str_elements.append('>')
30
31    for child in self.childs:
32      xml_str_elements.append(str(child))
33
34    xml_str_elements.append('</node>')
35
36    if not self.is_child:
37      xml_str_elements.append('</hierarchy>')
38
39    return ''.join(xml_str_elements)
40