class PluralTest(FixtureTestCase): check = PythonFormatCheck() def test_arabic(self): arabic = Language.objects.get(code='ar') translation = Translation(language=arabic, plural=arabic.plural) unit = Unit(translation=translation) # Singular, correct format string self.assertFalse(self.check.check_target_unit(['hello %s'], ['hell %s'], unit)) # Singular, missing format string self.assertTrue(self.check.check_target_unit(['hello %s'], ['hell'], unit)) # Plural, correct format string self.assertFalse( self.check.check_target_unit(['hello %s'] * 2, ['hell %s'] * 6, unit) ) # Plural, missing format string self.assertTrue( self.check.check_target_unit(['hello %s'] * 2, ['hell'] * 6, unit) ) # Plural, correct format string (missing on single value plurals) self.assertFalse( self.check.check_target_unit( ['hello %s'] * 2, ['hell'] * 3 + ['hello %s'] * 3, unit ) ) # Plural, missing format string on multi value plural self.assertTrue( self.check.check_target_unit( ['hello %s'] * 2, ['hell'] * 4 + ['hello %s'] * 2, unit ) )
class PythonFormatCheckTest(CheckTestCase): check = PythonFormatCheck() def setUp(self): super(PythonFormatCheckTest, self).setUp() self.test_highlight = ( 'python-format', '%sstring%d', [(0, 2, u'%s'), (8, 10, u'%d')], ) def test_no_format(self): self.assertFalse(self.check.check_format( 'strins', 'string', False )) def test_format(self): self.assertFalse(self.check.check_format( '%s string', '%s string', False )) def test_percent_format(self): self.assertFalse(self.check.check_format( '%d%% string', '%d%% string', False )) def test_named_format(self): self.assertFalse(self.check.check_format( '%(name)s string', '%(name)s string', False )) def test_missing_format(self): self.assertTrue(self.check.check_format( '%s string', 'string', False )) def test_missing_named_format(self): self.assertTrue(self.check.check_format( '%(name)s string', 'string', False )) def test_missing_named_format_ignore(self): self.assertFalse(self.check.check_format( '%(name)s string', 'string', True )) def test_wrong_format(self): self.assertTrue(self.check.check_format( '%s string', '%c string', False )) def test_reordered_format(self): self.assertTrue(self.check.check_format( '%s %d string', '%d %s string', False )) def test_wrong_named_format(self): self.assertTrue(self.check.check_format( '%(name)s string', '%(jmeno)s string', False )) def test_reordered_named_format(self): self.assertFalse(self.check.check_format( '%(name)s %(foo)s string', '%(foo)s %(name)s string', False )) def test_reordered_named_format_long(self): self.assertFalse(self.check.check_format( '%(count)d strings into %(languages)d languages %(percent)d%%', '%(languages)d dil içinde %(count)d satır %%%(percent)d', False ))
class PluralTest(FixtureTestCase): check = PythonFormatCheck() def test_arabic(self): arabic = Language.objects.get(code="ar") translation = Translation(language=arabic, plural=arabic.plural) unit = Unit(translation=translation) # Singular, correct format string self.assertFalse( self.check.check_target_unit(["hello %s"], ["hell %s"], unit)) # Singular, missing format string self.assertTrue( self.check.check_target_unit(["hello %s"], ["hell"], unit)) # Plural, correct format string self.assertFalse( self.check.check_target_unit(["hello %s"] * 2, ["hell %s"] * 6, unit)) # Plural, missing format string self.assertTrue( self.check.check_target_unit(["hello %s"] * 2, ["hell"] * 6, unit)) # Plural, correct format string (missing on single value plurals) self.assertFalse( self.check.check_target_unit(["hello %s"] * 2, ["hell"] * 3 + ["hello %s"] * 3, unit)) # Plural, missing format string on multi value plural self.assertTrue( self.check.check_target_unit(["hello %s"] * 2, ["hell"] * 4 + ["hello %s"] * 2, unit)) def test_non_format_singular(self): czech = Language.objects.get(code="cs") translation = Translation(language=czech, plural=czech.plural) unit = Unit(translation=translation) self.assertFalse( self.check.check_target_unit( ["One apple", "%d apples"], ["%d jablko", "%d jablka", "%d jablek"], unit, )) self.assertFalse( self.check.check_target_unit( ["One apple", "%d apples"], ["Jedno jablko", "%d jablka", "%d jablek"], unit, )) self.assertTrue( self.check.check_target_unit( ["One apple", "%d apples"], ["Jedno jablko", "jablka", "%d jablek"], unit, )) def test_non_format_singular_named(self): language = Language.objects.get(code="cs") translation = Translation(language=language, plural=language.plural) unit = Unit(translation=translation) self.assertFalse( self.check.check_target_unit( ["One apple", "%(count)s apples"], ["%(count)s jablko", "%(count)s jablka", "%(count)s jablek"], unit, )) self.assertFalse( self.check.check_target_unit( ["One apple", "%(count)s apples"], ["Jedno jablko", "%(count)s jablka", "%(count)s jablek"], unit, )) self.assertTrue( self.check.check_target_unit( ["One apple", "%(count)s apples"], ["Jedno jablko", "jablka", "%(count)s jablek"], unit, )) def test_non_format_singular_named_be(self): language = Language.objects.get(code="be") translation = Translation(language=language, plural=language.plural) unit = Unit(translation=translation) self.assertTrue( self.check.check_target_unit( ["One apple", "%(count)s apples"], ["Jedno jablko", "%(count)s jablka", "%(count)s jablek"], unit, )) def test_non_format_singular_named_kab(self): language = Language.objects.get(code="kab") translation = Translation(language=language, plural=language.plural) unit = Unit(translation=translation) self.assertFalse( self.check.check_target_unit( ["One apple", "%(count)s apples"], ["Jedno jablko", "%(count)s jablka", "%(count)s jablek"], unit, ))
class PythonFormatCheckTest(CheckTestCase): check = PythonFormatCheck() def setUp(self): super().setUp() self.test_highlight = ( "python-format", "%sstring%d", [(0, 2, "%s"), (8, 10, "%d")], ) def test_no_format(self): self.assertFalse(self.check.check_format("strins", "string", False)) def test_format(self): self.assertFalse( self.check.check_format("%s string", "%s string", False)) def test_space_format(self): self.assertTrue( self.check.check_format("%d % string", "%d % other", False)) def test_percent_format(self): self.assertFalse( self.check.check_format("%d%% string", "%d%% string", False)) self.assertTrue( self.check.check_format("12%% string", "12% string", False)) self.assertTrue( self.check.check_format("Save 12%%.", "Save 12%.", False)) self.assertFalse( self.check.check_format("Save 12%%.", "Save 12 percent.", False)) def test_named_format(self): self.assertFalse( self.check.check_format("%(name)s string", "%(name)s string", False)) def test_missing_format(self): self.assertTrue(self.check.check_format("%s string", "string", False)) def test_missing_named_format(self): self.assertTrue( self.check.check_format("%(name)s string", "string", False)) def test_missing_named_format_ignore(self): self.assertFalse( self.check.check_format("%(name)s string", "string", True)) def test_wrong_format(self): self.assertTrue( self.check.check_format("%s string", "%c string", False)) def test_reordered_format(self): self.assertTrue( self.check.check_format("%s %d string", "%d %s string", False)) def test_wrong_named_format(self): self.assertTrue( self.check.check_format("%(name)s string", "%(jmeno)s string", False)) def test_reordered_named_format(self): self.assertFalse( self.check.check_format("%(name)s %(foo)s string", "%(foo)s %(name)s string", False)) def test_reordered_named_format_long(self): self.assertFalse( self.check.check_format( "%(count)d strings into %(languages)d languages %(percent)d%%", "%(languages)d dil içinde %(count)d satır %%%(percent)d", False, )) def test_feedback(self): self.assertEqual( self.check.check_format("%(count)d", "%(languages)d", False), { "missing": ["(count)d"], "extra": ["(languages)d"] }, ) self.assertEqual( self.check.check_format("%(count)d", "count", False), { "missing": ["(count)d"], "extra": [] }, ) self.assertEqual( self.check.check_format("%(count)d", "%(count)d %(languages)d", False), { "missing": [], "extra": ["(languages)d"] }, ) self.assertEqual( self.check.check_format("%d", "%s", False), { "missing": ["d"], "extra": ["s"] }, ) self.assertEqual(self.check.check_format("%d", "ds", False), { "missing": ["d"], "extra": [] }) self.assertEqual( self.check.check_format("%d", "%d %s", False), { "missing": [], "extra": ["s"] }, ) self.assertEqual( self.check.check_format("%d %d", "%d", False), { "missing": ["d"], "extra": [] }, ) def test_description(self): unit = Unit( source="%(count)d", target="%(languages)d", extra_flags="python-format", ) check = Check(unit=unit) self.assertEqual( self.check.get_description(check), "Following format strings are missing: %(count)d<br />" "Following format strings are extra: %(languages)d", )
class PythonFormatCheckTest(CheckTestCase): check = PythonFormatCheck() def setUp(self): super().setUp() self.test_highlight = ( "python-format", "%sstring%d", [(0, 2, "%s"), (8, 10, "%d")], ) def test_no_format(self): self.assertFalse(self.check.check_format("strins", "string", False)) def test_format(self): self.assertFalse( self.check.check_format("%s string", "%s string", False)) def test_space_format(self): self.assertTrue( self.check.check_format("%d % string", "%d % other", False)) def test_percent_format(self): self.assertFalse( self.check.check_format("%d%% string", "%d%% string", False)) self.assertTrue( self.check.check_format("12%% string", "12% string", False)) self.assertTrue( self.check.check_format("Save 12%%.", "Save 12%.", False)) self.assertFalse( self.check.check_format("Save 12%%.", "Save 12 percent.", False)) def test_named_format(self): self.assertFalse( self.check.check_format("%(name)s string", "%(name)s string", False)) def test_missing_format(self): self.assertTrue(self.check.check_format("%s string", "string", False)) def test_missing_named_format(self): self.assertTrue( self.check.check_format("%(name)s string", "string", False)) def test_missing_named_format_ignore(self): self.assertFalse( self.check.check_format("%(name)s string", "string", True)) def test_wrong_format(self): self.assertTrue( self.check.check_format("%s string", "%c string", False)) def test_reordered_format(self): self.assertTrue( self.check.check_format("%s %d string", "%d %s string", False)) def test_wrong_named_format(self): self.assertTrue( self.check.check_format("%(name)s string", "%(jmeno)s string", False)) def test_reordered_named_format(self): self.assertFalse( self.check.check_format("%(name)s %(foo)s string", "%(foo)s %(name)s string", False)) def test_reordered_named_format_long(self): self.assertFalse( self.check.check_format( "%(count)d strings into %(languages)d languages %(percent)d%%", "%(languages)d dil içinde %(count)d satır %%%(percent)d", False, ))