Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiline statments in tests #712

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
**v5.2.0 (unreleased):**

* Migrate installation of `ottr` from `setup.sh` to `environment.yml` with the [`r-ottr` conda-forge recipe](https://anaconda.org/conda-forge/r-ottr)
* Updated Otter Assign to allow multiline statements in test cases per [#590](https://github.com/ucbds-infra/otter-grader/issues/590)

**v5.1.2:**

Expand Down
20 changes: 15 additions & 5 deletions otter/assign/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def remove_tag(cell, tag):
return cell


def str_to_doctest(code_lines, lines):
def str_to_doctest(code_lines, lines, opens=None):
"""
Convert a list of lines of Python code ``code_lines`` to the doctest format and appending the
results to ``lines``.
Expand All @@ -174,16 +174,26 @@ def str_to_doctest(code_lines, lines):
"""
if len(code_lines) == 0:
return lines
if opens is None:
opens = []
in_statement = len(opens) > 0
line = code_lines.pop(0)
for c in line:
if c == "(" or c == "[" or c == "{":
opens.append(c)
elif c == ")" or c == "]" or c == "}":
opens.pop()
if line.startswith(" ") or line.startswith("\t"):
return str_to_doctest(code_lines, lines + ["... " + line])
return str_to_doctest(code_lines, lines + ["... " + line], opens=opens)
elif bool(re.match(r"^except[\s\w]*:", line)) or line.startswith("elif ") or \
line.startswith("else:") or line.startswith("finally:"):
return str_to_doctest(code_lines, lines + ["... " + line])
return str_to_doctest(code_lines, lines + ["... " + line], opens=opens)
elif len(lines) > 0 and lines[-1].strip().endswith("\\"):
return str_to_doctest(code_lines, lines + ["... " + line])
return str_to_doctest(code_lines, lines + ["... " + line], opens=opens)
elif in_statement:
return str_to_doctest(code_lines, lines + ["... " + line], opens=opens)
else:
return str_to_doctest(code_lines, lines + [">>> " + line])
return str_to_doctest(code_lines, lines + [">>> " + line], opens=opens)


def run_tests(assignment, debug=False):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

test = { 'name': 'q1',
'points': None,
'suites': [ { 'cases': [ { 'code': '>>> isinstance(x, int)\nTrue',
'suites': [ { 'cases': [ { 'code': '>>> isinstance( x,\n... int,\n... )\nTrue',
'failure_message': 'This is not an int.',
'hidden': False,
'locked': False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@
{
"cases": [
{
"code": ">>> isinstance(x, int)\nTrue",
"code": ">>> isinstance( x,\n... int,\n... )\nTrue",
"failure_message": "This is not an int.",
"hidden": false,
"locked": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
{
"cases": [
{
"code": ">>> isinstance(x, int)\nTrue",
"code": ">>> isinstance( x,\n... int,\n... )\nTrue",
"failure_message": "This is not an int.",
"hidden": false,
"locked": false,
Expand Down
5 changes: 4 additions & 1 deletion test/test_assign/files/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@
"failure_message: This is not an int.\n",
"name: q1a-1\n",
"\"\"\" # END TEST CONFIG\n",
"isinstance(x, int)"
"isinstance(",
" x,\n",
" int,\n",
")"
]
},
{
Expand Down
6 changes: 5 additions & 1 deletion test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def assert_notebooks_equal(p1, p2):
# ignore cell IDs
for c in [*nb1.cells, *nb2.cells]:
c.pop("id", None)
assert nb1 == nb2
diff = subprocess.run(
["diff", "--context=5", p1, p2],
stdout=subprocess.PIPE,
).stdout.decode("utf-8")
assert nb1 == nb2, f"Contents of {p1} did not equal contents of {p2}:\n{diff}"


def assert_files_equal(p1, p2, ignore_trailing_whitespace=True):
Expand Down
Loading