translate: json cleanup improvement

master
mutantmonkey 2013-11-01 21:54:37 -07:00
parent 3988a73ade
commit e195830e0a
2 changed files with 10 additions and 9 deletions

View File

@ -15,26 +15,26 @@ class TestTranslation(unittest.TestCase):
def test_translate(self): def test_translate(self):
out = translate("plomo o plata", input='es') out = translate("plomo o plata", input='es')
self.assertEqual(('lead or silver', 'es'), out) self.assertEqual(('lead or silver', 'es'), out)
def test_tr(self): def test_tr(self):
input = Mock(groups=lambda: ('fr', 'en', 'mon chien')) input = Mock(groups=lambda: ('fr', 'en', 'mon chien'))
tr(self.phenny, input) tr(self.phenny, input)
out = self.phenny.reply.call_args[0][0] self.assertIn("my dog", 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): def test_tr2(self):
input = Mock(group=lambda x: 'Estoy bien') input = Mock(group=lambda x: 'Estoy bien')
tr2(self.phenny, input) tr2(self.phenny, input)
out = self.phenny.reply.call_args[0][0] self.assertIn("I'm fine", self.phenny.reply.call_args[0][0])
m = re.match("^\"I'm fine\" \(es to en, .*\)$",
out, flags=re.UNICODE) def test_translate_bracketnull(self):
self.assertTrue(m) input = Mock(group=lambda x: 'Moja je lebdjelica puna jegulja')
tr2(self.phenny, input)
self.assertIn("My hovercraft is full of eels",
self.phenny.reply.call_args[0][0])
def test_mangle(self): def test_mangle(self):
input = Mock(group=lambda x: 'Mangle this phrase!') input = Mock(group=lambda x: 'Mangle this phrase!')

View File

@ -34,6 +34,7 @@ def translate(text, input='auto', output='en'):
while ',,' in result: while ',,' in result:
result = result.replace(',,', ',null,') result = result.replace(',,', ',null,')
result = result.replace('[,', '[null,')
data = json.loads(result) data = json.loads(result)
if raw: if raw: