Package Martel :: Package test :: Module test_Time
[hide private]
[frames] | no frames]

Source Code for Module Martel.test.test_Time

  1  import re, time, string 
  2  from xml.sax import handler 
  3   
  4  import Martel 
  5  from Martel import Time 
  6   
7 -def test_terms(s, matches, non_matches):
8 pat = Time.make_pattern(s, tag_format = None) 9 match = re.compile(str(pat) + "$").match 10 11 exp = Time.make_expression(s) 12 exp_match = exp.make_parser().parseString 13 14 for m in matches: 15 if match(m) is None: 16 raise AssertionError("%re: s %s does not match %s" % \ 17 (repr(s), repr(pat), repr(m))) 18 try: 19 exp_match(m) 20 except Martel.Parser.ParserException: 21 raise AssertionError("exp: %s %s does not match %s" % \ 22 (repr(s), repr(pat), repr(m))) 23 24 25 for m in non_matches: 26 if match(m) is not None: 27 raise AssertionError("re: %s %s should not match %s" % \ 28 (repr(s), repr(pat), repr(m))) 29 try: 30 exp_match(m) 31 except Martel.Parser.ParserException: 32 pass 33 else: 34 raise AssertionError("exp: %s %s should not match %s" % \ 35 (repr(s), repr(pat), repr(m)))
36
37 -def test_syntax():
38 for pat in ("%A", "%%", "%A %A %B %C%%", "%", "Test%%", "Test%", 39 "%Test", "%Test%", "%%Test%%%", "nothing", "", 40 "%(A)", "%(day)"): 41 Time.make_pattern(pat) 42 Time.make_expression(pat) 43 44 for bad_pat in ("%9A", "%9", "%(", "%()", "%(missing)", "%!", "%Q"): 45 try: 46 Time.make_pattern(bad_pat) 47 except (TypeError, KeyError): 48 pass 49 else: 50 raise AssertionError("Should not have allowed: %s" % 51 (repr(bad_pat),))
52
53 -def test_times():
54 now = time.time() 55 table = [] 56 for c in Time._time_table.keys(): 57 if len(c) != 1: 58 continue 59 pat = Time.make_pattern("%" + c, tag_format = None) 60 exp = Time.make_expression("%" + c) 61 table.append( (c, pat, re.compile(pat + "$").match, 62 exp.make_parser().parseString) ) 63 64 for j in range(-5, 5): 65 for i in range(25): 66 t = time.localtime(now + (i + j*100)*23*3603) 67 for c, pat, match, parse in table: 68 s = time.strftime("%" + c, t) 69 m = match(s) 70 if m is None or m.endpos != len(s): 71 raise AssertionError("Bad re %" + \ 72 "%s %s %s" % (c, pat, s)) 73 try: 74 parse(s) 75 except Martel.Parser.ParserException: 76 raise AssertionError("Bad exp %" + \ 77 "%s %s %s" % (c, pat, s))
78
79 -def test_expand():
80 # make sure tag name expansion works correctly 81 class Capture: 82 def __init__(self): 83 self.capture = []
84 def __mod__(self, s): 85 self.capture.append(s) 86 return s 87 cap = Capture() 88 exp = Time.make_expression("%m-%Y", cap) 89 parser = exp.make_parser() 90 parser.parseString("05-1921") 91 x = cap.capture 92 x.sort() 93 assert x == ["month", "year"] 94 95
96 -def _find_quoted_words(line):
97 words = [] 98 i = 0 99 while 1: 100 i = string.find(line, '"', i) 101 if i == -1: 102 break 103 j = string.find(line, '"', i+1) 104 assert i < j, line 105 words.append(line[i+1:j]) 106 i = j + 1 107 return words
108
109 -def test_docstring():
110 check_pattern = 0 111 for line in Time.__doc__.split("\n"): 112 if line[:1] == "%": 113 pattern = line.split()[0] 114 check_pattern = 1 115 examples = [] 116 element_name = None 117 element_attrs = None 118 has_continue = "Definition" 119 continue 120 if not check_pattern: 121 continue 122 if line.find("Pattern:") != -1: 123 has_continue = "Pattern" 124 continue 125 if line.find("Example:") != -1: 126 has_continue = "Example" 127 examples.extend(_find_quoted_words(line)) 128 continue 129 if line.find("Element name:") != -1: 130 element_name, = _find_quoted_words(line) 131 has_continue = 0 132 continue 133 134 if line.find("Element attributes:") != -1: 135 element_attrs = _find_quoted_words(line) 136 has_continue = 0 137 if not element_attrs: 138 assert line.find("no attributes") != -1, line 139 else: 140 assert len(element_attrs) == 2, line 141 assert element_attrs[0] == "type", (line, element_attrs[0]) 142 elif line.find("Element:") != -1: 143 # Neither element name nor attrs may be specified here 144 has_continue = 0 145 assert element_name is None, element_name 146 assert element_attrs is None, element_attrs 147 else: 148 if has_continue in ("Pattern", "Definition"): 149 continue 150 if has_continue == "Example": 151 examples.extend(_find_quoted_words(line)) 152 continue 153 raise AssertionError(line) 154 155 assert (element_name is None) == (element_attrs is None), \ 156 (line, element_name, element_attrs) 157 158 exp = Time.make_expression(pattern) 159 parser = exp.make_parser() 160 161 class StoreAttrs(handler.ContentHandler): 162 def startElement(self, name, attrs): 163 self.name = name 164 self.attrs = attrs
165 store = StoreAttrs() 166 parser.setContentHandler(store) 167 parseString = parser.parseString 168 169 for example in examples: 170 parseString(example) 171 if element_name is not None: 172 assert element_name == store.name, (element_name, store.name) 173 if element_attrs: 174 if store.attrs[element_attrs[0]] != element_attrs[1]: 175 raise AssertionError( 176 "pattern = %s ; text = %s: %s != %s" % \ 177 (repr(pattern), repr(example), 178 repr(element_attrs[0]), repr(element_attrs[1]))) 179 check_pattern = 0 180 pattern = None 181 182
183 -def test():
184 test_docstring() 185 test_expand() 186 test_syntax() 187 188 test_times() 189 190 test_terms("%(Jan)", 191 ("Jan", "JAN", "jan", "Feb", "MAR", "apR", "mAy", 192 "jUN", "JUL", "aug", "sEP", "Oct", "NOv", "DeC"), 193 ("Jan.", "October", "")) 194 195 test_terms("%(January)", 196 ("May", "March", "January", "October"), 197 ("Jan.", "JAN", "jan", "", "Mayo")) 198 199 test_terms("%(Mon)day", 200 ("Monday", "TUEday", "Wedday", "fRiday", "THuday"), 201 ("MON", "Sun", "wednesday", "")) 202 203 test_terms("%(Monday)", 204 ("Monday", "TUESDAY", "WedNeSdAY"), 205 ("MON", "Mon", "mon", "wedday", "")) 206 207 test_terms("%(second)", 208 ("00", "09", "10", "01", "60", "61", "00"), 209 ("-1", "0", "9", " 0", "62", "A", "1A", "A1")) 210 211 test_terms("%(minute)", 212 ("00", "01", "09", "10", "59"), 213 ("60", "1", "A", "1A", "A1")) # Allow "1" and " 1"? 214 215 test_terms("%(hour)", 216 ("00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 217 "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 218 "20", "21", "22", "23", " 1", " 2", " 3", " 4", " 5", " 6", 219 " 7", " 8", " 9", "1", "2", "3", "4", "5", "6", "7", "8", "9"), 220 ("24", "-1", "A", "1A", "A1", "123", " 1")) 221 222 test_terms("%(12-hour)", 223 ("01", "02", "03", "04", "05", "06", "07", "08", "09", 224 "10", "11", "12", " 1", " 2", " 3", " 4", " 5", " 6", " 7", 225 " 8", " 9", "1", "2", "3", "4", "5", "6", "7", "8", "9"), 226 ("00", "13", "14", "15", "24", "20", "-1", "A", 227 "1A", "A1", "123", " 1")) 228 229 test_terms("%(24-hour)", 230 ("00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 231 "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 232 "20", "21", "22", "23", " 1", " 2", " 3", " 4", " 5", " 6", 233 " 7", " 8", " 9", "1", "2", "3", "4", "5", "6", "7", "8", "9"), 234 ("24", "-1", "A", "1A", "A1", "123", " 1")) 235 236 test_terms("%(day)", 237 ( "01", "02", "03", "04", "05", "06", "07", "08", "09", 238 "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 239 "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 240 "30", "31", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", 241 " 9", "1", "2", "3", "4", "5", "6", "7", "8", "9"), 242 ("00", "32", "3 ", "A", "3A")) 243 244 test_terms("%(month)", 245 ( "01", "02", "03", "04", "05", "06", "07", "08", "09", 246 "10", "11", "12", " 1", " 2", " 3", " 4", " 5", " 6", " 7", 247 " 8", " 9", "1", "2", "3", "4", "5", "6", "7", "8", "9"), 248 ("00", "13", "21", "20", "F", "1F")) 249 250 test_terms("%(YY)", 251 ("00", "01", "10", "99"), 252 ("100", "00A", "2000", "AA")) 253 254 test_terms("%(YYYY)", 255 ("2000", "2001", "1970", "1492"), 256 ("01", "ABCD", "1997A")) 257 258 test_terms("%(year)", 259 ("00", "01", "10", "99", "2000", "2001", "1970", "1492"), 260 ("100", "00A", "AA", "ABCD", "1997A", "")) 261 262 test_terms("%(Jan) %(day), %(YYYY)", 263 ("OCT 31, 2000", "Jan 01, 1900", "Jan 1, 1900"), 264 ("Aug 99, 2000", "Aug 22, 12345", "June 1 1900")) 265 266 test_terms("%j", 267 ("001", "002", "009", "099", "197", "226", "300", 268 "301", "309", "310", "320", "330", "340", "350", 269 "360", "365", "366"), 270 ("000", "0", "00", "1", "12", "367", "1A", "200B")) 271 272 test_terms("%Z", 273 ("MST", "GMT", "Pacific Standard Time", "GRNLNDST", 274 "MET DST", "New Zealand Standard Time", "NZST", 275 "SAST", "GMT+0200", "IDT"), 276 ("(MST)", "GMT0200", "+1000")) 277 278 test_terms("%A%%%a", 279 ("Monday%Mon", "Tuesday%FRI"), 280 ("Thu%THU", "Thursday%%Sunday", "Tuesday%FRI!")) 281 282 test_terms("%", 283 ("%",), 284 ("%%", "")) 285 286 # make sure terms like "." and "[]" are escaped before turning into 287 # a regular expression 288 test_terms("A.B][", 289 ("A.B][",), 290 ("A-B][",))
291 292 if __name__ == "__main__": 293 test() 294