comparison tests/unit.py @ 115:56db0b2b90af

fix casting
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 04 Jul 2012 09:54:37 -0700
parents a2184db43fe2
children 9d19ed8fd883
comparison
equal deleted inserted replaced
114:d1911d9b5b19 115:56db0b2b90af
3 """ 3 """
4 unit tests 4 unit tests
5 """ 5 """
6 6
7 import configuration 7 import configuration
8 import datetime
8 import os 9 import os
9 import sys 10 import sys
10 import tempfile 11 import tempfile
11 import unittest 12 import unittest
12 13
182 f.close() 183 f.close()
183 example.parse_args(['-a', 'ts', '-e', '/opt/bin/firefox', tf]) 184 example.parse_args(['-a', 'ts', '-e', '/opt/bin/firefox', tf])
184 self.assertEqual(example.config['preferences'], default_prefs) 185 self.assertEqual(example.config['preferences'], default_prefs)
185 os.remove(tf) 186 os.remove(tf)
186 187
188 def test_typecast(self):
189 """casting example"""
190
191 def todate(string):
192 return datetime.datetime.strptime(string, "%Y%m%d")
193
194 # make an example class
195 class TypecastExample(configuration.Configuration):
196 options = {'datestring': {'type': todate}}
197 example = TypecastExample()
198
199 # parse a date string
200 example({'datestring': "20120704"})
201
202 # ensure it works correctly
203 expected = datetime.datetime(2012, 7, 4, 0, 0)
204 self.assertEqual(example.config['datestring'], expected)
205
187 if __name__ == '__main__': 206 if __name__ == '__main__':
188 unittest.main() 207 unittest.main()