Skip to content

Commit

Permalink
Add allow_errors
Browse files Browse the repository at this point in the history
This can be reused in other xwhats
It should replace the pythonwhat standalone function allow_errors and the sqlwhat SCT function allow_error
  • Loading branch information
hermansje committed Jun 21, 2019
1 parent cf24cfd commit f3182e4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions protowhat/State.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(
):
args = locals().copy()
self.params = list()

for k, v in args.items():
if k != "self":
self.params.append(k)
Expand Down
17 changes: 17 additions & 0 deletions protowhat/checks/check_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ def has_chosen(state, correct, msgs):
return state


def allow_errors(state):
"""
Allow running the student code to generate errors.
This has to be used only once for every time code is executed or a different xwhat library is used.
In most exercises that means it should be used just once.
:Example:
The following SCT allows the student code to generate errors::
Ex().allow_errors()
"""
state.reporter.allow_errors()

return state


def success_msg(state, msg):
"""
Changes the success message to display if submission passes.
Expand Down
32 changes: 28 additions & 4 deletions tests/test_check_simple.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from protowhat.checks.check_simple import has_chosen, success_msg
from protowhat.checks.check_simple import has_chosen, success_msg, allow_errors
from protowhat.sct_syntax import Chain
from protowhat.State import State
from protowhat.Reporter import Reporter
from protowhat.Test import TestFail as TF
import pytest

sct_ctx = {"has_chosen": has_chosen, "success_msg": success_msg}
sct_ctx = {
"has_chosen": has_chosen,
"success_msg": success_msg,
"allow_errors": allow_errors,
}


def prepare_state(student_code):
Expand Down Expand Up @@ -52,7 +56,7 @@ def test_success_msg_pass():
success_msg(state, "NEW SUCCESS MSG")

sct_payload = state.reporter.build_final_payload()
assert sct_payload["correct"] == True
assert sct_payload["correct"] is True
assert sct_payload["message"] == "NEW SUCCESS MSG"


Expand All @@ -61,5 +65,25 @@ def test_success_msg_pass_ex():
Chain(state, sct_ctx).success_msg("NEW SUCCESS MSG")

sct_payload = state.reporter.build_final_payload()
assert sct_payload["correct"] == True
assert sct_payload["correct"] is True
assert sct_payload["message"] == "NEW SUCCESS MSG"


def test_no_allow_errors():
state = prepare_state("")
state.reporter.errors = ["Error"]
Chain(state, sct_ctx).success_msg("Good")
sct_payload = state.reporter.build_final_payload()
assert sct_payload["correct"] is False
assert (
sct_payload["message"] == "Your code generated an error. Fix it and try again!"
)


def test_allow_errors():
state = prepare_state("")
state.reporter.errors = ["Error"]
Chain(state, sct_ctx).allow_errors().success_msg("Good")
sct_payload = state.reporter.build_final_payload()
assert sct_payload["correct"] is True
assert sct_payload["message"] == "Good"
2 changes: 1 addition & 1 deletion tests/test_sct_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_sct_dict_creation():

sct_dict = get_checks_dict(check_simple)

assert len(sct_dict) == 3 # Feedback is also callable
assert len(sct_dict) == 4 # Feedback is also callable
assert sct_dict["has_chosen"] == check_simple.has_chosen
assert sct_dict["success_msg"] == check_simple.success_msg

Expand Down

0 comments on commit f3182e4

Please sign in to comment.