Skip to content

Commit

Permalink
Move private doctests to pytest16 (#929)
Browse files Browse the repository at this point in the history
Almost finishing....
  • Loading branch information
mmatera authored Oct 17, 2023
1 parent 3a65cc9 commit 08c5941
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 87 deletions.
33 changes: 0 additions & 33 deletions mathics/builtin/directories/directory_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -104,12 +87,6 @@ class DirectoryQ(Builtin):
= True
>> DirectoryQ["ExampleData/MythicalSubdir/"]
= False
#> DirectoryQ["ExampleData"]
= True
#> DirectoryQ["ExampleData/MythicalSubdir/NestedDir/"]
= False
"""

messages = {
Expand Down Expand Up @@ -150,12 +127,6 @@ class FileNameDepth(Builtin):
>> FileNameDepth["a/b/c/"]
= 3
#> FileNameDepth[x]
= FileNameDepth[x]
#> FileNameDepth[$RootDirectory]
= 0
"""

options = {
Expand Down Expand Up @@ -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 = {
Expand Down
45 changes: 0 additions & 45 deletions mathics/builtin/file_operations/file_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down
8 changes: 1 addition & 7 deletions mathics/builtin/file_operations/file_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@ class FindList(Builtin):
</dl>
>> 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 = {
Expand Down
64 changes: 64 additions & 0 deletions test/builtin/test_directories.py
Original file line number Diff line number Diff line change
@@ -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,
)
15 changes: 13 additions & 2 deletions test/builtin/test_evalution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
"""


from test.helper import check_evaluation, session
from test.helper import check_evaluation, reset_session, session

import pytest


@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),
(
Expand Down Expand Up @@ -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)
Expand Down
139 changes: 139 additions & 0 deletions test/builtin/test_file_operations.py
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit 08c5941

Please sign in to comment.