def test_matches(self): clm = Matcher() clm.add_handler(NoneTemplate(), lambda: self.return_0(), True) clm.add_handler(ConstTemplate(), lambda a, b: self.const_call(a, b), True) self.assertEqual(clm.matches("ASDASD"), "const_call")
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())
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())