blob: bd8402119060aa4dfaf3261fc4f7c3c6b5ffc801 [file] [log] [blame]
Guido van Rossum00efe7e2002-07-19 17:04:46 +00001"""PyUnit testing against strptime >= 2.1.0."""
2
3import sys
4sys.path.append('..')
5import unittest
6import time
7import locale
8import re
9
10import _strptime
11
12__version__ = (1,0,5)
13
14class LocaleTime_Tests(unittest.TestCase):
15 """Contains all tests for _strptime.LocaleTime."""
16
17 def setUp(self):
18 """Create time tuple based on current time."""
19 self.time_tuple = time.localtime()
20 self.LT_ins = _strptime.LocaleTime()
21
22 def compare_against_time(self, testing, directive, tuple_position, error_msg):
Tim Peters469cdad2002-08-08 20:19:19 +000023 """Helper method that tests testing against directive based on the
Guido van Rossum00efe7e2002-07-19 17:04:46 +000024 tuple_position of time_tuple. Uses error_msg as error message.
25
26 """
27 strftime_output = time.strftime(directive, self.time_tuple)
28 comparison = testing[self.time_tuple[tuple_position]]
29 self.failUnless(strftime_output in testing, "%s: not found in tuple" % error_msg)
30 self.failUnless(comparison == strftime_output, "%s: position within tuple incorrect; %s != %s" % (error_msg, comparison, strftime_output))
Tim Peters469cdad2002-08-08 20:19:19 +000031
Guido van Rossum00efe7e2002-07-19 17:04:46 +000032 def test_weekday(self):
Tim Peters469cdad2002-08-08 20:19:19 +000033 """Make sure that full and abbreviated weekday names are correct in
Guido van Rossum00efe7e2002-07-19 17:04:46 +000034 both string and position with tuple.
Tim Peters469cdad2002-08-08 20:19:19 +000035
Guido van Rossum00efe7e2002-07-19 17:04:46 +000036 """
37 self.compare_against_time(self.LT_ins.f_weekday, '%A', 6, "Testing of full weekday name failed")
38 self.compare_against_time(self.LT_ins.a_weekday, '%a', 6, "Testing of abbreviated weekday name failed")
39
40 def test_month(self):
Tim Peters469cdad2002-08-08 20:19:19 +000041 """Test full and abbreviated month names; both string and position
Guido van Rossum00efe7e2002-07-19 17:04:46 +000042 within the tuple.
43
44 """
45 self.compare_against_time(self.LT_ins.f_month, '%B', 1, "Testing against full month name failed")
46 self.compare_against_time(self.LT_ins.a_month, '%b', 1, "Testing against abbreviated month name failed")
47
48 def test_am_pm(self):
49 """Make sure AM/PM representation done properly."""
50 strftime_output = time.strftime("%p", self.time_tuple)
51 self.failUnless(strftime_output in self.LT_ins.am_pm, "AM/PM representation not in tuple")
52 if self.time_tuple[3] < 12: position = 0
53 else: position = 1
54 self.failUnless(strftime_output == self.LT_ins.am_pm[position], "AM/PM representation in the wrong position within the tuple")
55
56 def test_timezone(self):
57 """Make sure timezone is correct."""
58 if time.strftime("%Z", self.time_tuple):
59 self.compare_against_time(self.LT_ins.timezone, '%Z', 8, "Testing against timezone failed")
60
61 def test_date_time(self):
62 """Check that LC_date_time, LC_date, and LC_time are correct."""
63 strftime_output = time.strftime("%c", self.time_tuple)
64 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date_time, self.time_tuple), "LC_date_time incorrect")
65 strftime_output = time.strftime("%x", self.time_tuple)
66 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date, self.time_tuple), "LC_date incorrect")
67 strftime_output = time.strftime("%X", self.time_tuple)
68 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_time, self.time_tuple), "LC_time incorrect")
69
70 def test_lang(self):
71 """Make sure lang is set."""
72 self.failUnless(self.LT_ins.lang in (locale.getdefaultlocale()[0], locale.getlocale(locale.LC_TIME)), "Setting of lang failed")
73
74 def test_by_hand_input(self):
75 """Test passed-in initialization value checks."""
76 self.failUnless(_strptime.LocaleTime(f_weekday=range(7)), "Argument size check for f_weekday failed")
77 self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(8))
78 self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(6))
79 self.failUnless(_strptime.LocaleTime(a_weekday=range(7)), "Argument size check for a_weekday failed")
80 self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(8))
81 self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(6))
82 self.failUnless(_strptime.LocaleTime(f_month=range(12)), "Argument size check for f_month failed")
83 self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(11))
84 self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(13))
85 self.failUnless(len(_strptime.LocaleTime(f_month=range(12)).f_month) == 13, "dummy value for f_month not added")
86 self.failUnless(_strptime.LocaleTime(a_month=range(12)), "Argument size check for a_month failed")
87 self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(11))
88 self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(13))
89 self.failUnless(len(_strptime.LocaleTime(a_month=range(12)).a_month) == 13, "dummy value for a_month not added")
90 self.failUnless(_strptime.LocaleTime(am_pm=range(2)), "Argument size check for am_pm failed")
91 self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(1))
92 self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(3))
93 self.failUnless(_strptime.LocaleTime(timezone=range(2)), "Argument size check for timezone failed")
94 self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(1))
95 self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(3))
96
97class TimeRETests(unittest.TestCase):
98 """Tests for TimeRE."""
99
100 def setUp(self):
101 """Construct generic TimeRE object."""
102 self.time_re = _strptime.TimeRE()
103 self.locale_time = _strptime.LocaleTime()
104
105 def test_getitem(self):
106 """Make sure that __getitem__ works properly."""
107 self.failUnless(self.time_re['m'], "Fetching 'm' directive (built-in) failed")
108 self.failUnless(self.time_re['b'], "Fetching 'b' directive (built w/ __tupleToRE) failed")
109 for name in self.locale_time.a_month:
110 self.failUnless(self.time_re['b'].find(name) != -1, "Not all abbreviated month names in regex")
111 self.failUnless(self.time_re['c'], "Fetching 'c' directive (built w/ format) failed")
112 self.failUnless(self.time_re['c'].find('%') == -1, "Conversion of 'c' directive failed; '%' found")
113 self.assertRaises(KeyError, self.time_re.__getitem__, '1')
114
115 def test_pattern(self):
116 """Test TimeRE.pattern."""
117 pattern_string = self.time_re.pattern(r"%a %A %d")
118 self.failUnless(pattern_string.find(self.locale_time.a_weekday[2]) != -1, "did not find abbreviated weekday in pattern string '%s'" % pattern_string)
119 self.failUnless(pattern_string.find(self.locale_time.f_weekday[4]) != -1, "did not find full weekday in pattern string '%s'" % pattern_string)
120 self.failUnless(pattern_string.find(self.time_re['d']) != -1, "did not find 'd' directive pattern string '%s'" % pattern_string)
121
122 def test_compile(self):
123 """Check that compiled regex is correct."""
124 found = self.time_re.compile(r"%A").match(self.locale_time.f_weekday[6])
125 self.failUnless(found and found.group('A') == self.locale_time.f_weekday[6], "re object for '%A' failed")
126 compiled = self.time_re.compile(r"%a %b")
127 found = compiled.match("%s %s" % (self.locale_time.a_weekday[4], self.locale_time.a_month[4]))
Tim Peters469cdad2002-08-08 20:19:19 +0000128 self.failUnless(found,
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000129 "Match failed with '%s' regex and '%s' string" % (compiled.pattern, "%s %s" % (self.locale_time.a_weekday[4], self.locale_time.a_month[4])))
Tim Peters469cdad2002-08-08 20:19:19 +0000130 self.failUnless(found.group('a') == self.locale_time.a_weekday[4] and found.group('b') == self.locale_time.a_month[4],
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000131 "re object couldn't find the abbreviated weekday month in '%s' using '%s'; group 'a' = '%s', group 'b' = %s'" % (found.string, found.re.pattern, found.group('a'), found.group('b')))
132 for directive in ('a','A','b','B','c','d','H','I','j','m','M','p','S','U','w','W','x','X','y','Y','Z','%'):
133 compiled = self.time_re.compile("%%%s"% directive)
134 found = compiled.match(time.strftime("%%%s" % directive))
135 self.failUnless(found, "Matching failed on '%s' using '%s' regex" % (time.strftime("%%%s" % directive), compiled.pattern))
136
137class StrptimeTests(unittest.TestCase):
138 """Tests for _strptime.strptime."""
139
140 def setUp(self):
141 """Create testing time tuple."""
142 self.time_tuple = time.gmtime()
143
144 def test_TypeError(self):
145 """Make sure ValueError is raised when match fails."""
146 self.assertRaises(ValueError,_strptime.strptime, data_string="%d", format="%A")
147
148 def test_returning_RE(self):
149 """Make sure that an re can be returned."""
150 strp_output = _strptime.strptime(False, "%Y")
151 self.failUnless(isinstance(strp_output, type(re.compile(''))), "re object not returned correctly")
152 self.failUnless(_strptime.strptime("1999", strp_output), "Use or re object failed")
153 bad_locale_time = _strptime.LocaleTime(lang="gibberish")
154 self.assertRaises(TypeError, _strptime.strptime, data_string='1999', format=strp_output, locale_time=bad_locale_time)
155
156 def helper(self, directive, position):
157 """Helper fxn in testing."""
158 strf_output = time.strftime("%%%s" % directive, self.time_tuple)
159 strp_output = _strptime.strptime(strf_output, "%%%s" % directive)
160 self.failUnless(strp_output[position] == self.time_tuple[position], "testing of '%s' directive failed; '%s' -> %s != %s" % (directive, strf_output, strp_output[position], self.time_tuple[position]))
161
162 def test_year(self):
163 """Test that the year is handled properly."""
164 for directive in ('y', 'Y'):
165 self.helper(directive, 0)
166
167 def test_month(self):
168 """Test for month directives."""
169 for directive in ('B', 'b', 'm'):
170 self.helper(directive, 1)
171
172 def test_day(self):
173 """Test for day directives."""
174 self.helper('d', 2)
175
176 def test_hour(self):
177 """Test hour directives."""
178 self.helper('H', 3)
179 strf_output = time.strftime("%I %p", self.time_tuple)
180 strp_output = _strptime.strptime(strf_output, "%I %p")
181 self.failUnless(strp_output[3] == self.time_tuple[3], "testing of '%%I %%p' directive failed; '%s' -> %s != %s" % (strf_output, strp_output[3], self.time_tuple[3]))
182
183 def test_minute(self):
184 """Test minute directives."""
185 self.helper('M', 4)
186
187 def test_second(self):
188 """Test second directives."""
189 self.helper('S', 5)
190
191 def test_weekday(self):
192 """Test weekday directives."""
193 for directive in ('A', 'a', 'w'):
194 self.helper(directive,6)
195
196 def test_julian(self):
197 """Test julian directives."""
198 self.helper('j', 7)
199
200 def test_timezone(self):
201 """Test timezone directives.
Tim Peters469cdad2002-08-08 20:19:19 +0000202
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000203 When gmtime() is used with %Z, entire result of strftime() is empty.
Tim Peters469cdad2002-08-08 20:19:19 +0000204
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000205 """
206 time_tuple = time.localtime()
207 strf_output = time.strftime("%Z") #UTC does not have a timezone
208 strp_output = _strptime.strptime(strf_output, "%Z")
209 self.failUnless(strp_output[8] == time_tuple[8], "timezone check failed; '%s' -> %s != %s" % (strf_output, strp_output[8], time_tuple[8]))
210
211 def test_date_time(self):
212 """*** Test %c directive. ***"""
213 for position in range(6):
214 self.helper('c', position)
215
216 def test_date(self):
217 """*** Test %x directive. ***"""
218 for position in range(0,3):
219 self.helper('x', position)
220
221 def test_time(self):
222 """*** Test %X directive. ***"""
223 for position in range(3,6):
224 self.helper('X', position)
225
226 def test_percent(self):
227 """Make sure % signs are handled properly."""
228 strf_output = time.strftime("%m %% %Y", self.time_tuple)
229 strp_output = _strptime.strptime(strf_output, "%m %% %Y")
230 self.failUnless(strp_output[0] == self.time_tuple[0] and strp_output[1] == self.time_tuple[1], "handling of percent sign failed")
231
232class FxnTests(unittest.TestCase):
233 """Test functions that fill in info by validating result and are triggered properly."""
234
235 def setUp(self):
236 """Create an initial time tuple."""
237 self.time_tuple = time.gmtime()
238
239 def test_julianday_result(self):
240 """Test julianday."""
241 result = _strptime.julianday(self.time_tuple[0], self.time_tuple[1], self.time_tuple[2])
242 self.failUnless(result == self.time_tuple[7], "julianday failed; %s != %s" % (result, self.time_tuple[7]))
243
244 def test_julianday_trigger(self):
245 """Make sure julianday is called."""
246 strf_output = time.strftime("%Y-%m-%d", self.time_tuple)
247 strp_output = _strptime.strptime(strf_output, "%Y-%m-%d")
248 self.failUnless(strp_output[7] == self.time_tuple[7], "strptime did not trigger julianday(); %s != %s" % (strp_output[7], self.time_tuple[7]))
Tim Peters469cdad2002-08-08 20:19:19 +0000249
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000250 def test_gregorian_result(self):
251 """Test gregorian."""
252 result = _strptime.gregorian(self.time_tuple[7], self.time_tuple[0])
253 comparison = [self.time_tuple[0], self.time_tuple[1], self.time_tuple[2]]
254 self.failUnless(result == comparison, "gregorian() failed; %s != %s" % (result, comparison))
255
256 def test_gregorian_trigger(self):
257 """Test that gregorian() is triggered."""
258 strf_output = time.strftime("%j %Y", self.time_tuple)
259 strp_output = _strptime.strptime(strf_output, "%j %Y")
260 self.failUnless(strp_output[1] == self.time_tuple[1] and strp_output[2] == self.time_tuple[2], "gregorian() not triggered; month -- %s != %s, day -- %s != %s" % (strp_output[1], self.time_tuple[1], strp_output[2], self.time_tuple[2]))
261
262 def test_dayofweek_result(self):
263 """Test dayofweek."""
264 result = _strptime.dayofweek(self.time_tuple[0], self.time_tuple[1], self.time_tuple[2])
265 comparison = self.time_tuple[6]
266 self.failUnless(result == comparison, "dayofweek() failed; %s != %s" % (result, comparison))
267
268 def test_dayofweek_trigger(self):
269 """Make sure dayofweek() gets triggered."""
270 strf_output = time.strftime("%Y-%m-%d", self.time_tuple)
271 strp_output = _strptime.strptime(strf_output, "%Y-%m-%d")
272 self.failUnless(strp_output[6] == self.time_tuple[6], "triggering of dayofweek() failed; %s != %s" % (strp_output[6], self.time_tuple[6]))
273
274
275
276
277
278if __name__ == '__main__':
279 unittest.main()