def test_skip(self): unit = MockUnit(source='source') self.assertTrue(self.check.should_skip(unit)) unit.flags = 'java-messageformat' self.assertFalse(self.check.should_skip(unit)) unit.flags = 'auto-java-messageformat' self.assertTrue(self.check.should_skip(unit)) unit.source = '{0}' self.assertFalse(self.check.should_skip(unit))
def test_translate(self): machine_translation = self.get_machine(DummyTranslation) self.assertEqual( machine_translation.translate( 'cs', 'Hello', MockUnit(), HttpRequest() ), [] ) self.assertEqual( len( machine_translation.translate( 'cs', 'Hello, world!', MockUnit(), HttpRequest() ) ), 2 )
def test_simple(self): self.assertEqual( highlight_string( "simple {format} string", MockUnit(flags="python-brace-format") ), [(7, 15, "{format}")], )
def test_hit(self): self.assertTrue( self.check.check_target( ["string", "plural"], ["string", "string"], MockUnit("plural_partial_empty"), ))
def test_good(self): self.assertFalse( self.check.check_target( ["string", "plural"], ["translation", "trplural"], MockUnit("plural_good"), ))
def test_translate_language_map(self, **kwargs): machine = self.get_machine() with Stubber(machine.client) as stubber: stubber.add_response( "translate_text", { "TranslatedText": "Ahoj", "SourceLanguageCode": "en", "TargetLanguageCode": "cs", }, { "SourceLanguageCode": ANY, "TargetLanguageCode": ANY, "Text": ANY }, ) unit = MockUnit(code="cs_CZ", source="Hello") unit.translation.component.source_language.code = "en_US" translation = machine.translate(unit) self.assertIsInstance(translation, list) self.assertEqual( translation, [{ "text": "Ahoj", "quality": 88, "service": "AWS", "source": "Hello" }], )
def test_format(self): self.assertFalse( self.check.check_single( u'{{name}} string {{other}}', u'{{name}} {{other}} string', MockUnit('angularjs_format', flags='angularjs-format'), ))
def test_good(self): self.assertFalse( self.check.check_target( ['string', 'plural'], ['translation', 'trplural'], MockUnit('plural_good'), ))
def test_hit(self): self.assertTrue( self.check.check_target( ['string', 'plural'], ['string', 'string'], MockUnit('plural_partial_empty'), ))
def test_controlchars(self): unit = MockUnit(source='Foo\x1b') fix = RemoveControlChars() self.assertEqual(fix.fix_target(['Bar'], unit), (['Bar'], False)) self.assertEqual(fix.fix_target(['Bar\x1b'], unit), (['Bar\x1b'], False)) self.assertEqual(fix.fix_target(['Bar\n'], unit), (['Bar\n'], False))
def test_unicode_check(self): self.assertFalse( self.check.check_target( [self.test_good_matching_unicode[0]], [self.test_good_matching_unicode[1]], MockUnit(flags=self.test_good_matching_unicode[2]), ))
def test_failure_unicode_check(self): self.assertTrue( self.check.check_target( [self.test_good_matching_unicode[0]], [self.test_good_matching_unicode[1]], MockUnit(flags="max-length:10"), ))
def test_wrong_value(self): self.assertTrue( self.check.check_single( u'{{name}} string', u'{{nameerror}} string', MockUnit('angularjs_wrong_value', flags='angularjs-format'), ))
def test_translate_fallback_missing(self): machine_translation = self.get_machine(DummyTranslation) self.assertEqual( machine_translation.translate('de_CZ', 'Hello, world!', MockUnit(), None), [], )
def test_missing_format(self): self.assertTrue( self.check.check_single( u'{{name}} string', u'string', MockUnit('angularjs_missing_format', flags='angularjs-format'), ))
def test_no_format(self): self.assertFalse( self.check.check_single( 'strins', 'string', MockUnit('angularjs_no_format', flags='angularjs-format'), ))
def test_failure_check(self): self.assertTrue( self.check.check_target( [self.test_good_matching[0]], [self.test_good_matching[1]], MockUnit(flags='max-length:10') ) )
def test_multiple_flags_check(self): self.assertFalse( self.check.check_target( [self.test_good_matching_unicode[0]], [self.test_good_matching_unicode[1]], MockUnit(flags=('max-length:3,max-length:12')) ) )
def test_format_ignore_position(self): self.assertFalse( self.check.check_single( '{{name}} string {{other}}', '{{other}} string {{name}}', MockUnit('angularjs_format_ignore_position', flags='angularjs-format'), ) )
def test_check(self): self.assertFalse( self.check.check_target( [self.test_good_matching[0]], [self.test_good_matching[1]], MockUnit(flags=self.test_good_matching[2]) ) )
def test_different_whitespace(self): self.assertFalse( self.check.check_single( u'{{ name }} string', u'{{name}} string', MockUnit('angularjs_different_whitespace', flags='angularjs-format'), ))
def test_translate_fallback_missing(self): machine_translation = self.get_machine() self.assertEqual( machine_translation.translate( MockUnit(code=self.NOTSUPPORTED_VARIANT, source=self.SOURCE_TRANSLATED)), [], )
def test_check_no_highlight(self): highlights = list( self.check.check_highlight( "Hello, {name}!", MockUnit("java_format", flags="java-format") ) ) self.assertListEqual(highlights, [])
def test_format_diff(self): unit = MockUnit(source='Hello word!') self.assertEqual( format_translation( unit.source, unit.translation.component.project.source_language, diff='Hello world!', )['items'][0]['content'], 'Hello wor<del>l</del>d!')
def test_translate_fallback(self): machine_translation = self.get_machine(DummyTranslation) self.assertEqual( len( machine_translation.translate( MockUnit(code="cs_CZ", source="Hello, world!"))), 2, )
def test_multi(self): self.assertEqual( highlight_string( "simple {format} %d string", MockUnit(flags="python-brace-format, python-format"), ), [(7, 15, "{format}"), (16, 18, "%d")], )
def test_overlap(self): self.assertEqual( highlight_string( 'nested <a href="{format}">string</a>', MockUnit(flags="python-brace-format"), ), [(7, 26, '<a href="{format}">'), (32, 36, "</a>")], )
def test_batch(self, machine=None): self.mock_response() if machine is None: machine = self.get_machine() unit = MockUnit(code=self.SUPPORTED, source=self.SOURCE_TRANSLATED) machine.batch_translate([unit]) self.assertNotEqual(unit.machinery["best"], -1) self.assertIn("translation", unit.machinery)
def setUp(self): super().setUp() self.test_highlight = ( "java-messageformat", "{0}string{1}", [(0, 3, "{0}"), (9, 12, "{1}")], ) self.unit = MockUnit(source="source")
def test_translate_fallback(self): machine_translation = self.get_machine(DummyTranslation) self.assertEqual( len( machine_translation.translate('cs_CZ', 'Hello, world!', MockUnit(), None)), 2, )
def test_apostrophes(self): unit = MockUnit(source='Foo') fix = DoubleApostrophes() # No flags self.assertEqual(fix.fix_target(['Bar'], unit), (['Bar'], False)) # No format string, but forced unit.flags = 'java-messageformat' self.assertEqual(fix.fix_target(['Bar'], unit), (['Bar'], False)) # No format string unit.flags = 'auto-java-messageformat' self.assertEqual(fix.fix_target(['Bar'], unit), (['Bar'], False)) unit.source = 'test {0}' # Nothing to fix self.assertEqual(fix.fix_target(['r {0}'], unit), (['r {0}'], False)) # Correct string self.assertEqual(fix.fix_target(["''r'' {0}"], unit), (["''r'' {0}"], False)) # String with quoted format string self.assertEqual(fix.fix_target(["''r'' '{0}'"], unit), (["''r'' '{0}'"], False)) # Fixes self.assertEqual(fix.fix_target(["'r''' {0}"], unit), (["''r'' {0}"], True)) # Fixes keeping double ones self.assertEqual(fix.fix_target(["'''''''r'''' {0}"], unit), (["''''r'''' {0}"], True)) # Quoted format self.assertEqual(fix.fix_target(["'r''' {0}"], unit), (["''r'' {0}"], True)) unit.source = 'foo' unit.flags = 'java-messageformat' self.assertEqual(fix.fix_target(["bar'"], unit), (["bar''"], True))
def test_apostrophes(self): unit = MockUnit(source='Foo') fix = DoubleApostrophes() # No flags self.assertEqual(fix.fix_target(['Bar'], unit), (['Bar'], False)) # No format string, but forced unit.flags = 'java-messageformat' self.assertEqual(fix.fix_target(['Bar'], unit), (['Bar'], False)) # No format string unit.flags = 'auto-java-messageformat' self.assertEqual(fix.fix_target(['Bar'], unit), (['Bar'], False)) unit.source = 'test {0}' # Nothing to fix self.assertEqual(fix.fix_target(['r {0}'], unit), (['r {0}'], False)) # Correct string self.assertEqual( fix.fix_target(["''r'' {0}"], unit), (["''r'' {0}"], False) ) # String with quoted format string self.assertEqual( fix.fix_target(["''r'' '{0}'"], unit), (["''r'' '{0}'"], False) ) # Fixes self.assertEqual( fix.fix_target(["'r''' {0}"], unit), (["''r'' {0}"], True) ) # Fixes keeping double ones self.assertEqual( fix.fix_target(["'''''''r'''' {0}"], unit), (["''''r'''' {0}"], True) ) # Quoted format self.assertEqual( fix.fix_target(["'r''' {0}"], unit), (["''r'' {0}"], True) ) unit.source = 'foo' unit.flags = 'java-messageformat' self.assertEqual( fix.fix_target(["bar'"], unit), (["bar''"], True) )