dotcode_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 import rospkg
00036 
00037 import os
00038 from rqt_graph.dotcode import RosGraphDotcodeGenerator
00039 
00040 PKG='rqt_graph'
00041 
00042 class DotcodeTest(unittest.TestCase):
00043 
00044     def test_split_filter_empty(self):
00045         gen = RosGraphDotcodeGenerator()
00046         inc, exc = gen._split_filter_string('')
00047         self.assertEqual(['.*'], inc)
00048         self.assertEqual(0, len(exc))
00049         inc, exc = gen._split_filter_string('/')
00050         self.assertEqual(['.*'], inc)
00051         self.assertEqual(0, len(exc))
00052 
00053 
00054     def test_split_filter_includes(self):
00055         gen = RosGraphDotcodeGenerator()
00056         inc, exc = gen._split_filter_string('foo')
00057         self.assertEqual(['foo'], inc)
00058         self.assertEqual(0, len(exc))
00059         inc, exc = gen._split_filter_string('foo,bar')
00060         self.assertEqual(['foo', 'bar'], inc)
00061         self.assertEqual(0, len(exc))
00062 
00063     def test_split_filter_excludes(self):
00064         gen = RosGraphDotcodeGenerator()
00065         inc, exc = gen._split_filter_string('-foo')
00066         self.assertEqual(['.*'], inc)
00067         self.assertEqual(['foo'], exc)
00068         inc, exc = gen._split_filter_string('-foo,-bar')
00069         self.assertEqual(['.*'], inc)
00070         self.assertEqual(['foo', 'bar'], exc)
00071 
00072     def test_split_filter_both(self):
00073         gen = RosGraphDotcodeGenerator()
00074         inc, exc = gen._split_filter_string('-foo , bar ,baz, -bam')
00075         self.assertEqual(['bar', 'baz'], inc)
00076         self.assertEqual(['foo', 'bam'], exc)
00077 
00078     class MockEdge():
00079         def __init__(self, start, end):
00080             self.start = start
00081             self.end = end
00082 
00083     def test_get_node_edge_map(self):
00084         gen = RosGraphDotcodeGenerator()
00085         e1 = self.MockEdge('foo', 'bar')
00086         nmap = gen._get_node_edge_map([e1])
00087         self.assertEqual(2, len(nmap))
00088         self.assertTrue('foo' in nmap)
00089         self.assertTrue('bar' in nmap)
00090         self.assertEqual([], nmap['foo'].incoming)
00091         self.assertEqual([e1], nmap['foo'].outgoing)
00092         self.assertEqual([e1], nmap['bar'].incoming)
00093         self.assertEqual([], nmap['bar'].outgoing)
00094 
00095     def test_get_node_edge_map(self):
00096         gen = RosGraphDotcodeGenerator()
00097         e1 = self.MockEdge('foo', 'bar')
00098         e2 = self.MockEdge('bar', 'foo')
00099         e3 = self.MockEdge('foo', 'pam')
00100         nmap = gen._get_node_edge_map([e1, e2, e3])
00101         self.assertEqual(3, len(nmap))
00102         self.assertTrue('foo' in nmap)
00103         self.assertTrue('bar' in nmap)
00104         self.assertTrue('pam' in nmap)
00105         self.assertEqual([e2], nmap['foo'].incoming)
00106         self.assertEqual([e1, e3], nmap['foo'].outgoing)
00107         self.assertEqual([e1], nmap['bar'].incoming)
00108         self.assertEqual([e2], nmap['bar'].outgoing)
00109 
00110     def test_filter_leaf_topics_single_connection(self):
00111         gen = RosGraphDotcodeGenerator()
00112         topic_nodes = ['foo', 'bar', 'pam', 'boo']
00113         e1 = self.MockEdge('n1', 'foo')
00114         e2 = self.MockEdge('n2', 'foo')
00115         e3 = self.MockEdge('n3', 'bar')
00116         e4 = self.MockEdge('bar', 'n4')
00117         e5 = self.MockEdge('n5', 'pam')
00118         e6 = self.MockEdge('boo', 'n6')
00119         edges = [e1, e2, e3, e4, e5, e6]
00120         node_connections = gen._get_node_edge_map(edges)
00121         print(node_connections)
00122         rnodes, redges = gen._filter_leaf_topics(topic_nodes,
00123                                 edges,
00124                                 node_connections,
00125                                 True, #hide_single_connection_topics,
00126                                 False #hide_dead_end_topics
00127                                 )
00128         self.assertEqual(['foo', 'bar'], rnodes)
00129         self.assertEqual([e1, e2, e3, e4], redges)
00130         self.assertEqual(['foo', 'bar', 'pam', 'boo'], topic_nodes)
00131         self.assertEqual([e1, e2, e3, e4, e5, e6], edges)
00132 
00133     def test_filter_leaf_topics_dead_end(self):
00134         gen = RosGraphDotcodeGenerator()
00135         topic_nodes = ['foo', 'bar', 'pam', 'boo']
00136         e1 = self.MockEdge('n1', 'foo')
00137         e2 = self.MockEdge('n2', 'foo')
00138         e3 = self.MockEdge('n3', 'bar')
00139         e4 = self.MockEdge('bar', 'n4')
00140         e5 = self.MockEdge('n5', 'pam')
00141         e6 = self.MockEdge('boo', 'n6')
00142         edges = [e1, e2, e3, e4, e5, e6]
00143         node_connections = gen._get_node_edge_map(edges)
00144         print(node_connections)
00145         rnodes, redges = gen._filter_leaf_topics(topic_nodes,
00146                                 edges,
00147                                 node_connections,
00148                                 False, #hide_single_connection_topics,
00149                                 True #hide_dead_end_topics
00150                                 )
00151         self.assertEqual(['bar', 'boo'], rnodes)
00152         self.assertEqual([e3, e4, e6], redges)
00153         self.assertEqual(['foo', 'bar', 'pam', 'boo'], topic_nodes)
00154         self.assertEqual([e1, e2, e3, e4, e5, e6], edges)
00155 
00156 
00157     def test_filter_leaf_topics_both(self):
00158         gen = RosGraphDotcodeGenerator()
00159         topic_nodes = ['foo', 'bar', 'pam', 'boo']
00160         e1 = self.MockEdge('n1', 'foo')
00161         e2 = self.MockEdge('n2', 'foo')
00162         e3 = self.MockEdge('n3', 'bar')
00163         e4 = self.MockEdge('bar', 'n4')
00164         e5 = self.MockEdge('n5', 'pam')
00165         e6 = self.MockEdge('boo', 'n6')
00166         edges = [e1, e2, e3, e4, e5, e6]
00167         node_connections = gen._get_node_edge_map(edges)
00168         print(node_connections)
00169         rnodes, redges = gen._filter_leaf_topics(topic_nodes,
00170                                 edges,
00171                                 node_connections,
00172                                 True, #hide_single_connection_topics,
00173                                 True #hide_dead_end_topics
00174                                 )
00175         self.assertEqual(['bar'], rnodes)
00176         self.assertEqual([e3, e4], redges)
00177         self.assertEqual(['foo', 'bar', 'pam', 'boo'], topic_nodes)
00178         self.assertEqual([e1, e2, e3, e4, e5, e6], edges)
00179 
00180     def test_accumulate_action_topics(self):
00181         gen = RosGraphDotcodeGenerator()
00182         topic_nodes = ['foo/feedback', 'foo/goal', 'foo/cancel', 'foo/result', 'foo/status', 'bar']
00183         e1 = self.MockEdge('n1', 'foo/feedback')
00184         e2 = self.MockEdge('n1', 'foo/goal')
00185         e3 = self.MockEdge('n1', 'foo/cancel')
00186         e4 = self.MockEdge('n1', 'foo/result')
00187         e5 = self.MockEdge('n1', 'foo/status')
00188         e6 = self.MockEdge('n2', 'bar')
00189         edges = [e1, e2, e3, e4, e5, e6]
00190         node_connections = gen._get_node_edge_map(edges)
00191         rnodes, redges, raction_nodes = gen._accumulate_action_topics(topic_nodes, edges, node_connections)
00192         self.assertEqual(1, len(rnodes)) # node 'bar'
00193         self.assertEqual(1, len(redges))
00194         self.assertEqual(1, len(raction_nodes))
00195         self.assertEqual(['foo/feedback', 'foo/goal', 'foo/cancel', 'foo/result', 'foo/status', 'bar'], topic_nodes)
00196         self.assertEqual([e1, e2, e3, e4, e5, e6], edges)
00197 
00198 
00199 if __name__ == '__main__':
00200     import rosunit
00201     rosunit.unitrun(PKG, 'dotcode_test', DotcodeTest)


rqt_graph
Author(s): Dirk Thomas
autogenerated on Mon Oct 6 2014 07:15:04