-
Notifications
You must be signed in to change notification settings - Fork 0
/
please_test.py
148 lines (110 loc) · 5.84 KB
/
please_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import sys
import shutil
import unittest
import filecmp
from please.command_line.template import Template
from please.command_line.matcher import Matcher
from please.command_line import generate_tests
from please.command_line.commands import print_help
from please.command_line.generate_tests import generate_tests, generate_tests_with_tags
from please.checkers.standard_checkers_utils import add_standard_checker_to_solution
from please.template import problem_template_generator as problem_gen
from please.package import package_config
from please.tags import add_tags, show_tags, clear_tags
from please.latex import latex_tools
import please.globalconfig as globalconfig
from io import StringIO
class PleaseTest(unittest.TestCase):
def ifed(self):
if os.path.exists("problem_test"):
shutil.rmtree("problem_test")
def setUp(self):
self.ifed()
self.__matcher = Matcher()
self.__matcher.startdir = '.'
self.__matcher.add_handler(Template(["create", "problem", "#shortname"]), problem_gen.generate_problem, True)
self.__matcher.matches("create problem problem_test".split())
def tearDown(self):
self.ifed()
def test_problem_creation(self):
""" Checks command 'create problem problem_name' """
self.assertTrue(os.path.exists("problem_test"))
self.assertTrue(os.path.exists(os.path.join("problem_test", "solutions")))
self.assertTrue(os.path.exists(os.path.join("problem_test", "statements")))
self.assertTrue(os.path.exists(os.path.join("problem_test", "default.package")))
self.assertTrue(os.path.exists(os.path.join("problem_test", "tests.please")))
def test_add_tags(self):
""" Checks command 'add tags tag1 tag2 ... tagN' """
start_dir = os.getcwd()
os.chdir("problem_test")
#package_config.PackageConfig.configs_dict = {}
self.__matcher.add_handler(Template(["add", "tag|tags", "@tags"]), add_tags, True)
self.__matcher.matches("add tags tag1 tag2 tag3 tag4".split())
open_config = package_config.PackageConfig.get_config(ignore_cache = True)
os.chdir(start_dir)
self.assertEqual(open_config["tags"], ["tag1", "tag2", "tag3", "tag4"])
def test_show_tags(self):
""" Checks command 'show tags' """
start_dir = os.getcwd()
os.chdir("problem_test")
self.__matcher.add_handler(Template(["add", "tag|tags", "@tags"]), add_tags, True)
self.__matcher.matches("add tags tag1 tag2 tag3 tag4".split())
saveout = sys.stdout
sys.stdout = StringIO()
self.__matcher.add_handler(Template(["show", "tags"]), show_tags, True)
self.__matcher.matches("show tags".split())
tags_from_std = sys.stdout.getvalue().split("\n", 1)[0]
sys.stdout = saveout
open_config = package_config.PackageConfig.get_config(ignore_cache = True)
os.chdir(start_dir)
self.assertEqual(open_config["tags"], tags_from_std.split("; "))
def test_clear_tags(self):
""" Checks command 'clear tags' """
start_dir = os.getcwd()
os.chdir("problem_test")
self.__matcher.add_handler(Template(["add", "tag|tags", "@tags"]), add_tags, True)
self.__matcher.matches("add tags tag1 tag2 tag3 tag4".split())
self.__matcher.add_handler(Template(["clear", "tags"]), clear_tags, True)
self.__matcher.matches("clear tags".split())
saveout = sys.stdout
sys.stdout = StringIO()
self.__matcher.add_handler(Template(["show", "tags"]), show_tags, True)
self.__matcher.matches("show tags".split())
ttt = sys.stdout.getvalue()
tags_from_std = ttt.split("\n")[0]
sys.stdout = saveout
open_config = package_config.PackageConfig.get_config(ignore_cache = True)
os.chdir(start_dir)
self.assertEqual(tags_from_std, "")
def test_add_standard_checker(self):
""" Checks command 'add standard checker checker_name' """
start_dir = os.getcwd()
os.chdir("problem_test")
open_config = package_config.PackageConfig.get_config()
self.__matcher.add_handler(Template(["add", "standard", "checker", "#checker"]), add_standard_checker_to_solution, True)
self.__matcher.matches("add standard checker wcmp".split())
os.remove("wcmp.cpp")
os.chdir(start_dir)
self.assertEqual(os.path.join(start_dir, "please", "checkers", "wcmp.cpp"), open_config["checker"])
def test_generate_statement(self):
""" Checks command 'generate statement' """
start_dir = os.getcwd()
#shutil.copy(os.path.join("island", "statements", "default.ru.pdf"), ".")
test_problem_dir = os.path.join("test_problems", "island")
os.chdir(test_problem_dir)
if os.path.exists(os.path.join("statements", "default.ru.pdf")):
os.remove(os.path.join("statements", "default.ru.pdf"))
self.__matcher.add_handler(Template(["generate", "statement"]), latex_tools.generate_problem, True)
self.__matcher.matches("generate statement".split())
os.chdir(start_dir)
self.assertTrue(os.path.exists(os.path.join(test_problem_dir, "statements", "default.ru.pdf")))
def test_help(self):
""" Checks command 'help' """
open_config = package_config.PackageConfig.get_config('.')
in_problem_folder = (package_config != False)
globalconfig.in_problem_folder = in_problem_folder
self.__matcher.add_handler(Template(["help"]), print_help, True)
self.__matcher.matches("help".split())
if __name__ == "__main__":
unittest.main()