dot_to_qt_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2009, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 import unittest
35 
36 from qt_dotgraph.dot_to_qt import DotToQtGenerator, get_unquoted
37 from python_qt_binding.QtWidgets import QApplication
38 import sys
39 import subprocess
40 
41 
43  p = subprocess.Popen(sys.executable, stdin=subprocess.PIPE)
44  p.stdin.write(b'from python_qt_binding.QtWidgets import QApplication\n')
45  p.stdin.write(b'app = QApplication([])\n')
46  p.stdin.close()
47  p.communicate()
48 
49  print(p.returncode)
50 
51  return p.returncode == 0
52 
53 
54 class DotToQtGeneratorTest(unittest.TestCase):
55 
56  DOT_CODE = r'''
57  digraph graph_name {
58  graph [bb="0,0,154,108",
59  rank=same
60  ];
61  node [label="\N"];
62  subgraph cluster_foo {
63  graph [bb="1,1,100,101",
64  label=cluster_foo
65  ];
66  }
67  foo [height=0.5,
68  label=foo,
69  pos="77,90",
70  shape=box,
71  width=0.75];
72  bar [height=0.5,
73  label=barbarbarbarbarbarbarbar,
74  pos="77,18",
75  shape=box,
76  width=2.25];
77  foo -> bar [pos="e,77,36.104 77,71.697 77,63.983 77,54.712 77,46.112"];
78  }
79  '''
80 
81  _Q_APP = None
82 
83  def __init__(self, *args):
84  super(DotToQtGeneratorTest, self).__init__(*args)
85 
86  # needed for creation of QtGraphic items in NodeItem.__init__
87  if DotToQtGeneratorTest._Q_APP is None:
88  if check_x_server():
89  DotToQtGeneratorTest._Q_APP = QApplication([])
90 
92  if DotToQtGeneratorTest._Q_APP is None:
93  raise unittest.case.SkipTest
94 
95  (nodes, edges) = DotToQtGenerator().dotcode_to_qt_items(
96  DotToQtGeneratorTest.DOT_CODE, 1)
97  self.assertEqual(3, len(nodes)) # cluster_foo, foo and bar
98  self.assertEqual(1, len(edges)) # foo -> bar
99 
100  def test_label_sizes(self):
101  if DotToQtGeneratorTest._Q_APP is None:
102  raise unittest.case.SkipTest
103 
104  (nodes, edges) = DotToQtGenerator().dotcode_to_qt_items(DotToQtGeneratorTest.DOT_CODE, 1)
105 
106  self.longMessage = True
107  for name, node in nodes.items():
108  shape_rect = node._graphics_item.sceneBoundingRect()
109  label_rect = node._label.sceneBoundingRect()
110  self.assertLess(
111  label_rect.width(),
112  shape_rect.width(),
113  "Label text for '%s' is wider than surrounding shape." % name)
114  self.assertLess(
115  label_rect.height(),
116  shape_rect.height(),
117  "Label text for '%s' is higher than surrounding shape." % name)
118 
119  def test_unquoted(self):
120  self.assertEqual("foo", get_unquoted({'bar': 'foo'}, 'bar'))
121 
122  def test_recursive(self):
123  if DotToQtGeneratorTest._Q_APP is None:
124  raise unittest.case.SkipTest
125 
126  gen = DotToQtGenerator()
127  dotcode = r'''
128  strict digraph {
129  graph [bb="0,0,249,541",
130  compound=True,
131  rank=same,
132  rankdir=TB,
133  ranksep=0.2,
134  simplify=True
135  ];
136  node [label="\N"];
137  subgraph "/Container" {
138  graph [bb="8,67,241,321",
139  color=None,
140  compound=True,
141  label="/Container",
142  lheight=0.21,
143  lp="124.5,309.5",
144  lwidth=0.81,
145  rank=same,
146  rankdir=TB,
147  ranksep=0.2,
148  style=bold
149  ];
150  subgraph "/Container/Subcontainer" {
151  graph [bb="84,142,233,287",
152  color=None,
153  compound=True,
154  label="/Container/Subcontainer",
155  lheight=0.21,
156  lp="158.5,275.5",
157  lwidth=1.85,
158  rank=same,
159  rankdir=TB,
160  ranksep=0.2,
161  style=bold
162  ];
163  "/Container/Subcontainer/logstate1" [height=0.5,
164  label=logstate1,
165  pos="133,235",
166  shape=box,
167  url=None,
168  width=0.90278];
169  "/Container/Subcontainer/finished" [color=blue,
170  height=0.5,
171  label=finished,
172  pos="133,168",
173  shape=ellipse,
174  url=None,
175  width=1.0833];
176  "/Container/Subcontainer/logstate1" -> "/Container/Subcontainer/finished" [label=done,
177  lp="146.5,201.5",
178  pos="e,133,186.19 133,216.92 133,210.7 133,203.5 133,196.6",
179  url=None];
180  }
181  "/Container/finished" [color=blue,
182  height=0.5,
183  label=finished,
184  pos="86,93",
185  shape=ellipse,
186  url=None,
187  width=1.0833];
188  "/Container/Subcontainer/finished" -> "/Container/finished" [label=finished,
189  lp="132,126.5",
190  pos="e,96.623,110.5 122.33,150.44 116.39,141.19 108.85,129.5 102.19,119.15",
191  url=None];
192  "/Container/logstate" [height=0.5,
193  label=logstate,
194  pos="46,168",
195  shape=box,
196  url=None,
197  width=0.81944];
198  "/Container/logstate" -> "/Container/finished" [label=done,
199  lp="82.5,126.5",
200  pos="e,74.304,110.45 53.482,149.8 57.712,140.5 63.287,128.93 69,119 69.051,118.91 69.102,118.82 69.153,118.74",
201  url=None];
202  }
203  "/finished" [height=0.5,
204  pos="86,18",
205  width=1.1555];
206  "/Container/finished" -> "/finished" [label=finished,
207  lp="108,51.5",
208  pos="e,86,36.176 86,74.7 86,66.245 86,55.869 86,46.373",
209  url=None];
210  "/start" -> "/Container/Subcontainer/logstate1" [
211  lp="146.5,436.5",
212  pos="e,133,250.01 133,355.84 133,337.5 133,316.81 133,260.22",
213  url=None];
214  "/start" -> "/Container/logstate" [
215  lp="146.5,436.5",
216  pos="e,46,185.01 133,355.84 46,337.5 46,316.81 46,192.22",
217  url=None];
218  "/start" [height=0.5,
219  pos="133,373",
220  width=0.79437];
221  }
222  '''
223 
224  (nodes, edges) = gen.dotcode_to_qt_items(dotcode, 1)
225 
226  expected_nodes = [
227  '"/Container/Subcontainer"', '"/Container/finished"', '"/start"', '"/Container"',
228  '"/Container/Subcontainer/logstate1"', '"/Container/Subcontainer/finished"',
229  '"/Container/logstate"', '"/finished"']
230  expected_edges = [
231  '/Container/logstate_TO_/Container/finished_done',
232  '/Container/Subcontainer/finished_TO_/Container/finished_finished',
233  '/start_TO_/Container/Subcontainer/logstate1',
234  '/Container/finished_TO_/finished_finished',
235  '/start_TO_/Container/logstate',
236  '/Container/Subcontainer/logstate1_TO_/Container/Subcontainer/finished_done']
237  self.assertEqual(expected_nodes, nodes.keys())
238  self.assertEqual(expected_edges, edges.keys())
def get_unquoted(item, name)
Definition: dot_to_qt.py:47


qt_dotgraph
Author(s): Thibault Kruse, Dirk Thomas
autogenerated on Tue Apr 13 2021 03:03:12