diff --git a/README.md b/README.md index 6cc08c9..d50fb6a 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,14 @@ Check that each token has a position specifier with it. e.g. `%s` is not allowed +## Placeholder token explanation + +Identifier: `placeholder_token_explanation` +Opt-In: `true` + +Checks that if a placeholder is used in a string, the comment explicitly explains what it is replaced with. +Precondition: Each placeholder in the string and its explanation in comment is expected to follow `token_position_identifiers` rule. + # Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a diff --git a/localizationkit/localization_types.py b/localizationkit/localization_types.py index f839114..58fa749 100644 --- a/localizationkit/localization_types.py +++ b/localizationkit/localization_types.py @@ -36,6 +36,13 @@ def tokens(self) -> List[str]: """ return LocalizedString._TOKEN_PATTERN.findall(self.value) + def comment_tokens(self) -> List[str]: + """Find and return the tokens in the comment string. + + :returns: The list of tokens in the comment string + """ + return LocalizedString._TOKEN_PATTERN.findall(self.comment) + def __str__(self) -> str: """Generate and return the string representation of the object. diff --git a/localizationkit/tests/__init__.py b/localizationkit/tests/__init__.py index 141f1cc..48616f0 100644 --- a/localizationkit/tests/__init__.py +++ b/localizationkit/tests/__init__.py @@ -8,6 +8,7 @@ import localizationkit.tests.invalid_tokens import localizationkit.tests.key_length import localizationkit.tests.objectivec_alternative_tokens +import localizationkit.tests.placeholder_token_explanation import localizationkit.tests.swift_interpolation import localizationkit.tests.token_matching -import localizationkit.tests.token_position_identifiers +import localizationkit.tests.token_position_identifiers \ No newline at end of file diff --git a/localizationkit/tests/placeholder_token_explanation.py b/localizationkit/tests/placeholder_token_explanation.py new file mode 100644 index 0000000..d46fe96 --- /dev/null +++ b/localizationkit/tests/placeholder_token_explanation.py @@ -0,0 +1,40 @@ +"""Placeholder token explanation.""" + +from typing import Any, Dict, List + +from localizationkit.tests.test_case import LocalizationTestCase + + +class PlaceholderTokenExplanation(LocalizationTestCase): + """Check the placeholder tokens in strings have explanation in comments.""" + + @classmethod + def name(cls) -> str: + return "placeholder_token_explanation" + + @classmethod + def default_settings(cls) -> Dict[str, Any]: + return {} + + @classmethod + def is_opt_in(cls) -> bool: + return True + + def run_test(self) -> List[str]: + + violations = [] + + for string in self.collection.localized_strings: + tokens = set(string.tokens()) + comment_tokens = set(string.comment_tokens()) + + extra_in_tokens = tokens - comment_tokens + extra_in_comments = comment_tokens - tokens + + if len(extra_in_tokens) != 0: + violations.append(f"Tokens are not described in the comment ({extra_in_tokens}): {string}") + + if len(extra_in_comments) != 0: + violations.append(f"Extra tokens appear in the comment ({extra_in_comments}): {string}") + + return violations diff --git a/tests/test_placeholder_token_explanation.py b/tests/test_placeholder_token_explanation.py new file mode 100644 index 0000000..1ab7e78 --- /dev/null +++ b/tests/test_placeholder_token_explanation.py @@ -0,0 +1,73 @@ +"""Placeholder token explanation tests.""" + +# pylint: disable=line-too-long + +import os +import sys +import unittest + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.abspath(__file__), "..", ".."))) +import localizationkit + + +class PlaceholderTokenExplanationTests(unittest.TestCase): + """Placeholder token explanation tests.""" + + def setUp(self): + current_file_path = os.path.abspath(__file__) + current_folder_path = os.path.dirname(current_file_path) + self.config_path = os.path.abspath(os.path.join(current_folder_path, "config.toml")) + self.configuration = localizationkit.Configuration.from_file(self.config_path) + + 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())