|
2 | 2 | Test the C API functions related to virtual files.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import io |
5 | 6 | from importlib.util import find_spec
|
6 | 7 | from itertools import product
|
7 | 8 | from pathlib import Path
|
@@ -407,3 +408,69 @@ def test_inquire_virtualfile():
|
407 | 408 | ]:
|
408 | 409 | with lib.open_virtualfile(family, geometry, "GMT_OUT", None) as vfile:
|
409 | 410 | assert lib.inquire_virtualfile(vfile) == lib[family]
|
| 411 | + |
| 412 | + |
| 413 | +class TestVirtualfileFromStringIO: |
| 414 | + """ |
| 415 | + Test the virtualfile_from_stringio method. |
| 416 | + """ |
| 417 | + |
| 418 | + def _check_virtualfile_from_stringio(self, data: str): |
| 419 | + """ |
| 420 | + A helper function to check the output of the virtualfile_from_stringio method. |
| 421 | + """ |
| 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 |
| 434 | + |
| 435 | + def test_virtualfile_from_stringio(self): |
| 436 | + """ |
| 437 | + Test the virtualfile_from_stringio method. |
| 438 | + """ |
| 439 | + data = ( |
| 440 | + "# Comment\n" |
| 441 | + "H 24p Legend\n" |
| 442 | + "N 2\n" |
| 443 | + "S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n" |
| 444 | + ) |
| 445 | + self._check_virtualfile_from_stringio(data) |
| 446 | + |
| 447 | + def test_one_segment(self): |
| 448 | + """ |
| 449 | + Test the virtualfile_from_stringio method with one segment. |
| 450 | + """ |
| 451 | + data = ( |
| 452 | + "# Comment\n" |
| 453 | + "> 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" |
| 457 | + ) |
| 458 | + self._check_virtualfile_from_stringio(data) |
| 459 | + |
| 460 | + def test_multiple_segments(self): |
| 461 | + """ |
| 462 | + Test the virtualfile_from_stringio method with multiple segments. |
| 463 | + """ |
| 464 | + data = ( |
| 465 | + "# Comment line 1\n" |
| 466 | + "# Comment line 2\n" |
| 467 | + "> 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" |
| 471 | + "> 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" |
| 475 | + ) |
| 476 | + self._check_virtualfile_from_stringio(data) |
0 commit comments