Skip to content

Commit 1785b02

Browse files
committed
Move a doctest into a pytest
We abuse the doctest system with stuff that is less about documenting and more about edge-case testing. pytest is better suited for boring tests and those that require special conditions that need to be met.
1 parent cbbce7a commit 1785b02

2 files changed

Lines changed: 79 additions & 41 deletions

File tree

mathics/builtin/files.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import math
1919
import sympy
2020
import requests
21-
import tempfile
2221

2322

2423
from itertools import chain
@@ -1395,33 +1394,6 @@ class BinaryWrite(Builtin):
13951394
= {213, 143, 98, 112, 141, 183, 203, 247}
13961395
#> WRb[{384206740, 1676316040}, Table["UnsignedInteger32", {2}]]
13971396
= {148, 135, 230, 22, 136, 141, 234, 99}
1398-
1399-
######
1400-
## The below do not work on MacOSX
1401-
1402-
## UnsignedInteger64
1403-
S> WRb[7079445437368829279, "UnsignedInteger64"]
1404-
= {95, 5, 33, 229, 29, 62, 63, 98}
1405-
1406-
S> WRb[5381171935514265990, "UnsignedInteger64"]
1407-
= {134, 9, 161, 91, 93, 195, 173, 74}
1408-
1409-
## UnsignedInteger128
1410-
S> WRb[293382001665435747348222619884289871468, "UnsignedInteger128"]
1411-
= {108, 78, 217, 150, 88, 126, 152, 101, 231, 134, 176, 140, 118, 81, 183, 220}
1412-
S> WRb[253033302833692126095975097811212718901, "UnsignedInteger128"]
1413-
= {53, 83, 116, 79, 81, 100, 60, 126, 202, 52, 241, 48, 5, 113, 92, 190}
1414-
1415-
## Full File
1416-
## MacOSX doesn't have /dev/full
1417-
S> strm = OpenWrite["/dev/full", BinaryFormat -> True]
1418-
= OutputStream[...]
1419-
S> BinaryWrite[strm, {39, 4, 122}]
1420-
: No space left on device.
1421-
= OutputStream[...]
1422-
S> Close[strm]
1423-
: No space left on device.
1424-
= ...
14251397
"""
14261398

14271399
messages = {
@@ -1889,13 +1861,6 @@ class WriteString(Builtin):
18891861
| abc
18901862
18911863
#> WriteString[OpenWrite["/dev/zero"], "abc"] (* Null *)
1892-
1893-
S> str = OpenWrite["/dev/full"];
1894-
S> WriteString[str, "123"]
1895-
: No space left on device.
1896-
S> Close[str]
1897-
: No space left on device.
1898-
= /dev/full
18991864
"""
19001865

19011866
messages = {
@@ -2180,6 +2145,7 @@ class Put(BinaryOperator):
21802145
## * a bit fragile, somewhat
21812146
## * somewhat OS dependent,
21822147
## * can leave crap in the filesystem
2148+
## * put in a pytest
21832149
##
21842150
## For these reasons this should be done a a pure test
21852151
## rather than intermingled with the doc system.

test/test_evaluation.py

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
21
from mathics.session import MathicsSession
32

3+
import sys
44
from mathics.core.parser import parse, SingleLineFeeder
55
from mathics.core.definitions import Definitions
66
from mathics.core.evaluation import Evaluation
@@ -10,8 +10,6 @@
1010
session = MathicsSession(add_builtin=True, catch_interrupt=False)
1111

1212

13-
14-
1513
@pytest.mark.parametrize(
1614
"str_expr,str_expected",
1715
[
@@ -45,12 +43,13 @@
4543
(r'StringInsert["", "X", 1]', r'"X"'),
4644
(r'StringInsert["", "X", -1]', r'"X"'),
4745
(r'StringInsert["abcdefghijklm", "", -1]', r'"abcdefghijklm"'),
48-
(r'StringInsert[{"abcdefghijklm", "Mathics"}, "X", {}]', r'{"abcdefghijklm", "Mathics"}'),
46+
(
47+
r'StringInsert[{"abcdefghijklm", "Mathics"}, "X", {}]',
48+
r'{"abcdefghijklm", "Mathics"}',
49+
),
4950
],
5051
)
51-
5252
def test_evaluation(str_expr: str, str_expected: str, message=""):
53-
global session
5453
result = session.evaluate(str_expr)
5554
expected = session.evaluate(str_expected)
5655

@@ -60,6 +59,79 @@ def test_evaluation(str_expr: str, str_expected: str, message=""):
6059
assert result == expected
6160

6261

62+
if sys.platform in ("linux",):
63+
64+
def test_system_specific_long_integer():
65+
session.evaluate("""
66+
WRb[bytes_, form_] := Module[{str, res={}, byte}, str = OpenWrite[BinaryFormat -> True];
67+
BinaryWrite[str, bytes, form];
68+
str = OpenRead[Close[str], BinaryFormat -> True];
69+
While[Not[SameQ[byte = BinaryRead[str], EndOfFile]], res = Join[res, {byte}];];
70+
Close[str]; res]
71+
""")
72+
for str_expr, str_expected, message in (
73+
(
74+
r'WRb[{1885507541, 4157323149}, Table["UnsignedInteger32", {2}]]',
75+
r"{213, 143, 98, 112, 141, 183, 203, 247}",
76+
"UnsignedInteger32",
77+
),
78+
(
79+
r'WRb[{384206740, 1676316040}, Table["UnsignedInteger32", {2}]]',
80+
r"{148, 135, 230, 22, 136, 141, 234, 99}",
81+
"UnsignedInteger32 - 2nd test",
82+
),
83+
(
84+
r'WRb[7079445437368829279, "UnsignedInteger64"]',
85+
r"{95, 5, 33, 229, 29, 62, 63, 98}",
86+
"UnsignedInteger64",
87+
),
88+
(
89+
r'WRb[5381171935514265990, "UnsignedInteger64"]',
90+
r"{134, 9, 161, 91, 93, 195, 173, 74}",
91+
"UnsignedInteger64 - 2nd test",
92+
),
93+
(
94+
r'WRb[293382001665435747348222619884289871468, "UnsignedInteger128"]',
95+
r"{108, 78, 217, 150, 88, 126, 152, 101, 231, 134, 176, 140, 118, 81, 183, 220}",
96+
"UnsignedInteger128",
97+
),
98+
(
99+
r'WRb[253033302833692126095975097811212718901, "UnsignedInteger128"]',
100+
r"{53, 83, 116, 79, 81, 100, 60, 126, 202, 52, 241, 48, 5, 113, 92, 190}",
101+
"UnsignedInteger128 - 2nd test",
102+
),
103+
):
104+
105+
test_evaluation(str_expr, str_expected, message)
106+
107+
# import os.path as osp
108+
# def test_evaluation_with_err(str_expr: str, expected: str, message=""):
109+
# import pdb; pdb.set_trace()
110+
# result = session.evaluate(str_expr)
111+
112+
# if message:
113+
# assert result == expected, message
114+
# else:
115+
# assert result == expected
116+
117+
# if osp.exists("/dev/zero"):
118+
# def test_system_specific_write_string():
119+
# for str_expr, str_expected, message in (
120+
# (
121+
# r'WriteString[OpenWrite["/dev/full"], "123"]',
122+
# r": No space left on device.",
123+
# "WriteString to full device",
124+
# ),
125+
# # (
126+
# # r'Close[str]',
127+
# # r"No space left on device.",
128+
# # "Close on full device",
129+
# # ),
130+
# ):
131+
132+
# test_evaluation_with_err(str_expr, str_expected, message)
133+
134+
63135
def test_exit():
64136
global session
65137
try:

0 commit comments

Comments
 (0)