Beispiel #1
0
    def test_key_length(self):
        """Test run_tests."""
        good_strings = [
            localizationkit.LocalizedString("Key", "Value", "Comment", "en"),
            localizationkit.LocalizedString("Longer key", "Value", "Comment",
                                            "en"),
            localizationkit.LocalizedString(
                "This is a decently long key to run tests on", "Value",
                "Comment", "en"),
        ]

        bad_strings = [
            localizationkit.LocalizedString(None, "Value", "Comment", "en"),
            localizationkit.LocalizedString("", "Value", "Comment", "en"),
            localizationkit.LocalizedString("a", "Value", "Comment", "en"),
            localizationkit.LocalizedString("ab", "Value", "Comment", "en"),
        ]

        for string in good_strings:
            collection = localizationkit.LocalizedCollection([string])
            key_length_test = localizationkit.tests.key_length.KeyLength(
                self.configuration, collection)
            result = key_length_test.execute()
            self.assertTrue(result.succeeded(), str(result.violations))

        for string in bad_strings:
            collection = localizationkit.LocalizedCollection([string])
            key_length_test = localizationkit.tests.key_length.KeyLength(
                self.configuration, collection)
            result = key_length_test.execute()
            self.assertFalse(result.succeeded())
    def test_has_comments(self):
        """Test that has comments works"""
        bad_comments = [None, "", "Hello", "Hello world", "This is a comment"]
        good_comments = [
            "This is a nice and long comment with lots of words",
            "Here is another comment that also has a lot of words in it",
        ]

        for comment in bad_comments:
            string = localizationkit.LocalizedString("Key", "Value", comment, "en")
            collection = localizationkit.LocalizedCollection([string])
            has_comment_test = localizationkit.tests.has_comments.HasComments(
                self.configuration, collection
            )
            result = has_comment_test.execute()
            self.assertFalse(result.succeeded())

        for comment in good_comments:
            string = localizationkit.LocalizedString("Key", "Value", comment, "en")
            collection = localizationkit.LocalizedCollection([string])
            has_comment_test = localizationkit.tests.has_comments.HasComments(
                self.configuration, collection
            )
            result = has_comment_test.execute()
            self.assertTrue(result.succeeded())
Beispiel #3
0
    def test_alternative_tokens(self):
        """Test that the Swift interpolation test works"""

        good_values = [
            "This is a string",
            "This is another string with (parentheses)",
            "This string has \\( but no closing parenthesis",
        ]

        bad_values = [
            "This string \\(has) interpolation",
            "This string has interpolation at the \\(end)",
            "\\(This) string has interpolation at the \\(start)",
            "\\(This) string has interpolation at both \\(ends)",
        ]

        for value in good_values:
            string = localizationkit.LocalizedString("Key", value, "Comment",
                                                     "en")
            collection = localizationkit.LocalizedCollection([string])
            test = localizationkit.tests.swift_interpolation.SwiftInterpolation(
                self.configuration, collection)
            result = test.execute()
            self.assertTrue(result.succeeded(), str(result.violations))

        for value in bad_values:
            string = localizationkit.LocalizedString("Key", value, "Comment",
                                                     "en")
            collection = localizationkit.LocalizedCollection([string])
            test = localizationkit.tests.swift_interpolation.SwiftInterpolation(
                self.configuration, collection)
            result = test.execute()
            self.assertFalse(result.succeeded())
 def test_run_tests(self):
     """Test run_tests."""
     strings = localizationkit.LocalizedString("Key", "Value", "Comment",
                                               "en")
     collection = localizationkit.LocalizedCollection([strings])
     results = localizationkit.run_tests(self.configuration, collection)
     self.assertGreater(len(results), 0)
    def test_duplicate_keys(self):
        """Test that comment similarity works"""
        good_checks = [
            [
                localizationkit.LocalizedString("Key", "Value", "Comment",
                                                "en"),
                localizationkit.LocalizedString("Key", "Value", "Comment",
                                                "fr"),
            ],
            [
                localizationkit.LocalizedString("Key", "Value", "Comment",
                                                "en"),
                localizationkit.LocalizedString("Keys", "Value", "Comment",
                                                "en"),
            ],
        ]

        bad_checks = [
            [
                localizationkit.LocalizedString("Key", "Value", "Comment",
                                                "en"),
                localizationkit.LocalizedString("Key", "Value", "Comment",
                                                "en"),
            ],
            [
                localizationkit.LocalizedString("", "Value", "Comment", "en"),
                localizationkit.LocalizedString("", "Value", "Comment", "en"),
            ],
        ]

        for strings in good_checks:
            collection = localizationkit.LocalizedCollection(strings)
            test = localizationkit.tests.duplicate_keys.DuplicateKeys(
                self.configuration, collection)
            result = test.execute()
            self.assertTrue(result.succeeded(), str(result.violations))

        for strings in bad_checks:
            collection = localizationkit.LocalizedCollection(strings)
            test = localizationkit.tests.duplicate_keys.DuplicateKeys(
                self.configuration, collection)
            result = test.execute()
            self.assertFalse(result.succeeded())
    def test_placeholder_token_explanation(self):
        """Test that all placeholder token explanations exists in comments"""
        test_cases = [
            (
                True,
                localizationkit.LocalizedString(
                    "Key", "This is a string with no tokens", "Some comment",
                    "en"),
            ),
            (
                True,
                localizationkit.LocalizedString(
                    "Key", "This is a string with one token: %@",
                    "Some comment %@ token explanation", "en"),
            ),
            (
                True,
                localizationkit.LocalizedString(
                    "Key", "This is a string with two tokens: %1$@ %2$@",
                    "Some comment %1$@ token explanantion %2$@ token explanantion",
                    "en"),
            ),
            (
                False,
                localizationkit.LocalizedString(
                    "Key",
                    "This is a string with two tokens: %1$@ %2$@",
                    "Some comment missing all token explanation",
                    "en",
                ),
            ),
            (
                False,
                localizationkit.LocalizedString(
                    "Key", "This is a string with two tokens: %1$@ %2$@",
                    "Some comment %@ token explanation missing some token explanantion",
                    "en"),
            ),
            (
                False,
                localizationkit.LocalizedString(
                    "Key", "This is a string",
                    "Some comment %@ extra token explanation", "en"),
            ),
        ]

        for expected_result, string in test_cases:
            collection = localizationkit.LocalizedCollection([string])
            test = localizationkit.tests.placeholder_token_explanation.PlaceholderTokenExplanation(
                self.configuration, collection)
            result = test.execute()
            self.assertEqual(expected_result, result.succeeded())
    def test_has_value(self):
        """Test that has value works"""
        bad_values = [None, ""]
        good_values = [" ", "1", "Two three four", "     "]

        for value in bad_values:
            string = localizationkit.LocalizedString("Key", value, "Comment",
                                                     "en")
            collection = localizationkit.LocalizedCollection([string])
            has_value_test = localizationkit.tests.has_value.HasValue(
                self.configuration, collection)
            result = has_value_test.execute()
            self.assertFalse(result.succeeded())

        for value in good_values:
            string = localizationkit.LocalizedString("Key", value, "Comment",
                                                     "en")
            collection = localizationkit.LocalizedCollection([string])
            has_value_test = localizationkit.tests.has_value.HasValue(
                self.configuration, collection)
            result = has_value_test.execute()
            self.assertTrue(result.succeeded(), str(result.violations))
Beispiel #8
0
    def test_comment_linebreaks(self):
        """Test that has comments works"""
        bad_comments = [
            "\n",
            "\r",
            "\r\n",
            "\n\r",
            "Hello\nWorld",
            "\nHello World",
            "Hello World\n",
        ]
        good_comments = [
            None,
            "",
            "Hello World",
            "This is a nice and long comment with lots of words that would " +
            "normally induce a linebreak for automatic wrapping, but " +
            "shouldn't here. This is a nice and long comment with lots of " +
            "words that would normally induce a linebreak for automatic " +
            "wrapping, but shouldn't here.",
        ]

        for comment in bad_comments:
            string = localizationkit.LocalizedString("Key", "Value", comment,
                                                     "en")
            collection = localizationkit.LocalizedCollection([string])
            comment_linebreak_test = localizationkit.tests.comment_linebreaks.CommentLinebreaks(
                self.configuration, collection)
            result = comment_linebreak_test.execute()
            self.assertFalse(result.succeeded())

        for comment in good_comments:
            string = localizationkit.LocalizedString("Key", "Value", comment,
                                                     "en")
            collection = localizationkit.LocalizedCollection([string])
            comment_linebreak_test = localizationkit.tests.comment_linebreaks.CommentLinebreaks(
                self.configuration, collection)
            result = comment_linebreak_test.execute()
            self.assertTrue(result.succeeded())
Beispiel #9
0
    def test_alternative_tokens(self):
        """Test that the Objective-C alternative token test works"""

        good_values = ["%@", "%1$@", "%%", "%%1"]

        bad_values = ["%1@", "%1@ %2@"]

        for value in good_values:
            string = localizationkit.LocalizedString("Key", value, "Comment",
                                                     "en")
            collection = localizationkit.LocalizedCollection([string])
            test = localizationkit.tests.objectivec_alternative_tokens.ObjectivecAlternativeTokens(
                self.configuration, collection)
            result = test.execute()
            self.assertTrue(result.succeeded(), str(result.violations))

        for value in bad_values:
            string = localizationkit.LocalizedString("Key", value, "Comment",
                                                     "en")
            collection = localizationkit.LocalizedCollection([string])
            test = localizationkit.tests.objectivec_alternative_tokens.ObjectivecAlternativeTokens(
                self.configuration, collection)
            result = test.execute()
            self.assertFalse(result.succeeded())
Beispiel #10
0
    def test_token_position_identifiers(self):
        """Test that token position identifiers works"""
        test_cases = [
            (
                True,
                localizationkit.LocalizedString(
                    "Key", "This is a string with no tokens", "Some comment",
                    "en"),
            ),
            (
                True,
                localizationkit.LocalizedString(
                    "Key", "This is a string with one token: %@",
                    "Some comment", "en"),
            ),
            (
                True,
                localizationkit.LocalizedString(
                    "Key", "This is a string with two tokens: %1$@ %2$@",
                    "Some comment", "en"),
            ),
            (
                False,
                localizationkit.LocalizedString(
                    "Key",
                    "This is a string with two non-positional tokens: %@ %@",
                    "Some comment",
                    "en",
                ),
            ),
            (
                False,
                localizationkit.LocalizedString(
                    "Key", "This is a string with a skipped index: %1$@ %3$@",
                    "Some comment", "en"),
            ),
        ]

        for (expected_result, string) in test_cases:
            collection = localizationkit.LocalizedCollection([string])
            test = localizationkit.tests.token_position_identifiers.TokenPositionIdentifiers(
                self.configuration, collection)
            result = test.execute()
            if expected_result:
                self.assertTrue(result.succeeded())
            else:
                self.assertFalse(result.succeeded())
    def test_token_matching(self):
        """Test that token matching works"""
        test_cases = [
            (
                True,
                [
                    localizationkit.LocalizedString(
                        "Key", "This is a string with no tokens",
                        "Some comment", "en"),
                    localizationkit.LocalizedString(
                        "Key", "This is a string with no tokens",
                        "Some comment", "fr"),
                ],
            ),
            (
                True,
                [
                    localizationkit.LocalizedString(
                        "Key", "This is a string with a token: %@",
                        "Some comment", "en"),
                    localizationkit.LocalizedString(
                        "Key", "This is a string with a token: %@",
                        "Some comment", "fr"),
                ],
            ),
            (
                True,
                [
                    localizationkit.LocalizedString(
                        "Key",
                        "This is a string with multiple tokens: %1$d, %2$s, %3$@",
                        "Some comment",
                        "en",
                    ),
                    localizationkit.LocalizedString(
                        "Key",
                        "This is a string with multiple tokens: %1$d, %2$s, %3$@",
                        "Some comment",
                        "fr",
                    ),
                ],
            ),
            (
                False,
                [
                    localizationkit.LocalizedString(
                        "Key",
                        "This is a string with multiple tokens: %1$d, %2$s, %3$@, %4$@",
                        "Some comment",
                        "en",
                    ),
                    localizationkit.LocalizedString(
                        "Key",
                        "This is a string with multiple tokens: %1$d, %2$s, %3$@",
                        "Some comment",
                        "fr",
                    ),
                ],
            ),
        ]

        for (expected_result, strings) in test_cases:
            collection = localizationkit.LocalizedCollection(strings)
            test = localizationkit.tests.token_matching.TokenMatching(
                self.configuration, collection)
            result = test.execute()
            if expected_result:
                self.assertTrue(result.succeeded())
            else:
                self.assertFalse(result.succeeded())