Skip to content

Commit 850337e

Browse files
committed
Add some tests
1 parent 026f6e4 commit 850337e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

pygmt/tests/test_clib_virtualfiles.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test the C API functions related to virtual files.
33
"""
44

5+
import io
56
from importlib.util import find_spec
67
from itertools import product
78
from pathlib import Path
@@ -407,3 +408,69 @@ def test_inquire_virtualfile():
407408
]:
408409
with lib.open_virtualfile(family, geometry, "GMT_OUT", None) as vfile:
409410
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

Comments
 (0)