pygraphviz_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.pygraphvizfactory import PygraphvizFactory
37 
38 
39 class PygraphvizFactoryTest(unittest.TestCase):
40 
41  def test_get_graph(self):
42  fac = PygraphvizFactory()
43  g = fac.get_graph()
44  self.assertEquals('same', g.graph_attr['rank'])
45  self.assertTrue(g.is_directed())
46 
47  def test_add_node(self):
48  fac = PygraphvizFactory()
49  g = fac.get_graph()
50  fac.add_node_to_graph(g, 'foo')
51  self.assertEqual(1, len(g.nodes()))
52  self.assertEqual('foo', g.nodes()[0].get_name())
53  self.assertEqual('foo', g.nodes()[0].attr['label'])
54 
56  fac = PygraphvizFactory()
57  g = fac.get_graph()
58  fac.add_node_to_graph(g, 'graph')
59  self.assertEqual(1, len(g.nodes()))
60  self.assertEqual('graph', g.nodes()[0].get_name())
61  self.assertEqual('graph', g.nodes()[0].attr['label'])
62 
63  def test_add_edge(self):
64  fac = PygraphvizFactory()
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.nodes()))
70  self.assertEqual(1, len(g.edges()))
71  self.assertEqual('foo', g.edges()[0][0])
72  self.assertEqual('bar', g.edges()[0][1])
73 
74  def test_add_subgraph(self):
75  fac = PygraphvizFactory()
76  g = fac.get_graph()
77  fac.add_subgraph_to_graph(g, 'foo')
78  self.assertEqual(1, len(g.subgraphs()))
79  self.assertEqual('cluster_foo', g.subgraphs()[0].get_name())
80  self.assertEqual('foo', g.subgraphs()[0].graph_attr['label'])
81 
83  fac = PygraphvizFactory()
84  g = fac.get_graph()
85  fac.add_subgraph_to_graph(g, 'graph')
86  self.assertEqual(1, len(g.subgraphs()))
87  self.assertEqual('cluster_graph', g.subgraphs()[0].get_name())
88  self.assertEqual('graph', g.subgraphs()[0].graph_attr['label'])
89 
90  def test_create_dot(self):
91  fac = PygraphvizFactory()
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, 'graph')
97  snippets = ['strict digraph { graph',
98  'foo',
99  'label=foo',
100  '"edge"',
101  'label="edge"',
102  'foo -> "edge"']
103  result = fac.create_dot(g)
104  # get rid of version specific quotes / whitespaces
105  result = re.sub('""', ' ', result)
106  result = re.sub('[\n\t ]+', ' ', result)
107  for sn in snippets:
108  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