R mode
+ + + +MIME types defined: text/x-rsrc
.
Development of the CodeMirror R mode was kindly sponsored + by Ubalo.
+ +From 8b95f40cb626a7be629ee6da6e37ea389ceb379d Mon Sep 17 00:00:00 2001 From: prathamesh Date: Fri, 27 Mar 2020 13:03:36 +0530 Subject: Add tests for R evaluator and codemirror R mode --- yaksh/evaluator_tests/test_r_evaluation.py | 200 +++++++++++++++++++++ yaksh/r_code_evaluator.py | 5 +- yaksh/static/yaksh/js/codemirror/mode/r/index.html | 85 +++++++++ yaksh/static/yaksh/js/codemirror/mode/r/r.js | 164 +++++++++++++++++ yaksh/static/yaksh/js/requesthandler.js | 3 +- yaksh/templates/yaksh/question.html | 1 + 6 files changed, 455 insertions(+), 3 deletions(-) create mode 100644 yaksh/evaluator_tests/test_r_evaluation.py create mode 100644 yaksh/static/yaksh/js/codemirror/mode/r/index.html create mode 100644 yaksh/static/yaksh/js/codemirror/mode/r/r.js (limited to 'yaksh') diff --git a/yaksh/evaluator_tests/test_r_evaluation.py b/yaksh/evaluator_tests/test_r_evaluation.py new file mode 100644 index 0000000..d4272a5 --- /dev/null +++ b/yaksh/evaluator_tests/test_r_evaluation.py @@ -0,0 +1,200 @@ +from __future__ import unicode_literals +import unittest +from textwrap import dedent +import os +import tempfile +import shutil +from psutil import Process + +from yaksh.grader import Grader +from yaksh.settings import SERVER_TIMEOUT +from yaksh.evaluator_tests.test_python_evaluation import EvaluatorBaseTest + + +class RAssertionEvaluationTestCase(EvaluatorBaseTest): + def setUp(self): + self.tmp_file = os.path.join(tempfile.gettempdir(), 'test.txt') + with open(self.tmp_file, 'wb') as f: + f.write('2'.encode('ascii')) + tmp_in_dir_path = tempfile.mkdtemp() + self.in_dir = tmp_in_dir_path + self.test_case = dedent( + ''' + source("function.r") + check_empty = function(obj){ + stopifnot(is.null(obj) == FALSE) + } + check = function(input, output){ + stopifnot(input == output) + } + is_correct = function(){ + if (count == 3){ + quit("no", 31) + } + } + check_empty(odd_or_even(3)) + check(odd_or_even(6), "EVEN") + check(odd_or_even(1), "ODD") + check(odd_or_even(10), "EVEN") + check(odd_or_even(777), "ODD") + check(odd_or_even(778), "EVEN") + count = 3 + is_correct() + ''' + ) + self.test_case_data = [{"test_case": self.test_case, + "test_case_type": "standardtestcase", + "weight": 0.0 + }] + self.timeout_msg = ("Code took more than {0} seconds to run. " + "You probably have an infinite loop in" + " your code.").format(SERVER_TIMEOUT) + self.file_paths = None + + def tearDown(self): + os.remove(self.tmp_file) + shutil.rmtree(self.in_dir) + + def test_correct_answer(self): + # Given + user_answer = dedent( + ''' + odd_or_even <- function(n){ + if(n %% 2 == 0){ + return("EVEN") + } + return("ODD") + } + ''' + ) + kwargs = {'metadata': { + 'user_answer': user_answer, + 'file_paths': self.file_paths, + 'partial_grading': False, + 'language': 'r'}, + 'test_case_data': self.test_case_data, + } + + # When + grader = Grader(self.in_dir) + result = grader.evaluate(kwargs) + + # Then + self.assertTrue(result.get('success')) + + def test_incorrect_answer(self): + # Given + user_answer = dedent( + ''' + odd_or_even <- function(n){ + if(n %% 2 == 0){ + return("ODD") + } + return("EVEN") + } + ''' + ) + err = ['Error: input == output is not TRUE\nExecution halted\n'] + kwargs = { + 'metadata': { + 'user_answer': user_answer, + 'file_paths': self.file_paths, + 'partial_grading': False, + 'language': 'r' + }, 'test_case_data': self.test_case_data, + } + + # When + grader = Grader(self.in_dir) + result = grader.evaluate(kwargs) + errors = result.get('error') + # Then + self.assertFalse(result.get('success')) + self.assertEqual(errors, err) + + def test_error_code(self): + # Given + user_answer = dedent( + ''' + odd_or_even <- function(n){ + a + } + ''' + ) + kwargs = { + 'metadata': { + 'user_answer': user_answer, + 'file_paths': self.file_paths, + 'partial_grading': False, + 'language': 'r' + }, 'test_case_data': self.test_case_data, + } + + # When + grader = Grader(self.in_dir) + result = grader.evaluate(kwargs) + errors = result.get('error') + + # Then + self.assertFalse(result.get("success")) + self.assertIn("object 'a' not found", errors[0]) + + def test_empty_function(self): + # Given + user_answer = dedent( + ''' + odd_or_even <- function(n){ + } + ''' + ) + kwargs = { + 'metadata': { + 'user_answer': user_answer, + 'file_paths': self.file_paths, + 'partial_grading': False, + 'language': 'r' + }, 'test_case_data': self.test_case_data, + } + + # When + grader = Grader(self.in_dir) + result = grader.evaluate(kwargs) + errors = result.get('error') + + # Then + self.assertFalse(result.get("success")) + self.assertIn("Error: is.null(obj) == FALSE is not TRUE", errors[0]) + + def test_infinite_loop(self): + # Given + user_answer = dedent( + ''' + odd_or_even <- function(n){ + while(0 == 0){ + a <- 1 + } + } + ''' + ) + kwargs = { + 'metadata': { + 'user_answer': user_answer, + 'file_paths': self.file_paths, + 'partial_grading': False, + 'language': 'r' + }, 'test_case_data': self.test_case_data, + } + + # When + grader = Grader(self.in_dir) + result = grader.evaluate(kwargs) + + # Then + self.assertFalse(result.get("success")) + self.assert_correct_output(self.timeout_msg, + result.get("error")[0]["message"] + ) + parent_proc = Process(os.getpid()).children() + if parent_proc: + children_procs = Process(parent_proc[0].pid) + self.assertFalse(any(children_procs.children(recursive=True))) diff --git a/yaksh/r_code_evaluator.py b/yaksh/r_code_evaluator.py index ca4c94a..11bc970 100644 --- a/yaksh/r_code_evaluator.py +++ b/yaksh/r_code_evaluator.py @@ -49,7 +49,7 @@ class RCodeEvaluator(BaseEvaluator): # Throw message if there are commmands that terminates scilab add_err = "" if terminate_commands: - add_err = "Please do not use quit() in your\ + add_err = "Please do not use quit() q() in your\ code.\n Otherwise your code will not be evaluated\ correctly.\n" @@ -79,7 +79,8 @@ class RCodeEvaluator(BaseEvaluator): new_string = "" terminate_commands = False for line in string.splitlines(): - new_line = re.sub(r"quit.*$", "", line) + new_line = re.sub(r'quit(.*$)', "", line) + new_line = re.sub(r'q(.*$)', "", new_line) if line != new_line: terminate_commands = True new_string = new_string + '\n' + new_line diff --git a/yaksh/static/yaksh/js/codemirror/mode/r/index.html b/yaksh/static/yaksh/js/codemirror/mode/r/index.html new file mode 100644 index 0000000..6dd9634 --- /dev/null +++ b/yaksh/static/yaksh/js/codemirror/mode/r/index.html @@ -0,0 +1,85 @@ + + +
MIME types defined: text/x-rsrc
.
Development of the CodeMirror R mode was kindly sponsored + by Ubalo.
+ +