SupyML: make all the tests work
parent
09fb7b36ab
commit
0ad6fb5bfd
|
@ -60,22 +60,30 @@ class LoopError(Exception):
|
||||||
class LoopTypeIsMissing(Exception):
|
class LoopTypeIsMissing(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
parseMessage = re.compile('\w+: (?P<content>.*)')
|
||||||
class FakeIrc():
|
class FakeIrc():
|
||||||
def __init__(self, irc):
|
def __init__(self, irc):
|
||||||
self._irc = irc
|
self._irc = irc
|
||||||
self._message = ''
|
self._message = ''
|
||||||
self._data = ''
|
self._data = ''
|
||||||
|
self._rawData = None
|
||||||
def error(self, message):
|
def error(self, message):
|
||||||
message = message
|
message = message
|
||||||
self._data = message
|
self._data = message
|
||||||
def reply(self, message):
|
def reply(self, message):
|
||||||
self._data = message
|
self._data = message
|
||||||
def queueMsg(self, message):
|
def queueMsg(self, message):
|
||||||
if message.command == 'PRIVMSG':
|
self._rawData = message
|
||||||
self._data = message.args[1]
|
if message.command in ('PRIVMSG', 'NOTICE'):
|
||||||
|
parsed = parseMessage.match(message.args[1])
|
||||||
|
if parsed is not None:
|
||||||
|
message = parsed.group('content')
|
||||||
|
else:
|
||||||
|
message = message.args[1]
|
||||||
|
self._data = message
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
if name == '_data':
|
if name == '_data' or name == '_rawData':
|
||||||
return self.__dict__['_data']
|
return self.__dict__[name]
|
||||||
return getattr(self.__dict__['_irc'], name)
|
return getattr(self.__dict__['_irc'], name)
|
||||||
|
|
||||||
class SupyMLParser:
|
class SupyMLParser:
|
||||||
|
@ -85,7 +93,7 @@ class SupyMLParser:
|
||||||
self._msg = msg
|
self._msg = msg
|
||||||
self._code = code
|
self._code = code
|
||||||
self.warnings = []
|
self.warnings = []
|
||||||
self._parse(code)
|
self.data = self._parse(code)
|
||||||
|
|
||||||
def _run(self, code, proxify):
|
def _run(self, code, proxify):
|
||||||
"""Runs the command using Supybot engine"""
|
"""Runs the command using Supybot engine"""
|
||||||
|
@ -96,6 +104,7 @@ class SupyMLParser:
|
||||||
else:
|
else:
|
||||||
fakeIrc = self._irc
|
fakeIrc = self._irc
|
||||||
self._plugin.Proxy(fakeIrc, self._msg, tokens)
|
self._plugin.Proxy(fakeIrc, self._msg, tokens)
|
||||||
|
self.rawData = fakeIrc._rawData
|
||||||
if proxify:
|
if proxify:
|
||||||
# TODO : don't wait if the plugin is not threaded
|
# TODO : don't wait if the plugin is not threaded
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
@ -109,6 +118,7 @@ class SupyMLParser:
|
||||||
|
|
||||||
def _processDocument(self, dom, variables={}, proxify=False):
|
def _processDocument(self, dom, variables={}, proxify=False):
|
||||||
"""Handles the root node and call child nodes"""
|
"""Handles the root node and call child nodes"""
|
||||||
|
proxify = True
|
||||||
for childNode in dom.childNodes:
|
for childNode in dom.childNodes:
|
||||||
if isinstance(childNode, minidom.Element):
|
if isinstance(childNode, minidom.Element):
|
||||||
output = self._processNode(childNode, variables, proxify)
|
output = self._processNode(childNode, variables, proxify)
|
||||||
|
@ -177,9 +187,9 @@ class SupyMLParser:
|
||||||
if loopType == 'while':
|
if loopType == 'while':
|
||||||
try:
|
try:
|
||||||
while utils.str.toBool(self._parse(loopCond, variables,
|
while utils.str.toBool(self._parse(loopCond, variables,
|
||||||
True).split(': ')[1]):
|
True).split(': ')[-1]):
|
||||||
loopContent = '<echo>%s</echo>' % loopContent
|
loopContent = '<echo>%s</echo>' % loopContent
|
||||||
output += self._parse(loopContent)
|
output += self._parse(loopContent) or ''
|
||||||
except AttributeError: # toBool() failed
|
except AttributeError: # toBool() failed
|
||||||
pass
|
pass
|
||||||
except ValueError: # toBool() failed
|
except ValueError: # toBool() failed
|
||||||
|
@ -206,6 +216,10 @@ class SupyML(callbacks.Plugin):
|
||||||
parser = SupyMLParser(self, irc, msg, code)
|
parser = SupyMLParser(self, irc, msg, code)
|
||||||
if world.testing and len(parser.warnings) != 0:
|
if world.testing and len(parser.warnings) != 0:
|
||||||
print parser.warnings
|
print parser.warnings
|
||||||
|
if parser.rawData is not None:
|
||||||
|
irc.queueMsg(parser.rawData)
|
||||||
|
else:
|
||||||
|
irc.reply(parser.data)
|
||||||
|
|
||||||
eval=wrap(eval, ['text'])
|
eval=wrap(eval, ['text'])
|
||||||
SupyML = internationalizeDocstring(SupyML)
|
SupyML = internationalizeDocstring(SupyML)
|
||||||
|
|
|
@ -32,7 +32,7 @@ import time
|
||||||
from supybot.test import *
|
from supybot.test import *
|
||||||
|
|
||||||
class SupyMLTestCase(ChannelPluginTestCase):
|
class SupyMLTestCase(ChannelPluginTestCase):
|
||||||
plugins = ('SupyML', 'Utilities', 'Conditional')
|
plugins = ('SupyML', 'Utilities', 'Conditional', 'Math')
|
||||||
#################################
|
#################################
|
||||||
# Utilities
|
# Utilities
|
||||||
def _getIfAnswerIsEqual(self, msg):
|
def _getIfAnswerIsEqual(self, msg):
|
||||||
|
@ -116,6 +116,26 @@ class SupyMLTestCase(ChannelPluginTestCase):
|
||||||
'</loop>'
|
'</loop>'
|
||||||
'</echo>',
|
'</echo>',
|
||||||
'bar')
|
'bar')
|
||||||
|
self.assertResponse('SupyML eval '
|
||||||
|
'<echo>'
|
||||||
|
'<set name="foo">3</set>'
|
||||||
|
'<loop>'
|
||||||
|
'<while>'
|
||||||
|
'<nne>'
|
||||||
|
'<var name="foo" /> 5'
|
||||||
|
'</nne>'
|
||||||
|
'</while>'
|
||||||
|
'<set name="foo">'
|
||||||
|
'<calc>'
|
||||||
|
'<var name="foo" /> + 1'
|
||||||
|
'</calc>'
|
||||||
|
'</set>'
|
||||||
|
'<echo>'
|
||||||
|
'<var name="foo" />'
|
||||||
|
'</echo>'
|
||||||
|
'</loop>'
|
||||||
|
'</echo>',
|
||||||
|
'45')
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
||||||
|
|
Loading…
Reference in New Issue