def test_open_file(self): extractor = parser.Strings() dir = os.path.dirname(__file__) file = os.path.join(dir, "test.strings") self.assertEquals( extractor._open_file(file)[0].read(), StringsTests.sample_strings)
def test_translate(self, mkdir_func, listdir_func): listdir_func.return_value = [ "Localizable.strings", "Localizable.stringsdict" ] lproj_parser = parser.LPROJ() test_file = teststringio.TestStringIO() fake_strings_parser = parser.Strings() fake_stringsdict_parser = parser.StringsDict() vcs_class = mock.Mock() lproj_parser._open_file = mock.Mock(return_value=test_file) lproj_parser._create_strings_parser = mock.Mock( return_value=fake_strings_parser) lproj_parser._create_stringsdict_parser = mock.Mock( return_value=fake_stringsdict_parser) fake_stringsdict_parser.translate = mock.Mock() string_mapping = stringmapping.StringMapping() string_mapping.add_mapping('"SomeString"', u"Translation for some string") string_mapping.add_mapping('"NewString"', u"Untranslated string") string_mapping.add_mapping('InfoPlistVar', u"Untranslated string") fake_strings_parser.extract_mapping_from_filename = mock.Mock( return_value=string_mapping) translation_dict = { u"Translation for some string": u"Traduzione di Bablefish per questa stringa", u"Extra string": u"Not in file to localize", } output_filenames = lproj_parser.translate("en.lproj", "Resources", translation_dict, "Italian", "it", True, vcs_class) mkdir_func.assert_called_with(os.path.join("Resources", "it.lproj")) lproj_parser._open_file.assert_called_with( os.path.join("Resources", "it.lproj", "Localizable.strings")) self.assertEquals(output_filenames, os.path.join("Resources", "it.lproj")) self.assertEquals( test_file.getvalue(), """"NewString" = "Untranslated string"; "SomeString" = "Traduzione di Bablefish per questa stringa"; InfoPlistVar = "Untranslated string";\n""") fake_stringsdict_parser.translate.assert_called_with( os.path.join("en.lproj", "Localizable.stringsdict"), os.path.join("Resources", "it.lproj"), translation_dict, "Italian", "it", True, vcs_class) vcs_class.add_file.assert_called_with( os.path.join("Resources", "it.lproj", "Localizable.strings"))
def test_write_mapping_does_not_over_escape_newline(self): file = cStringIO.StringIO() parser.Strings().write_mapping( file, { u'"SomeString"': u'String with a \r\n newline', }) # This test is a little deceptive because we have to escape the python # string. The real string has only one backslash for each escaped # character. It is "String with a \r\n newline". self.assertEquals( file.getvalue(), """"SomeString" = "String with a \\r\\n newline"; """)
def test_write_mapping(self): file = cStringIO.StringIO() parser.Strings().write_mapping( file, { u'"SomeString"': u'Translation for some string', u'"SomeOtherString"': u'Can\'t "quote" this!' }) self.assertEquals( file.getvalue(), """"SomeOtherString" = "Can\'t \\"quote\\" this!"; "SomeString" = "Translation for some string"; """) file.close()
def test_extract_strings_from_filename(self): extractor = parser.Strings() extractor._open_file = mock.Mock( return_value=(cStringIO.StringIO(StringsTests.sample_strings), "utf_8")) strings = extractor.extract_strings_from_filename("some_file") self.assertEquals( strings, set([ u"SomeString", u"SomeOtherString", u"A string that translates to itself", u"YetAnotherString", u"Could not open font \\\"{0}\\\".", u"InfoPlistVariable", u"base localization string" ]))
def test_extract_mapping_from_filename(self): extractor = parser.Strings() extractor._open_file = mock.Mock( return_value=(cStringIO.StringIO(StringsTests.sample_strings), "utf_8")) string_mapping = extractor.extract_mapping_from_filename("some_file") self.assertEquals( string_mapping.string_mapping_dict, { u"SomeString": u"Translation for some string", u"SomeOtherString": u"Translation for the other string", u"A string that translates to itself": u"A string that translates to itself", u"YetAnotherString": u"Yet another\\nstring", u"Could not open font \"{0}\".": u"Could not open font \"{0}\".", u"InfoPlistVariable": u"Info Plist Variable", u"base localization string": u"base localization string" }) for key, value in string_mapping.string_mapping_dict.iteritems(): self.assertEquals(type(key), types.UnicodeType) self.assertEquals(type(value), types.UnicodeType)
def test_create_strings_parser(self): lproj_parser = parser.LPROJ() self.assertEquals(type(lproj_parser._create_strings_parser()), type(parser.Strings()))