Skip to content

Commit

Permalink
Add a "syntax_error" key to the result if the test input is invalid
Browse files Browse the repository at this point in the history
# This should help avoid the need to repeatedly check invalid test strings
# against multiple possible answers.
  • Loading branch information
jsharkey13 committed Oct 17, 2018
1 parent c216c8e commit 875262d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions checker/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,20 @@ def check(test_str, target_str, symbols=None, check_symbols=True, description=No
return dict(error="Empty string as argument.")

# Cleanup the strings before anything is done to them:
error_is_test = False
try:
target_str = parsing.cleanup_string(target_str, reject_unsafe_input=True)
error_is_test = True
test_str = parsing.cleanup_string(test_str, reject_unsafe_input=True)
except parsing.UnsafeInputException:
print "ERROR: Input contained non-whitelisted characters!"
print "Test string: '%s'" % test_str
result = dict(error="Bad input provided!")
if error_is_test:
print "Test string: '%s'" % test_str
result["syntax_error"] = str(True).lower()
if not _quiet:
print "=" * 50
return dict(error="Bad input provided!")
return result

print "Target string: '%s'" % target_str
print "Test string: '%s'" % test_str
Expand Down Expand Up @@ -606,6 +611,7 @@ def check(test_str, target_str, symbols=None, check_symbols=True, description=No
if not _quiet:
print "=" * 50
result["error"] = "Parsing Test Expression Failed!"
result["syntax_error"] = str(True).lower()
return result

result["parsed_target"] = str(target_expr)
Expand Down
13 changes: 13 additions & 0 deletions checker/unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,19 @@ def test_plus_or_minus(self):
self.assertTrue(response["equality_type"] == "exact", 'For these expressions, expected "equality_type" to be "exact", got "%s"!' % response["equality_type"])
print " PASS ".center(75, "#")

def test_syntax_error(self):
print "\n\n\n" + " Test if Syntax Errors are Reported ".center(75, "#")
test_str = "(a + b +"
target_str = "a + b"
symbols = None
response = api.check(test_str, target_str, symbols)

self.assertTrue("error" in response, 'Expected "error" in response!')
self.assertTrue("syntax_error" in response, 'Key "syntax_error" not in response!')
print response["syntax_error"], type(response["syntax_error"])
self.assertTrue(response["syntax_error"] == "true", 'Expected "syntax_error" to be "true", got "%s"!' % response["syntax_error"])
print " PASS ".center(75, "#")

# def test_factorials_limited(self):
# print "\n\n\n" + " Test Factorials are correctly Limited ".center(75, "#")
# test_str = "factorial(1000)"
Expand Down

0 comments on commit 875262d

Please sign in to comment.