1 import Martel
2 from xml.sax import handler, saxutils
3 import string, StringIO
4
5
6
7
14
23
24 -class Capture(handler.ContentHandler):
28 if not self.capture:
29 self.capture.append( (name, attrs) )
30 self.capture.append( (name, attrs) )
33
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
55
56
57
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
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
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
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
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
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
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
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
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
197
198 if __name__ == "__main__":
199 test()
200