|
15 | 15 | import pygraphviz as pgv |
16 | 16 | import pygraphml as pgml |
17 | 17 |
|
18 | | -from subprocess import Popen,PIPE |
| 18 | +from subprocess import Popen,PIPE,call |
19 | 19 | from os.path import abspath |
20 | 20 |
|
21 | 21 | class GardenerTestCases(unittest.TestCase): |
@@ -185,30 +185,113 @@ def test_SimpleCallWithSinglePath( self ): |
185 | 185 | graph_str2 = pipe.communicate()[0].decode("utf-8") |
186 | 186 | self.assertEqual( graph_str1, graph_str2 ) |
187 | 187 |
|
188 | | - |
189 | | - def test_SimpleCallWithSinglePath_GraphmlOutput( self ): |
190 | | - """ Tests "include_gardener test_files -f xml -I test_files/inc" |
191 | | -
|
192 | | - The test expects that the result can be read by graphml |
193 | | - and that there is at least one node. |
| 188 | + def graphml_gardener_call( self, options ): |
| 189 | + """ |
| 190 | + Runs the include_gardener with the xml option, |
| 191 | + extracts the result and returns the graph. |
194 | 192 | """ |
195 | 193 | pipe = Popen( [ self.G_PATH, self.T_PATH, |
196 | | - '-f', 'xml', '-I', self.T_PATH + '/inc/' ], stdout=PIPE ) |
| 194 | + '-f', 'xml' ] + options, stdout=PIPE ) |
197 | 195 | graphml_str = pipe.communicate()[0] |
198 | 196 | temp = tempfile.NamedTemporaryFile() |
199 | 197 | temp.write( graphml_str ) |
200 | 198 | temp.flush() |
201 | 199 | parser = pgml.GraphMLParser() |
202 | 200 |
|
203 | 201 | # get the result from the system call: |
204 | | - g1 = parser.parse( temp.name ) |
| 202 | + return parser.parse( temp.name ) |
| 203 | + |
| 204 | + |
| 205 | + def test_SimpleCallWithSinglePath_GraphmlOutput( self ): |
| 206 | + """ Tests "include_gardener test_files -f xml -I test_files/inc" |
| 207 | +
|
| 208 | + The test expects that the result can be read by graphml |
| 209 | + and that there is at least one node. |
| 210 | + """ |
| 211 | + g1 = self.graphml_gardener_call( [ '-I', self.T_PATH + '/inc/' ] ) |
205 | 212 |
|
206 | 213 | # get a reference graph |
207 | 214 | g2 = self.build_reference_graph() |
208 | 215 |
|
209 | 216 | # both graphs shall be the same |
210 | 217 | self.compare( g1, g2 ) |
211 | 218 |
|
| 219 | + |
| 220 | + def test_LevelOption( self ): |
| 221 | + """ Tests "include_gardener test_files -f xml" once without -L, |
| 222 | + once with -L 0, once with -L 1 and once with -L 2. |
| 223 | +
|
| 224 | + The test expects that -L 0 counts no nodes, -L 1 counts just the nodes of |
| 225 | + the result of processing the files in src, and L 2 the same than wihtout |
| 226 | + the -L option. |
| 227 | + """ |
| 228 | + g1 = self.graphml_gardener_call( [ ] ) |
| 229 | + g2 = self.graphml_gardener_call( [ '-L', '0' ] ) |
| 230 | + g3 = self.graphml_gardener_call( [ '-L', '1' ] ) |
| 231 | + g4 = self.graphml_gardener_call( [ '-L', '2' ] ) |
| 232 | + |
| 233 | + self.assertEqual( len( g2.nodes() ), 0 ) |
| 234 | + self.assertEqual( len( g3.nodes() ), 8 ) |
| 235 | + self.compare( g1, g4 ) |
| 236 | + |
| 237 | + |
| 238 | + def test_ThreadOption( self ): |
| 239 | + """ Tests "include_gardener test_files -f xml" with -j ranging |
| 240 | + from 1 to 5, each 10 times. Each call should produce the same |
| 241 | + result. |
| 242 | + """ |
| 243 | + |
| 244 | + g1 = None |
| 245 | + for j in range( 1, 5 ): |
| 246 | + for cnt in range( 0, 10 ): |
| 247 | + if not g1: |
| 248 | + # initialize reference |
| 249 | + g1 = self.graphml_gardener_call( [ '-I', self.T_PATH + '/inc/', '-j', str(j) ] ) |
| 250 | + else: |
| 251 | + g2 = self.graphml_gardener_call( [ '-I', self.T_PATH + '/inc/', '-j', str(j) ] ) |
| 252 | + self.compare( g1, g2 ) |
| 253 | + |
| 254 | + |
| 255 | + def test_ThreadNullOption( self ): |
| 256 | + """ Tests "include_gardener test_files -f xml -j 0" which should fail. |
| 257 | + """ |
| 258 | + pipe = Popen( [ self.G_PATH, self.T_PATH, |
| 259 | + '-f', 'xml', '-j', '0' ], stderr=PIPE ) |
| 260 | + graphml_str = pipe.communicate()[1].decode("utf-8") |
| 261 | + self.assertNotEqual( len( graphml_str ), 0 ) |
| 262 | + self.assertIn( "Error", graphml_str ) |
| 263 | + |
| 264 | + |
| 265 | + def test_ExcludeOption( self ): |
| 266 | + """ Tests "include_gardener test_files -f xml" |
| 267 | + once without the -e option, once with -e \.x and once with -e \.x -e \y. |
| 268 | +
|
| 269 | + The test expects that the first includes all files, |
| 270 | + the second call one file less and the third two files less. |
| 271 | + """ |
| 272 | + g1 = self.graphml_gardener_call( [ ] ) |
| 273 | + g2 = self.graphml_gardener_call( [ '-e', '\.x' ] ) |
| 274 | + g3 = self.graphml_gardener_call( [ '-e', '\.x', '-e', '\.y'] ) |
| 275 | + |
| 276 | + self.assertEqual( len( g1.nodes() ), len( g2.nodes() )+1 ) |
| 277 | + self.assertEqual( len( g1.nodes() ), len( g3.nodes() )+2 ) |
| 278 | + |
| 279 | + |
| 280 | + def test_OutputFile( self ): |
| 281 | + """ Tests if the gardener can write into an output file. |
| 282 | +
|
| 283 | + The test expects that there is at least one node. |
| 284 | + """ |
| 285 | + temp = tempfile.NamedTemporaryFile() |
| 286 | + call( [ self.G_PATH, self.T_PATH, |
| 287 | + '-f', 'xml', '-o', temp.name ] ) |
| 288 | + parser = pgml.GraphMLParser() |
| 289 | + |
| 290 | + # get the result from the system call: |
| 291 | + g = parser.parse( temp.name ) |
| 292 | + |
| 293 | + self.assertNotEqual( len( g.nodes() ), 0 ) |
| 294 | + |
212 | 295 | if __name__ == "__main__": |
213 | 296 | if len( sys.argv ) > 2: |
214 | 297 | GardenerTestCases.T_PATH = abspath( sys.argv.pop() ) |
|
0 commit comments