Convert other enums to int for Python 2.5 (Stephen Watson).
[rox-lib/lack.git] / tests / python / testoptions.py
blob25bc6cbc723f38cbbee48f81034b22ddd28b9385
1 #!/usr/bin/env python2.3
2 import unittest
3 import os, sys, shutil
4 from os.path import dirname, abspath, join
5 rox_lib = dirname(dirname(dirname(abspath(sys.argv[0]))))
6 sys.path.insert(0, join(rox_lib, 'python'))
8 os.environ['CHOICESPATH'] = '/tmp/choices:/tmp/choices2'
9 os.environ['XDG_CONFIG_HOME'] = '/tmp/config'
11 from rox import basedir, choices, options
13 class TestOptions(unittest.TestCase):
14 def setUp(self):
15 for d in ['/tmp/choices', '/tmp/choices2', '/tmp/config']:
16 if os.path.isdir(d):
17 shutil.rmtree(d)
19 def testChoices(self):
20 group = options.OptionGroup('MyProg', 'Options')
21 o1 = options.Option('colour', 'red', group)
22 assert not os.path.isfile('/tmp/choices/MyProg/Options')
23 group.notify()
24 group.save()
25 assert os.path.isfile('/tmp/choices/MyProg/Options')
27 g2 = options.OptionGroup('MyProg', 'Options')
28 o1 = options.Option('colour', 'green', g2)
29 g2.notify()
30 self.assertEquals('red', o1.value)
32 def testXDG(self):
33 group = options.OptionGroup('MyProg', 'Options', 'site')
34 o1 = options.Option('colour', 'red', group)
35 assert not os.path.isfile('/tmp/config/site/MyProg/Options')
36 group.notify()
37 group.save()
38 assert os.path.isfile('/tmp/config/site/MyProg/Options')
40 g2 = options.OptionGroup('MyProg', 'Options', 'site')
41 o1 = options.Option('colour', 'green', g2)
42 g2.notify()
43 self.assertEquals('red', o1.value)
45 def testNotify(self):
46 self.c = 0
47 def notify():
48 self.c += 1
49 group = options.OptionGroup('MyProg', 'Options', 'site')
50 o1 = options.Option('colour', 'green', group)
51 group.add_notify(notify)
52 self.assertEquals(0, self.c)
53 group.notify()
54 self.assertEquals(1, self.c)
56 try:
57 options.Option('size', 'small', group)
58 raise Exception('Too late!')
59 except AssertionError:
60 pass
62 group.remove_notify(notify)
63 group.notify()
64 self.assertEquals(1, self.c)
66 assert not o1.has_changed
67 o1._set('hi')
68 assert o1.has_changed
69 group.notify()
70 assert not o1.has_changed
73 suite = unittest.makeSuite(TestOptions)
74 if __name__ == '__main__':
75 sys.argv.append('-v')
76 unittest.main()