def test_save(self): """ save """ p = self.preferences # Get the application scope. application = p.node('application/') tmp = join(self.tmpdir, 'test.ini') application.filename = tmp # Set a value. p.set('acme.ui.bgcolor', 'red') # Save all scopes. p.save() # Make sure a file was written. self.assertEqual(True, os.path.exists(tmp)) # Load the 'ini' file into a new preferences node and make sure the # preference is in there. p = Preferences() p.load(tmp) self.assertEqual('red', p.get('acme.ui.bgcolor')) # Cleanup. os.remove(tmp) return
def test_save(self): """ save """ p = self.preferences # Load the preferences from an 'ini' file. p.load(self.example) # Make sure it was all loaded! self.assertEqual('blue', p.get('acme.ui.bgcolor')) self.assertEqual('50', p.get('acme.ui.width')) self.assertEqual('1.0', p.get('acme.ui.ratio')) self.assertEqual('True', p.get('acme.ui.visible')) self.assertEqual('acme ui', p.get('acme.ui.description')) self.assertEqual('[1, 2, 3, 4]', p.get('acme.ui.offsets')) self.assertEqual("['joe', 'fred', 'jane']", p.get('acme.ui.names')) self.assertEqual('splash', p.get('acme.ui.splash_screen.image')) self.assertEqual('red', p.get('acme.ui.splash_screen.fgcolor')) # Make a change. p.set('acme.ui.bgcolor', 'yellow') # Save it to another file. tmp = join(self.tmpdir, 'tmp.ini') p.save(tmp) try: # Load it into a new node. p = Preferences() p.load(tmp) # Make sure it was all loaded! self.assertEqual('yellow', p.get('acme.ui.bgcolor')) self.assertEqual('50', p.get('acme.ui.width')) self.assertEqual('1.0', p.get('acme.ui.ratio')) self.assertEqual('True', p.get('acme.ui.visible')) self.assertEqual('acme ui', p.get('acme.ui.description')) self.assertEqual('[1, 2, 3, 4]', p.get('acme.ui.offsets')) self.assertEqual("['joe', 'fred', 'jane']", p.get('acme.ui.names')) self.assertEqual('splash', p.get('acme.ui.splash_screen.image')) self.assertEqual('red', p.get('acme.ui.splash_screen.fgcolor')) finally: # Clean up! os.remove(tmp) return
def test_flush(self): """ flush """ p = self.preferences # A temporary .ini file for this test. tmp = join(self.tmpdir, 'tmp.ini') # This could be set in the constructor of course, its just here we # want to use the instance declared in 'setUp'. p.filename = tmp try: # Load the preferences from an 'ini' file. p.load(self.example) # Flush it. p.flush() # Load it into a new node. p = Preferences() p.load(tmp) # Make sure it was all loaded! self.assertEqual('blue', p.get('acme.ui.bgcolor')) self.assertEqual('50', p.get('acme.ui.width')) self.assertEqual('1.0', p.get('acme.ui.ratio')) self.assertEqual('True', p.get('acme.ui.visible')) self.assertEqual('acme ui', p.get('acme.ui.description')) self.assertEqual('[1, 2, 3, 4]', p.get('acme.ui.offsets')) self.assertEqual("['joe', 'fred', 'jane']", p.get('acme.ui.names')) self.assertEqual('splash', p.get('acme.ui.splash_screen.image')) self.assertEqual('red', p.get('acme.ui.splash_screen.fgcolor')) finally: # Clean up! os.remove(tmp) return
def test_preferences_node_changed(self): """ preferences node changed """ p = self.preferences p.load(self.example) class AcmeUIPreferencesHelper(PreferencesHelper): """ A helper! """ # The path to the preferences node that contains our preferences. preferences_path = 'acme.ui' # The traits that we want to initialize from preferences. bgcolor = Str width = Int ratio = Float visible = Bool description = Unicode offsets = List(Int) names = List(Str) helper = AcmeUIPreferencesHelper() # We only listen to some of the traits so the testing is easier. helper.on_trait_change(listener, ['bgcolor', 'width']) # Create a new preference node. p1 = Preferences() p1.load(self.example) p1.set('acme.ui.bgcolor', 'red') p1.set('acme.ui.width', 40) # Set the new preferences helper.preferences = p1 # Test event handling. self.assertEqual(helper, listener.obj) self.assertEqual('width', listener.trait_name) self.assertEqual(50, listener.old) self.assertEqual(40, listener.new) # Test re-initialization. self.assertEqual(helper.bgcolor, 'red') self.assertEqual(helper.width, 40) # Test event handling. p1.set('acme.ui.bgcolor', 'black') self.assertEqual(helper, listener.obj) self.assertEqual('bgcolor', listener.trait_name) self.assertEqual('red', listener.old) self.assertEqual('black', listener.new) # This should not trigger any new changes since we are setting values # on the old preferences node. p.set('acme.ui.bgcolor', 'white') self.assertEqual(helper, listener.obj) self.assertEqual('bgcolor', listener.trait_name) self.assertEqual('red', listener.old) self.assertEqual('black', listener.new) return