@@ -89,6 +89,93 @@ describe("Graph", function() {
8989 } ) ;
9090 } ) ;
9191
92+ describe ( "filterNodes" , function ( ) {
93+ it ( "returns an identical graph when the filter selects everything" , function ( ) {
94+ g . setGraph ( "graph label" ) ;
95+ g . setNode ( "a" , 123 ) ;
96+ g . setPath ( [ "a" , "b" , "c" ] ) ;
97+ g . setEdge ( "a" , "c" , 456 ) ;
98+ var g2 = g . filterNodes ( function ( ) { return true ; } ) ;
99+ expect ( _ . sortBy ( g2 . nodes ( ) ) ) . eqls ( [ "a" , "b" , "c" ] ) ;
100+ expect ( _ . sortBy ( g2 . successors ( "a" ) ) ) . eqls ( [ "b" , "c" ] ) ;
101+ expect ( _ . sortBy ( g2 . successors ( "b" ) ) ) . eqls ( [ "c" ] ) ;
102+ expect ( g2 . node ( "a" ) ) . eqls ( 123 ) ;
103+ expect ( g2 . edge ( "a" , "c" ) ) . eqls ( 456 ) ;
104+ expect ( g2 . graph ( ) ) . eqls ( "graph label" ) ;
105+ } ) ;
106+
107+ it ( "returns an empty graph when the filter selects nothing" , function ( ) {
108+ g . setPath ( [ "a" , "b" , "c" ] ) ;
109+ var g2 = g . filterNodes ( function ( ) { return false ; } ) ;
110+ expect ( g2 . nodes ( ) ) . eqls ( [ ] ) ;
111+ expect ( g2 . edges ( ) ) . eqls ( [ ] ) ;
112+ } ) ;
113+
114+ it ( "only includes nodes for which the filter returns true" , function ( ) {
115+ g . setNodes ( [ "a" , "b" ] ) ;
116+ var g2 = g . filterNodes ( function ( v ) { return v === "a" ; } ) ;
117+ expect ( g2 . nodes ( ) ) . eqls ( [ "a" ] ) ;
118+ } ) ;
119+
120+ it ( "removes edges that are connected to removed nodes" , function ( ) {
121+ g . setEdge ( "a" , "b" ) ;
122+ var g2 = g . filterNodes ( function ( v ) { return v === "a" ; } ) ;
123+ expect ( _ . sortBy ( g2 . nodes ( ) ) ) . eqls ( [ "a" ] ) ;
124+ expect ( g2 . edges ( ) ) . eqls ( [ ] ) ;
125+ } ) ;
126+
127+ it ( "preserves the directed option" , function ( ) {
128+ g = new Graph ( { directed : true } ) ;
129+ expect ( g . filterNodes ( function ( ) { return true ; } ) . isDirected ( ) ) . to . be . true ;
130+
131+ g = new Graph ( { directed : false } ) ;
132+ expect ( g . filterNodes ( function ( ) { return true ; } ) . isDirected ( ) ) . to . be . false ;
133+ } ) ;
134+
135+ it ( "preserves the multigraph option" , function ( ) {
136+ g = new Graph ( { multigraph : true } ) ;
137+ expect ( g . filterNodes ( function ( ) { return true ; } ) . isMultigraph ( ) ) . to . be . true ;
138+
139+ g = new Graph ( { multigraph : false } ) ;
140+ expect ( g . filterNodes ( function ( ) { return true ; } ) . isMultigraph ( ) ) . to . be . false ;
141+ } ) ;
142+
143+ it ( "preserves the compound option" , function ( ) {
144+ g = new Graph ( { compound : true } ) ;
145+ expect ( g . filterNodes ( function ( ) { return true ; } ) . isCompound ( ) ) . to . be . true ;
146+
147+ g = new Graph ( { compound : false } ) ;
148+ expect ( g . filterNodes ( function ( ) { return true ; } ) . isCompound ( ) ) . to . be . false ;
149+ } ) ;
150+
151+ it ( "includes subgraphs" , function ( ) {
152+ g = new Graph ( { compound : true } ) ;
153+ g . setParent ( "a" , "parent" ) ;
154+
155+ var g2 = g . filterNodes ( function ( ) { return true ; } ) ;
156+ expect ( g2 . parent ( "a" ) ) . eqls ( "parent" ) ;
157+ } ) ;
158+
159+ it ( "includes multi-level subgraphs" , function ( ) {
160+ g = new Graph ( { compound : true } ) ;
161+ g . setParent ( "a" , "parent" ) ;
162+ g . setParent ( "parent" , "root" ) ;
163+
164+ var g2 = g . filterNodes ( function ( ) { return true ; } ) ;
165+ expect ( g2 . parent ( "a" ) ) . eqls ( "parent" ) ;
166+ expect ( g2 . parent ( "parent" ) ) . eqls ( "root" ) ;
167+ } ) ;
168+
169+ it ( "promotes a node to a higher subgraph if its parent is not included" , function ( ) {
170+ g = new Graph ( { compound : true } ) ;
171+ g . setParent ( "a" , "parent" ) ;
172+ g . setParent ( "parent" , "root" ) ;
173+
174+ var g2 = g . filterNodes ( function ( v ) { return v !== "parent" ; } ) ;
175+ expect ( g2 . parent ( "a" ) ) . eqls ( "root" ) ;
176+ } ) ;
177+ } ) ;
178+
92179 describe ( "setNodes" , function ( ) {
93180 it ( "creates multiple nodes" , function ( ) {
94181 g . setNodes ( [ "a" , "b" , "c" ] ) ;
0 commit comments