def test_error_in_check_code(self): results = run_python("""a = 17""", """b = 1/0""") self.assertEqual("", results["stdout"]) checks = results["checks"] self.assertEqual(len(checks), 1) self.assertEqual(checks[0]["status"], "ERROR") self.assertEqual("ZeroDivisionError", checks[0]["exception"]["type"]) self.assertEqual("b = 1/0", checks[0]["exception"]["traceback"][-1]["text"])
def test_error_in_check_code(self): results = run_python("""a = 17""", """b = 1/0""") self.assertEqual("", results['stdout']) checks = results['checks'] self.assertEqual(len(checks), 1) self.assertEqual(checks[0]['status'], "ERROR") self.assertEqual("ZeroDivisionError", checks[0]['exception']['type']) self.assertEqual("b = 1/0", checks[0]['exception']['traceback'][-1]['text'])
def test_syntaxerror_in_check_code(self): results = run_python("""a = 17""", """1'hello'""") self.assertEqual("", results["stdout"]) checks = results["checks"] self.assertEqual(len(checks), 1) self.assertEqual(checks[0]["status"], "ERROR") self.assertEqual("SyntaxError", checks[0]["exception"]["type"]) self.assertEqual("invalid syntax", checks[0]["exception"]["args"][0]) self.assertEqual((1, 8, "1'hello'\n"), checks[0]["exception"]["args"][1][1:])
def test_indentation_error_in_user_code(self): results = run_python("""a = 1\n b = 2""", "") self.assertEqual("", results["stdout"]) checks = results["checks"] self.assertEqual(len(checks), 1) self.assertEqual(checks[0]["status"], "EXCEPTION") self.assertEqual("IndentationError", checks[0]["exception"]["type"]) self.assertEqual("b = 2", checks[0]["exception"]["traceback"][-1]["text"]) self.assertEqual(2, checks[0]["exception"]["traceback"][-1]["offset"])
def test_syntax_error_in_user_code(self): results = run_python("""a = 1'hello'""", "") self.assertEqual("", results["stdout"]) checks = results["checks"] self.assertEqual(len(checks), 1) self.assertEqual(checks[0]["status"], "EXCEPTION") self.assertEqual("SyntaxError", checks[0]["exception"]["type"]) self.assertEqual("a = 1'hello'", checks[0]["exception"]["traceback"][-1]["text"]) self.assertEqual(12, checks[0]["exception"]["traceback"][-1]["offset"])
def test_syntaxerror_in_check_code(self): results = run_python("""a = 17""", """1'hello'""") self.assertEqual("", results['stdout']) checks = results['checks'] self.assertEqual(len(checks), 1) self.assertEqual(checks[0]['status'], "ERROR") self.assertEqual("SyntaxError", checks[0]['exception']['type']) self.assertEqual('invalid syntax', checks[0]['exception']['args'][0]) self.assertEqual((1, 8, "1'hello'\n"), checks[0]['exception']['args'][1][1:])
def test_indentation_error_in_user_code(self): results = run_python("""a = 1\n b = 2""", "") self.assertEqual("", results['stdout']) checks = results['checks'] self.assertEqual(len(checks), 1) self.assertEqual(checks[0]['status'], "EXCEPTION") self.assertEqual("IndentationError", checks[0]['exception']['type']) self.assertEqual("b = 2", checks[0]['exception']['traceback'][-1]['text']) self.assertEqual(2, checks[0]['exception']['traceback'][-1]['offset'])
def test_syntax_error_in_user_code(self): results = run_python("""a = 1'hello'""", "") self.assertEqual("", results['stdout']) checks = results['checks'] self.assertEqual(len(checks), 1) self.assertEqual(checks[0]['status'], "EXCEPTION") self.assertEqual("SyntaxError", checks[0]['exception']['type']) self.assertEqual("a = 1'hello'", checks[0]['exception']['traceback'][-1]['text']) self.assertEqual(12, checks[0]['exception']['traceback'][-1]['offset'])
def run(request, exid=None): the_code = request.POST.get('code') if exid: ex = get_object_or_404(Exercise, pk=exid) check_code = ex.check else: check_code = request.POST.get('check', '') if not check_code: return HttpResponse("No check code to run", status=400) results = run_python(the_code, check_code) results['status'] = 'ok' return HttpResponse(json.dumps(results), mimetype="application/json")
def test_output(self): results = run_python("""print 'hello!'""", "") self.assertEqual(results['stdout'], "hello!\n")
def run_python_dedented(self, a, b): return run_python(textwrap.dedent(a), textwrap.dedent(b))
def test_output(self): results = run_python("""print 'hello!'""", "") self.assertEqual(results["stdout"], "hello!\n")