def setUp(self): self.path = "../test/testconfig.ini" # Создадим небольшой файл настроек with open(self.path, "w", encoding='utf8') as fp: fp.write("[Test]\n") fp.write("intval=100\n") fp.write("boolval=True\n") fp.write("datetimeval=2012-08-25 16:18:24.171654\n") fp.write("datetimeerror=sdfasdfasdf\n") fp.write("strval=тест\n") fp.write( "style_01=fore:#AAAAAA,back:#111111,bold,italic,underline\n") fp.write("style_02=back:#111111,bold,italic,underline\n") fp.write("style_03=bold\n") fp.write("style_invalid_01=asdfadsfads\n") fp.write("list1=элемент 1;элемент 2;элемент 3\n") fp.write("list2=элемент 1\n") fp.write("list3=\n") fp.write("list4=;\n") fp.write("list5=;;\n") fp.write("list6=элемент 1;\n") fp.write("list8=элемент 1|элемент 2|элемент 3\n") fp.write("list9=элемент 1\n") fp.write("list10=\n") fp.write("list11=|\n") fp.write("list12=элемент 1|\n") self.config = Config(self.path)
def testHasSection(self): config = Config(self.path) config.set("Секция 1", "Параметр 1", "Значение 1") config.set("Секция 1", "Параметр 2", 111) self.assertEqual(config.has_section("Секция 1"), True) config.remove_section("Секция 1") self.assertEqual(config.has_section("Секция 1"), False)
def testWrite(self): """ Тесты на то, что измененные значения сразу сохраняются в файл """ config = Config(self.path) config.set("Секция 1", "Параметр 1", "Значение 1") config.set("Секция 1", "Параметр 2", 111) config2 = Config(self.path) self.assertEqual(config2.get("Секция 1", "Параметр 1"), "Значение 1") self.assertEqual(config2.getint("Секция 1", "Параметр 2"), 111)
def testWrite (self): """ Тесты на то, что измененные значения сразу сохраняются в файл """ config = Config (self.path) config.set (u"Секция 1", u"Параметр 1", u"Значение 1") config.set (u"Секция 1", u"Параметр 2", 111) config2 = Config (self.path) self.assertEqual (config2.get (u"Секция 1", u"Параметр 1"), u"Значение 1") self.assertEqual (config2.getint (u"Секция 1", u"Параметр 2"), 111)
def testRemoveSection(self): config = Config(self.path) config.set("Секция 1", "Параметр 1", "Значение 1") config.set("Секция 1", "Параметр 2", 111) config.remove_section("Секция 1") config2 = Config(self.path) self.assertRaises(configparser.NoSectionError, config2.get, "Секция 1", "Параметр 1")
def testSave(self): config = Config(self.path) recent = RecentWiki(config) recent.add("path1") recent.add("path2") recent.add("path3") config2 = Config(self.path) recent2 = RecentWiki(config2) self.assertEqual(len(recent2), 3) self.assertEqual(recent2[0], "path3") self.assertEqual(recent2[1], "path2") self.assertEqual(recent2[2], "path1")
def testGetSet(self): config = Config(self.path) config.set("Секция 1", "Параметр 1", "Значение 1") config.set("Секция 1", "Параметр 2", 111) self.assertEqual(config.get("Секция 1", "Параметр 1"), "Значение 1") self.assertEqual(config.getint("Секция 1", "Параметр 2"), 111)
def init(self, configFilename): """ Initialize config and locale """ self.config = Config(configFilename) self.recentWiki = RecentWiki(self.config) self.__initLocale()
def init(self, fullConfigPath): """ Initialize config and locale """ self.fullConfigPath = fullConfigPath self.config = Config(fullConfigPath) self.recentWiki = RecentWiki(self.config)
def setUp (self): self.path = u"../test/testconfig.ini" # Создадим небольшой файл настроек with open (self.path, "wb") as fp: fp.write (u"[Test]\n") fp.write (u"intval=100\n") fp.write (u"boolval=True\n") fp.write (u"datetimeval=2012-08-25 16:18:24.171654\n") fp.write (u"datetimeerror=sdfasdfasdf\n") fp.write (u"strval=тест\n".encode ("utf-8")) fp.write (u"list1=элемент 1;элемент 2;элемент 3\n".encode ("utf-8")) fp.write (u"list2=элемент 1\n".encode ("utf-8")) fp.write (u"list3=\n".encode ("utf-8")) fp.write (u"list4=;\n".encode ("utf-8")) fp.write (u"list5=;;\n".encode ("utf-8")) fp.write (u"list6=элемент 1;\n".encode ("utf-8")) fp.write (u"list8=элемент 1|элемент 2|элемент 3\n".encode ("utf-8")) fp.write (u"list9=элемент 1\n".encode ("utf-8")) fp.write (u"list10=\n".encode ("utf-8")) fp.write (u"list11=|\n".encode ("utf-8")) fp.write (u"list12=элемент 1|\n".encode ("utf-8")) self.config = Config (self.path)
def testBoolOpt3(self): opt = BooleanOption(self.config, "Test", "Boolval3", False) opt.value = True newconfig = Config(self.path) newopt = BooleanOption(newconfig, "Test", "Boolval3", False) self.assertEqual(newopt.value, True)
def testHotKeyOptionEmptyConfig(self): config = Config(self.path) hotKeyDefault = HotKey("F11", ctrl=True) section = "TestHotKey" paramName = "MyHotKey" option = HotKeyOption(config, section, paramName, hotKeyDefault) self.assertEqual(option.value, hotKeyDefault)
def testIntOpt3(self): opt = IntegerOption(self.config, "Test", "intval3", 777) opt.value = 666 newconfig = Config(self.path) newopt = IntegerOption(newconfig, "Test", "intval3", 888) self.assertEqual(newopt.value, 666)
def testStringOpt3(self): opt = StringOption(self.config, "Test", "strval3", "defaultval") opt.value = "проверка" newconfig = Config(self.path) newopt = StringOption(newconfig, "Test", "strval3", "defaultval") self.assertEqual(newopt.value, "проверка")
def testGetSet (self): config = Config (self.path) config.set (u"Секция 1", u"Параметр 1", u"Значение 1") config.set (u"Секция 1", u"Параметр 2", 111) self.assertEqual (config.get (u"Секция 1", u"Параметр 1"), u"Значение 1") self.assertEqual (config.getint (u"Секция 1", u"Параметр 2"), 111)
def testDateTimeOpt4(self): newdate = datetime.datetime(2012, 8, 25) opt = DateTimeOption(self.config, "Test", "datetimeval2", None) opt.value = newdate newconfig = Config(self.path) newopt = DateTimeOption(newconfig, "Test", "datetimeval2", None) self.assertEqual(newopt.value, newdate)
def testRead(self): config = Config(self.path) content1 = EmptyContent(config) content2 = EmptyContent(config) content1.content = self.text # Проверим, что есть какое-то непустое значение по умолчанию self.assertEqual(content2.content, self.text) self.assertEqual(content2.content, content1.content)
def testRemoveSection(self): config = Config(self.path) config.set(u"Секция 1", u"Параметр 1", u"Значение 1") config.set(u"Секция 1", u"Параметр 2", 111) config.remove_section(u"Секция 1") config2 = Config(self.path) self.assertRaises(ConfigParser.NoSectionError, config2.get, u"Секция 1", u"Параметр 1")
def testInvalidConfigFile(self): invalid_fname = "outwiker_invalid.ini" src_invalid_full_path = os.path.join("../test", "samplefiles", invalid_fname) shutil.copy(src_invalid_full_path, self.tempdir) fullpath = os.path.join(self.tempdir, invalid_fname) Config(fullpath) self.assertTrue(os.path.exists(fullpath)) self.assertTrue(os.path.exists(fullpath + ".bak"))
def testSaveListOption2(self): testlist = [u"элемент 1"] opt = ListOption(self.config, u"Test", u"savelist", []) opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, u"Test", u"savelist", []) self.assertEqual(newopt.value, testlist) stringopt = StringOption(newconfig, u"Test", u"savelist", "") self.assertEqual(stringopt.value.strip(), u"элемент 1")
def testHotKeyWrite2(self): config = Config(self.path) section = "TestHotKey" paramName = "MyHotKey" option = HotKeyOption(config, section, paramName, None) option.value = None with open(self.path) as fp: resultFile = fp.read() self.assertTrue("[TestHotKey]" in resultFile) self.assertTrue("myhotkey =" in resultFile)
def testSaveListOption3(self): testlist = [] opt = ListOption(self.config, "Test", "savelist", []) opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", []) self.assertEqual(newopt.value, [""]) stringopt = StringOption(newconfig, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "")
def testSaveListOption9(self): testlist = [""] opt = ListOption(self.config, "Test", "savelist", [], separator="|") opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", [], separator="|") self.assertEqual(newopt.value, testlist) stringopt = StringOption(self.config, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "")
def testReadOption3(self): with open(self.path, "w") as fp: fp.write("""[TestHotKey] MyHotKey=Ctrl+""") config = Config(self.path) section = "TestHotKey" paramName = "MyHotKey" option = HotKeyOption(config, section, paramName, None) result = option.value self.assertEqual(result, None)
def testSaveListOption4(self): testlist = [u"элемент 1", u"элемент 2", u"элемент 3"] opt = ListOption(self.config, u"Test", u"savelist", [], separator="|") opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, u"Test", u"savelist", [], separator="|") self.assertEqual(newopt.value, testlist) stringopt = StringOption(newconfig, u"Test", u"savelist", "") self.assertEqual(stringopt.value.strip(), u"элемент 1|элемент 2|элемент 3")
def testSaveListOption1(self): testlist = ["элемент 1", "элемент 2", "элемент 3"] opt = ListOption(self.config, "Test", "savelist", []) opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", []) self.assertEqual(newopt.value, testlist) stringopt = StringOption(newconfig, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "элемент 1;элемент 2;элемент 3")
def testHotKeyWrite1(self): config = Config(self.path) hotkey = HotKey("F11", ctrl=True) section = "TestHotKey" paramName = "MyHotKey" option = HotKeyOption(config, section, paramName, None) option.value = hotkey with open(self.path) as fp: resultFile = fp.read() self.assertTrue("[TestHotKey]" in resultFile) self.assertTrue("myhotkey = Ctrl+F11" in resultFile)
def testReadOption4(self): with open(self.path, "w") as fp: fp.write("""[TestHotKey] MyHotKey=Ctrl+DEL""") config = Config(self.path) section = "TestHotKey" paramName = "MyHotKey2" hotKeyDefault = HotKey("F11", ctrl=True) option = HotKeyOption(config, section, paramName, hotKeyDefault) result = option.value self.assertEqual(result, hotKeyDefault)
def testRepeat2(self): """ Проверка случая, когда один и тот же файл открывается несолько раз """ config = Config(self.path) recent = RecentWiki(config) recent.add("path1") recent.add("path2") recent.add("path3") recent.add("path1") self.assertEqual(len(recent), 3) self.assertEqual(recent[0], "path1") self.assertEqual(recent[1], "path3") self.assertEqual(recent[2], "path2")
def testReadOption1(self): with open(self.path, "w") as fp: fp.write("""[TestHotKey] MyHotKey=Ctrl+T""") config = Config(self.path) section = "TestHotKey" paramName = "MyHotKey" option = HotKeyOption(config, section, paramName, None) result = option.value self.assertNotEqual(result, None) self.assertEqual(result.key, "T") self.assertTrue(result.ctrl) self.assertFalse(result.alt) self.assertFalse(result.shift)
def testAdd1(self): config = Config(self.path) recent = RecentWiki(config) self.assertEqual(len(recent), 0) recent.add("path1") self.assertEqual(len(recent), 1) self.assertEqual(recent[0], "path1") recent.add("path2") self.assertEqual(len(recent), 2) self.assertEqual(recent[0], "path2") self.assertEqual(recent[1], "path1") recent.add("path3") self.assertEqual(len(recent), 3) self.assertEqual(recent[0], "path3") self.assertEqual(recent[1], "path2") self.assertEqual(recent[2], "path1")
def testLength1(self): """ Тесты, связанные с длиной списка последних открытых вики """ config = Config(self.path) recent = RecentWiki(config) # Значение по умолчанию self.assertEqual(recent.maxlen, 5) recent.add("path1") recent.add("path2") recent.add("path3") recent.add("path4") recent.add("path5") recent.add("path6") self.assertEqual(len(recent), recent.maxlen) self.assertEqual(recent[0], "path6") self.assertEqual(recent[1], "path5") self.assertEqual(recent[2], "path4") self.assertEqual(recent[3], "path3") self.assertEqual(recent[4], "path2")
def setUp(self): self.path = "../test/testconfig.ini" # Создадим небольшой файл настроек with open(self.path, "w", encoding='utf8') as fp: fp.write("[Test]\n") fp.write("intval=100\n") fp.write("boolval=True\n") fp.write("datetimeval=2012-08-25 16:18:24.171654\n") fp.write("datetimeerror=sdfasdfasdf\n") fp.write("strval=тест\n") fp.write("style_01=fore:#AAAAAA,back:#111111,bold,italic,underline\n") fp.write("style_02=back:#111111,bold,italic,underline\n") fp.write("style_03=bold\n") fp.write("style_invalid_01=asdfadsfads\n") fp.write("list1=элемент 1;элемент 2;элемент 3\n") fp.write("list2=элемент 1\n") fp.write("list3=\n") fp.write("list4=;\n") fp.write("list5=;;\n") fp.write("list6=элемент 1;\n") fp.write("list8=элемент 1|элемент 2|элемент 3\n") fp.write("list9=элемент 1\n") fp.write("list10=\n") fp.write("list11=|\n") fp.write("list12=элемент 1|\n") fp.write("jsonval_01={}\n") fp.write('jsonval_02="строка"\n') fp.write("jsonval_03=[]\n") fp.write("jsonval_04=[1, 2, 3]\n") fp.write('jsonval_05={"x": 100}\n') self.config = Config(self.path)
def setUp(self): self.path = "../test/testconfig.ini" self.config = Config(self.path) self.trayConfig = TrayConfig(self.config)
class ConfigOptionsTest (unittest.TestCase): def setUp (self): self.path = u"../test/testconfig.ini" # Создадим небольшой файл настроек with open (self.path, "wb") as fp: fp.write (u"[Test]\n") fp.write (u"intval=100\n") fp.write (u"boolval=True\n") fp.write (u"datetimeval=2012-08-25 16:18:24.171654\n") fp.write (u"datetimeerror=sdfasdfasdf\n") fp.write (u"strval=тест\n".encode ("utf-8")) fp.write (u"list1=элемент 1;элемент 2;элемент 3\n".encode ("utf-8")) fp.write (u"list2=элемент 1\n".encode ("utf-8")) fp.write (u"list3=\n".encode ("utf-8")) fp.write (u"list4=;\n".encode ("utf-8")) fp.write (u"list5=;;\n".encode ("utf-8")) fp.write (u"list6=элемент 1;\n".encode ("utf-8")) fp.write (u"list8=элемент 1|элемент 2|элемент 3\n".encode ("utf-8")) fp.write (u"list9=элемент 1\n".encode ("utf-8")) fp.write (u"list10=\n".encode ("utf-8")) fp.write (u"list11=|\n".encode ("utf-8")) fp.write (u"list12=элемент 1|\n".encode ("utf-8")) self.config = Config (self.path) def tearDown (self): os.remove (self.path) # Строковые опции def testStringOpt1 (self): opt = StringOption (self.config, u"Test", u"strval", "defaultval") self.assertEqual (opt.value, u"тест") def testStringOpt2 (self): opt = StringOption (self.config, u"Test", u"strval2", "defaultval") self.assertEqual (opt.value, u"defaultval") def testStringOpt3 (self): opt = StringOption (self.config, u"Test", u"strval3", "defaultval") opt.value = u"проверка" newconfig = Config (self.path) newopt = StringOption (newconfig, u"Test", u"strval3", "defaultval") self.assertEqual (newopt.value, u"проверка") def testStringOpt4 (self): opt = StringOption (self.config, u"Test", u"strval3", "defaultval") newopt = StringOption (self.config, u"Test", u"strval3", "defaultval") opt.value = u"проверка" self.assertEqual (newopt.value, u"проверка") # Целочисленные опции def testIntOpt1 (self): opt = IntegerOption (self.config, u"Test", u"intval", 777) self.assertEqual (opt.value, 100) def testIntOpt2 (self): opt = IntegerOption (self.config, u"Test", u"intval2", 777) self.assertEqual (opt.value, 777) def testIntOpt3 (self): opt = IntegerOption (self.config, u"Test", u"intval3", 777) opt.value = 666 newconfig = Config (self.path) newopt = IntegerOption (newconfig, u"Test", u"intval3", 888) self.assertEqual (newopt.value, 666) def testIntOpt4 (self): opt = IntegerOption (self.config, u"Test", u"intval3", 777) newopt = IntegerOption (self.config, u"Test", u"intval3", 888) opt.value = 666 self.assertEqual (newopt.value, 666) # Опции для хранения даты/времени def testDateTimeOpt1 (self): strdatetime = "2012-08-25 16:18:24.171654" opt = DateTimeOption (self.config, u"Test", u"datetimeval", None) self.assertEqual (opt.value, datetime.datetime.strptime (strdatetime, DateTimeOption.formatDate)) def testDateTimeOpt2 (self): opt = DateTimeOption (self.config, u"Test", u"datetimeval_invalid", None) self.assertEqual (opt.value, None) def testDateTimeOpt3 (self): defaultValue = datetime.datetime (2012, 8, 25) opt = DateTimeOption (self.config, u"Test", u"datetimeval_invalid", defaultValue) self.assertEqual (opt.value, defaultValue) def testDateTimeOpt4 (self): newdate = datetime.datetime (2012, 8, 25) opt = DateTimeOption (self.config, u"Test", u"datetimeval2", None) opt.value = newdate newconfig = Config (self.path) newopt = DateTimeOption (newconfig, u"Test", u"datetimeval2", None) self.assertEqual (newopt.value, newdate) def testDateTimeOpt5 (self): defaultValue = datetime.datetime (2012, 8, 25) opt = DateTimeOption (self.config, u"Test", u"datetimeerror", defaultValue) self.assertEqual (opt.value, defaultValue) # Булевы опции def testBoolOpt1 (self): opt = BooleanOption (self.config, u"Test", u"Boolval", False) self.assertEqual (opt.value, True) def testBoolOpt2 (self): opt = BooleanOption (self.config, u"Test", u"Boolval2", False) self.assertEqual (opt.value, False) def testBoolOpt3 (self): opt = BooleanOption (self.config, u"Test", u"Boolval3", False) opt.value = True newconfig = Config (self.path) newopt = BooleanOption (newconfig, u"Test", u"Boolval3", False) self.assertEqual (newopt.value, True) def testBoolOpt4 (self): opt = BooleanOption (self.config, u"Test", u"Boolval3", False) newopt = BooleanOption (self.config, u"Test", u"Boolval3", False) opt.value = True self.assertEqual (newopt.value, True) def testRemoveOption1 (self): opt = StringOption (self.config, u"Test", u"strval", u"Значение по умолчанию") self.assertEqual (opt.value, u"тест") opt.remove_option() self.assertEqual (opt.value, u"Значение по умолчанию") def testRemoveOption2 (self): opt = StringOption (self.config, u"Test", u"strval", u"Значение по умолчанию") opt.remove_option() opt2 = StringOption (self.config, u"Test", u"strval", u"Значение по умолчанию") self.assertEqual (opt2.value, u"Значение по умолчанию") def testRemoveOption3 (self): opt = StringOption (self.config, u"Test", u"invalid", u"Значение по умолчанию") opt.remove_option() def testListOption1 (self): opt1 = ListOption (self.config, u"Test", u"list1", []) self.assertEqual (opt1.value, [u"элемент 1", u"элемент 2", u"элемент 3"]) opt2 = ListOption (self.config, u"Test", u"list2", []) self.assertEqual (opt2.value, [u"элемент 1"]) opt3 = ListOption (self.config, u"Test", u"list3", []) self.assertEqual (opt3.value, [u""]) opt4 = ListOption (self.config, u"Test", u"list4", []) self.assertEqual (opt4.value, [u"", u""]) opt5 = ListOption (self.config, u"Test", u"list5", []) self.assertEqual (opt5.value, [u"", u"", u""]) opt6 = ListOption (self.config, u"Test", u"list6", []) self.assertEqual (opt6.value, [u"элемент 1", u""]) opt7 = ListOption (self.config, u"Test", u"list7", []) self.assertEqual (opt7.value, []) def testListOption2 (self): opt8 = ListOption (self.config, u"Test", u"list8", [], separator="|") self.assertEqual (opt8.value, [u"элемент 1", u"элемент 2", u"элемент 3"]) opt9 = ListOption (self.config, u"Test", u"list9", [], separator="|") self.assertEqual (opt9.value, [u"элемент 1"]) opt10 = ListOption (self.config, u"Test", u"list10", [], separator="|") self.assertEqual (opt10.value, [u""]) opt11 = ListOption (self.config, u"Test", u"list11", [], separator="|") self.assertEqual (opt11.value, [u"", u""]) opt12 = ListOption (self.config, u"Test", u"list12", [], separator="|") self.assertEqual (opt12.value, [u"элемент 1", u""]) def testSaveListOption1 (self): testlist = [u"элемент 1", u"элемент 2", u"элемент 3"] opt = ListOption (self.config, u"Test", u"savelist", []) opt.value = testlist newconfig = Config (self.path) newopt = ListOption (self.config, u"Test", u"savelist", []) self.assertEqual (newopt.value, testlist) stringopt = StringOption (self.config, u"Test", u"savelist", "") self.assertEqual (stringopt.value.strip(), u"элемент 1;элемент 2;элемент 3") def testSaveListOption2 (self): testlist = [u"элемент 1"] opt = ListOption (self.config, u"Test", u"savelist", []) opt.value = testlist newconfig = Config (self.path) newopt = ListOption (self.config, u"Test", u"savelist", []) self.assertEqual (newopt.value, testlist) stringopt = StringOption (self.config, u"Test", u"savelist", "") self.assertEqual (stringopt.value.strip(), u"элемент 1") def testSaveListOption3 (self): testlist = [] opt = ListOption (self.config, u"Test", u"savelist", []) opt.value = testlist newconfig = Config (self.path) newopt = ListOption (self.config, u"Test", u"savelist", []) self.assertEqual (newopt.value, [u""]) stringopt = StringOption (self.config, u"Test", u"savelist", "") self.assertEqual (stringopt.value.strip(), u"") def testSaveListOption4 (self): testlist = [u"элемент 1", u"элемент 2", u"элемент 3"] opt = ListOption (self.config, u"Test", u"savelist", [], separator="|") opt.value = testlist newconfig = Config (self.path) newopt = ListOption (self.config, u"Test", u"savelist", [], separator="|") self.assertEqual (newopt.value, testlist) stringopt = StringOption (self.config, u"Test", u"savelist", "") self.assertEqual (stringopt.value.strip(), u"элемент 1|элемент 2|элемент 3") def testSaveListOption5 (self): testlist = [u"элемент 1"] opt = ListOption (self.config, u"Test", u"savelist", [], separator="|") opt.value = testlist newconfig = Config (self.path) newopt = ListOption (self.config, u"Test", u"savelist", [], separator="|") self.assertEqual (newopt.value, testlist) stringopt = StringOption (self.config, u"Test", u"savelist", "") self.assertEqual (stringopt.value.strip(), u"элемент 1") def testSaveListOption6 (self): testlist = [] opt = ListOption (self.config, u"Test", u"savelist", [], separator="|") opt.value = testlist newconfig = Config (self.path) newopt = ListOption (self.config, u"Test", u"savelist", [], separator="|") self.assertEqual (newopt.value, [""]) stringopt = StringOption (self.config, u"Test", u"savelist", "") self.assertEqual (stringopt.value.strip(), u"") def testSaveListOption7 (self): testlist = [u""] opt = ListOption (self.config, u"Test", u"savelist", [], separator="|") opt.value = testlist newconfig = Config (self.path) newopt = ListOption (self.config, u"Test", u"savelist", [], separator="|") self.assertEqual (newopt.value, testlist) stringopt = StringOption (self.config, u"Test", u"savelist", "") self.assertEqual (stringopt.value.strip(), u"") def testSaveListOption8 (self): testlist = [u""] opt = ListOption (self.config, u"Test", u"savelist", []) opt.value = testlist newconfig = Config (self.path) newopt = ListOption (self.config, u"Test", u"savelist", []) self.assertEqual (newopt.value, testlist) stringopt = StringOption (self.config, u"Test", u"savelist", "") self.assertEqual (stringopt.value.strip(), u"") def testSaveListOption9 (self): testlist = [u""] opt = ListOption (self.config, u"Test", u"savelist", [], separator="|") opt.value = testlist newconfig = Config (self.path) newopt = ListOption (self.config, u"Test", u"savelist", [], separator="|") self.assertEqual (newopt.value, testlist) stringopt = StringOption (self.config, u"Test", u"savelist", "") self.assertEqual (stringopt.value.strip(), u"") def testStringListSection1 (self): section = u"listSection" paramname = u"param_" testlist = [u"Бла-бла-бла", u"Строка 1", u"Строка 2"] self.config.remove_section (section) opt = StringListSection (self.config, section, paramname) self.assertEqual (opt.value, []) # Установим список opt.value = testlist self.assertEqual (len (opt.value), 3) self.assertEqual (opt.value[0], u"Бла-бла-бла") self.assertEqual (opt.value[1], u"Строка 1") self.assertEqual (opt.value[2], u"Строка 2") self.assertTrue (self.config.has_section (section)) self.assertEqual (self.config.get (section, u"param_0"), u"Бла-бла-бла") self.assertEqual (self.config.get (section, u"param_1"), u"Строка 1") self.assertEqual (self.config.get (section, u"param_2"), u"Строка 2") self.config.remove_section (section) def testStringListSection2 (self): section = u"listSection" paramname = u"param_" testlist = [u"Бла-бла-бла", u"Строка 1", u"Строка 2"] self.config.remove_section (section) opt = StringListSection (self.config, section, paramname) self.assertEqual (opt.value, []) # Установим список opt.value = testlist opt_other = StringListSection (self.config, section, paramname) self.assertEqual (len (opt_other.value), 3) self.assertEqual (opt_other.value[0], u"Бла-бла-бла") self.assertEqual (opt_other.value[1], u"Строка 1") self.assertEqual (opt_other.value[2], u"Строка 2") self.config.remove_section (section)
class ConfigOptionsTest(unittest.TestCase): def setUp(self): self.path = "../test/testconfig.ini" # Создадим небольшой файл настроек with open(self.path, "w", encoding='utf8') as fp: fp.write("[Test]\n") fp.write("intval=100\n") fp.write("boolval=True\n") fp.write("datetimeval=2012-08-25 16:18:24.171654\n") fp.write("datetimeerror=sdfasdfasdf\n") fp.write("strval=тест\n") fp.write("style_01=fore:#AAAAAA,back:#111111,bold,italic,underline\n") fp.write("style_02=back:#111111,bold,italic,underline\n") fp.write("style_03=bold\n") fp.write("style_invalid_01=asdfadsfads\n") fp.write("list1=элемент 1;элемент 2;элемент 3\n") fp.write("list2=элемент 1\n") fp.write("list3=\n") fp.write("list4=;\n") fp.write("list5=;;\n") fp.write("list6=элемент 1;\n") fp.write("list8=элемент 1|элемент 2|элемент 3\n") fp.write("list9=элемент 1\n") fp.write("list10=\n") fp.write("list11=|\n") fp.write("list12=элемент 1|\n") fp.write("jsonval_01={}\n") fp.write('jsonval_02="строка"\n') fp.write("jsonval_03=[]\n") fp.write("jsonval_04=[1, 2, 3]\n") fp.write('jsonval_05={"x": 100}\n') self.config = Config(self.path) def tearDown(self): os.remove(self.path) # Строковые опции def testStringOpt1(self): opt = StringOption(self.config, "Test", "strval", "defaultval") self.assertEqual(opt.value, "тест") def testStringOpt2(self): opt = StringOption(self.config, "Test", "strval2", "defaultval") self.assertEqual(opt.value, "defaultval") def testStringOpt3(self): opt = StringOption(self.config, "Test", "strval3", "defaultval") opt.value = "проверка" newconfig = Config(self.path) newopt = StringOption(newconfig, "Test", "strval3", "defaultval") self.assertEqual(newopt.value, "проверка") def testStringOpt4(self): opt = StringOption(self.config, "Test", "strval3", "defaultval") newopt = StringOption(self.config, "Test", "strval3", "defaultval") opt.value = "проверка" self.assertEqual(newopt.value, "проверка") # Целочисленные опции def testIntOpt1(self): opt = IntegerOption(self.config, "Test", "intval", 777) self.assertEqual(opt.value, 100) def testIntOpt2(self): opt = IntegerOption(self.config, "Test", "intval2", 777) self.assertEqual(opt.value, 777) def testIntOpt3(self): opt = IntegerOption(self.config, "Test", "intval3", 777) opt.value = 666 newconfig = Config(self.path) newopt = IntegerOption(newconfig, "Test", "intval3", 888) self.assertEqual(newopt.value, 666) def testIntOpt4(self): opt = IntegerOption(self.config, "Test", "intval3", 777) newopt = IntegerOption(self.config, "Test", "intval3", 888) opt.value = 666 self.assertEqual(newopt.value, 666) # Опции для хранения даты/времени def testDateTimeOpt1(self): strdatetime = "2012-08-25 16:18:24.171654" opt = DateTimeOption(self.config, "Test", "datetimeval", None) self.assertEqual(opt.value, datetime.datetime.strptime( strdatetime, DateTimeOption.formatDate)) def testDateTimeOpt2(self): opt = DateTimeOption(self.config, "Test", "datetimeval_invalid", None) self.assertEqual(opt.value, None) def testDateTimeOpt3(self): defaultValue = datetime.datetime(2012, 8, 25) opt = DateTimeOption(self.config, "Test", "datetimeval_invalid", defaultValue) self.assertEqual(opt.value, defaultValue) def testDateTimeOpt4(self): newdate = datetime.datetime(2012, 8, 25) opt = DateTimeOption(self.config, "Test", "datetimeval2", None) opt.value = newdate newconfig = Config(self.path) newopt = DateTimeOption(newconfig, "Test", "datetimeval2", None) self.assertEqual(newopt.value, newdate) def testDateTimeOpt5(self): defaultValue = datetime.datetime(2012, 8, 25) opt = DateTimeOption(self.config, "Test", "datetimeerror", defaultValue) self.assertEqual(opt.value, defaultValue) # Булевы опции def testBoolOpt1(self): opt = BooleanOption(self.config, "Test", "Boolval", False) self.assertEqual(opt.value, True) def testBoolOpt2(self): opt = BooleanOption(self.config, "Test", "Boolval2", False) self.assertEqual(opt.value, False) def testBoolOpt3(self): opt = BooleanOption(self.config, "Test", "Boolval3", False) opt.value = True newconfig = Config(self.path) newopt = BooleanOption(newconfig, "Test", "Boolval3", False) self.assertEqual(newopt.value, True) def testBoolOpt4(self): opt = BooleanOption(self.config, "Test", "Boolval3", False) newopt = BooleanOption(self.config, "Test", "Boolval3", False) opt.value = True self.assertEqual(newopt.value, True) def testBoolOpt5(self): self.config.set('Test', 'Boolval', True) self.assertEqual(self.config.getbool('Test', 'Boolval'), True) def testBoolOpt6(self): self.config.set('Test', 'Boolval', False) self.assertEqual(self.config.getbool('Test', 'Boolval'), False) def testBoolOpt7(self): self.config.set('Test', 'Boolval', 'True') self.assertEqual(self.config.getbool('Test', 'Boolval'), True) def testBoolOpt8(self): self.config.set('Test', 'Boolval', 'False') self.assertEqual(self.config.getbool('Test', 'Boolval'), False) def testBoolOpt9(self): self.config.set('Test', 'Boolval', ' True ') self.assertEqual(self.config.getbool('Test', 'Boolval'), True) def testBoolOpt10(self): self.config.set('Test', 'Boolval', ' False ') self.assertEqual(self.config.getbool('Test', 'Boolval'), False) def testBoolOpt11(self): self.config.set('Test', 'Boolval', ' sdfasgfadsf ') self.assertEqual(self.config.getbool('Test', 'Boolval'), False) def testRemoveOption1(self): opt = StringOption(self.config, "Test", "strval", "Значение по умолчанию") self.assertEqual(opt.value, "тест") opt.remove_option() self.assertEqual(opt.value, "Значение по умолчанию") def testRemoveOption2(self): opt = StringOption(self.config, "Test", "strval", "Значение по умолчанию") opt.remove_option() opt2 = StringOption(self.config, "Test", "strval", "Значение по умолчанию") self.assertEqual(opt2.value, "Значение по умолчанию") def testRemoveOption3(self): opt = StringOption(self.config, "Test", "invalid", "Значение по умолчанию") opt.remove_option() def testListOption1(self): opt1 = ListOption(self.config, "Test", "list1", []) self.assertEqual(opt1.value, ["элемент 1", "элемент 2", "элемент 3"]) opt2 = ListOption(self.config, "Test", "list2", []) self.assertEqual(opt2.value, ["элемент 1"]) opt3 = ListOption(self.config, "Test", "list3", []) self.assertEqual(opt3.value, [""]) opt4 = ListOption(self.config, "Test", "list4", []) self.assertEqual(opt4.value, ["", ""]) opt5 = ListOption(self.config, "Test", "list5", []) self.assertEqual(opt5.value, ["", "", ""]) opt6 = ListOption(self.config, "Test", "list6", []) self.assertEqual(opt6.value, ["элемент 1", ""]) opt7 = ListOption(self.config, "Test", "list7", []) self.assertEqual(opt7.value, []) def testListOption2(self): opt8 = ListOption(self.config, "Test", "list8", [], separator="|") self.assertEqual(opt8.value, ["элемент 1", "элемент 2", "элемент 3"]) opt9 = ListOption(self.config, "Test", "list9", [], separator="|") self.assertEqual(opt9.value, ["элемент 1"]) opt10 = ListOption(self.config, "Test", "list10", [], separator="|") self.assertEqual(opt10.value, [""]) opt11 = ListOption(self.config, "Test", "list11", [], separator="|") self.assertEqual(opt11.value, ["", ""]) opt12 = ListOption(self.config, "Test", "list12", [], separator="|") self.assertEqual(opt12.value, ["элемент 1", ""]) def testSaveListOption1(self): testlist = ["элемент 1", "элемент 2", "элемент 3"] opt = ListOption(self.config, "Test", "savelist", []) opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", []) self.assertEqual(newopt.value, testlist) stringopt = StringOption(newconfig, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "элемент 1;элемент 2;элемент 3") def testSaveListOption2(self): testlist = ["элемент 1"] opt = ListOption(self.config, "Test", "savelist", []) opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", []) self.assertEqual(newopt.value, testlist) stringopt = StringOption(newconfig, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "элемент 1") def testSaveListOption3(self): testlist = [] opt = ListOption(self.config, "Test", "savelist", []) opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", []) self.assertEqual(newopt.value, [""]) stringopt = StringOption(newconfig, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "") def testSaveListOption4(self): testlist = ["элемент 1", "элемент 2", "элемент 3"] opt = ListOption(self.config, "Test", "savelist", [], separator="|") opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", [], separator="|") self.assertEqual(newopt.value, testlist) stringopt = StringOption(newconfig, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "элемент 1|элемент 2|элемент 3") def testSaveListOption5(self): testlist = ["элемент 1"] opt = ListOption(self.config, "Test", "savelist", [], separator="|") opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", [], separator="|") self.assertEqual(newopt.value, testlist) stringopt = StringOption(newconfig, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "элемент 1") def testSaveListOption6(self): testlist = [] opt = ListOption(self.config, "Test", "savelist", [], separator="|") opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", [], separator="|") self.assertEqual(newopt.value, [""]) stringopt = StringOption(newconfig, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "") def testSaveListOption7(self): testlist = [""] opt = ListOption(self.config, "Test", "savelist", [], separator="|") opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", [], separator="|") self.assertEqual(newopt.value, testlist) stringopt = StringOption(newconfig, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "") def testSaveListOption8(self): testlist = [""] opt = ListOption(self.config, "Test", "savelist", []) opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", []) self.assertEqual(newopt.value, testlist) stringopt = StringOption(self.config, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "") def testSaveListOption9(self): testlist = [""] opt = ListOption(self.config, "Test", "savelist", [], separator="|") opt.value = testlist newconfig = Config(self.path) newopt = ListOption(newconfig, "Test", "savelist", [], separator="|") self.assertEqual(newopt.value, testlist) stringopt = StringOption(self.config, "Test", "savelist", "") self.assertEqual(stringopt.value.strip(), "") def testStringListSection1(self): section = "listSection" paramname = "param_" testlist = ["Бла-бла-бла", "Строка 1", "Строка 2"] self.config.remove_section(section) opt = StringListSection(self.config, section, paramname) self.assertEqual(opt.value, []) # Установим список opt.value = testlist self.assertEqual(len(opt.value), 3) self.assertEqual(opt.value[0], "Бла-бла-бла") self.assertEqual(opt.value[1], "Строка 1") self.assertEqual(opt.value[2], "Строка 2") self.assertTrue(self.config.has_section(section)) self.assertEqual(self.config.get(section, "param_0"), "Бла-бла-бла") self.assertEqual(self.config.get(section, "param_1"), "Строка 1") self.assertEqual(self.config.get(section, "param_2"), "Строка 2") self.config.remove_section(section) def testStringListSection2(self): section = "listSection" paramname = "param_" testlist = ["Бла-бла-бла", "Строка 1", "Строка 2"] self.config.remove_section(section) opt = StringListSection(self.config, section, paramname) self.assertEqual(opt.value, []) # Установим список opt.value = testlist opt_other = StringListSection(self.config, section, paramname) self.assertEqual(len(opt_other.value), 3) self.assertEqual(opt_other.value[0], "Бла-бла-бла") self.assertEqual(opt_other.value[1], "Строка 1") self.assertEqual(opt_other.value[2], "Строка 2") self.config.remove_section(section) def testStcStyle_01(self): defaultStyle = StcStyle() opt = StcStyleOption(self.config, "Test", "style_01", defaultStyle) self.assertEqual(opt.value.fore, "#AAAAAA") self.assertEqual(opt.value.back, "#111111") self.assertEqual(opt.value.bold, True) self.assertEqual(opt.value.italic, True) self.assertEqual(opt.value.underline, True) def testStcStyle_02(self): defaultStyle = StcStyle() opt = StcStyleOption(self.config, "Test", "style_02", defaultStyle) self.assertEqual(opt.value.fore, "#000000") self.assertEqual(opt.value.back, "#111111") self.assertEqual(opt.value.bold, True) self.assertEqual(opt.value.italic, True) self.assertEqual(opt.value.underline, True) def testStcStyle_03(self): defaultStyle = StcStyle() opt = StcStyleOption(self.config, "Test", "style_03", defaultStyle) self.assertEqual(opt.value.fore, "#000000") self.assertEqual(opt.value.back, "#FFFFFF") self.assertEqual(opt.value.bold, True) self.assertEqual(opt.value.italic, False) self.assertEqual(opt.value.underline, False) def testStcStyle_invalid_01(self): defaultStyle = StcStyle() opt = StcStyleOption(self.config, "Test", "style_invalid_01", defaultStyle) self.assertEqual(opt.value.fore, "#000000") self.assertEqual(opt.value.back, "#FFFFFF") self.assertEqual(opt.value.bold, False) self.assertEqual(opt.value.italic, False) self.assertEqual(opt.value.underline, False) def testJSON_01(self): opt = JSONOption(self.config, "Test", "jsonval_none", None) self.assertEqual(opt.value, None) def testJSON_02(self): opt = JSONOption(self.config, "Test", "jsonval_01", None) self.assertEqual(opt.value, {}) def testJSON_03(self): opt = JSONOption(self.config, "Test", "jsonval_02", None) self.assertEqual(opt.value, 'строка') def testJSON_04(self): opt = JSONOption(self.config, "Test", "jsonval_03", None) self.assertEqual(opt.value, []) def testJSON_05(self): opt = JSONOption(self.config, "Test", "jsonval_04", None) self.assertEqual(opt.value, [1, 2, 3]) def testJSON_06(self): opt = JSONOption(self.config, "Test", "jsonval_05", None) self.assertEqual(opt.value['x'], 100) def testJSON_07(self): opt = JSONOption(self.config, "Test", "json_test", None) newopt = JSONOption(self.config, "Test", "json_test", None) opt.value = [1, 2, 3] self.assertEqual(newopt.value, [1, 2, 3]) def testJSON_08(self): opt = JSONOption(self.config, "Test", "json_test", None) newopt = JSONOption(self.config, "Test", "json_test", None) opt.value = {'x': 100, 'y': 200} self.assertEqual(newopt.value, {'x': 100, 'y': 200}) def testJSON_09(self): opt = JSONOption(self.config, "Test", "json_test", None) newopt = JSONOption(self.config, "Test", "json_test", None) opt.value = '111\n222\n333' self.assertEqual(newopt.value, '111\n222\n333')
def setUp(self): self.path = "../test/testconfig.ini" self.config = Config(self.path) self.editorConfig = EditorConfig(self.config)