Commits

Apostolis Bessas  committed d92c0d7

Support checking for missing keys in prtinf-format expressions in validators for lotte.

  • Participants
  • Parent commits 2d89152

Comments (0)

Files changed (2)

File transifex/resources/formats/validators.py

         old_matches = list(self.printf_re.finditer(old))
         new_matches = list(self.printf_re.finditer(new))
         new_conv_specifiers = [pattern.group('type') for pattern in new_matches]
+        new_keys = [pattern.group('key') for pattern in new_matches]
         for pattern in old_matches:
+            key = pattern.group('key')
+            if key not in new_keys:
+                msg = "The expression '%s' is not present in the translation."
+                raise ValidationError( _(msg  % pattern.group(0)))
             conversion_specifier = pattern.group('type')
             try:
                 new_conv_specifiers.remove(conversion_specifier)
         old_matches = list(self.printf_re.finditer(old))
         new_matches = list(self.printf_re.finditer(new))
         old_conv_specifiers = [pattern.group('type') for pattern in old_matches]
+        old_keys = [pattern.group('key') for pattern in old_matches]
         for pattern in new_matches:
+            key = pattern.group('key')
+            if key not in old_keys:
+                msg = "The expression '%s' is not present in the source_string."
+                raise ValidationError( _(msg  % pattern.group(0)))
             conversion_specifier = pattern.group('type')
             try:
                 old_conv_specifiers.remove(conversion_specifier)

File transifex/resources/tests/lib/validators/__init__.py

 # -*- coding: utf-8 -*-
 
-import unittest
+from __future__ import with_statement
+from django.test import TestCase
 from transifex.languages.models import Language
 from transifex.resources.formats.validators import *
 
 
-class TestValidators(unittest.TestCase):
+class TestValidators(TestCase):
 
     def test_empty_translation(self):
         old = 'old'
         new = "%2$d %1$s"
         v(old, new)
 
+        old = "%(foo)s %(bar)s"
+        new = "%(fo0)s %(bar)s"
+        with self.assertRaises(ValidationError) as cm:
+            v(old, new)
+        self.assertIn('foo', unicode(cm.exception))
+        new = "%(foo)s"
+        with self.assertRaises(ValidationError) as cm:
+            v(old, new)
+        self.assertIn('bar', unicode(cm.exception))
+        new = "%(bar)s"
+        with self.assertRaises(ValidationError) as cm:
+            v(old, new)
+        self.assertIn('foo', unicode(cm.exception))
+        new = "%(bar)s %(foo)s"
+        v(old, new)
+
     def test_translation_printf_format(self):
         v = PrintfFormatTranslationValidator()
         old = "%s %d asda %f"
         self.assertRaises(ValidationError, v, old, new)
         old = "%s %d asda %k"
         self.assertRaises(ValidationError, v, old, new)
+
         old = "%s %d"
         new = "%2$d %1$s"
         v(old, new)
 
+        old = "%(foo)s %(bar)s"
+        new = "%(fo0)s %(bar)s"
+        with self.assertRaises(ValidationError) as cm:
+            v(old, new)
+        self.assertIn('fo0', unicode(cm.exception))
+        new = "%(baz)s"
+        with self.assertRaises(ValidationError) as cm:
+            v(old, new)
+        self.assertIn('baz', unicode(cm.exception))
+        new = "%(bar)s %(foo)s"
+        v(old, new)
+
     def test_singular_printf_number(self):
 
         class Language(object):