@@ -228,6 +228,52 @@ def test_ancestors(self):
228228 self .assertCountEqual (graph .ancestors (c ), [c , a ])
229229 self .assertCountEqual (graph .ancestors (d ), [d , b , c , a ])
230230
231+ def test_shortest_path (self ):
232+ # Looking for paths from a to f, we have:
233+ # a -> b -> e -> f
234+ # a -> c -> f (shortest)
235+ a = []
236+ b = []
237+ c = []
238+ d = []
239+ e = []
240+ f = []
241+ a .append (b )
242+ a .append (c )
243+ a .append (d )
244+ b .append (e )
245+ e .append (f )
246+ c .append (f )
247+ graph = ObjectGraph ([a , b , c , d , e , f ])
248+ path = graph .shortest_path (a , f )
249+ self .assertIsInstance (path , IDirectedGraph )
250+ self .assertEqual (len (path .vertices ), 3 )
251+
252+ self .assertIn (a , path .vertices )
253+ self .assertIn (c , path .vertices )
254+ self .assertIn (f , path .vertices )
255+
256+ def test_shortest_path_no_path (self ):
257+ a = []
258+ b = []
259+ c = []
260+ a .append (b )
261+ c .append (b )
262+ graph = ObjectGraph ([a , b , c ])
263+ with self .assertRaises (ValueError ):
264+ graph .shortest_path (a , c )
265+
266+ def test_shortest_path_start_equals_end (self ):
267+ a = []
268+ b = []
269+ a .append (b )
270+ b .append (a )
271+ a .append (a )
272+ graph = ObjectGraph ([a ])
273+ path = graph .shortest_path (a , a )
274+ self .assertIsInstance (path , IDirectedGraph )
275+ self .assertEqual (len (path .vertices ), 1 )
276+
231277 def test_to_dot (self ):
232278 a = []
233279 b = []
0 commit comments