/usr/share/pyshared/ometa/test/test_runtime.py is in python-parsley 1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | import unittest
from ometa.runtime import OMetaBase, ParseError, expected, eof
class RuntimeTests(unittest.TestCase):
"""
Tests for L{pymeta.runtime}.
"""
def test_anything(self):
"""
L{OMetaBase.rule_anything} returns each item from the input
along with its position.
"""
data = "foo"
o = OMetaBase(data)
for i, c in enumerate(data):
v, e = o.rule_anything()
self.assertEqual((c, i), (v, e[0]))
def test_exactly(self):
"""
L{OMetaBase.rule_exactly} returns the requested item from the input
string along with its position, if it's there.
"""
data = "foo"
o = OMetaBase(data)
v, e = o.rule_exactly("f")
self.assertEqual(v, "f")
self.assertEqual(e[0], 0)
def test_exactly_multi(self):
"""
L{OMetaBase.rule_exactly} returns the requested item from the input
string along with its position, if it's there.
"""
data = "foo"
o = OMetaBase(data)
v, e = o.rule_exactly("fo")
self.assertEqual(v, "fo")
self.assertEqual(e[0], 0)
def test_exactlyFail(self):
"""
L{OMetaBase.rule_exactly} raises L{ParseError} when the requested item
doesn't match the input. The error contains info on what was expected
and the position.
"""
data = "foo"
o = OMetaBase(data)
with self.assertRaises(ParseError) as e:
o.rule_exactly("g")
self.assertEquals(e.exception[1], expected(None, "g"))
self.assertEquals(e.exception[0], 0)
def test_token(self):
"""
L{OMetaBase.rule_token} matches all the characters in the given string
plus any preceding whitespace.
"""
data = " foo bar"
o = OMetaBase(data)
v, e = o.rule_token("foo")
self.assertEqual(v, "foo")
self.assertEqual(e[0], 4)
v, e = o.rule_token("bar")
self.assertEqual(v, "bar")
self.assertEqual(e[0], 8)
def test_tokenFailed(self):
"""
On failure, L{OMetaBase.rule_token} produces an error indicating the
position where match failure occurred and the expected character.
"""
data = "foozle"
o = OMetaBase(data)
with self.assertRaises(ParseError) as e:
o.rule_token("fog")
self.assertEqual(e.exception[0], 2)
self.assertEqual(e.exception[1], expected("token", "fog"))
def test_many(self):
"""
L{OMetaBase.many} returns a list of parsed values and the error that
caused the end of the loop.
"""
data = "ooops"
o = OMetaBase(data)
self.assertEqual(o.many(lambda: o.rule_exactly('o')),
(['o'] * 3, ParseError(o.input, 3,
expected(None, 'o'))))
def test_or(self):
"""
L{OMetaBase._or} returns the result of the first of its
arguments to succeed.
"""
data = "a"
o = OMetaBase(data)
called = [False, False, False]
targets = ['b', 'a', 'c']
matchers = []
for i, m in enumerate(targets):
def match(i=i, m=m):
called[i] = True
return o.exactly(m)
matchers.append(match)
v, e = o._or(matchers)
self.assertEqual(called, [True, True, False])
self.assertEqual(v, 'a')
self.assertEqual(e[0], 0)
def test_orSimpleFailure(self):
"""
When none of the alternatives passed to L{OMetaBase._or} succeed, the
one that got the furthest is returned.
"""
data = "foozle"
o = OMetaBase(data)
with self.assertRaises(ParseError) as e:
o._or([lambda: o.token("fog"),
lambda: o.token("foozik"),
lambda: o.token("woozle")])
self.assertEqual(e.exception[0], 4)
self.assertEqual(e.exception[1], expected("token", "foozik"))
def test_orFalseSuccess(self):
"""
When a failing branch of L{OMetaBase._or} gets further than a
succeeding one, its error is returned instead of the success branch's.
"""
data = "foozle"
o = OMetaBase(data)
v, e = o._or( [lambda: o.token("fog"),
lambda: o.token("foozik"),
lambda: o.token("f")])
self.assertEqual(e[0], 4)
self.assertEqual(e[1], expected("token", "foozik"))
def test_orErrorTie(self):
"""
When branches of L{OMetaBase._or} produce errors that tie for rightmost
position, they are merged.
"""
data = "foozle"
o = OMetaBase(data)
v, e = o._or( [lambda: o.token("fog"),
lambda: o.token("foz"),
lambda: o.token("f")])
self.assertEqual(e[0], 2)
self.assertEqual(e[1], [expected("token", "fog")[0],
expected("token", "foz")[0]])
def test_notError(self):
"""
When L{OMetaBase._not} fails, its error contains the current
input position and no error info.
"""
data = "xy"
o = OMetaBase(data)
with self.assertRaises(ParseError) as e:
o._not(lambda: o.exactly("x"))
self.assertEqual(e.exception[0], 1)
self.assertEqual(e.exception[1], None)
def test_spaces(self):
"""
L{OMetaBase.rule_spaces} provides error information.
"""
data = " xyz"
o = OMetaBase(data)
v, e = o.rule_spaces()
self.assertEqual(e[0], 2)
def test_predSuccess(self):
"""
L{OMetaBase.pred} returns True and empty error info on success.
"""
o = OMetaBase("")
v, e = o.pred(lambda: (True, ParseError(o.input, 0, None)))
self.assertEqual((v, e), (True, ParseError(o.input, 0, None)))
def test_predFailure(self):
"""
L{OMetaBase.pred} returns True and empty error info on success.
"""
o = OMetaBase("")
with self.assertRaises(ParseError) as e:
o.pred(lambda: (False, ParseError(o.input, 0, None)))
self.assertEqual(e.exception, ParseError(o.input, 0, None))
def test_end(self):
"""
L{OMetaBase.rule_end} matches the end of input and raises L{ParseError}
if input is left.
"""
o = OMetaBase("abc")
with self.assertRaises(ParseError) as e:
o.rule_end()
self.assertEqual(e.exception, ParseError(o.input, 1, None))
o.many(o.rule_anything)
self.assertEqual(o.rule_end(), (True, ParseError("abc", 3, None)))
def test_label(self):
"""
L{OMetaBase.label} returns a list of parsed values and the error that
caused the end of the loop.
"""
data = "ooops"
label = 'CustomLabel'
o = OMetaBase(data)
with self.assertRaises(ParseError) as e:
o.label(lambda: o.rule_exactly('x'), label)
self.assertEqual(e.exception,
ParseError(o.input, 0, expected(label)).withMessage([("Custom Exception:", label, None)]))
def test_letter(self):
"""
L{OMetaBase.rule_letter} matches letters.
"""
o = OMetaBase("a1")
v, e = o.rule_letter()
self.assertEqual((v, e), ("a", ParseError(o.input, 0, None)))
with self.assertRaises(ParseError) as e:
o.rule_letter()
self.assertEqual(e.exception, ParseError(o.input, 1,
expected("letter")))
def test_letterOrDigit(self):
"""
L{OMetaBase.rule_letterOrDigit} matches alphanumerics.
"""
o = OMetaBase("a1@")
v, e = o.rule_letterOrDigit()
self.assertEqual((v, e), ("a", ParseError(None, 0, None)))
v, e = o.rule_letterOrDigit()
self.assertEqual((v, e), ("1", ParseError(None, 1, None)))
with self.assertRaises(ParseError) as e:
o.rule_letterOrDigit()
self.assertEqual(e.exception,
ParseError(o.input, 2, expected("letter or digit")))
def test_digit(self):
"""
L{OMetaBase.rule_digit} matches digits.
"""
o = OMetaBase("1a")
v, e = o.rule_digit()
self.assertEqual((v, e), ("1", ParseError("1a", 0, None)))
with self.assertRaises(ParseError) as e:
o.rule_digit()
self.assertEqual(e.exception, ParseError(o.input, 1, expected("digit")))
def test_listpattern(self):
"""
L{OMetaBase.rule_listpattern} matches contents of lists.
"""
o = OMetaBase([["a"]], tree=True)
v, e = o.listpattern(lambda: o.exactly("a"))
self.assertEqual((v, e), (["a"], ParseError("a", 0, None)))
|