00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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,
00128 False
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,
00151 True
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,
00175 True
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))
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)