11# -*- coding: utf-8 -*-
2- from .helper import check_evaluation
2+ import tempfile
3+ import os
4+ import os .path as osp
5+ import sys
6+ from .helper import check_evaluation , session
37
48
59def test_import ():
@@ -12,3 +16,49 @@ def test_import():
1216 ),
1317 ):
1418 check_evaluation (str_expr , str_expected , message )
19+
20+ def run_export (temp_dirname : str , short_name : str , file_data :str , character_encoding ):
21+ file_path = osp .join (temp_dirname , short_name )
22+ expr = fr'Export["{ file_path } ", { file_data } '
23+ expr += ', CharacterEncoding -> "{character_encoding}"' if character_encoding else ""
24+ expr += "]"
25+ result = session .evaluate (expr )
26+ assert result .to_python (string_quotes = False ) == file_path
27+ return file_path
28+
29+ def check_data (temp_dirname : str , short_name : str , file_data :str ,
30+ character_encoding = None , expected_data = None ):
31+ file_path = run_export (temp_dirname , short_name , fr'"{ file_data } "' , character_encoding )
32+ if expected_data is None :
33+ expected_data = file_data
34+ assert open (file_path , "r" ).read () == expected_data
35+
36+
37+ # Github Action Windows CI servers have problems with releasing files using
38+ # a tempfile.TemporaryDirectory context manager.
39+ # Leave out until we figure how to work around this.
40+ if not (os .environ .get ("CI" , False ) or sys .platform in ("win32" ,)):
41+ def test_export ():
42+ with tempfile .TemporaryDirectory (prefix = "mtest-" ) as temp_dirname :
43+ # Check exporting text files (file extension ".txt")
44+ check_data (temp_dirname , "add_expr.txt" , "1 + x + y" )
45+ check_data (temp_dirname , "AAcute.txt" , "\u00C1 " , "ISOLatin1" )
46+ check_data (temp_dirname , "AAcuteUTF.txt" , "\u00C1 " , "UTF-8" )
47+
48+ # Check exporting CSV files (file extension ".csv")
49+ file_path = run_export (temp_dirname , "csv_list.csv" , "{{1, 2, 3}, {4, 5, 6}}" , None )
50+ assert open (file_path , "r" ).read () == "1,2,3\n 4,5,6"
51+
52+ # Check exporting SVG files (file extension ".svg")
53+ file_path = run_export (temp_dirname , "sine.svg" , "Plot[Sin[x], {x,0,1}]" , None )
54+ data = open (file_path , "r" ).read ().strip ()
55+ if not os .environ .get ("CI" , None ):
56+ assert data .startswith ("<svg" )
57+ assert data .endswith ("</svg>" )
58+
59+ # TODO:
60+ # mmatera: please put in pytest conditionally
61+ # >> System`Convert`B64Dump`B64Encode["∫ f x"]
62+ # = 4oirIGYg752MIHg=
63+ # >> System`Convert`B64Dump`B64Decode[%]
64+ # = ∫ f x
0 commit comments