def test_incorrect_answer_integer(self): # Given self.test_case_data = [{ "expected_input": "1\n2", "expected_output": "3", "weight": 0.0 }] user_answer = dedent(""" a = int(input()) b = int(input()) print(a-b) """) kwargs = { 'user_answer': user_answer, 'test_case_data': self.test_case_data, 'partial_grading': False } # When evaluator = PythonStdioEvaluator() result = evaluator.evaluate(**kwargs) # Then self.assertFalse(result.get('success')) self.assertIn("Incorrect answer", result.get('error'))
def test_file_based_answer(self): # Given self.test_case_data = [{ "expected_input": "", "expected_output": "2", "weight": 0.0 }] self.file_paths = [('/tmp/test.txt', False)] user_answer = dedent(""" with open("test.txt") as f: a = f.read() print(a[0]) """) kwargs = { 'user_answer': user_answer, 'test_case_data': self.test_case_data, 'file_paths': self.file_paths, 'partial_grading': False } # When evaluator = PythonStdioEvaluator() result = evaluator.evaluate(**kwargs) # Then self.assertEqual(result.get('error'), "Correct answer\n") self.assertTrue(result.get('success'))
def test_correct_answer_list(self): # Given self.test_case_data = [{ "expected_input": "1,2,3\n5,6,7", "expected_output": "[1, 2, 3, 5, 6, 7]", "weight": 0.0 }] user_answer = dedent(""" from six.moves import input input_a = input() input_b = input() a = [int(i) for i in input_a.split(',')] b = [int(i) for i in input_b.split(',')] print(a+b) """) kwargs = { 'user_answer': user_answer, 'test_case_data': self.test_case_data, 'partial_grading': False } # When evaluator = PythonStdioEvaluator() result = evaluator.evaluate(**kwargs) # Then self.assertTrue(result.get('success')) self.assertIn("Correct answer", result.get('error'))
def test_correct_answer_string(self): # Given self.test_case_data = [{ "expected_input": ("the quick brown fox jumps over the lazy dog\nthe"), "expected_output": "2", "weight": 0.0 }] user_answer = dedent(""" from six.moves import input a = str(input()) b = str(input()) print(a.count(b)) """) kwargs = { 'user_answer': user_answer, 'test_case_data': self.test_case_data, 'partial_grading': False } # When evaluator = PythonStdioEvaluator() result = evaluator.evaluate(**kwargs) # Then self.assertTrue(result.get('success')) self.assertIn("Correct answer", result.get('error'))
def test_incorrect_answer(self): user_answer = "a,b=0,1\nfor i in range(5):\n\tprint b,\n\ta,b=b,a+b" get_class = PythonStdioEvaluator() kwargs = { 'user_answer': user_answer, 'test_case_data': self.test_case_data, 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) self.assertIn("Incorrect Answer", result.get('error'))
def test_file_based_answer(self): self.test_case_data = [{"expected_input": "", "expected_output": "2"}] self.file_paths = [(os.getcwd() + "/yaksh/test.txt", False)] user_answer = dedent(""" with open("test.txt") as f: a = f.read() print a[0] """) get_class = PythonStdioEvaluator() kwargs = { 'user_answer': user_answer, 'test_case_data': self.test_case_data, 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success'))
def test_add_two_integers_incorrect(self): self.test_case_data = [{ "expected_input": "1\n2", "expected_output": "3" }] user_answer = dedent(""" a = int(raw_input()) b = int(raw_input()) print a-b """) get_class = PythonStdioEvaluator() kwargs = { 'user_answer': user_answer, 'test_case_data': self.test_case_data, } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) self.assertIn("Incorrect Answer", result.get('error'))
def test_find_string_in_sentence(self): self.test_case_data = [{ "expected_input": """the quick brown fox jumps\ over the lazy dog\nthe""", "expected_output": "2" }] user_answer = dedent(""" a = raw_input() b = raw_input() print (a.count(b)) """) get_class = PythonStdioEvaluator() kwargs = { 'user_answer': user_answer, 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) self.assertTrue(result.get('success')) self.assertIn("Correct Answer", result.get('error'))
def test_add_two_lists(self): self.test_case_data = [{ "expected_input": "[1,2,3]\n[5,6,7]", "expected_output": "[1, 2, 3, 5, 6, 7]" }] user_answer = dedent(""" from ast import literal_eval a = literal_eval(raw_input()) b = literal_eval(raw_input()) print a+b """) get_class = PythonStdioEvaluator() kwargs = { 'user_answer': user_answer, 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) self.assertTrue(result.get('success')) self.assertIn("Correct Answer", result.get('error'))
def test_infinite_loop(self): # Given test_case_data = [{ "expected_input": "1\n2", "expected_output": "3", "weight": 0.0 }] timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in" " your code.").format(SERVER_TIMEOUT) user_answer = "while True:\n\tpass" kwargs = { 'user_answer': user_answer, 'test_case_data': test_case_data, 'partial_grading': False } # When evaluator = PythonStdioEvaluator() result = evaluator.evaluate(**kwargs) # Then self.assertEqual(result.get('error'), timeout_msg) self.assertFalse(result.get('success'))