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 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,
00126 False
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,
00149 True
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,
00173 True
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))
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)