From 08c59416f0140ba25ee6d54cfacc770921055260 Mon Sep 17 00:00:00 2001 From: Juan Mauricio Matera Date: Mon, 16 Oct 2023 21:26:30 -0300 Subject: [PATCH] Move private doctests to pytest16 (#929) Almost finishing.... --- .../builtin/directories/directory_names.py | 33 ----- .../file_operations/file_properties.py | 45 ------ .../builtin/file_operations/file_utilities.py | 8 +- test/builtin/test_directories.py | 64 ++++++++ test/builtin/test_evalution.py | 15 +- test/builtin/test_file_operations.py | 139 ++++++++++++++++++ 6 files changed, 217 insertions(+), 87 deletions(-) create mode 100644 test/builtin/test_directories.py create mode 100644 test/builtin/test_file_operations.py diff --git a/mathics/builtin/directories/directory_names.py b/mathics/builtin/directories/directory_names.py index 7724e1f58..21129c10a 100644 --- a/mathics/builtin/directories/directory_names.py +++ b/mathics/builtin/directories/directory_names.py @@ -30,23 +30,6 @@ class DirectoryName(Builtin): >> DirectoryName["a/b/c", 2] = a - - #> DirectoryName["a/b/c", 3] // InputForm - = "" - #> DirectoryName[""] // InputForm - = "" - - #> DirectoryName["a/b/c", x] - : Positive machine-sized integer expected at position 2 in DirectoryName[a/b/c, x]. - = DirectoryName[a/b/c, x] - - #> DirectoryName["a/b/c", -1] - : Positive machine-sized integer expected at position 2 in DirectoryName[a/b/c, -1]. - = DirectoryName[a/b/c, -1] - - #> DirectoryName[x] - : String expected at position 1 in DirectoryName[x]. - = DirectoryName[x] """ messages = { @@ -104,12 +87,6 @@ class DirectoryQ(Builtin): = True >> DirectoryQ["ExampleData/MythicalSubdir/"] = False - - #> DirectoryQ["ExampleData"] - = True - - #> DirectoryQ["ExampleData/MythicalSubdir/NestedDir/"] - = False """ messages = { @@ -150,12 +127,6 @@ class FileNameDepth(Builtin): >> FileNameDepth["a/b/c/"] = 3 - - #> FileNameDepth[x] - = FileNameDepth[x] - - #> FileNameDepth[$RootDirectory] - = 0 """ options = { @@ -254,10 +225,6 @@ class FileNameSplit(Builtin): >> FileNameSplit["example/path/file.txt"] = {example, path, file.txt} - - #> FileNameSplit["example/path", OperatingSystem -> x] - : The value of option OperatingSystem -> x must be one of "MacOSX", "Windows", or "Unix". - = {example, path} """ messages = { diff --git a/mathics/builtin/file_operations/file_properties.py b/mathics/builtin/file_operations/file_properties.py index 176221c2a..87f0e8725 100644 --- a/mathics/builtin/file_operations/file_properties.py +++ b/mathics/builtin/file_operations/file_properties.py @@ -48,17 +48,6 @@ class FileDate(Builtin): >> FileDate["ExampleData/sunflowers.jpg", "Rules"] = ... - - #> FileDate["MathicsNonExistantExample"] - : File not found during FileDate[MathicsNonExistantExample]. - = FileDate[MathicsNonExistantExample] - #> FileDate["MathicsNonExistantExample", "Modification"] - : File not found during FileDate[MathicsNonExistantExample, Modification]. - = FileDate[MathicsNonExistantExample, Modification] - - #> FileDate["ExampleData/sunflowers.jpg", "Fail"] - : Date type Fail should be "Access", "Modification", "Creation" (Windows only), "Change" (Macintosh and Unix only), or "Rules". - = FileDate[ExampleData/sunflowers.jpg, Fail] """ messages = { @@ -155,24 +144,6 @@ class FileHash(Builtin): >> FileHash["ExampleData/sunflowers.jpg", "SHA256"] = 111619807552579450300684600241129773909359865098672286468229443390003894913065 - - #> FileHash["ExampleData/sunflowers.jpg", "CRC32"] - = 933095683 - #> FileHash["ExampleData/sunflowers.jpg", "SHA"] - = 851696818771101405642332645949480848295550938123 - #> FileHash["ExampleData/sunflowers.jpg", "SHA224"] - = 8723805623766373862936267623913366865806344065103917676078120867011 - #> FileHash["ExampleData/sunflowers.jpg", "SHA384"] - = 28288410602533803613059815846847184383722061845493818218404754864571944356226472174056863474016709057507799332611860 - #> FileHash["ExampleData/sunflowers.jpg", "SHA512"] - = 10111462070211820348006107532340854103555369343736736045463376555356986226454343186097958657445421102793096729074874292511750542388324853755795387877480102 - - #> FileHash["ExampleData/sunflowers.jpg", xyzsymbol] - = FileHash[ExampleData/sunflowers.jpg, xyzsymbol] - #> FileHash["ExampleData/sunflowers.jpg", "xyzstr"] - = FileHash[ExampleData/sunflowers.jpg, xyzstr, Integer] - #> FileHash[xyzsymbol] - = FileHash[xyzsymbol] """ attributes = A_PROTECTED | A_READ_PROTECTED @@ -221,10 +192,6 @@ class FileType(Builtin): = Directory >> FileType["ExampleData/nonexistent"] = None - - #> FileType[x] - : File specification x is not a string of one or more characters. - = FileType[x] """ messages = { @@ -275,19 +242,7 @@ class SetFileDate(Builtin): >> FileDate[tmpfilename, "Access"] = {2002, 1, 1, 0, 0, 0.} - #> SetFileDate[tmpfilename, {2002, 1, 1, 0, 0, 0.}]; - #> FileDate[tmpfilename, "Access"] - = {2002, 1, 1, 0, 0, 0.} - - #> SetFileDate[tmpfilename] - #> FileDate[tmpfilename, "Access"] - = {...} - #> DeleteFile[tmpfilename] - - #> SetFileDate["MathicsNonExample"] - : File not found during SetFileDate[MathicsNonExample]. - = $Failed """ messages = { diff --git a/mathics/builtin/file_operations/file_utilities.py b/mathics/builtin/file_operations/file_utilities.py index ae63a0403..a341994f0 100644 --- a/mathics/builtin/file_operations/file_utilities.py +++ b/mathics/builtin/file_operations/file_utilities.py @@ -28,17 +28,11 @@ class FindList(Builtin): >> stream = FindList["ExampleData/EinsteinSzilLetter.txt", "uranium"]; - #> Length[stream] + >> Length[stream] = 7 >> FindList["ExampleData/EinsteinSzilLetter.txt", "uranium", 1] = {in manuscript, leads me to expect that the element uranium may be turned into} - - #> FindList["ExampleData/EinsteinSzilLetter.txt", "project"] - = {} - - #> FindList["ExampleData/EinsteinSzilLetter.txt", "uranium", 0] - = $Failed """ messages = { diff --git a/test/builtin/test_directories.py b/test/builtin/test_directories.py new file mode 100644 index 000000000..7dcc1a6eb --- /dev/null +++ b/test/builtin/test_directories.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +""" +Unit tests for mathics.builtin.directories +""" + +import sys +import time +from test.helper import check_evaluation, evaluate + +import pytest + + +@pytest.mark.parametrize( + ("str_expr", "msgs", "str_expected", "fail_msg"), + [ + ('DirectoryName["a/b/c", 3] // InputForm', None, '""', None), + ('DirectoryName[""] // InputForm', None, '""', None), + ( + 'DirectoryName["a/b/c", x]', + ( + "Positive machine-sized integer expected at position 2 in DirectoryName[a/b/c, x].", + ), + "DirectoryName[a/b/c, x]", + None, + ), + ( + 'DirectoryName["a/b/c", -1]', + ( + "Positive machine-sized integer expected at position 2 in DirectoryName[a/b/c, -1].", + ), + "DirectoryName[a/b/c, -1]", + None, + ), + ( + "DirectoryName[x]", + ("String expected at position 1 in DirectoryName[x].",), + "DirectoryName[x]", + None, + ), + ('DirectoryQ["ExampleData"]', None, "True", None), + ('DirectoryQ["ExampleData/MythicalSubdir/NestedDir/"]', None, "False", None), + ("FileNameDepth[x]", None, "FileNameDepth[x]", None), + ("FileNameDepth[$RootDirectory]", None, "0", None), + ( + 'FileNameSplit["example/path", OperatingSystem -> x]', + ( + 'The value of option OperatingSystem -> x must be one of "MacOSX", "Windows", or "Unix".', + ), + "{example, path}", + None, + ), + ], +) +def test_private_doctests_directory_names(str_expr, msgs, str_expected, fail_msg): + """exp_structure.size_and_sig""" + check_evaluation( + str_expr, + str_expected, + to_string_expr=True, + to_string_expected=True, + hold_expected=True, + failure_message=fail_msg, + expected_messages=msgs, + ) diff --git a/test/builtin/test_evalution.py b/test/builtin/test_evalution.py index 5b678bd9f..50f43c6a3 100644 --- a/test/builtin/test_evalution.py +++ b/test/builtin/test_evalution.py @@ -4,7 +4,7 @@ """ -from test.helper import check_evaluation, session +from test.helper import check_evaluation, reset_session, session import pytest @@ -12,7 +12,13 @@ @pytest.mark.parametrize( ("str_expr", "msgs", "str_expected", "fail_msg"), [ - ("ClearAll[a];$RecursionLimit = 20", None, "20", None), + ( + None, + None, + None, + None, + ), + ("$RecursionLimit = 20", None, "20", None), ("a = a + a", ("Recursion depth of 20 exceeded.",), "$Aborted", None), ("$RecursionLimit = 200", None, "200", None), ( @@ -76,6 +82,11 @@ def test_private_doctests_evaluation(str_expr, msgs, str_expected, fail_msg): # TODO: Maybe it makes sense to clone this exception handling in # the check_evaluation function. # + + if str_expr is None: + reset_session() + return + def eval_expr(expr_str): query = session.evaluation.parse(expr_str) res = session.evaluation.evaluate(query) diff --git a/test/builtin/test_file_operations.py b/test/builtin/test_file_operations.py new file mode 100644 index 000000000..3a9db5146 --- /dev/null +++ b/test/builtin/test_file_operations.py @@ -0,0 +1,139 @@ +# -*- coding: utf-8 -*- +""" +Unit tests for mathics.builtin.file_operations +""" + +import sys +import time +from test.helper import check_evaluation, evaluate + +import pytest + + +@pytest.mark.parametrize( + ("str_expr", "msgs", "str_expected", "fail_msg"), + [ + ( + 'FileDate["MathicsNonExistantExample"]', + ("File not found during FileDate[MathicsNonExistantExample].",), + "FileDate[MathicsNonExistantExample]", + None, + ), + ( + 'FileDate["MathicsNonExistantExample", "Modification"]', + ( + "File not found during FileDate[MathicsNonExistantExample, Modification].", + ), + "FileDate[MathicsNonExistantExample, Modification]", + None, + ), + ( + 'FileDate["ExampleData/sunflowers.jpg", "Fail"]', + ( + 'Date type Fail should be "Access", "Modification", "Creation" (Windows only), "Change" (Macintosh and Unix only), or "Rules".', + ), + "FileDate[ExampleData/sunflowers.jpg, Fail]", + None, + ), + ('FileHash["ExampleData/sunflowers.jpg", "CRC32"]', None, "933095683", None), + ( + 'FileHash["ExampleData/sunflowers.jpg", "SHA"]', + None, + "851696818771101405642332645949480848295550938123", + None, + ), + ( + 'FileHash["ExampleData/sunflowers.jpg", "SHA224"]', + None, + "8723805623766373862936267623913366865806344065103917676078120867011", + None, + ), + ( + 'FileHash["ExampleData/sunflowers.jpg", "SHA384"]', + None, + "28288410602533803613059815846847184383722061845493818218404754864571944356226472174056863474016709057507799332611860", + None, + ), + ( + 'FileHash["ExampleData/sunflowers.jpg", "SHA512"]', + None, + "10111462070211820348006107532340854103555369343736736045463376555356986226454343186097958657445421102793096729074874292511750542388324853755795387877480102", + None, + ), + ( + 'FileHash["ExampleData/sunflowers.jpg", xyzsymbol]', + None, + "FileHash[ExampleData/sunflowers.jpg, xyzsymbol]", + None, + ), + ( + 'FileHash["ExampleData/sunflowers.jpg", "xyzstr"]', + None, + "FileHash[ExampleData/sunflowers.jpg, xyzstr, Integer]", + None, + ), + ("FileHash[xyzsymbol]", None, "FileHash[xyzsymbol]", None), + ( + "FileType[x]", + ("File specification x is not a string of one or more characters.",), + "FileType[x]", + None, + ), + ( + 'tmpfilename = $TemporaryDirectory <> "/tmp0";Close[OpenWrite[tmpfilename]];', + None, + "Null", + None, + ), + ( + 'SetFileDate[tmpfilename, {2002, 1, 1, 0, 0, 0.}];FileDate[tmpfilename, "Access"]', + None, + "{2002, 1, 1, 0, 0, 0.}", + None, + ), + ("SetFileDate[tmpfilename]", None, "Null", None), + ('FileDate[tmpfilename, "Access"]//Length', None, "6", None), + ( + 'DeleteFile[tmpfilename];SetFileDate["MathicsNonExample"]', + ("File not found during SetFileDate[MathicsNonExample].",), + "$Failed", + None, + ), + ], +) +def test_private_doctests_file_properties(str_expr, msgs, str_expected, fail_msg): + """file_opertions.file_properties""" + check_evaluation( + str_expr, + str_expected, + to_string_expr=True, + to_string_expected=True, + hold_expected=True, + failure_message=fail_msg, + expected_messages=msgs, + ) + + +@pytest.mark.parametrize( + ("str_expr", "msgs", "str_expected", "fail_msg"), + [ + ('FindList["ExampleData/EinsteinSzilLetter.txt", "project"]', None, "{}", None), + ( + 'FindList["ExampleData/EinsteinSzilLetter.txt", "uranium", 0]', + None, + "$Failed", + None, + ), + ], +) +def test_private_doctests_file_utilities(str_expr, msgs, str_expected, fail_msg): + """file_opertions.file_utilities""" + check_evaluation( + str_expr, + str_expected, + to_string_expr=True, + to_string_expected=True, + hold_expected=True, + failure_message=fail_msg, + expected_messages=msgs, + )