2017-03-10 05:20:03 +01:00
|
|
|
from unittest import TestCase
|
2017-03-03 12:42:07 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
from tools.lib.capitalization import check_capitalization, get_safe_text, is_capitalized
|
|
|
|
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
class GetSafeTextTestCase(TestCase):
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_get_safe_text(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
string = "Messages in __page_params.product_name__ go to a stream and have a topic."
|
2017-03-03 12:42:07 +01:00
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
safe_text, "Messages in __page_params_product_name__ go to a stream and have a topic."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = "Zulip Zulip. Zulip some text!"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "Zulip zulip. Zulip some text!")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = "Zulip Zulip? Zulip some text!"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "Zulip zulip? Zulip some text!")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = "Zulip Zulip! Zulip some text!"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "Zulip zulip! Zulip some text!")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = "Zulip Zulip, Zulip some text!"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "Zulip zulip, zulip some text!")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = "Some text 25MiB"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "Some text 25mib")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = "Not Ignored Phrase"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "Not Ignored Phrase")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = "Not ignored phrase"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "Not ignored phrase")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = ""
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = """
|
|
|
|
<p>Please re-enter your password to confirm your identity.
|
|
|
|
(<a href="/accounts/password/reset/" target="_blank">Forgotten it?</a>)</p>
|
|
|
|
"""
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
soup = BeautifulSoup(safe_text, "lxml")
|
|
|
|
rendered_text = " ".join(soup.text.split())
|
2017-03-03 12:42:07 +01:00
|
|
|
self.assertEqual(safe_text, rendered_text)
|
|
|
|
|
|
|
|
string = "Edited (__last_edit_timestr__)"
|
|
|
|
safe_text = get_safe_text(string)
|
|
|
|
self.assertEqual(safe_text, string)
|
|
|
|
|
|
|
|
string = "iPhone application"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "Iphone application")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = "One two etc. three"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "One two etc_ three")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
string = "One two etc. three. four"
|
|
|
|
safe_text = get_safe_text(string)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(safe_text, "One two etc_ three. four")
|
2017-03-03 12:42:07 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-03-03 12:42:07 +01:00
|
|
|
class IsCapitalizedTestCase(TestCase):
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_process_text(self) -> None:
|
2017-03-03 12:42:07 +01:00
|
|
|
string = "Zulip zulip. Zulip some text!"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
|
|
|
string = "Zulip zulip? Zulip some text!"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
|
|
|
string = "Zulip zulip! Zulip some text!"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
|
|
|
string = "Zulip zulip, Zulip some text!"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
|
|
|
string = "Some number 25mib"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
|
|
|
string = "Not Ignored Phrase"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertFalse(capitalized)
|
|
|
|
|
|
|
|
string = "Not ignored phrase"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
|
|
|
string = ""
|
|
|
|
capitalized = is_capitalized(string)
|
2020-09-02 02:50:08 +02:00
|
|
|
self.assertTrue(capitalized)
|
2017-03-03 12:42:07 +01:00
|
|
|
|
2020-09-02 02:50:08 +02:00
|
|
|
string = "Please re-enter your password to confirm your identity. (Forgotten it?)"
|
2017-03-03 12:42:07 +01:00
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
|
|
|
string = "Edited (__last_edit_timestr__)"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
|
|
|
string = "Iphone application"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
|
|
|
string = "One two etc_ three"
|
|
|
|
capitalized = is_capitalized(string)
|
|
|
|
self.assertTrue(capitalized)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-03-03 12:42:07 +01:00
|
|
|
class CheckCapitalizationTestCase(TestCase):
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_check_capitalization(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
strings = [
|
|
|
|
"Zulip Zulip. Zulip some text!",
|
|
|
|
"Zulip Zulip? Zulip some text!",
|
|
|
|
"Zulip Zulip! Zulip some text!",
|
|
|
|
"Zulip Zulip, Zulip some text!",
|
|
|
|
"Some number 25MiB",
|
|
|
|
"Not Ignored Phrase",
|
|
|
|
"Not ignored phrase",
|
|
|
|
"Some text with realm in it",
|
|
|
|
"Realm in capital case",
|
|
|
|
(
|
|
|
|
'<p class="bot-settings-note padded-container"> Looking for our '
|
|
|
|
'<a href="/integrations" target="_blank">Integrations</a> or '
|
|
|
|
'<a href="/api" target="_blank">API</a> '
|
2021-02-12 08:20:45 +01:00
|
|
|
"documentation? </p>"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
]
|
2018-03-11 06:06:54 +01:00
|
|
|
errored, ignored, banned = check_capitalization(strings)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(errored, ["Not Ignored Phrase"])
|
2017-03-10 11:47:06 +01:00
|
|
|
self.assertEqual(
|
|
|
|
ignored,
|
2021-02-12 08:19:30 +01:00
|
|
|
sorted(
|
|
|
|
[
|
|
|
|
"Zulip Zulip. Zulip some text!",
|
2017-03-10 11:47:06 +01:00
|
|
|
"Zulip Zulip? Zulip some text!",
|
|
|
|
"Zulip Zulip! Zulip some text!",
|
|
|
|
"Zulip Zulip, Zulip some text!",
|
|
|
|
"Some number 25MiB",
|
2021-02-12 08:19:30 +01:00
|
|
|
(
|
|
|
|
'<p class="bot-settings-note padded-container"> Looking '
|
|
|
|
'for our <a href="/integrations" target="_blank">'
|
|
|
|
'Integrations</a> or <a href="/api" '
|
|
|
|
'target="_blank">API</a> documentation? </p>'
|
|
|
|
),
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
banned,
|
|
|
|
sorted(
|
|
|
|
[
|
|
|
|
"realm found in 'Some text with realm in it'. "
|
|
|
|
"The term realm should not appear in user-facing "
|
|
|
|
"strings. Use organization instead.",
|
|
|
|
"realm found in 'Realm in capital case'. "
|
|
|
|
"The term realm should not appear in user-facing "
|
|
|
|
"strings. Use organization instead.",
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|