dot_to_qt_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2009, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 import unittest
00035 
00036 from qt_dotgraph.dot_to_qt import DotToQtGenerator, get_unquoted
00037 from python_qt_binding.QtWidgets import QApplication
00038 import sys
00039 import subprocess
00040 
00041 
00042 def check_x_server():
00043     p = subprocess.Popen(sys.executable, stdin=subprocess.PIPE)
00044     p.stdin.write('from python_qt_binding.QtWidgets import QApplication\n')
00045     p.stdin.write('app = QApplication([])\n')
00046     p.stdin.close()
00047     p.communicate()
00048 
00049     print(p.returncode)
00050 
00051     return p.returncode == 0
00052 
00053 
00054 
00055 class DotToQtGeneratorTest(unittest.TestCase):
00056 
00057     DOT_CODE = '''
00058     digraph graph_name {
00059         graph [bb="0,0,154,108",
00060             rank=same
00061         ];
00062         node [label="\N"];
00063         subgraph cluster_foo {
00064             graph [bb="1,1,100,101",
00065                 label=cluster_foo
00066             ];
00067         }
00068         foo      [height=0.5,
00069             label=foo,
00070             pos="77,90",
00071             shape=box,
00072             width=0.75];
00073         bar      [height=0.5,
00074             label=barbarbarbarbarbarbarbar,
00075             pos="77,18",
00076             shape=box,
00077             width=2.25];
00078         foo -> bar       [pos="e,77,36.104 77,71.697 77,63.983 77,54.712 77,46.112"];
00079     }
00080     '''
00081 
00082     _Q_APP = None
00083 
00084     def __init__(self, *args):
00085         super(DotToQtGeneratorTest, self).__init__(*args)
00086         # needed for creation of QtGraphic items in NodeItem.__init__
00087         if DotToQtGeneratorTest._Q_APP is None:
00088             if check_x_server():
00089                 DotToQtGeneratorTest._Q_APP = QApplication([])
00090 
00091     def test_simple_integration(self):
00092         if DotToQtGeneratorTest._Q_APP is None:
00093             raise unittest.case.SkipTest
00094 
00095         (nodes, edges) = DotToQtGenerator().dotcode_to_qt_items(
00096             DotToQtGeneratorTest.DOT_CODE, 1)
00097         self.assertEqual(3, len(nodes))  # cluster_foo, foo and bar
00098         self.assertEqual(1, len(edges))  # foo -> bar
00099 
00100     def test_label_sizes(self):
00101         if DotToQtGeneratorTest._Q_APP is None:
00102             raise unittest.case.SkipTest
00103 
00104         (nodes, edges) = DotToQtGenerator().dotcode_to_qt_items(DotToQtGeneratorTest.DOT_CODE, 1)
00105 
00106         self.longMessage = True
00107         for name, node in nodes.items():
00108             shape_rect = node._graphics_item.sceneBoundingRect()
00109             label_rect = node._label.sceneBoundingRect()
00110             self.assertLess(
00111                 label_rect.width(),
00112                 shape_rect.width(),
00113                 "Label text for '%s' is wider than surrounding shape." % name)
00114             self.assertLess(
00115                 label_rect.height(),
00116                 shape_rect.height(),
00117                 "Label text for '%s' is higher than surrounding shape." % name)
00118 
00119     def test_unquoted(self):
00120         self.assertEqual("foo", get_unquoted({'bar': 'foo'}, 'bar'))


qt_dotgraph
Author(s): Thibault Kruse
autogenerated on Fri Feb 3 2017 03:42:09