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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
import unittest
from unittests import wtc
import wx
import os
cfgFilename = os.path.join(os.path.dirname(__file__), 'cfgtest')
#---------------------------------------------------------------------------
class ConfigTests(wtc.WidgetTestCase):
def writeStuff(self, cfg):
cfg.SetPath('/one/two/three')
cfg.Write('key', 'value')
cfg.WriteInt('int', 123)
cfg.WriteFloat('float', 123.456)
cfg.WriteBool('bool', True)
cfg.Flush()
def test_Config1(self):
null = wx.LogNull()
name = cfgFilename + '_1'
cfg = wx.Config('unittest_ConfigTests', localFilename=name)
self.writeStuff(cfg)
del cfg
if os.path.exists(name):
os.remove(name)
def test_Config2(self):
null = wx.LogNull()
name = cfgFilename + '_2'
cfg = wx.Config('unittest_ConfigTests', localFilename=name)
self.writeStuff(cfg)
cfg.SetPath('/before')
self.assertTrue(cfg.GetPath() == '/before')
changer = wx.ConfigPathChanger(cfg, '/one/two/three/')
self.assertTrue(cfg.GetPath() == '/one/two/three')
del changer
self.assertTrue(cfg.GetPath() == '/before')
del cfg
if os.path.exists(name):
os.remove(name)
def test_Config3(self):
null = wx.LogNull()
name = cfgFilename + '_3'
cfg = wx.Config('unittest_ConfigTests', localFilename=name)
self.writeStuff(cfg)
cfg.SetPath('/before')
self.assertTrue(cfg.GetPath() == '/before')
with wx.ConfigPathChanger(cfg, '/one/two/three/'):
self.assertTrue(cfg.GetPath() == '/one/two/three')
self.assertTrue(cfg.GetPath() == '/before')
del cfg
if os.path.exists(name):
os.remove(name)
def test_Config4(self):
null = wx.LogNull()
name = cfgFilename + '_4'
cfg0 = wx.Config('unittest_ConfigTests', localFilename=name)
wx.Config.Set(cfg0)
cfg = wx.Config.Get(False)
#self.assertTrue(cfg is cfg0)
self.writeStuff(cfg)
del cfg
cfg = wx.Config.Get()
cfg.SetPath('/one/two/three')
self.assertTrue(cfg.GetPath() == '/one/two/three')
self.assertTrue(cfg.GetNumberOfEntries() == 4)
self.assertTrue(cfg.Read('key') == 'value')
self.assertTrue(cfg.ReadInt('int') == 123)
self.assertTrue(cfg.ReadFloat('float') == 123.456)
self.assertTrue(cfg.ReadBool('bool') == True)
self.assertTrue(cfg.Read('no-value', 'defvalue') == 'defvalue')
wx.Config.Set(None)
self.myYield()
if os.path.exists(name):
os.remove(name)
def test_Config5(self):
null = wx.LogNull()
name = cfgFilename + '_5'
cfg = wx.Config('unittest_ConfigTests', localFilename=name)
cfg.SetPath('/zero')
cfg.Write('key1', 'value')
cfg.Write('key2', 'value')
cfg.Write('key3', 'value')
cfg.SetPath('/one')
cfg.Write('key1', 'value')
cfg.Write('key2', 'value')
cfg.Write('key3', 'value')
cfg.SetPath('/two')
cfg.Write('key1', 'value')
cfg.Write('key2', 'value')
cfg.Write('key3', 'value')
cfg.SetPath('/three')
cfg.Write('key1', 'value')
cfg.Write('key2', 'value')
cfg.Write('key3', 'value')
cfg.Flush()
cfg.SetPath('/')
count = 0
more, group, index = cfg.GetFirstGroup()
while more:
count += 1
more, group, index = cfg.GetNextGroup(index)
self.assertEqual(count, 4)
cfg.SetPath('/two')
count = 0
more, entry, index = cfg.GetFirstEntry()
while more:
count += 1
more, entry, index = cfg.GetNextEntry(index)
self.assertEqual(count, 3)
del cfg
if os.path.exists(name):
os.remove(name)
def test_Config6(self):
null = wx.LogNull()
name = cfgFilename + '_6'
cfg = wx.FileConfig('unittest_ConfigTests', localFilename=name)
cfg.SetPath('/zero')
cfg.Write('key1', 'value')
cfg.Write('key2', 'value')
cfg.Write('key3', 'value')
cfg.SetPath('/one')
cfg.Write('key1', 'value')
cfg.Write('key2', 'value')
cfg.Write('key3', 'value')
cfg.SetPath('/two')
cfg.Write('key1', 'value')
cfg.Write('key2', 'value')
cfg.Write('key3', 'value')
cfg.SetPath('/three')
cfg.Write('key1', 'value')
cfg.Write('key2', 'value')
cfg.Write('key3', 'value')
cfg.Flush()
cfg.SetPath('/')
count = 0
more, group, index = cfg.GetFirstGroup()
while more:
count += 1
more, group, index = cfg.GetNextGroup(index)
self.assertEqual(count, 4)
cfg.SetPath('/two')
count = 0
more, entry, index = cfg.GetFirstEntry()
while more:
count += 1
more, entry, index = cfg.GetNextEntry(index)
self.assertEqual(count, 3)
del cfg
if os.path.exists(name):
os.remove(name)
def test_ConfigEnums(self):
# Test for presence of config enums
wx.CONFIG_USE_LOCAL_FILE
wx.CONFIG_USE_GLOBAL_FILE
wx.CONFIG_USE_RELATIVE_PATH
wx.CONFIG_USE_NO_ESCAPE_CHARACTERS
wx.CONFIG_USE_SUBDIR
#---------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()
|