Skip to content

Commit

Permalink
Add more Read[] tests ... (#1127)
Browse files Browse the repository at this point in the history
mostly around error checking
  • Loading branch information
rocky authored Oct 13, 2024
1 parent 0f4650c commit aa1a33d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
5 changes: 2 additions & 3 deletions mathics/eval/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import mathics
import mathics.core.parser
import mathics.core.streams
from mathics.core.atoms import String
from mathics.core.builtin import MessageException
from mathics.core.convert.expression import to_expression, to_mathics_list
from mathics.core.convert.python import from_python
Expand Down Expand Up @@ -200,9 +201,7 @@ def eval_Read(
print(e)

if expr is SymbolEndOfFile:
evaluation.message(
name, "readt", tmp, to_expression("InputSteam", name, n)
)
evaluation.message(name, "readt", tmp, String(stream.name))
return SymbolFailed
elif isinstance(expr, BaseElement):
if typ is SymbolHoldExpression:
Expand Down
40 changes: 40 additions & 0 deletions test/builtin/files_io/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,43 @@ def test_read_list(str_expr, msgs, str_expected, fail_msg):
failure_message=fail_msg,
expected_messages=msgs,
)


lisp1_path = f"{data_dir}/lisp1.m"
invalid_path = f"{data_dir}/file-should-not-appear"


@pytest.mark.parametrize(
("str_expr", "msgs", "str_expected", "fail_msg"),
[
(
f'Read["{lisp1_path}", Junk]',
["Junk is not a valid format specification."],
f'Read["{lisp1_path}", Junk]',
"Read invalid format specification",
),
(
f'Read["{invalid_path}"]',
[f"Cannot open {invalid_path}."],
"$Failed",
"Read[] with missing path",
),
(
f'Read["{lisp1_path}"]',
[f"Invalid input found when reading '(1) from {lisp1_path}."],
"$Failed",
"Read[] with unparsable default data",
),
],
)
def test_read(str_expr, msgs, str_expected, fail_msg):
"""Read[] tests."""
check_evaluation(
str_expr,
str_expected,
to_string_expr=False,
to_string_expected=False,
hold_expected=False,
failure_message=fail_msg,
expected_messages=msgs,
)

0 comments on commit aa1a33d

Please sign in to comment.