@@ -266,56 +266,84 @@ def test_grid_make_inactive_to_side(basic_grid):
266266 assert 0 == target_line_after .to_status
267267
268268
269- def test_from_txt_file (tmp_path ):
270- """Test that Grid can be created from txt file"""
271- txt_file = tmp_path / "tmp_grid"
272- txt_file .write_text ("S1 2\n S1 3 open\n 2 7\n 3 5\n 3 6 transformer\n 5 7\n 7 8\n 8 9" , encoding = "utf-8" )
273- grid = Grid .from_txt_file (txt_file )
274- txt_file .unlink ()
275-
276- assert 8 == grid .node .size
277- assert 1 == grid .branches .filter (to_status = 0 ).size
278- assert 1 == grid .transformer .size
279- np .testing .assert_array_equal ([14 , 10 , 11 , 12 , 13 , 15 , 16 , 17 ], grid .branches .id )
280-
281-
282- def test_from_txt_file_with_branch_ids (tmp_path ):
283- txt_file = tmp_path / "tmp_grid"
284- txt_file .write_text (
285- "S1 2 91\n S1 3 92,open\n 2 7 93\n 3 5 94\n 3 6 transformer,95\n 5 7 96\n 7 8 97\n 8 9 98" , encoding = "utf-8"
286- )
287- grid = Grid .from_txt_file (txt_file )
288- txt_file .unlink ()
289-
290- assert 8 == grid .node .size
291- assert 1 == grid .branches .filter (to_status = 0 ).size
292- assert 1 == grid .transformer .size
293- np .testing .assert_array_equal ([95 , 91 , 92 , 93 , 94 , 96 , 97 , 98 ], grid .branches .id )
294-
295-
296- def test_from_txt_file_conflicting_ids (tmp_path ):
297- txt_file = tmp_path / "tmp_grid"
298- txt_file .write_text ("S1 2\n 1 3" , encoding = "utf-8" )
299-
300- with pytest .raises (ValueError ):
301- Grid .from_txt_file (txt_file )
302-
303- txt_file .unlink ()
304-
305-
306- def test_from_txt_file_with_unordered_node_ids (tmp_path ):
307- txt_file = tmp_path / "tmp_grid"
308- txt_file .write_text ("S1 2\n S1 10\n 10 11\n 2 5\n 5 6\n 3 4\n 3 7" , encoding = "utf-8" )
309- grid = Grid .from_txt_file (txt_file )
310- txt_file .unlink ()
311-
312- assert 9 == grid .node .size
313-
314-
315269def test_grid_as_str (basic_grid ):
316270 grid = basic_grid
317271
318272 grid_as_string = str (grid )
319273
320274 assert "102 106 301,transformer" in grid_as_string
321275 assert "103 104 203,open" in grid_as_string
276+
277+
278+ class TestFromTxt :
279+ def test_from_txt_lines (self ):
280+ grid = Grid .from_txt (
281+ "S1 2" ,
282+ "S1 3 open" ,
283+ "2 7" ,
284+ "3 5" ,
285+ "3 6 transformer" ,
286+ "5 7" ,
287+ "7 8" ,
288+ "8 9" ,
289+ )
290+ assert 8 == grid .node .size
291+ assert 1 == grid .branches .filter (to_status = 0 ).size
292+ assert 1 == grid .transformer .size
293+ np .testing .assert_array_equal ([14 , 10 , 11 , 12 , 13 , 15 , 16 , 17 ], grid .branches .id )
294+
295+ def test_from_txt_string (self ):
296+ txt_string = "S1 2\n S1 3 open\n 2 7\n 3 5\n 3 6 transformer\n 5 7\n 7 8\n 8 9"
297+ assert Grid .from_txt (txt_string )
298+
299+ def test_from_txt_string_with_spaces (self ):
300+ txt_string = "S1 2 \n S1 3 open\n 2 7\n 3 5\n 3 6 transformer\n 5 7\n 7 8\n 8 9"
301+ assert Grid .from_txt (txt_string )
302+
303+ def test_from_docstring (self ):
304+ assert Grid .from_txt ("""
305+ S1 2
306+ S1 3 open
307+ 2 7
308+ 3 5
309+ 3 6 transformer
310+ 5 7
311+ 7 8
312+ 8 9
313+ """ )
314+
315+ def test_from_txt_with_branch_ids (self ):
316+ grid = Grid .from_txt (
317+ "S1 2 91" , "S1 3 92,open" , "2 7 93" , "3 5 94" , "3 6 transformer,95" , "5 7 96" , "7 8 97" , "8 9 98"
318+ )
319+ assert 8 == grid .node .size
320+ assert 1 == grid .branches .filter (to_status = 0 ).size
321+ assert 1 == grid .transformer .size
322+ np .testing .assert_array_equal ([95 , 91 , 92 , 93 , 94 , 96 , 97 , 98 ], grid .branches .id )
323+
324+ def test_from_txt_with_conflicting_ids (self ):
325+ with pytest .raises (ValueError ):
326+ Grid .from_txt ("S1 2" , "1 3" )
327+
328+ def test_from_txt_with_invalid_line (self ):
329+ with pytest .raises (ValueError ):
330+ Grid .from_txt ("S1" )
331+
332+ def test_from_txt_with_unordered_node_ids (self ):
333+ grid = Grid .from_txt ("S1 2" , "S1 10" , "10 11" , "2 5" , "5 6" , "3 4" , "3 7" )
334+ assert 9 == grid .node .size
335+
336+ def test_from_txt_with_unordered_branch_ids (self ):
337+ grid = Grid .from_txt ("5 6 16" , "3 4 17" , "3 7 18" , "S1 2 12" , "S1 10 13" , "10 11 14" , "2 5 15" )
338+ assert 9 == grid .node .size
339+
340+ def test_from_txt_file (self , tmp_path ):
341+ txt_file = tmp_path / "tmp_grid"
342+ txt_file .write_text ("S1 2\n S1 3 open\n 2 7\n 3 5\n 3 6 transformer\n 5 7\n 7 8\n 8 9" , encoding = "utf-8" )
343+ grid = Grid .from_txt_file (txt_file )
344+ txt_file .unlink ()
345+
346+ assert 8 == grid .node .size
347+ assert 1 == grid .branches .filter (to_status = 0 ).size
348+ assert 1 == grid .transformer .size
349+ np .testing .assert_array_equal ([14 , 10 , 11 , 12 , 13 , 15 , 16 , 17 ], grid .branches .id )
0 commit comments