@@ -3,14 +3,14 @@ import { Graph } from '../Graph.js';
33import { hasCycle } from './hasCycle.js' ;
44
55describe ( 'hasCycle' , ( ) => {
6- it ( 'Should detect cycle.' , function ( ) {
6+ it ( 'should detect cycle.' , function ( ) {
77 const graph = new Graph ( ) ;
88 graph . addEdge ( 'a' , 'b' ) ;
99 graph . addEdge ( 'b' , 'a' ) ;
1010 expect ( hasCycle ( graph ) ) . toBe ( true ) ;
1111 } ) ;
1212
13- it ( 'Should detect cycle (long).' , function ( ) {
13+ it ( 'should detect cycle (long).' , function ( ) {
1414 const graph = new Graph ( ) ;
1515 graph . addEdge ( 'a' , 'b' ) ;
1616 graph . addEdge ( 'b' , 'c' ) ;
@@ -19,15 +19,42 @@ describe('hasCycle', () => {
1919 expect ( hasCycle ( graph ) ) . toBe ( true ) ;
2020 } ) ;
2121
22- it ( 'Should detect cycle (loop).' , function ( ) {
22+ it ( 'should detect cycle (loop).' , function ( ) {
2323 const graph = new Graph ( ) ;
2424 graph . addEdge ( 'a' , 'a' ) ;
2525 expect ( hasCycle ( graph ) ) . toBe ( true ) ;
2626 } ) ;
2727
28- it ( 'Should not detect cycle.' , function ( ) {
28+ it ( 'should not detect cycle.' , function ( ) {
2929 const graph = new Graph ( ) ;
3030 graph . addEdge ( 'a' , 'b' ) ;
3131 expect ( hasCycle ( graph ) ) . toBe ( false ) ;
3232 } ) ;
33+
34+ it ( 'should ignore the cycle in another sub-graph.' , function ( ) {
35+ const graph = new Graph ( ) ;
36+ graph . addEdge ( 'a' , 'b' ) ;
37+ graph . addEdge ( 'b' , 'c' ) ;
38+ graph . addEdge ( 'c' , 'd' ) ;
39+ graph . addEdge ( 'd' , 'a' ) ;
40+
41+ graph . addEdge ( 'm' , 'n' ) ;
42+
43+ expect ( hasCycle ( graph , { sourceNodes : [ 'm' ] } ) ) . toBe ( false ) ;
44+ } ) ;
45+
46+ it ( 'should not detect the cycle when the traversing is stopped by the shouldFollow option.' , function ( ) {
47+ const graph = new Graph < string , string > ( ) ;
48+ graph . addEdge ( 'a' , 'b' , undefined , 'foo' ) ;
49+ graph . addEdge ( 'b' , 'c' , undefined , 'foo' ) ;
50+ graph . addEdge ( 'c' , 'd' , undefined , 'foo' ) ;
51+ graph . addEdge ( 'd' , 'a' , undefined , 'bar' ) ;
52+
53+ expect (
54+ hasCycle ( graph , {
55+ shouldFollow : ( source , target ) =>
56+ graph . getEdgeProperties ( source , target ) === 'foo' ,
57+ } ) ,
58+ ) . toBe ( false ) ;
59+ } ) ;
3360} ) ;
0 commit comments