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


rqt_graph
Author(s): Dirk Thomas
autogenerated on Fri Jan 3 2014 11:54:23