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_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_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_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_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_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.args[0], 0)
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)