diff --git a/plugins/MoobotFactoids.py b/plugins/MoobotFactoids.py index 9d0899c2a..43f1627df 100644 --- a/plugins/MoobotFactoids.py +++ b/plugins/MoobotFactoids.py @@ -77,10 +77,10 @@ class OptionList(object): while True: token = lexer.get_token() if not token: - raise SyntaxError, 'Missing ")"' + return '(%s' % ''.join(ret) #) elif token == ')': if len(ret) > 1: - return ret + return [x for x in ret if x != '|'] elif len(ret) == 1: return '(%s)' % ret[0] else: @@ -88,7 +88,7 @@ class OptionList(object): elif token == '(': ret.append(self._insideParens(lexer)) elif token == '|': - continue + ret.append(token) else: ret.append(token) @@ -106,9 +106,19 @@ class OptionList(object): elif token == '(': ret.append(self._insideParens(lexer)) elif token == ')': - raise SyntaxError, 'Spurious ")"' - else: + 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: + ret.append(token) return ret def tokenize(s): diff --git a/test/test_MoobotFactoids.py b/test/test_MoobotFactoids.py index 53719a027..0907779f4 100644 --- a/test/test_MoobotFactoids.py +++ b/test/test_MoobotFactoids.py @@ -38,18 +38,29 @@ except ImportError: if sqlite is not None: MoobotFactoids = Owner.loadPluginModule('MoobotFactoids') + MF = MoobotFactoids class OptionListTestCase(unittest.TestCase): def testEmptyParens(self): - self.assertEqual(MoobotFactoids.tokenize('()'), ['()']) + self.assertEqual(MF.tokenize('()'), ['()']) def testNoBarParens(self): - self.assertEqual(MoobotFactoids.tokenize('(foo)'), ['(foo)']) + self.assertEqual(MF.tokenize('(foo)'), ['(foo)']) def testDanglingParens(self): - self.assertEqual(MoobotFactoids.tokenize('(foo'), ['(foo']) - self.assertEqual(MoobotFactoids.tokenize('(foo|bar'),['(foo|bar']) - self.assertEqual(MoobotFactoids.tokenize('foo)'), ['foo)']) - self.assertEqual(MoobotFactoids.tokenize('foo|bar)'),['foo|bar)']) + self.assertEqual(MF.tokenize('(foo'), ['(foo']) + self.assertEqual(MF.tokenize('(foo|bar'),['(foo|bar']) + self.assertEqual(MF.tokenize('foo)'), ['foo)']) + 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): plugins = ('MoobotFactoids', 'User', 'Utilities')