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