diff --git a/mathics/builtin/files_io/importexport.py b/mathics/builtin/files_io/importexport.py index 8f70ca7f3..176d34dbd 100644 --- a/mathics/builtin/files_io/importexport.py +++ b/mathics/builtin/files_io/importexport.py @@ -1092,7 +1092,7 @@ class RegisterImport(Builtin): def apply(self, formatname, function, posts, evaluation, options): """ImportExport`RegisterImport[formatname_String, function_, posts_, - OptionsPattern[ImportExport`RegisterImport]]""" + OptionsPattern[ImportExport`RegisterImport]]""" if function.has_form("List", None): leaves = function.get_leaves() @@ -1174,7 +1174,7 @@ class RegisterExport(Builtin): def apply(self, formatname, function, evaluation, options): """ImportExport`RegisterExport[formatname_String, function_, - OptionsPattern[ImportExport`RegisterExport]]""" + OptionsPattern[ImportExport`RegisterExport]]""" EXPORTERS[formatname.get_string_value()] = (function, options) return Symbol("Null") @@ -1704,41 +1704,6 @@ class Export(Builtin): ## FORMATS - ## Text - #> Export["abc.txt", 1 + x + y] - = abc.txt - #> FilePrint[%] - | 1 + x + y - #> DeleteFile[%%] - - #> Export["abc.txt", "ä", CharacterEncoding -> "ISOLatin1"]; - #> strm = OpenRead["abc.txt", BinaryFormat -> True]; - #> BinaryRead[strm] - = 195 - #> Close[strm]; - #> DeleteFile["abc.txt"]; - - #> Export["abc.txt", "ä", CharacterEncoding -> "UTF-8"]; - #> strm = OpenRead["abc.txt", BinaryFormat -> True]; - #> BinaryRead[strm] - = 195 - #> Close[strm]; - #> DeleteFile["abc.txt"]; - - ## CSV - #> Export["abc.csv", {{1, 2, 3}, {4, 5, 6}}] - = abc.csv - #> FilePrint[%] - | 1,2,3 - | 4,5,6 - #> DeleteFile[%%] - - ## SVG - #> Export["sine.svg", Plot[Sin[x], {x,0,1}]] - = sine.svg - #> FileFormat[%] - = SVG - #> DeleteFile[%%] """ messages = { @@ -2193,12 +2158,6 @@ class B64Encode(Builtin): = Integrate[f[x], {x, 0, 2}] """ - # mmatera: please put in pytest conditionally - # >> System`Convert`B64Dump`B64Encode["∫ f  x"] - # = 4oirIGYg752MIHg= - # >> System`Convert`B64Dump`B64Decode[%] - # = ∫ f  x - context = "System`Convert`B64Dump`" name = "B64Encode" diff --git a/test/test_datentime.py b/test/test_datentime.py index a512282c0..b993473ae 100644 --- a/test/test_datentime.py +++ b/test/test_datentime.py @@ -54,7 +54,7 @@ def test_datelist(): check_evaluation(str_expr, str_expected) -def test_datelist(): +def test_datestring(): for str_expr, str_expected in ( ## Check Leading 0s # ( diff --git a/test/test_importexport.py b/test/test_importexport.py index 0c9c04e9e..4203c5d5c 100644 --- a/test/test_importexport.py +++ b/test/test_importexport.py @@ -1,5 +1,9 @@ # -*- coding: utf-8 -*- -from .helper import check_evaluation +import tempfile +import os +import os.path as osp +import sys +from .helper import check_evaluation, session def test_import(): @@ -12,3 +16,49 @@ def test_import(): ), ): check_evaluation(str_expr, str_expected, message) + +def run_export(temp_dirname: str, short_name: str, file_data:str, character_encoding): + file_path = osp.join(temp_dirname, short_name) + expr = fr'Export["{file_path}", {file_data}' + expr += ', CharacterEncoding -> "{character_encoding}"' if character_encoding else "" + expr += "]" + result = session.evaluate(expr) + assert result.to_python(string_quotes=False) == file_path + return file_path + +def check_data(temp_dirname: str, short_name: str, file_data:str, + character_encoding=None, expected_data=None): + file_path = run_export(temp_dirname, short_name, fr'"{file_data}"', character_encoding) + if expected_data is None: + expected_data = file_data + assert open(file_path, "r").read() == expected_data + + +# Github Action Windows CI servers have problems with releasing files using +# a tempfile.TemporaryDirectory context manager. +# Leave out until we figure how to work around this. +if not (os.environ.get("CI", False) or sys.platform in ("win32",)): + def test_export(): + with tempfile.TemporaryDirectory(prefix="mtest-") as temp_dirname: + # Check exporting text files (file extension ".txt") + check_data(temp_dirname, "add_expr.txt", "1 + x + y") + check_data(temp_dirname, "AAcute.txt", "\u00C1", "ISOLatin1") + check_data(temp_dirname, "AAcuteUTF.txt", "\u00C1", "UTF-8") + + # Check exporting CSV files (file extension ".csv") + file_path = run_export(temp_dirname, "csv_list.csv", "{{1, 2, 3}, {4, 5, 6}}", None) + assert open(file_path, "r").read() == "1,2,3\n4,5,6" + + # Check exporting SVG files (file extension ".svg") + file_path = run_export(temp_dirname, "sine.svg", "Plot[Sin[x], {x,0,1}]", None) + data = open(file_path, "r").read().strip() + if not os.environ.get("CI", None): + assert data.startswith("") + +# TODO: +# mmatera: please put in pytest conditionally +# >> System`Convert`B64Dump`B64Encode["∫ f  x"] +# = 4oirIGYg752MIHg= +# >> System`Convert`B64Dump`B64Decode[%] +# = ∫ f  x