From f995769b6f72d90c073b78084c43997db18deb84 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Sat, 2 Jun 2012 00:58:19 -0700 Subject: [PATCH] fix translate and add test --- modules/test/test_translate.py | 43 ++++++++++++++++++++++++++++++++++ modules/translate.py | 16 ++++++------- 2 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 modules/test/test_translate.py diff --git a/modules/test/test_translate.py b/modules/test/test_translate.py new file mode 100644 index 0000000..b127a5c --- /dev/null +++ b/modules/test/test_translate.py @@ -0,0 +1,43 @@ +""" +test_translate.py - tests for the translation module +author: mutantmonkey +""" + +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') diff --git a/modules/translate.py b/modules/translate.py index 6fdc3a4..b23fdb3 100644 --- a/modules/translate.py +++ b/modules/translate.py @@ -24,8 +24,9 @@ def translate(text, input='auto', output='en'): '(X11; U; Linux i686)' + 'Gecko/20071127 Firefox/2.0.0.11' )] - input, output = urllib.parse.quote(input), urllib.parse.quote(output) - text = urllib.parse.quote(text) + input = urllib.parse.quote(input) + 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?' + ('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.') input = input or 'auto' - input = input.encode('utf-8') - output = (output or 'en').encode('utf-8') + output = (output or 'en') if input != output: msg, input = translate(phrase, input, output) - output = output.decode('utf-8') if msg: msg = web.decode(msg) # msg.replace(''', "'") msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output) @@ -90,7 +89,8 @@ def tr2(phenny, input): if langcode(prefix): args[i] = prefix[1:] command = cmd - phrase = command.encode('utf-8') + #phrase = command.encode('utf-8') + phrase = command if (len(phrase) > 350) and (not input.admin): return phenny.reply('Phrase must be under 350 characters.') @@ -115,14 +115,14 @@ def mangle(phenny, input): phrase = input.group(2) for lang in ['fr', 'de', 'es', 'it', 'ja']: backup = phrase - phrase = translate(phrase, 'en', lang) + phrase, inputlang = translate(phrase, 'en', lang) if not phrase: phrase = backup break __import__('time').sleep(0.5) backup = phrase - phrase = translate(phrase, lang, 'en') + phrase, inputlang = translate(phrase, lang, 'en') if not phrase: phrase = backup break