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

Source Code for Module Martel.test.test_macros

  1  import Martel 
  2  from xml.sax import handler, saxutils 
  3  import string, StringIO 
  4   
  5  # Various "macro" definitions so you don't need to come up with your 
  6  # own regular expressions. 
  7   
8 -def must_parse(test_name, parseString, term):
9 try: 10 parseString(term) 11 except Martel.Parser.ParserException: 12 raise AssertionError("%s: Cannot parse %s" % \ 13 (test_name, repr(term)))
14
15 -def must_not_parse(test_name, parseString, term):
16 try: 17 parseString(term) 18 except Martel.Parser.ParserException: 19 pass 20 else: 21 raise AssertionError("%s: Could parse %s" % \ 22 (test_name, repr(term)))
23
24 -class Capture(handler.ContentHandler):
25 - def startDocument(self):
26 self.capture = []
27 - def startElement(self, name, attrs):
28 if not self.capture: 29 self.capture.append( (name, attrs) ) # dup first item 30 self.capture.append( (name, attrs) )
31 - def endElement(self, name):
32 self.capture.pop()
33
34 -def has_group(format, term, name, attr):
35 parser = format.make_parser() 36 cap = Capture() 37 parser.setContentHandler(cap) 38 try: 39 parser.parseString(term) 40 except Martel.Parser.ParserException: 41 raise AssertionError("Cannot parse %s" % (repr(term),)) 42 assert len(cap.capture) == 1, "Should only have dup of first item" 43 assert cap.capture[0][0] == name, (cap.capture[0], name) 44 assert cap.capture[0][1]["x"] == attr, (cap.capture[0][1], attr)
45
46 -def has_no_group(format, term):
47 parser = format.make_parser() 48 cap = Capture() 49 parser.setContentHandler(cap) 50 try: 51 parser.parseString(term) 52 except Martel.Parser.ParserException: 53 raise AssertionError("Cannot parse %s" % (repr(term),)) 54 assert not cap.capture, "Should have nothing, not %s" % (cap.capture,)
55 56 57
58 -def test_Float():
59 parseString = Martel.Float().make_parser().parseString 60 61 for head in ("", "-", "+", "-1", "+2", "3"): 62 for tail in ("", "E0", "E+0", "E-0", "E4", "e+5", "e-6", 63 "E10", "E-19", "e+28"): 64 for middle in (".1", "5.", "7.6", "989", ".0001"): 65 must_parse("Float", parseString, head + middle + tail) 66 67 for term in ("1E", ".E", "1.E", "1/", "E0", "1.2E0K", 68 "=1", "+-1", ".", "e", "-e", "-e0"): 69 must_not_parse("not Float", parseString, term) 70 71 has_group(Martel.Float("spam", {"x": "spot"}), "1.0", "spam", "spot") 72 has_group(Martel.Float("eggs", {"x": "SPOT"}), "0.8", "eggs", "SPOT") 73 has_no_group(Martel.Float(), "+1")
74
75 -def test_Digits():
76 parseString = Martel.Digits().make_parser().parseString 77 for term in ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 78 "20", "99", "453", "34653", "34359739467623"): 79 must_parse("Digits", parseString, term) 80 81 for term in ("A", "1A", "123123123T", "-1"): 82 must_not_parse("not Digits", parseString, term) 83 84 has_group(Martel.Digits("spam", {"x": "this"}), "5", "spam", "this") 85 has_group(Martel.Digits("eggs", {"x": "that"}), "9", "eggs", "that") 86 has_no_group(Martel.Digits(), "00")
87
88 -def test_Word():
89 parseString = Martel.Word().make_parser().parseString 90 for term in ("Andrew", "Dalke", "was_here", "test12", "12df"): 91 must_parse("Word", parseString, term) 92 for term in ("*", "", "this-that"): 93 must_not_parse("not Word", parseString, term) 94 95 has_group(Martel.Word("spam", {"x": "fly"}), "_", "spam", "fly") 96 has_group(Martel.Word("eggs", {"x": "boy"}), "9", "eggs", "boy") 97 has_no_group(Martel.Word(), "__")
98
99 -def test_Spaces():
100 parseString = Martel.Spaces().make_parser().parseString 101 for term in (" ", "\t", " ", " \t \t\t "): 102 must_parse("Spaces", parseString, term) 103 for term in ("\n", " \n", " X ", ""): 104 must_not_parse("not Spaces", parseString, term) 105 has_group(Martel.Spaces("spam", {"x": "pick"}), " "*100, 106 "spam", "pick") 107 has_group(Martel.Spaces("eggs", {"x": "name"}), "\t"*200, 108 "eggs", "name") 109 has_no_group(Martel.Spaces(), " ")
110
111 -def test_Unprintable():
112 parseString = Martel.Unprintable().make_parser().parseString 113 unprintables = [] 114 for i in range(0, 256): 115 c = chr(i) 116 if c in string.printable: 117 must_not_parse("not Unprintable", parseString, c) 118 else: 119 must_parse("Unprintable", parseString, c) 120 unprintables.append(c) 121 122 has_group(Martel.Unprintable("spam", {"x": "import"}), 123 unprintables[0], "spam", "import") 124 has_group(Martel.Unprintable("eggs", {"x": "export"}), 125 unprintables[-1], "eggs", "export") 126 has_no_group(Martel.Unprintable(), unprintables[1])
127
128 -def test_Punctuation():
129 parseString = Martel.Punctuation().make_parser().parseString 130 for i in range(0, 256): 131 c = chr(i) 132 if c in string.punctuation: 133 must_parse("Punctuation", parseString, c) 134 else: 135 must_not_parse("not Punctuation", parseString, c) 136 137 has_group(Martel.Punctuation("spam", {"x": "Iran"}), 138 string.punctuation[0], "spam", "Iran") 139 has_group(Martel.Punctuation("eggs", {"x": "Iraq"}), 140 string.punctuation[-1], "eggs", "Iraq") 141 has_no_group(Martel.Punctuation(), string.punctuation[1])
142
143 -def test_ToEol():
144 parser = Martel.ToEol("SantaFe").make_parser() 145 parseString = parser.parseString 146 must_parse("ToEol", parseString, "Testing, 1, 2, 3!\n") 147 must_parse("ToEol", parseString, "Andrew\n") 148 must_not_parse("ToEol", parseString, "Dalke") 149 must_not_parse("ToEol", parseString, "This\nis") 150 must_not_parse("ToEol", parseString, "This\nis a test\n") 151 152 file = StringIO.StringIO() 153 parser.setContentHandler(saxutils.XMLGenerator(file)) 154 parser.parseString("This is a test.\n") 155 s = file.getvalue() 156 expect = "<SantaFe>This is a test.</SantaFe>\n" 157 assert string.find(s, expect) != -1, ("Got: %s" % (repr(s),))
158
159 -def test_ToSep():
160 exp = Martel.Group("test", 161 Martel.ToSep("colon", ":") + \ 162 Martel.ToSep("space", " ") + \ 163 Martel.ToSep("empty", "!")) 164 parser = exp.make_parser() 165 166 file = StringIO.StringIO() 167 parser.setContentHandler(saxutils.XMLGenerator(file)) 168 parser.parseString("q:wxy !") 169 s = file.getvalue() 170 expect = "<test><colon>q</colon>:<space>wxy</space> <empty></empty>!</test>" 171 assert string.find(s, expect) != -1, ("Got: %s" % (repr(s),))
172 173
174 -def test_DelimitedFields():
175 exp = Martel.Group("test", Martel.DelimitedFields("Field", "/")) 176 parser = exp.make_parser() 177 178 file = StringIO.StringIO() 179 parser.setContentHandler(saxutils.XMLGenerator(file)) 180 parser.parseString("a/b/cde/f//\n") 181 s = file.getvalue() 182 expect = "<test><Field>a</Field>/<Field>b</Field>/<Field>cde</Field>/" \ 183 "<Field>f</Field>/<Field></Field>/<Field></Field>\n</test>" 184 assert string.find(s, expect) != -1, ("Got: %s" % (repr(s),))
185 186
187 -def test():
188 test_Digits() 189 test_Float() 190 test_Word() 191 test_Spaces() 192 test_Unprintable() 193 test_Punctuation() 194 test_ToEol() 195 test_ToSep() 196 test_DelimitedFields()
197 198 if __name__ == "__main__": 199 test() 200