tools/tests: Use Python 3 syntax for typing.

This commit is contained in:
rht 2017-12-13 05:45:46 +00:00 committed by Tim Abbott
parent da65e1dcb6
commit 70a41cc2eb
6 changed files with 48 additions and 94 deletions

View File

@ -5,8 +5,7 @@ from tools.lib.capitalization import check_capitalization, is_capitalized, \
get_safe_text get_safe_text
class GetSafeTextTestCase(TestCase): class GetSafeTextTestCase(TestCase):
def test_get_safe_text(self): def test_get_safe_text(self) -> None:
# type: () -> None
string = ('Messages in __page_params.product_name__ go to a ' string = ('Messages in __page_params.product_name__ go to a '
'stream and have a topic.') 'stream and have a topic.')
safe_text = get_safe_text(string) safe_text = get_safe_text(string)
@ -71,8 +70,7 @@ class GetSafeTextTestCase(TestCase):
self.assertEqual(safe_text, 'One two etc_ three. four') self.assertEqual(safe_text, 'One two etc_ three. four')
class IsCapitalizedTestCase(TestCase): class IsCapitalizedTestCase(TestCase):
def test_process_text(self): def test_process_text(self) -> None:
# type: () -> None
string = "Zulip zulip. Zulip some text!" string = "Zulip zulip. Zulip some text!"
capitalized = is_capitalized(string) capitalized = is_capitalized(string)
self.assertTrue(capitalized) self.assertTrue(capitalized)
@ -123,8 +121,7 @@ class IsCapitalizedTestCase(TestCase):
self.assertTrue(capitalized) self.assertTrue(capitalized)
class CheckCapitalizationTestCase(TestCase): class CheckCapitalizationTestCase(TestCase):
def test_check_capitalization(self): def test_check_capitalization(self) -> None:
# type: () -> None
strings = ["Zulip Zulip. Zulip some text!", strings = ["Zulip Zulip. Zulip some text!",
"Zulip Zulip? Zulip some text!", "Zulip Zulip? Zulip some text!",
"Zulip Zulip! Zulip some text!", "Zulip Zulip! Zulip some text!",

View File

@ -15,8 +15,7 @@ except ImportError:
sys.exit(1) sys.exit(1)
class ParserTestHappyPath(unittest.TestCase): class ParserTestHappyPath(unittest.TestCase):
def test_basic_parse(self): def test_basic_parse(self) -> None:
# type: () -> None
my_selector = 'li.foo' my_selector = 'li.foo'
my_block = '''{ my_block = '''{
color: red; color: red;
@ -31,8 +30,7 @@ class ParserTestHappyPath(unittest.TestCase):
self.assertEqual(declaration.css_property, 'color') self.assertEqual(declaration.css_property, 'color')
self.assertEqual(declaration.css_value.text().strip(), 'red') self.assertEqual(declaration.css_value.text().strip(), 'red')
def test_same_line_comment(self): def test_same_line_comment(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
li.hide { li.hide {
display: none; /* comment here */ display: none; /* comment here */
@ -46,8 +44,7 @@ class ParserTestHappyPath(unittest.TestCase):
declaration = block.declarations[0] declaration = block.declarations[0]
self.assertIn('/* comment here */', declaration.text()) self.assertIn('/* comment here */', declaration.text())
def test_no_semicolon(self): def test_no_semicolon(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
p { color: red } p { color: red }
''' '''
@ -62,8 +59,7 @@ class ParserTestHappyPath(unittest.TestCase):
self.assertFalse(section.declaration_block.declarations[0].semicolon) self.assertFalse(section.declaration_block.declarations[0].semicolon)
def test_empty_block(self): def test_empty_block(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
div { div {
}''' }'''
@ -71,8 +67,7 @@ class ParserTestHappyPath(unittest.TestCase):
with self.assertRaisesRegex(CssParserException, error): with self.assertRaisesRegex(CssParserException, error):
parse(my_css) parse(my_css)
def test_multi_line_selector(self): def test_multi_line_selector(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
h1, h1,
h2, h2,
@ -84,8 +79,7 @@ class ParserTestHappyPath(unittest.TestCase):
selectors = section.selector_list.selectors selectors = section.selector_list.selectors
self.assertEqual(len(selectors), 3) self.assertEqual(len(selectors), 3)
def test_media_block(self): def test_media_block(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
@media (max-width: 300px) { @media (max-width: 300px) {
h5 { h5 {
@ -107,13 +101,11 @@ class ParserTestSadPath(unittest.TestCase):
of selectors. Some of this is just for expediency; of selectors. Some of this is just for expediency;
some of this is to enforce consistent formatting. some of this is to enforce consistent formatting.
''' '''
def _assert_error(self, my_css, error): def _assert_error(self, my_css: str, error: str) -> None:
# type: (str, str) -> None
with self.assertRaisesRegex(CssParserException, error): with self.assertRaisesRegex(CssParserException, error):
parse(my_css) parse(my_css)
def test_unexpected_end_brace(self): def test_unexpected_end_brace(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
@media (max-width: 975px) { @media (max-width: 975px) {
body { body {
@ -123,8 +115,7 @@ class ParserTestSadPath(unittest.TestCase):
error = 'unexpected }' error = 'unexpected }'
self._assert_error(my_css, error) self._assert_error(my_css, error)
def test_empty_section(self): def test_empty_section(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
/* nothing to see here, move along */ /* nothing to see here, move along */
@ -132,8 +123,7 @@ class ParserTestSadPath(unittest.TestCase):
error = 'unexpected empty section' error = 'unexpected empty section'
self._assert_error(my_css, error) self._assert_error(my_css, error)
def test_missing_colon(self): def test_missing_colon(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
.hide .hide
{ {
@ -142,14 +132,12 @@ class ParserTestSadPath(unittest.TestCase):
error = 'We expect a colon here' error = 'We expect a colon here'
self._assert_error(my_css, error) self._assert_error(my_css, error)
def test_unclosed_comment(self): def test_unclosed_comment(self) -> None:
# type: () -> None
my_css = ''' /* comment with no end''' my_css = ''' /* comment with no end'''
error = 'unclosed comment' error = 'unclosed comment'
self._assert_error(my_css, error) self._assert_error(my_css, error)
def test_missing_selectors(self): def test_missing_selectors(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
/* no selectors here */ /* no selectors here */
{ {
@ -158,8 +146,7 @@ class ParserTestSadPath(unittest.TestCase):
error = 'Missing selector' error = 'Missing selector'
self._assert_error(my_css, error) self._assert_error(my_css, error)
def test_missing_value(self): def test_missing_value(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
h1 h1
{ {
@ -168,8 +155,7 @@ class ParserTestSadPath(unittest.TestCase):
error = 'Missing value' error = 'Missing value'
self._assert_error(my_css, error) self._assert_error(my_css, error)
def test_disallow_comments_in_selectors(self): def test_disallow_comments_in_selectors(self) -> None:
# type: () -> None
my_css = ''' my_css = '''
h1, h1,
h2, /* comment here not allowed by Zulip */ h2, /* comment here not allowed by Zulip */

View File

@ -17,8 +17,7 @@ TEST_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "t
class TestHtmlBranches(unittest.TestCase): class TestHtmlBranches(unittest.TestCase):
def test_get_tag_info(self): def test_get_tag_info(self) -> None:
# type: () -> None
html = """ html = """
<p id="test" class="test1 test2">foo</p> <p id="test" class="test1 test2">foo</p>
""" """
@ -31,8 +30,7 @@ class TestHtmlBranches(unittest.TestCase):
self.assertEqual(start_tag_info.text(), 'p.test1.test2#test') self.assertEqual(start_tag_info.text(), 'p.test1.test2#test')
self.assertEqual(end_tag_info.text(), 'p') self.assertEqual(end_tag_info.text(), 'p')
def test_html_tag_tree(self): def test_html_tag_tree(self) -> None:
# type: () -> None
html = """ html = """
<!-- test --> <!-- test -->
<!DOCTYPE html> <!DOCTYPE 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.kind, 'html_start')
self.assertEqual(tree.children[0].children[1].children[1].token.tag, 'p') self.assertEqual(tree.children[0].children[1].children[1].token.tag, 'p')
def test_html_branches(self): def test_html_branches(self) -> None:
# type: () -> None
html = """ html = """
<!-- test --> <!-- test -->
<!DOCTYPE html> <!DOCTYPE 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[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') self.assertEqual(branches[2].staircase_text(), '\n html\n body\n p\n')
def test_build_id_dict(self): def test_build_id_dict(self) -> None:
# type: () -> None
templates = ["test_template1.html", "test_template2.html"] templates = ["test_template1.html", "test_template2.html"]
templates = [os.path.join(TEST_TEMPLATES_DIR, fn) for fn in templates] 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'], [ self.assertEqual(template_id_dict['below_navbar'], [
'Line 10:%s/tools/tests/test_template_data/test_template2.html' % (ZULIP_PATH)]) 'Line 10:%s/tools/tests/test_template_data/test_template2.html' % (ZULIP_PATH)])
def test_split_for_id_and_class(self): def test_split_for_id_and_class(self) -> None:
# type: () -> None
id1 = "{{ red|blue }}" id1 = "{{ red|blue }}"
id2 = "search_box_{{ page }}" id2 = "search_box_{{ page }}"

View File

@ -13,8 +13,7 @@ CHECK_MESSAGE = "Fix the corresponding rule in `tools/linter_lib/custom_check.py
class TestCustomRules(TestCase): class TestCustomRules(TestCase):
def setUp(self): def setUp(self) -> None:
# type: () -> None
self.all_rules = [] # type: List[Dict[str, Any]] 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: 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', by_lang = dict.fromkeys(['py', 'js', 'sh', 'css', 'handlebars', 'html',
@ -27,8 +26,7 @@ class TestCustomRules(TestCase):
rule_set = call_args[0][2] rule_set = call_args[0][2]
self.all_rules.extend(rule_set) self.all_rules.extend(rule_set)
def test_paths_in_rules(self): def test_paths_in_rules(self) -> None:
# type: () -> None
"""Verifies that the paths mentioned in linter rules actually exist""" """Verifies that the paths mentioned in linter rules actually exist"""
for rule in self.all_rules: for rule in self.all_rules:
for path in rule.get('exclude', {}): for path in rule.get('exclude', {}):
@ -47,8 +45,7 @@ class TestCustomRules(TestCase):
self.assertTrue(path.endswith('/'), self.assertTrue(path.endswith('/'),
"The path '{}' should end with '/'. {}".format(path, CHECK_MESSAGE)) "The path '{}' should end with '/'. {}".format(path, CHECK_MESSAGE))
def test_rule_patterns(self): def test_rule_patterns(self) -> None:
# type: () -> None
"""Verifies that the search regex specified in a custom rule actually matches """Verifies that the search regex specified in a custom rule actually matches
the expectation and doesn't throw false positives.""" the expectation and doesn't throw false positives."""
for rule in self.all_rules: for rule in self.all_rules:

View File

@ -390,12 +390,10 @@ GOOD_HTML14 = """
</div> </div>
""" """
class TestPrettyPrinter(unittest.TestCase): class TestPrettyPrinter(unittest.TestCase):
def compare(self, a, b): def compare(self, a: str, b: str) -> None:
# type: (str, str) -> None
self.assertEqual(a.split('\n'), b.split('\n')) self.assertEqual(a.split('\n'), b.split('\n'))
def test_pretty_print(self): def test_pretty_print(self) -> None:
# type: () -> None
self.compare(pretty_print_html(GOOD_HTML), GOOD_HTML) self.compare(pretty_print_html(GOOD_HTML), GOOD_HTML)
self.compare(pretty_print_html(BAD_HTML), GOOD_HTML) self.compare(pretty_print_html(BAD_HTML), GOOD_HTML)
self.compare(pretty_print_html(BAD_HTML1), GOOD_HTML1) self.compare(pretty_print_html(BAD_HTML1), GOOD_HTML1)

View File

@ -15,18 +15,16 @@ except ImportError:
sys.exit(1) sys.exit(1)
class ParserTest(unittest.TestCase): class ParserTest(unittest.TestCase):
def _assert_validate_error(self, error, fn=None, text=None, check_indent=True): def _assert_validate_error(self, error: str, fn: Optional[str]=None,
# type: (str, Optional[str], Optional[str], bool) -> None text: Optional[str]=None, check_indent: bool=True) -> None:
with self.assertRaisesRegex(TemplateParserException, error): with self.assertRaisesRegex(TemplateParserException, error):
validate(fn=fn, text=text, check_indent=check_indent) validate(fn=fn, text=text, check_indent=check_indent)
def test_is_django_block_tag(self): def test_is_django_block_tag(self) -> None:
# type: () -> None
self.assertTrue(is_django_block_tag('block')) self.assertTrue(is_django_block_tag('block'))
self.assertFalse(is_django_block_tag('not a django tag')) self.assertFalse(is_django_block_tag('not a django tag'))
def test_validate_vanilla_html(self): def test_validate_vanilla_html(self) -> None:
# type: () -> None
''' '''
Verify that validate() does not raise errors for Verify that validate() does not raise errors for
well-formed HTML. well-formed HTML.
@ -39,8 +37,7 @@ class ParserTest(unittest.TestCase):
</table>''' </table>'''
validate(text=my_html) validate(text=my_html)
def test_validate_handlebars(self): def test_validate_handlebars(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
{{#with stream}} {{#with stream}}
<p>{{stream}}</p> <p>{{stream}}</p>
@ -48,16 +45,14 @@ class ParserTest(unittest.TestCase):
''' '''
validate(text=my_html) validate(text=my_html)
def test_validate_comment(self): def test_validate_comment(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
<!--- <!---
<h1>foo</h1> <h1>foo</h1>
-->''' -->'''
validate(text=my_html) validate(text=my_html)
def test_validate_django(self): def test_validate_django(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
{% include "some_other.html" %} {% include "some_other.html" %}
{% if foo %} {% if foo %}
@ -75,22 +70,19 @@ class ParserTest(unittest.TestCase):
''' '''
validate(text=my_html) validate(text=my_html)
def test_validate_no_start_tag(self): def test_validate_no_start_tag(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
foo</p> foo</p>
''' '''
self._assert_validate_error('No start tag', text=my_html) self._assert_validate_error('No start tag', text=my_html)
def test_validate_mismatched_tag(self): def test_validate_mismatched_tag(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
<b>foo</i> <b>foo</i>
''' '''
self._assert_validate_error('Mismatched tag.', text=my_html) self._assert_validate_error('Mismatched tag.', text=my_html)
def test_validate_bad_indentation(self): def test_validate_bad_indentation(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
<p> <p>
foo foo
@ -98,53 +90,46 @@ class ParserTest(unittest.TestCase):
''' '''
self._assert_validate_error('Bad indentation.', text=my_html, check_indent=True) self._assert_validate_error('Bad indentation.', text=my_html, check_indent=True)
def test_validate_state_depth(self): def test_validate_state_depth(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
<b> <b>
''' '''
self._assert_validate_error('Missing end tag', text=my_html) self._assert_validate_error('Missing end tag', text=my_html)
def test_validate_incomplete_handlebars_tag_1(self): def test_validate_incomplete_handlebars_tag_1(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
{{# foo {{# foo
''' '''
self._assert_validate_error('''Tag missing "}}" at Line 2 Col 13:"{{# foo self._assert_validate_error('''Tag missing "}}" at Line 2 Col 13:"{{# foo
"''', text=my_html) "''', text=my_html)
def test_validate_incomplete_handlebars_tag_2(self): def test_validate_incomplete_handlebars_tag_2(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
{{# foo } {{# foo }
''' '''
self._assert_validate_error('Tag missing "}}" at Line 2 Col 13:"{{# foo }\n"', text=my_html) self._assert_validate_error('Tag missing "}}" at Line 2 Col 13:"{{# foo }\n"', text=my_html)
def test_validate_incomplete_django_tag_1(self): def test_validate_incomplete_django_tag_1(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
{% foo {% foo
''' '''
self._assert_validate_error('''Tag missing "%}" at Line 2 Col 13:"{% foo self._assert_validate_error('''Tag missing "%}" at Line 2 Col 13:"{% foo
"''', text=my_html) "''', text=my_html)
def test_validate_incomplete_django_tag_2(self): def test_validate_incomplete_django_tag_2(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
{% foo % {% foo %
''' '''
self._assert_validate_error('Tag missing "%}" at Line 2 Col 13:"{% foo %\n"', text=my_html) self._assert_validate_error('Tag missing "%}" at Line 2 Col 13:"{% foo %\n"', text=my_html)
def test_validate_incomplete_html_tag_1(self): def test_validate_incomplete_html_tag_1(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
<b <b
''' '''
self._assert_validate_error('''Tag missing ">" at Line 2 Col 13:"<b self._assert_validate_error('''Tag missing ">" at Line 2 Col 13:"<b
"''', text=my_html) "''', text=my_html)
def test_validate_incomplete_html_tag_2(self): def test_validate_incomplete_html_tag_2(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
<a href=" <a href="
''' '''
@ -156,15 +141,13 @@ class ParserTest(unittest.TestCase):
self._assert_validate_error('''Unbalanced Quotes at Line 2 Col 13:"<a href=" self._assert_validate_error('''Unbalanced Quotes at Line 2 Col 13:"<a href="
"''', text=my_html) "''', text=my_html)
def test_validate_empty_html_tag(self): def test_validate_empty_html_tag(self) -> None:
# type: () -> None
my_html = ''' my_html = '''
< > < >
''' '''
self._assert_validate_error('Tag name missing', text=my_html) self._assert_validate_error('Tag name missing', text=my_html)
def test_code_blocks(self): def test_code_blocks(self) -> None:
# type: () -> None
# This is fine. # This is fine.
my_html = ''' my_html = '''
@ -185,8 +168,7 @@ class ParserTest(unittest.TestCase):
''' '''
self._assert_validate_error('Code tag is split across two lines.', text=my_html) self._assert_validate_error('Code tag is split across two lines.', text=my_html)
def test_anchor_blocks(self): def test_anchor_blocks(self) -> None:
# type: () -> None
# This is allowed, although strange. # This is allowed, although strange.
my_html = ''' my_html = '''
@ -209,8 +191,7 @@ class ParserTest(unittest.TestCase):
''' '''
validate(text=my_html) validate(text=my_html)
def test_tokenize(self): def test_tokenize(self) -> None:
# type: () -> None
tag = '<meta whatever>bla' tag = '<meta whatever>bla'
token = tokenize(tag)[0] token = tokenize(tag)[0]
self.assertEqual(token.kind, 'html_special') self.assertEqual(token.kind, 'html_special')