Fixed some bugs, tested some more and fixed those two. Boomshakalaka!

This commit is contained in:
Jeremy Fincher 2003-11-10 22:43:19 +00:00
parent 473ce16776
commit bb9cb5d67c
2 changed files with 32 additions and 11 deletions

View File

@ -77,10 +77,10 @@ class OptionList(object):
while True: while True:
token = lexer.get_token() token = lexer.get_token()
if not token: if not token:
raise SyntaxError, 'Missing ")"' return '(%s' % ''.join(ret) #)
elif token == ')': elif token == ')':
if len(ret) > 1: if len(ret) > 1:
return ret return [x for x in ret if x != '|']
elif len(ret) == 1: elif len(ret) == 1:
return '(%s)' % ret[0] return '(%s)' % ret[0]
else: else:
@ -88,7 +88,7 @@ class OptionList(object):
elif token == '(': elif token == '(':
ret.append(self._insideParens(lexer)) ret.append(self._insideParens(lexer))
elif token == '|': elif token == '|':
continue ret.append(token)
else: else:
ret.append(token) ret.append(token)
@ -106,7 +106,17 @@ class OptionList(object):
elif token == '(': elif token == '(':
ret.append(self._insideParens(lexer)) ret.append(self._insideParens(lexer))
elif token == ')': elif token == ')':
raise SyntaxError, 'Spurious ")"' if ret: #(
ret[-1] += ')'
else: #(
ret.append(')')
elif token == '|':
ret.append(token)
else:
if ret and ret[-1] == '|':
pipe = ret.pop()
first = ret.pop()
ret.append(pipe.join([first, token]))
else: else:
ret.append(token) ret.append(token)
return ret return ret

View File

@ -38,18 +38,29 @@ except ImportError:
if sqlite is not None: if sqlite is not None:
MoobotFactoids = Owner.loadPluginModule('MoobotFactoids') MoobotFactoids = Owner.loadPluginModule('MoobotFactoids')
MF = MoobotFactoids
class OptionListTestCase(unittest.TestCase): class OptionListTestCase(unittest.TestCase):
def testEmptyParens(self): def testEmptyParens(self):
self.assertEqual(MoobotFactoids.tokenize('()'), ['()']) self.assertEqual(MF.tokenize('()'), ['()'])
def testNoBarParens(self): def testNoBarParens(self):
self.assertEqual(MoobotFactoids.tokenize('(foo)'), ['(foo)']) self.assertEqual(MF.tokenize('(foo)'), ['(foo)'])
def testDanglingParens(self): def testDanglingParens(self):
self.assertEqual(MoobotFactoids.tokenize('(foo'), ['(foo']) self.assertEqual(MF.tokenize('(foo'), ['(foo'])
self.assertEqual(MoobotFactoids.tokenize('(foo|bar'),['(foo|bar']) self.assertEqual(MF.tokenize('(foo|bar'),['(foo|bar'])
self.assertEqual(MoobotFactoids.tokenize('foo)'), ['foo)']) self.assertEqual(MF.tokenize('foo)'), ['foo)'])
self.assertEqual(MoobotFactoids.tokenize('foo|bar)'),['foo|bar)']) self.assertEqual(MF.tokenize('foo|bar)'),['foo|bar)'])
def testPipesOutsideParens(self):
self.assertEqual(MF.tokenize('1|2'), ['1|2'])
def testStandardBehavior(self):
self.assertEqual(MF.tokenize('(foo|bar)'), [['foo', 'bar']])
self.assertEqual(MF.tokenize('(foo|bar|baz)'),
[['foo','bar','baz']])
self.assertEqual(MF.tokenize('(foo|(bar|baz))'),
[['foo', ['bar', 'baz']]])
class FactoidsTestCase(PluginTestCase, PluginDocumentation): class FactoidsTestCase(PluginTestCase, PluginDocumentation):
plugins = ('MoobotFactoids', 'User', 'Utilities') plugins = ('MoobotFactoids', 'User', 'Utilities')