1
2
3 from Martel import Re
4 from Martel.Expression import *
5
6 from Martel.Expression import _minimize_any_range, _minimize_escape_range, \
7 _minimize_escape_char, _verify_name
8
9
11 assert s1 == s2, "%s != %s" % (repr(s1), repr(s2))
12
15
22
31
35
38
41
44
46 compare(str(Group(None, Literal("a"))), "(a)")
47 compare(str(Group("name1", Literal("a"))), "(?P<name1>a)")
48
49 compare(str(Group(None, Str("abcdef"))), "(abcdef)")
50 compare(str(Group("name1", Str("abcdef"))), "(?P<name1>abcdef)")
51 try:
52 Group("illegal name", Literal("a"))
53 except AssertionError:
54 pass
55 else:
56 raise AssertionError, "wasn't catching illegal name"
57
61
65
67 compare(str(MaxRepeat(Literal("a"))), "a*")
68 compare(str(MaxRepeat(Literal("a"), 1)), "a+")
69 compare(str(MaxRepeat(Literal("a"), 0, 1)), "a?")
70 compare(str(MaxRepeat(Literal("a"), 1, 1)), "a")
71 compare(str(MaxRepeat(Literal("a"), 5, 5)), "a{5}")
72 compare(str(MaxRepeat(Literal("a"), 0, 2)), "a{,2}")
73 compare(str(MaxRepeat(Literal("a"), 8)), "a{8,}")
74 compare(str(MaxRepeat(Literal("a"), 3, 9)), "a{3,9}")
75
76 compare(str(MaxRepeat(Literal("a"), "z", "z")), "a{z}")
77
78
79 compare(str(MaxRepeat(Literal("a"), "x", 9)), "a{x,9}")
80 compare(str(MaxRepeat(Literal("a"), "x", "y")), "a{x,y}")
81
82 compare(str(MaxRepeat(Str("test"), 5, 6)), "(test){5,6}")
83 compare(str(MaxRepeat( Literal("a") | Str("test"))), "(a|test)*")
84 compare(str(MaxRepeat( Literal("a") + Str(" test"))), "(a test)*")
85
89
91 compare(str(Str("Andrew Dalke")), "Andrew Dalke")
92
94 compare(str(Alt( (Any("abc", 1), Dot(), Literal("Q"), Str("hello")) )),
95 "[^a-c]|.|Q|hello")
96
97
98 L = Literal
99 alt1 = Alt( (L("a"), L("b"), L("c")) )
100 alt2 = Alt( (L("x"), L("y"), L("z")) )
101
102 alt = Alt( (alt1, alt2, alt1, alt2) )
103
104 compare(str(alt), "a|b|c|x|y|z|a|b|c|x|y|z")
105
106
108 compare(str(Seq( (Any("abc", 1), Dot(), Literal("Q"), Str("hello")) )),
109 "[^a-c].Qhello")
110
111
112 L = Literal
113 alt = Alt( (L("a"), L("b"), L("c")) )
114
115 compare(str(Seq( (alt, alt) )), "(a|b|c)(a|b|c)")
116 compare(str(Seq( (alt, L("x"), alt) )), "(a|b|c)x(a|b|c)")
117
118 seq = Seq( (L("a"), L("b"), L("c")) )
119 compare(str(Seq( (seq, seq) )), "abcabc")
120
121 compare(str(Seq( (seq, alt, seq, alt) )), "abc(a|b|c)abc(a|b|c)")
122
123
125 compare(str(NoCase(Literal("A"))), "[Aa]")
126 compare(str(NoCase(Literal("9.8"))), "9\\.8")
127 compare(str(NoCase(Str("A"))), "[Aa]")
128 compare(str(NoCase(Str("AB"))), "[Aa][Bb]")
129 compare(str(NoCase(Re("[ab]"))), "[ABab]")
130 compare(str(NoCase(Re("[abC]"))), "[A-Ca-c]")
131 compare(str(NoCase(Any("1AbC9"))), "[19A-Ca-c]")
132 compare(str(NoCase(Str("age = 10"))), "[Aa][Gg][Ee] = 10")
133 compare(str(NoCase(Alt( (Str("A")|Str("B")|Str("C"),)))), "[Aa]|[Bb]|[Cc]")
134
152
153
154
156 data = (
157 ("a", "a"),
158 ("ab", "ab"),
159 ("abc", "a-c"),
160 ("abcdef", "a-f"),
161 ("abcj", "a-cj"),
162 ("abcjk", "a-cjk"),
163 ("abcjkl", "a-cj-l"),
164 ("0123456789", r"\d"),
165 (string.lowercase, r"a-z"),
166 (string.lowercase + string.uppercase, r"A-Za-z"),
167 (string.lowercase + string.uppercase + string.digits, r"\dA-Za-z"),
168 ("\007\010", r"\a\b"),
169 ("1357", "1357"),
170 ("", ""),
171 ("$", "$"),
172 ("\\", "\\\\"),
173 ("-0123456789", r"\d-"),
174 ("-", "-"),
175 ("^", r"\^"),
176 ("^-", r"\^-"),
177 ("[]", r"\[\]"),
178 ("\001", "\\\001"),
179 ("\001\002\003\004\005\006\007", "\\\001-\\a"),
180 )
181 for input, output in data:
182 x = _minimize_any_range(input)
183 assert x == output, "%s : %s != %s" % (repr(input), repr(output), repr(x))
184
186 a = Literal('a')
187 b = Literal('b')
188 g1 = Group("foo", a)
189 g2 = Group("bar", b)
190 g = Group("spam", Alt([g1, g2]))
191 assert str(g) == "(?P<spam>(?P<foo>a)|(?P<bar>b))", str(g)
192 assert g.group_names() == ("spam", "foo", "bar"), g.group_names()
193
194 h = g.copy()
195 assert str(h) == "(?P<spam>(?P<foo>a)|(?P<bar>b))", str(h)
196
197 g2.name = "baz"
198 assert g.group_names() == ("spam", "foo", "baz"), g.group_names()
199
200 assert str(h) == "(?P<spam>(?P<foo>a)|(?P<bar>b))", str(h)
201
202 g._select_names(["foo", "baz"])
203 assert g.group_names() == ("foo", "baz"), g.group_names()
204
205
207 good_names = (
208 "x",
209 "name",
210 "spec:header",
211 "xmlns:xlink",
212 ":",
213 ":something",
214 ":012",
215 "_",
216 "_9",
217 "_-_",
218 "A-",
219 "this-field",
220 ":::",
221 "MixedCase",
222 "UPPER",
223 "this._is_.valid",
224 "x0")
225 for name in good_names:
226 _verify_name(name)
227
228 bad_names = (
229 "0",
230 "\b",
231 "-",
232 "A B",
233 "AB ",
234 " AB",
235 ".invalid",
236 )
237 for name in bad_names:
238 try:
239 _verify_name(name)
240 except AssertionError:
241 pass
242 else:
243 raise AssertionError, "Should not have allowed %s" % repr(name)
244
250
251 if __name__ == "__main__":
252 test()
253