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

Source Code for Module Martel.test.test_Expression

  1  # regression tests for the Expression module 
  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   
10 -def compare(s1, s2):
11 assert s1 == s2, "%s != %s" % (repr(s1), repr(s2))
12
13 -def test__add__():
14 compare(str(Literal("a") + Literal("b") + Literal("c")), "abc")
15
16 -def test__or__():
17 compare(str(Literal("a") | Literal("b") | Literal("c")), "a|b|c") 18 19 # compose a couple of trees while I'm here 20 compare(str(Literal("a") | (Literal("b") + Literal("c"))), "a|bc") 21 compare(str(Literal("a") + (Literal("b") | Literal("c"))), "a(b|c)")
22
23 -def test_any():
24 compare(str(Any("abcdef")), "[a-f]") 25 compare(str(Any("135")), "[135]") 26 compare(str(Any("0123456789")), r"[\d]") 27 28 compare(str(Any("abcdef", 1)), "[^a-f]") 29 compare(str(Any("135", 1)), "[^135]") 30 compare(str(Any("0123456789", 1)), r"[^\d]")
31
32 -def test_assert():
33 compare(str(Assert(Literal("a"))), "(?=a)") 34 compare(str(Assert(Literal("a"), 1)), "(?!a)")
35
36 -def test_at_beginning():
37 compare(str(AtBeginning()), "^")
38
39 -def test_at_end():
40 compare(str(AtEnd()), "$")
41
42 -def test_dot():
43 compare(str(Dot()), ".")
44
45 -def test_group():
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
58 -def test_groupref():
59 compare(str(GroupRef("name1")), "(?P=name1)") 60 compare(str(GroupRef("N:a:m-e.1")), "(?P=N:a:m-e.1)")
61
62 -def test_literal():
63 compare(str(Literal("x")), "x") 64 compare(str(Literal("y")), "y")
65
66 -def test_max_repeat():
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 # Not that these two are supported, but I expect they will be someday 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
86 -def test_null_op():
87 compare(str(NullOp()), "") 88 compare(str(NullOp() + Str("TeSt")), str(Str("TeSt")))
89
90 -def test_str():
91 compare(str(Str("Andrew Dalke")), "Andrew Dalke")
92
93 -def test_alt():
94 compare(str(Alt( (Any("abc", 1), Dot(), Literal("Q"), Str("hello")) )), 95 "[^a-c]|.|Q|hello") 96 97 # Check that an Alt of Alts is handled correctly 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
107 -def test_seq():
108 compare(str(Seq( (Any("abc", 1), Dot(), Literal("Q"), Str("hello")) )), 109 "[^a-c].Qhello") 110 111 # check that the Alt is correctly grouped 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
124 -def test_nocase():
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
135 -def test_nodes():
136 test__add__() 137 test__or__() 138 test_any() 139 test_assert() 140 test_at_beginning() 141 test_at_end() 142 test_dot() 143 test_group() 144 test_groupref() 145 test_literal() 146 test_max_repeat() 147 test_null_op() 148 test_str() 149 test_alt() 150 test_seq() 151 test_nocase()
152 153 154
155 -def test_minimize():
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
185 -def test_group_names():
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
206 -def test_valid_names():
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
245 -def test():
246 test_valid_names() 247 test_nodes() 248 test_minimize() 249 test_group_names()
250 251 if __name__ == "__main__": 252 test() 253