pydot_factory_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 re
35 import unittest
36 from qt_dotgraph.pydotfactory import PydotFactory
37 
38 
39 class PyDotFactoryTest(unittest.TestCase):
40 
41  def test_get_graph(self):
42  fac = PydotFactory()
43  g = fac.get_graph()
44  self.assertEquals('same', g.get_rank())
45  self.assertEquals('digraph', g.get_graph_type())
46 
47  def test_add_node(self):
48  fac = PydotFactory()
49  g = fac.get_graph()
50  fac.add_node_to_graph(g, 'foo')
51  self.assertEqual(1, len(g.get_nodes()))
52  self.assertEqual('foo', g.get_nodes()[0].get_name())
53  self.assertEqual('foo', g.get_nodes()[0].get_label())
54 
56  fac = PydotFactory()
57  g = fac.get_graph()
58  fac.add_node_to_graph(g, 'graph')
59  self.assertEqual(1, len(g.get_nodes()))
60  self.assertEqual('graph_', g.get_nodes()[0].get_name())
61  self.assertEqual('graph_', g.get_nodes()[0].get_label())
62 
63  def test_add_edge(self):
64  fac = PydotFactory()
65  g = fac.get_graph()
66  fac.add_node_to_graph(g, 'foo')
67  fac.add_node_to_graph(g, 'bar')
68  fac.add_edge_to_graph(g, 'foo', 'bar')
69  self.assertEqual(2, len(g.get_nodes()))
70  self.assertEqual(1, len(g.get_edges()))
71  self.assertEqual('foo', g.get_edges()[0].get_source())
72  self.assertEqual('bar', g.get_edges()[0].get_destination())
73 
74  def test_add_subgraph(self):
75  fac = PydotFactory()
76  g = fac.get_graph()
77  fac.add_subgraph_to_graph(g, 'foo')
78  self.assertEqual(1, len(g.get_subgraph_list()))
79  self.assertEqual('cluster_foo', g.get_subgraph_list()[0].get_name())
80  self.assertEqual('foo', g.get_subgraph_list()[0].get_label())
81 
83  fac = PydotFactory()
84  g = fac.get_graph()
85  fac.add_subgraph_to_graph(g, 'graph')
86  self.assertEqual(1, len(g.get_subgraph_list()))
87  self.assertEqual('cluster_graph_', g.get_subgraph_list()[0].get_name())
88  self.assertEqual('graph_', g.get_subgraph_list()[0].get_label())
89 
90  def test_create_dot(self):
91  fac = PydotFactory()
92  g = fac.get_graph()
93  fac.add_node_to_graph(g, 'foo')
94  fac.add_node_to_graph(g, 'edge')
95  fac.add_edge_to_graph(g, 'foo', 'edge')
96  fac.add_subgraph_to_graph(g, 'foo')
97  snippets = ['digraph graphname { graph [',
98  'rankdir=TB',
99  'compound=True',
100  'rank=same',
101  'node [label="\\N"]',
102  'subgraph cluster_foo { graph [',
103  'foo [',
104  'label=foo',
105  'shape=box',
106  'pos="',
107  'edge_ [',
108  'label=edge_',
109  'foo -> edge_ [',
110  '"]; }']
111  result = fac.create_dot(g)
112  # get rid of version specific whitespaces
113  result = re.sub('[\n\t ]+', ' ', result)
114  for sn in snippets:
115  self.assertTrue(sn in result, '%s \nmissing in\n %s' % (sn, result))


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