From 70a41cc2ebcfec9489631e151876704bb488a9fc Mon Sep 17 00:00:00 2001
From: rht
Date: Wed, 13 Dec 2017 05:45:46 +0000
Subject: [PATCH] tools/tests: Use Python 3 syntax for typing.
---
tools/tests/test_capitalization_checker.py | 9 ++--
tools/tests/test_css_parser.py | 42 +++++----------
tools/tests/test_html_branches.py | 15 ++----
tools/tests/test_linter_custom_check.py | 9 ++--
tools/tests/test_pretty_print.py | 6 +--
tools/tests/test_template_parser.py | 61 ++++++++--------------
6 files changed, 48 insertions(+), 94 deletions(-)
diff --git a/tools/tests/test_capitalization_checker.py b/tools/tests/test_capitalization_checker.py
index 792d27f779..09986e8ae7 100644
--- a/tools/tests/test_capitalization_checker.py
+++ b/tools/tests/test_capitalization_checker.py
@@ -5,8 +5,7 @@ from tools.lib.capitalization import check_capitalization, is_capitalized, \
get_safe_text
class GetSafeTextTestCase(TestCase):
- def test_get_safe_text(self):
- # type: () -> None
+ def test_get_safe_text(self) -> None:
string = ('Messages in __page_params.product_name__ go to a '
'stream and have a topic.')
safe_text = get_safe_text(string)
@@ -71,8 +70,7 @@ class GetSafeTextTestCase(TestCase):
self.assertEqual(safe_text, 'One two etc_ three. four')
class IsCapitalizedTestCase(TestCase):
- def test_process_text(self):
- # type: () -> None
+ def test_process_text(self) -> None:
string = "Zulip zulip. Zulip some text!"
capitalized = is_capitalized(string)
self.assertTrue(capitalized)
@@ -123,8 +121,7 @@ class IsCapitalizedTestCase(TestCase):
self.assertTrue(capitalized)
class CheckCapitalizationTestCase(TestCase):
- def test_check_capitalization(self):
- # type: () -> None
+ def test_check_capitalization(self) -> None:
strings = ["Zulip Zulip. Zulip some text!",
"Zulip Zulip? Zulip some text!",
"Zulip Zulip! Zulip some text!",
diff --git a/tools/tests/test_css_parser.py b/tools/tests/test_css_parser.py
index 78b155625b..bc1e633e01 100644
--- a/tools/tests/test_css_parser.py
+++ b/tools/tests/test_css_parser.py
@@ -15,8 +15,7 @@ except ImportError:
sys.exit(1)
class ParserTestHappyPath(unittest.TestCase):
- def test_basic_parse(self):
- # type: () -> None
+ def test_basic_parse(self) -> None:
my_selector = 'li.foo'
my_block = '''{
color: red;
@@ -31,8 +30,7 @@ class ParserTestHappyPath(unittest.TestCase):
self.assertEqual(declaration.css_property, 'color')
self.assertEqual(declaration.css_value.text().strip(), 'red')
- def test_same_line_comment(self):
- # type: () -> None
+ def test_same_line_comment(self) -> None:
my_css = '''
li.hide {
display: none; /* comment here */
@@ -46,8 +44,7 @@ class ParserTestHappyPath(unittest.TestCase):
declaration = block.declarations[0]
self.assertIn('/* comment here */', declaration.text())
- def test_no_semicolon(self):
- # type: () -> None
+ def test_no_semicolon(self) -> None:
my_css = '''
p { color: red }
'''
@@ -62,8 +59,7 @@ class ParserTestHappyPath(unittest.TestCase):
self.assertFalse(section.declaration_block.declarations[0].semicolon)
- def test_empty_block(self):
- # type: () -> None
+ def test_empty_block(self) -> None:
my_css = '''
div {
}'''
@@ -71,8 +67,7 @@ class ParserTestHappyPath(unittest.TestCase):
with self.assertRaisesRegex(CssParserException, error):
parse(my_css)
- def test_multi_line_selector(self):
- # type: () -> None
+ def test_multi_line_selector(self) -> None:
my_css = '''
h1,
h2,
@@ -84,8 +79,7 @@ class ParserTestHappyPath(unittest.TestCase):
selectors = section.selector_list.selectors
self.assertEqual(len(selectors), 3)
- def test_media_block(self):
- # type: () -> None
+ def test_media_block(self) -> None:
my_css = '''
@media (max-width: 300px) {
h5 {
@@ -107,13 +101,11 @@ class ParserTestSadPath(unittest.TestCase):
of selectors. Some of this is just for expediency;
some of this is to enforce consistent formatting.
'''
- def _assert_error(self, my_css, error):
- # type: (str, str) -> None
+ def _assert_error(self, my_css: str, error: str) -> None:
with self.assertRaisesRegex(CssParserException, error):
parse(my_css)
- def test_unexpected_end_brace(self):
- # type: () -> None
+ def test_unexpected_end_brace(self) -> None:
my_css = '''
@media (max-width: 975px) {
body {
@@ -123,8 +115,7 @@ class ParserTestSadPath(unittest.TestCase):
error = 'unexpected }'
self._assert_error(my_css, error)
- def test_empty_section(self):
- # type: () -> None
+ def test_empty_section(self) -> None:
my_css = '''
/* nothing to see here, move along */
@@ -132,8 +123,7 @@ class ParserTestSadPath(unittest.TestCase):
error = 'unexpected empty section'
self._assert_error(my_css, error)
- def test_missing_colon(self):
- # type: () -> None
+ def test_missing_colon(self) -> None:
my_css = '''
.hide
{
@@ -142,14 +132,12 @@ class ParserTestSadPath(unittest.TestCase):
error = 'We expect a colon here'
self._assert_error(my_css, error)
- def test_unclosed_comment(self):
- # type: () -> None
+ def test_unclosed_comment(self) -> None:
my_css = ''' /* comment with no end'''
error = 'unclosed comment'
self._assert_error(my_css, error)
- def test_missing_selectors(self):
- # type: () -> None
+ def test_missing_selectors(self) -> None:
my_css = '''
/* no selectors here */
{
@@ -158,8 +146,7 @@ class ParserTestSadPath(unittest.TestCase):
error = 'Missing selector'
self._assert_error(my_css, error)
- def test_missing_value(self):
- # type: () -> None
+ def test_missing_value(self) -> None:
my_css = '''
h1
{
@@ -168,8 +155,7 @@ class ParserTestSadPath(unittest.TestCase):
error = 'Missing value'
self._assert_error(my_css, error)
- def test_disallow_comments_in_selectors(self):
- # type: () -> None
+ def test_disallow_comments_in_selectors(self) -> None:
my_css = '''
h1,
h2, /* comment here not allowed by Zulip */
diff --git a/tools/tests/test_html_branches.py b/tools/tests/test_html_branches.py
index 069072de58..d253030d45 100644
--- a/tools/tests/test_html_branches.py
+++ b/tools/tests/test_html_branches.py
@@ -17,8 +17,7 @@ TEST_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "t
class TestHtmlBranches(unittest.TestCase):
- def test_get_tag_info(self):
- # type: () -> None
+ def test_get_tag_info(self) -> None:
html = """
foo
"""
@@ -31,8 +30,7 @@ class TestHtmlBranches(unittest.TestCase):
self.assertEqual(start_tag_info.text(), 'p.test1.test2#test')
self.assertEqual(end_tag_info.text(), 'p')
- def test_html_tag_tree(self):
- # type: () -> None
+ def test_html_tag_tree(self) -> None:
html = """
@@ -74,8 +72,7 @@ class TestHtmlBranches(unittest.TestCase):
self.assertEqual(tree.children[0].children[1].children[1].token.kind, 'html_start')
self.assertEqual(tree.children[0].children[1].children[1].token.tag, 'p')
- def test_html_branches(self):
- # type: () -> None
+ def test_html_branches(self) -> None:
html = """
@@ -104,8 +101,7 @@ class TestHtmlBranches(unittest.TestCase):
self.assertEqual(branches[1].staircase_text(), '\n html\n body\n p\n br\n')
self.assertEqual(branches[2].staircase_text(), '\n html\n body\n p\n')
- def test_build_id_dict(self):
- # type: () -> None
+ def test_build_id_dict(self) -> None:
templates = ["test_template1.html", "test_template2.html"]
templates = [os.path.join(TEST_TEMPLATES_DIR, fn) for fn in templates]
@@ -122,8 +118,7 @@ class TestHtmlBranches(unittest.TestCase):
self.assertEqual(template_id_dict['below_navbar'], [
'Line 10:%s/tools/tests/test_template_data/test_template2.html' % (ZULIP_PATH)])
- def test_split_for_id_and_class(self):
- # type: () -> None
+ def test_split_for_id_and_class(self) -> None:
id1 = "{{ red|blue }}"
id2 = "search_box_{{ page }}"
diff --git a/tools/tests/test_linter_custom_check.py b/tools/tests/test_linter_custom_check.py
index 8486a1bf05..b99ade37bb 100644
--- a/tools/tests/test_linter_custom_check.py
+++ b/tools/tests/test_linter_custom_check.py
@@ -13,8 +13,7 @@ CHECK_MESSAGE = "Fix the corresponding rule in `tools/linter_lib/custom_check.py
class TestCustomRules(TestCase):
- def setUp(self):
- # type: () -> None
+ def setUp(self) -> None:
self.all_rules = [] # type: List[Dict[str, Any]]
with patch('tools.linter_lib.custom_check.custom_check_file', return_value=False) as mock_custom_check_file:
by_lang = dict.fromkeys(['py', 'js', 'sh', 'css', 'handlebars', 'html',
@@ -27,8 +26,7 @@ class TestCustomRules(TestCase):
rule_set = call_args[0][2]
self.all_rules.extend(rule_set)
- def test_paths_in_rules(self):
- # type: () -> None
+ def test_paths_in_rules(self) -> None:
"""Verifies that the paths mentioned in linter rules actually exist"""
for rule in self.all_rules:
for path in rule.get('exclude', {}):
@@ -47,8 +45,7 @@ class TestCustomRules(TestCase):
self.assertTrue(path.endswith('/'),
"The path '{}' should end with '/'. {}".format(path, CHECK_MESSAGE))
- def test_rule_patterns(self):
- # type: () -> None
+ def test_rule_patterns(self) -> None:
"""Verifies that the search regex specified in a custom rule actually matches
the expectation and doesn't throw false positives."""
for rule in self.all_rules:
diff --git a/tools/tests/test_pretty_print.py b/tools/tests/test_pretty_print.py
index e69ef4dac1..08a29ef305 100644
--- a/tools/tests/test_pretty_print.py
+++ b/tools/tests/test_pretty_print.py
@@ -390,12 +390,10 @@ GOOD_HTML14 = """
"""
class TestPrettyPrinter(unittest.TestCase):
- def compare(self, a, b):
- # type: (str, str) -> None
+ def compare(self, a: str, b: str) -> None:
self.assertEqual(a.split('\n'), b.split('\n'))
- def test_pretty_print(self):
- # type: () -> None
+ def test_pretty_print(self) -> None:
self.compare(pretty_print_html(GOOD_HTML), GOOD_HTML)
self.compare(pretty_print_html(BAD_HTML), GOOD_HTML)
self.compare(pretty_print_html(BAD_HTML1), GOOD_HTML1)
diff --git a/tools/tests/test_template_parser.py b/tools/tests/test_template_parser.py
index 6a20bcd50c..c464fc5071 100644
--- a/tools/tests/test_template_parser.py
+++ b/tools/tests/test_template_parser.py
@@ -15,18 +15,16 @@ except ImportError:
sys.exit(1)
class ParserTest(unittest.TestCase):
- def _assert_validate_error(self, error, fn=None, text=None, check_indent=True):
- # type: (str, Optional[str], Optional[str], bool) -> None
+ def _assert_validate_error(self, error: str, fn: Optional[str]=None,
+ text: Optional[str]=None, check_indent: bool=True) -> None:
with self.assertRaisesRegex(TemplateParserException, error):
validate(fn=fn, text=text, check_indent=check_indent)
- def test_is_django_block_tag(self):
- # type: () -> None
+ def test_is_django_block_tag(self) -> None:
self.assertTrue(is_django_block_tag('block'))
self.assertFalse(is_django_block_tag('not a django tag'))
- def test_validate_vanilla_html(self):
- # type: () -> None
+ def test_validate_vanilla_html(self) -> None:
'''
Verify that validate() does not raise errors for
well-formed HTML.
@@ -39,8 +37,7 @@ class ParserTest(unittest.TestCase):
'''
validate(text=my_html)
- def test_validate_handlebars(self):
- # type: () -> None
+ def test_validate_handlebars(self) -> None:
my_html = '''
{{#with stream}}
{{stream}}
@@ -48,16 +45,14 @@ class ParserTest(unittest.TestCase):
'''
validate(text=my_html)
- def test_validate_comment(self):
- # type: () -> None
+ def test_validate_comment(self) -> None:
my_html = '''
'''
validate(text=my_html)
- def test_validate_django(self):
- # type: () -> None
+ def test_validate_django(self) -> None:
my_html = '''
{% include "some_other.html" %}
{% if foo %}
@@ -75,22 +70,19 @@ class ParserTest(unittest.TestCase):
'''
validate(text=my_html)
- def test_validate_no_start_tag(self):
- # type: () -> None
+ def test_validate_no_start_tag(self) -> None:
my_html = '''
foo
'''
self._assert_validate_error('No start tag', text=my_html)
- def test_validate_mismatched_tag(self):
- # type: () -> None
+ def test_validate_mismatched_tag(self) -> None:
my_html = '''
foo
'''
self._assert_validate_error('Mismatched tag.', text=my_html)
- def test_validate_bad_indentation(self):
- # type: () -> None
+ def test_validate_bad_indentation(self) -> None:
my_html = '''
foo
@@ -98,53 +90,46 @@ class ParserTest(unittest.TestCase):
'''
self._assert_validate_error('Bad indentation.', text=my_html, check_indent=True)
- def test_validate_state_depth(self):
- # type: () -> None
+ def test_validate_state_depth(self) -> None:
my_html = '''
'''
self._assert_validate_error('Missing end tag', text=my_html)
- def test_validate_incomplete_handlebars_tag_1(self):
- # type: () -> None
+ def test_validate_incomplete_handlebars_tag_1(self) -> None:
my_html = '''
{{# foo
'''
self._assert_validate_error('''Tag missing "}}" at Line 2 Col 13:"{{# foo
"''', text=my_html)
- def test_validate_incomplete_handlebars_tag_2(self):
- # type: () -> None
+ def test_validate_incomplete_handlebars_tag_2(self) -> None:
my_html = '''
{{# foo }
'''
self._assert_validate_error('Tag missing "}}" at Line 2 Col 13:"{{# foo }\n"', text=my_html)
- def test_validate_incomplete_django_tag_1(self):
- # type: () -> None
+ def test_validate_incomplete_django_tag_1(self) -> None:
my_html = '''
{% foo
'''
self._assert_validate_error('''Tag missing "%}" at Line 2 Col 13:"{% foo
"''', text=my_html)
- def test_validate_incomplete_django_tag_2(self):
- # type: () -> None
+ def test_validate_incomplete_django_tag_2(self) -> None:
my_html = '''
{% foo %
'''
self._assert_validate_error('Tag missing "%}" at Line 2 Col 13:"{% foo %\n"', text=my_html)
- def test_validate_incomplete_html_tag_1(self):
- # type: () -> None
+ def test_validate_incomplete_html_tag_1(self) -> None:
my_html = '''
" at Line 2 Col 13:" None
+ def test_validate_incomplete_html_tag_2(self) -> None:
my_html = '''
None
+ def test_validate_empty_html_tag(self) -> None:
my_html = '''
< >
'''
self._assert_validate_error('Tag name missing', text=my_html)
- def test_code_blocks(self):
- # type: () -> None
+ def test_code_blocks(self) -> None:
# This is fine.
my_html = '''
@@ -185,8 +168,7 @@ class ParserTest(unittest.TestCase):
'''
self._assert_validate_error('Code tag is split across two lines.', text=my_html)
- def test_anchor_blocks(self):
- # type: () -> None
+ def test_anchor_blocks(self) -> None:
# This is allowed, although strange.
my_html = '''
@@ -209,8 +191,7 @@ class ParserTest(unittest.TestCase):
'''
validate(text=my_html)
- def test_tokenize(self):
- # type: () -> None
+ def test_tokenize(self) -> None:
tag = 'bla'
token = tokenize(tag)[0]
self.assertEqual(token.kind, 'html_special')