@@ -415,62 +415,99 @@ class TestVirtualfileFromStringIO:
415
415
Test the virtualfile_from_stringio method.
416
416
"""
417
417
418
- def _check_virtualfile_from_stringio (self , data : str ):
418
+ def _stringio_to_dataset (self , data : io . StringIO ):
419
419
"""
420
- A helper function to check the output of the virtualfile_from_stringio method.
420
+ A helper function for check the virtualfile_from_stringio method.
421
+
422
+ The function does the following:
423
+
424
+ 1. Creates a virtual file from the input StringIO object.
425
+ 2. Pass the virtual file to the ``read`` module, which reads the virtual file
426
+ and writes it to another virtual file.
427
+ 3. Reads the output virtual file as a GMT_DATASET object.
428
+ 4. Extracts the header and the trailing text from the dataset and returns it as
429
+ a string.
421
430
"""
422
- # The expected output is the data with all comment lines removed.
423
- expected = (
424
- "\n " .join (line for line in data .splitlines () if not line .startswith ("#" ))
425
- + "\n "
426
- )
427
- stringio = io .StringIO (data )
428
- with GMTTempFile () as outfile :
429
- with clib .Session () as lib :
430
- with lib .virtualfile_from_stringio (stringio ) as vintbl :
431
- lib .call_module ("write" , args = [vintbl , f"->{ outfile .name } " , "-Td" ])
432
- output = outfile .read ()
433
- assert output == expected
431
+ with clib .Session () as lib :
432
+ with (
433
+ lib .virtualfile_from_stringio (data ) as vintbl ,
434
+ lib .virtualfile_out (kind = "dataset" ) as vouttbl ,
435
+ ):
436
+ lib .call_module ("read" , args = [vintbl , vouttbl , "-Td" ])
437
+ ds = lib .read_virtualfile (vouttbl , kind = "dataset" ).contents
438
+
439
+ output = []
440
+ table = ds .table [0 ].contents
441
+ for segment in table .segment [: table .n_segments ]:
442
+ seg = segment .contents
443
+ output .append (f"> { seg .header .decode ()} " if seg .header else ">" )
444
+ output .extend (np .char .decode (seg .text [: seg .n_rows ]))
445
+ return "\n " .join (output ) + "\n "
434
446
435
447
def test_virtualfile_from_stringio (self ):
436
448
"""
437
449
Test the virtualfile_from_stringio method.
438
450
"""
439
- data = (
451
+ data = io . StringIO (
440
452
"# Comment\n "
441
453
"H 24p Legend\n "
442
454
"N 2\n "
443
455
"S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n "
444
456
)
445
- self ._check_virtualfile_from_stringio (data )
457
+ expected = (
458
+ ">\n "
459
+ "H 24p Legend\n "
460
+ "N 2\n "
461
+ "S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n "
462
+ )
463
+ assert self ._stringio_to_dataset (data ) == expected
446
464
447
465
def test_one_segment (self ):
448
466
"""
449
467
Test the virtualfile_from_stringio method with one segment.
450
468
"""
451
- data = (
469
+ data = io . StringIO (
452
470
"# Comment\n "
453
471
"> Segment 1\n "
454
- "H 24p Legend\n "
455
- "N 2\n "
456
- "S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n "
472
+ "1 2 3 ABC\n "
473
+ "4 5 DE\n "
474
+ "6 7 8 9 FGHIJK LMN OPQ\n "
475
+ "RSTUVWXYZ\n "
457
476
)
458
- self ._check_virtualfile_from_stringio (data )
477
+ expected = (
478
+ "> Segment 1\n "
479
+ "1 2 3 ABC\n "
480
+ "4 5 DE\n "
481
+ "6 7 8 9 FGHIJK LMN OPQ\n "
482
+ "RSTUVWXYZ\n "
483
+ )
484
+ assert self ._stringio_to_dataset (data ) == expected
459
485
460
486
def test_multiple_segments (self ):
461
487
"""
462
488
Test the virtualfile_from_stringio method with multiple segments.
463
489
"""
464
- data = (
490
+ data = io . StringIO (
465
491
"# Comment line 1\n "
466
492
"# Comment line 2\n "
467
493
"> Segment 1\n "
468
- "H 24p Legend\n "
469
- "N 2\n "
470
- "S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n "
494
+ "1 2 3 ABC\n "
495
+ "4 5 DE\n "
496
+ "6 7 8 9 FG\n "
497
+ "# Comment line 3\n "
471
498
"> Segment 2\n "
472
- "H 24p Legend\n "
473
- "N 2\n "
474
- "S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n "
499
+ "1 2 3 ABC\n "
500
+ "4 5 DE\n "
501
+ "6 7 8 9 FG\n "
502
+ )
503
+ expected = (
504
+ "> Segment 1\n "
505
+ "1 2 3 ABC\n "
506
+ "4 5 DE\n "
507
+ "6 7 8 9 FG\n "
508
+ "> Segment 2\n "
509
+ "1 2 3 ABC\n "
510
+ "4 5 DE\n "
511
+ "6 7 8 9 FG\n "
475
512
)
476
- self ._check_virtualfile_from_stringio (data )
513
+ assert self ._stringio_to_dataset (data ) == expected
0 commit comments