def test_save(self): filename = "test_load_settings.ini" sections = ['Color', 'Resolution', 'Food'] data_color = { 'description': 'Pick a color', 'default_option': 'Blue', 'current_option': 'Green', 'options': "[Red,Blue,Green,Black]", } data_res = { 'description': 'Screen resolution', 'default_option': '600x400', 'current_option': '1920x1080', 'options': "[600x400,1024x768,1920x1080,1920x1200]", } data_food = { 'description': 'Favorite food', 'default_option': 'Broccoli', 'current_option': 'Ice Cream', 'options': "[Pizza, Steak, Ice Cream, Broccoli]", } datas = [data_color, data_res, data_food] nopts = len(sections) settings = Settings.Settings(filename) for i in xrange(nopts): setting_name = sections[i] data = datas[i] opts = Util.Formatting.str_to_struct(data['options'], str) di = opts.index(data['default_option']) ci = opts.index(data['current_option']) setting = Settings.Setting(name=setting_name, description=data['description'], default_index=di, current_index=ci, options=opts) settings.add_setting(setting_name, setting) settings.save() #Relying on load working here, but we test that above, so it's cool? same_settings = Settings.Settings(filename) same_settings.load() opts1 = settings.settings opts2 = same_settings.settings for i in xrange(nopts): opt1 = opts1[i] opt2 = opts2[i] self.assertEqual(opt1.description, opt2.description) self.assertEqual(opt1.default_option, opt2.default_option) self.assertEqual(opt1.current_option, opt2.current_option) self.assertEqual(opt1.name, opt2.name) self.assertEqual(opt1.options, opt2.options) import os try: os.remove(filename) except WindowsError: pass
def setUp(self): unittest.TestCase.setUp(self) options = ["Option0", "Option1", "Option2"] self.my_setting = Settings.Setting(name="MySetting", description="A test setting", default_index=0, current_index=1, options=options) color_options = ["Yellow", "Red", "Blue"] self.color_setting = Settings.Setting(name="ColorSetting", description="Pick a color", default_index=0, current_index=0, options=color_options) #New settings object self.settings = Settings.Settings() #Add in settings using __setitem__ self.settings["MySetting"] = self.my_setting self.settings["ColorSetting"] = self.color_setting
def test_load(self): filename = "test_load_settings.ini" write_file = open(filename, 'w') write_file.write("") write_file.close() def write(line): write_file = open(filename, 'a') write_file.write(line+"\n") write_file.close() sections = ['Color', 'Resolution', 'Food'] data_color = { 'description': 'Pick a color', 'default_option': 'Blue', 'current_option': 'Green', 'options': "[Red,Blue,Green,Black]", } data_res = { 'description': 'Screen resolution', 'default_option': '600x400', 'current_option': '1920x1080', 'options': "[600x400,1024x768,1920x1080,1920x1200]", } data_food = { 'description': 'Favorite food', 'default_option': 'Broccoli', 'current_option': 'Ice Cream', 'options': "[Pizza, Steak, Ice Cream, Broccoli]", } datas = [data_color, data_res, data_food] nopts = len(sections) for i in xrange(nopts): section = sections[i] data = datas[i] #Write section header write("["+section+"]") #Write the options for option in data.keys(): write(option+": "+data[option]) #Single space between settings write("") settings = Settings.Settings(filename) settings.load() for i in xrange(nopts): setting_name = sections[i] setting = settings[setting_name] data = datas[i] self.assertEqual(data['description'], setting.description) self.assertEqual(data['default_option'], setting.default_option) self.assertEqual(data['current_option'], setting.current_option) opts = Util.Formatting.str_to_struct(data['options'], str) self.assertEqual(opts, setting.options) import os try: os.remove(filename) except WindowsError: pass