fix translate and add test

master
mutantmonkey 2012-06-02 00:58:19 -07:00
parent 743d92700c
commit f995769b6f
2 changed files with 51 additions and 8 deletions

View File

@ -0,0 +1,43 @@
"""
test_translate.py - tests for the translation module
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
"""
import re
import unittest
from mock import MagicMock, Mock
from modules.translate import translate, tr, tr2, mangle
class TestTranslation(unittest.TestCase):
def setUp(self):
self.phenny = MagicMock()
def test_translate(self):
out = translate("plomo o plata", input='es')
self.assertEqual(('lead or silver', 'es'), out)
def test_tr(self):
input = Mock(groups=lambda: ('fr', 'en', 'mon chien'))
tr(self.phenny, input)
out = self.phenny.reply.call_args[0][0]
m = re.match("^\"my dog\" \(fr to en, .*\)$",
out, flags=re.UNICODE)
self.assertTrue(m)
def test_tr2(self):
input = Mock(group=lambda x: 'Estoy bien')
tr2(self.phenny, input)
out = self.phenny.reply.call_args[0][0]
m = re.match("^\"I'm fine\" \(es to en, .*\)$",
out, flags=re.UNICODE)
self.assertTrue(m)
def test_mangle(self):
input = Mock(group=lambda x: 'Mangle this phrase!')
mangle(self.phenny, input)
self.phenny.reply.assert_not_called_with('ERRORS SRY')

View File

@ -24,8 +24,9 @@ def translate(text, input='auto', output='en'):
'(X11; U; Linux i686)' + '(X11; U; Linux i686)' +
'Gecko/20071127 Firefox/2.0.0.11' 'Gecko/20071127 Firefox/2.0.0.11'
)] )]
input, output = urllib.parse.quote(input), urllib.parse.quote(output) input = urllib.parse.quote(input)
text = urllib.parse.quote(text) output = urllib.parse.quote(output.encode('utf-8'))
text = urllib.parse.quote(text.encode('utf-8'))
result = opener.open('http://translate.google.com/translate_a/t?' + result = opener.open('http://translate.google.com/translate_a/t?' +
('client=t&hl=en&sl=%s&tl=%s&multires=1' % (input, output)) + ('client=t&hl=en&sl=%s&tl=%s&multires=1' % (input, output)) +
@ -54,12 +55,10 @@ def tr(phenny, context):
return phenny.reply('Phrase must be under 350 characters.') return phenny.reply('Phrase must be under 350 characters.')
input = input or 'auto' input = input or 'auto'
input = input.encode('utf-8') output = (output or 'en')
output = (output or 'en').encode('utf-8')
if input != output: if input != output:
msg, input = translate(phrase, input, output) msg, input = translate(phrase, input, output)
output = output.decode('utf-8')
if msg: if msg:
msg = web.decode(msg) # msg.replace('&#39;', "'") msg = web.decode(msg) # msg.replace('&#39;', "'")
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output) msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
@ -90,7 +89,8 @@ def tr2(phenny, input):
if langcode(prefix): if langcode(prefix):
args[i] = prefix[1:] args[i] = prefix[1:]
command = cmd command = cmd
phrase = command.encode('utf-8') #phrase = command.encode('utf-8')
phrase = command
if (len(phrase) > 350) and (not input.admin): if (len(phrase) > 350) and (not input.admin):
return phenny.reply('Phrase must be under 350 characters.') return phenny.reply('Phrase must be under 350 characters.')
@ -115,14 +115,14 @@ def mangle(phenny, input):
phrase = input.group(2) phrase = input.group(2)
for lang in ['fr', 'de', 'es', 'it', 'ja']: for lang in ['fr', 'de', 'es', 'it', 'ja']:
backup = phrase backup = phrase
phrase = translate(phrase, 'en', lang) phrase, inputlang = translate(phrase, 'en', lang)
if not phrase: if not phrase:
phrase = backup phrase = backup
break break
__import__('time').sleep(0.5) __import__('time').sleep(0.5)
backup = phrase backup = phrase
phrase = translate(phrase, lang, 'en') phrase, inputlang = translate(phrase, lang, 'en')
if not phrase: if not phrase:
phrase = backup phrase = backup
break break