summaryrefslogtreecommitdiff
path: root/yaksh/evaluator_tests
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/evaluator_tests')
-rw-r--r--yaksh/evaluator_tests/test_bash_evaluation.py81
-rw-r--r--yaksh/evaluator_tests/test_c_cpp_evaluation.py254
-rw-r--r--yaksh/evaluator_tests/test_code_evaluation.py4
-rw-r--r--yaksh/evaluator_tests/test_java_evaluation.py147
-rw-r--r--yaksh/evaluator_tests/test_python_evaluation.py179
5 files changed, 609 insertions, 56 deletions
diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py
index 1070c6a..addc5e6 100644
--- a/yaksh/evaluator_tests/test_bash_evaluation.py
+++ b/yaksh/evaluator_tests/test_bash_evaluation.py
@@ -1,9 +1,12 @@
import unittest
import os
from yaksh.bash_code_evaluator import BashCodeEvaluator
+from yaksh.bash_stdio_evaluator import BashStdioEvaluator
from yaksh.settings import SERVER_TIMEOUT
+from textwrap import dedent
-class BashEvaluationTestCases(unittest.TestCase):
+
+class BashAssertionEvaluationTestCases(unittest.TestCase):
def setUp(self):
self.test_case_data = [
{"test_case": "bash_files/sample.sh,bash_files/sample.args"}
@@ -66,5 +69,81 @@ class BashEvaluationTestCases(unittest.TestCase):
self.assertTrue(result.get("success"))
self.assertEquals(result.get("error"), "Correct answer")
+class BashStdioEvaluationTestCases(unittest.TestCase):
+ def setUp(self):
+ self.timeout_msg = ("Code took more than {0} seconds to run. "
+ "You probably have an infinite loop in your"
+ " code.").format(SERVER_TIMEOUT)
+
+ def test_correct_answer(self):
+ user_answer = dedent(""" #!/bin/bash
+ read A
+ read B
+ echo -n `expr $A + $B`
+ """
+ )
+ test_case_data = [{'expected_output': '11', 'expected_input': '5\n6'}]
+ get_class = BashStdioEvaluator()
+ kwargs = {"user_answer": user_answer,
+ "test_case_data": test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_array_input(self):
+ user_answer = dedent(""" readarray arr;
+ COUNTER=0
+ while [ $COUNTER -lt 3 ]; do
+ echo -n "${arr[$COUNTER]}"
+ let COUNTER=COUNTER+1
+ done
+ """
+ )
+ test_case_data = [{'expected_output': '1 2 3\n4 5 6\n7 8 9\n',
+ 'expected_input': '1,2,3\n4,5,6\n7,8,9'
+ }]
+ get_class = BashStdioEvaluator()
+ kwargs = {"user_answer": user_answer,
+ "test_case_data": test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_incorrect_answer(self):
+ user_answer = dedent(""" #!/bin/bash
+ read A
+ read B
+ echo -n `expr $A - $B`
+ """
+ )
+ test_case_data = [{'expected_output': '11', 'expected_input': '5\n6'}]
+ get_class = BashStdioEvaluator()
+ kwargs = {"user_answer": user_answer,
+ "test_case_data": test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertIn("Incorrect", result.get('error'))
+ self.assertFalse(result.get('success'))
+
+ def test_stdout_only(self):
+ user_answer = dedent(""" #!/bin/bash
+ A=6
+ B=4
+ echo -n `expr $A + $B`
+ """
+ )
+ test_case_data = [{'expected_output': '10',
+ 'expected_input': ''
+ }]
+ get_class = BashStdioEvaluator()
+ kwargs = {"user_answer": user_answer,
+ "test_case_data": test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
if __name__ == '__main__':
unittest.main()
diff --git a/yaksh/evaluator_tests/test_c_cpp_evaluation.py b/yaksh/evaluator_tests/test_c_cpp_evaluation.py
index 71af177..0042d0f 100644
--- a/yaksh/evaluator_tests/test_c_cpp_evaluation.py
+++ b/yaksh/evaluator_tests/test_c_cpp_evaluation.py
@@ -1,10 +1,12 @@
import unittest
import os
from yaksh.cpp_code_evaluator import CppCodeEvaluator
+from yaksh.cpp_stdio_evaluator import CppStdioEvaluator
from yaksh.settings import SERVER_TIMEOUT
from textwrap import dedent
-class CEvaluationTestCases(unittest.TestCase):
+
+class CAssertionEvaluationTestCases(unittest.TestCase):
def setUp(self):
self.test_case_data = [{"test_case": "c_cpp_files/main.cpp"}]
self.in_dir = os.getcwd()
@@ -83,5 +85,255 @@ class CEvaluationTestCases(unittest.TestCase):
self.assertEquals(result.get('error'), "Correct answer")
+class CppStdioEvaluationTestCases(unittest.TestCase):
+
+ def setUp(self):
+ self.test_case_data = [{'expected_output': '11', 'expected_input': '5\n6'}]
+ self.in_dir = os.getcwd()
+ self.timeout_msg = ("Code took more than {0} seconds to run. "
+ "You probably have an infinite loop in"
+ " your code.").format(SERVER_TIMEOUT)
+
+ def test_correct_answer(self):
+ user_answer = dedent("""
+ #include<stdio.h>
+ int main(void){
+ int a,b;
+ scanf("%d%d",&a,&b);
+ printf("%d",a+b);
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_array_input(self):
+ self.test_case_data = [{'expected_output': '561',
+ 'expected_input': '5\n6\n1'}]
+ user_answer = dedent("""
+ #include<stdio.h>
+ int main(void){
+ int a[3],i;
+ for(i=0;i<3;i++){
+ scanf("%d",&a[i]);}
+ for(i=0;i<3;i++){
+ printf("%d",a[i]);}
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_string_input(self):
+ self.test_case_data = [{'expected_output': 'abc',
+ 'expected_input': 'abc'}]
+ user_answer = dedent("""
+ #include<stdio.h>
+ int main(void){
+ char a[4];
+ scanf("%s",a);
+ printf("%s",a);
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_incorrect_answer(self):
+ user_answer = dedent("""
+ #include<stdio.h>
+ int main(void){
+ int a=10;
+ printf("%d",a);
+ }""")
+ get_class = CppStdioEvaluator()
+ 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", result.get('error'))
+ self.assertTrue(result.get('error').splitlines > 1)
+
+ def test_error(self):
+ user_answer = dedent("""
+ #include<stdio.h>
+ int main(void){
+ int a=10;
+ printf("%d",a)
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertFalse(result.get("success"))
+ self.assertTrue("Compilation Error" in result.get("error"))
+
+ def test_infinite_loop(self):
+ user_answer = dedent("""
+ #include<stdio.h>
+ int main(void){
+ while(0==0){
+ printf("abc");}
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertFalse(result.get("success"))
+ self.assertEquals(result.get("error"), self.timeout_msg)
+
+ def test_only_stdout(self):
+ self.test_case_data = [{'expected_output': '11',
+ 'expected_input': ''}]
+ user_answer = dedent("""
+ #include<stdio.h>
+ int main(void){
+ int a=5,b=6;
+ printf("%d",a+b);
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_cpp_correct_answer(self):
+ user_answer = dedent("""
+ #include<iostream>
+ using namespace std;
+ int main(void){
+ int a,b;
+ cin>>a>>b;
+ cout<<a+b;
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_cpp_array_input(self):
+ self.test_case_data = [{'expected_output': '561',
+ 'expected_input': '5\n6\n1'}]
+ user_answer = dedent("""
+ #include<iostream>
+ using namespace std;
+ int main(void){
+ int a[3],i;
+ for(i=0;i<3;i++){
+ cin>>a[i];}
+ for(i=0;i<3;i++){
+ cout<<a[i];}
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_cpp_string_input(self):
+ self.test_case_data = [{'expected_output': 'abc',
+ 'expected_input': 'abc'}]
+ user_answer = dedent("""
+ #include<iostream>
+ using namespace std;
+ int main(void){
+ char a[4];
+ cin>>a;
+ cout<<a;
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_cpp_incorrect_answer(self):
+ user_answer = dedent("""
+ #include<iostream>
+ using namespace std;
+ int main(void){
+ int a=10;
+ cout<<a;
+ }""")
+ get_class = CppStdioEvaluator()
+ 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", result.get('error'))
+ self.assertTrue(result.get('error').splitlines > 1)
+
+ def test_cpp_error(self):
+ user_answer = dedent("""
+ #include<iostream>
+ using namespace std;
+ int main(void){
+ int a=10;
+ cout<<a
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertFalse(result.get("success"))
+ self.assertTrue("Compilation Error" in result.get("error"))
+
+ def test_cpp_infinite_loop(self):
+ user_answer = dedent("""
+ #include<iostream>
+ using namespace std;
+ int main(void){
+ while(0==0){
+ cout<<"abc";}
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertFalse(result.get("success"))
+ self.assertEquals(result.get("error"), self.timeout_msg)
+
+ def test_cpp_only_stdout(self):
+ self.test_case_data = [{'expected_output': '11',
+ 'expected_input': ''}]
+ user_answer = dedent("""
+ #include<iostream>
+ using namespace std;
+ int main(void){
+ int a=5,b=6;
+ cout<<a+b;
+ }""")
+ get_class = CppStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
if __name__ == '__main__':
unittest.main()
diff --git a/yaksh/evaluator_tests/test_code_evaluation.py b/yaksh/evaluator_tests/test_code_evaluation.py
index 51c0c51..cbca32d 100644
--- a/yaksh/evaluator_tests/test_code_evaluation.py
+++ b/yaksh/evaluator_tests/test_code_evaluation.py
@@ -17,7 +17,7 @@ class RegistryTestCase(unittest.TestCase):
)
code_evaluators['python'] = \
{"standardtestcase": assertion_evaluator_path,
- "stdoutbasedtestcase": stdout_evaluator_path
+ "stdiobasedtestcase": stdout_evaluator_path
}
def test_set_register(self):
@@ -35,7 +35,7 @@ class RegistryTestCase(unittest.TestCase):
)
self.registry_object.register("python",
{"standardtestcase": assertion_evaluator_path,
- "stdoutbasedtestcase": stdout_evaluator_path
+ "stdiobasedtestcase": stdout_evaluator_path
}
)
self.assertEquals(evaluator_class, class_name)
diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py
index ed28745..74ac677 100644
--- a/yaksh/evaluator_tests/test_java_evaluation.py
+++ b/yaksh/evaluator_tests/test_java_evaluation.py
@@ -2,10 +2,12 @@ import unittest
import os
from yaksh import code_evaluator as evaluator
from yaksh.java_code_evaluator import JavaCodeEvaluator
+from yaksh.java_stdio_evaluator import JavaStdioEvaluator
from yaksh.settings import SERVER_TIMEOUT
from textwrap import dedent
-class JavaEvaluationTestCases(unittest.TestCase):
+
+class JavaAssertionEvaluationTestCases(unittest.TestCase):
def setUp(self):
self.test_case_data = [
{"test_case": "java_files/main_square.java"}
@@ -97,5 +99,148 @@ class JavaEvaluationTestCases(unittest.TestCase):
self.assertTrue(result.get("success"))
self.assertEquals(result.get("error"), "Correct answer")
+class JavaStdioEvaluationTestCases(unittest.TestCase):
+
+ def setUp(self):
+ self.test_case_data = [{'expected_output': '11',
+ 'expected_input': '5\n6'}]
+ evaluator.SERVER_TIMEOUT = 4
+ self.timeout_msg = ("Code took more than {0} seconds to run. "
+ "You probably have an infinite loop in"
+ " your code.").format(evaluator.SERVER_TIMEOUT)
+
+ def teardown(self):
+ evaluator.SERVER_TIMEOUT = 4
+
+ def test_correct_answer(self):
+ user_answer = dedent("""
+ import java.util.Scanner;
+ class Test
+ {public static void main(String[] args){
+ Scanner s = new Scanner(System.in);
+ int a = s.nextInt();
+ int b = s.nextInt();
+ System.out.print(a+b);
+ }}""")
+ get_class = JavaStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_array_input(self):
+
+ self.test_case_data = [{'expected_output': '561',
+ 'expected_input': '5\n6\n1'}]
+ user_answer = dedent("""
+ import java.util.Scanner;
+ class Test
+ {public static void main(String[] args){
+ Scanner s = new Scanner(System.in);
+ int a[] = new int[3];
+ for (int i=0;i<3;i++){
+ a[i] = s.nextInt();
+ System.out.print(a[i]);}
+ }}""")
+ get_class = JavaStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_incorrect_answer(self):
+
+ user_answer = dedent("""
+ import java.util.Scanner;
+ class Test
+ {public static void main(String[] args){
+ Scanner s = new Scanner(System.in);
+ int a = s.nextInt();
+ int b = s.nextInt();
+ System.out.print(a);
+ }}""")
+ get_class = JavaStdioEvaluator()
+ 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", result.get('error'))
+ self.assertTrue(result.get('error').splitlines > 1)
+
+ def test_error(self):
+
+ user_answer = dedent("""
+ class Test
+ {
+ System.out.print("a");
+ }""")
+ get_class = JavaStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertFalse(result.get("success"))
+ self.assertTrue("Compilation Error" in result.get("error"))
+
+ def test_infinite_loop(self):
+
+ user_answer = dedent("""
+ class Test
+ {public static void main(String[] args){
+ while(0==0)
+ {
+ System.out.print("a");}
+ }}""")
+ get_class = JavaStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertFalse(result.get("success"))
+ self.assertEquals(result.get("error"), self.timeout_msg)
+
+ def test_only_stdout(self):
+ self.test_case_data = [{'expected_output': '11',
+ 'expected_input': ''}]
+ user_answer = dedent("""
+ class Test
+ {public static void main(String[] args){
+ int a = 5;
+ int b = 6;
+ System.out.print(a+b);
+ }}""")
+ get_class = JavaStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_string_input(self):
+ self.test_case_data = [{'expected_output': 'HelloWorld',
+ 'expected_input': 'Hello\nWorld'}]
+ user_answer = dedent("""
+ import java.util.Scanner;
+ class Test
+ {public static void main(String[] args){
+ Scanner s = new Scanner(System.in);
+ String a = s.nextLine();
+ String b = s.nextLine();
+ System.out.print(a+b);
+ }}""")
+ get_class = JavaStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
if __name__ == '__main__':
unittest.main()
diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py
index b432630..e46b21f 100644
--- a/yaksh/evaluator_tests/test_python_evaluation.py
+++ b/yaksh/evaluator_tests/test_python_evaluation.py
@@ -1,7 +1,7 @@
import unittest
import os
from yaksh.python_assertion_evaluator import PythonAssertionEvaluator
-from yaksh.python_stdout_evaluator import PythonStdoutEvaluator
+from yaksh.python_stdio_evaluator import PythonStdioEvaluator
from yaksh.settings import SERVER_TIMEOUT
from textwrap import dedent
@@ -52,7 +52,7 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase):
self.assertFalse(result.get('success'))
self.assertEqual(result.get('error'), self.timeout_msg)
- def test_syntax_error(self):
+
user_answer = dedent("""
def add(a, b);
return a + b
@@ -173,16 +173,16 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase):
return int(a) + int(b) + int(c)
""")
value_error_msg = ["Traceback",
- "call",
- "ValueError",
- "invalid literal",
- "base"
- ]
+ "call",
+ "ValueError",
+ "invalid literal",
+ "base"
+ ]
get_class = PythonAssertionEvaluator()
kwargs = {'user_answer': user_answer,
- 'test_case_data': self.test_case_data,
- 'file_paths': self.file_paths
- }
+ 'test_case_data': self.test_case_data,
+ 'file_paths': self.file_paths
+ }
result = get_class.evaluate(**kwargs)
err = result.get("error").splitlines()
self.assertFalse(result.get("success"))
@@ -209,73 +209,150 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase):
class PythonStdoutEvaluationTestCases(unittest.TestCase):
def setUp(self):
- self.test_case_data = [{"expected_output": "0 1 1 2 3"}]
+ self.test_case_data = [{"expected_input": None,
+ "expected_output": "0 1 1 2 3"
+ }]
+
self.timeout_msg = ("Code took more than {0} seconds to run. "
- "You probably have an infinite loop"
- " in your code.").format(SERVER_TIMEOUT)
+ "You probably have an infinite loop"
+ " in your code.").format(SERVER_TIMEOUT)
self.file_paths = None
def test_correct_answer(self):
user_answer = "a,b=0,1\nfor i in range(5):\n\tprint a,\n\ta,b=b,a+b"
- get_class = PythonStdoutEvaluator()
+ get_class = PythonStdioEvaluator()
kwargs = {'user_answer': user_answer,
- 'test_case_data': self.test_case_data,
- 'file_paths': self.file_paths
- }
+ '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.assertEqual(result.get('error'), "Correct Answer")
self.assertTrue(result.get('success'))
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 = PythonStdoutEvaluator()
+ get_class = PythonStdioEvaluator()
kwargs = {'user_answer': user_answer,
- 'test_case_data': self.test_case_data,
- 'file_paths': self.file_paths
- }
+ 'test_case_data': self.test_case_data,
+ 'file_paths': self.file_paths
+ }
result = get_class.evaluate(**kwargs)
self.assertFalse(result.get('success'))
- self.assertEqual(result.get('error'), "Incorrect Answer")
+ self.assertIn("Incorrect Answer", result.get('error'))
- def test_direct_printed_answer(self):
- user_answer = "print '0 1 1 2 3'"
- error_msg = ("Incorrect Answer: Please avoid printing"
- " the expected output directly"
- )
- get_class = PythonStdoutEvaluator()
- kwargs = {'user_answer': user_answer,
- 'test_case_data': self.test_case_data,
- 'file_paths': self.file_paths
- }
+ def test_infinite_loop(self):
+ user_answer = "def add(a, b):\n\twhile True:\n\t\tpass\nadd(1,2)"
+ get_class = PythonStdioEvaluator()
+ kwargs = {'user_answer': user_answer,
+ 'test_case_data': self.test_case_data
+ }
+
+class PythonStdIOEvaluator(unittest.TestCase):
+
+ def setUp(self):
+ self.timeout_msg = ("Code took more than {0} seconds to run. "
+ "You probably have an infinite loop"
+ " in your code.").format(SERVER_TIMEOUT)
+
+ def test_add_two_integers_correct(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.assertEqual(result.get('error'), error_msg)
+ 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()
- def test_infinite_loop(self):
- user_answer = "def add(a, b):\n\twhile True:\n\t\tpass"
- get_class = PythonStdoutEvaluator()
kwargs = {'user_answer': user_answer,
- 'test_case_data': self.test_case_data,
- 'file_paths': self.file_paths
- }
+ '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_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_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.assertEqual(result.get('error'), 'Incorrect Answer')
+ self.assertIn("Incorrect Answer", result.get('error'))
def test_file_based_answer(self):
- self.test_case_data = [{"expected_output": "2\n\n"}]
+ 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:
- print f.read()
- """)
- get_class = PythonStdoutEvaluator()
+ 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
- }
+ '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.assertEqual(result.get('error'), "Correct Answer")
self.assertTrue(result.get('success'))
if __name__ == '__main__':