import unittest from tools.lib.pretty_print import pretty_print_html # Note that GOOD_HTML isn't necessarily beautiful HTML. Apart # from adjusting indentation, we mostly leave things alone to # respect whatever line-wrapping styles were in place before. BAD_HTML = """ Test

Hello
world!

Goodbyeworld!

5
            print 'hello world'
    
{{ bla }}
""" GOOD_HTML = """ Test

Hello
world!

Goodbyeworld!

5
            print 'hello world'
    
{{ bla }}
""" BAD_HTML1 = """ foobarfoobarfoobar """ GOOD_HTML1 = """ foobarfoobarfoobar """ BAD_HTML2 = """ {{# foobar area}} foobarfoobarfoobar {{/ foobar area}} """ GOOD_HTML2 = """ {{# foobar area}} foobarfoobarfoobar {{/ foobar area}} """ # The old GOOD_HTML3 test was flawed. BAD_HTML4 = """
foo

hello

bar
""" GOOD_HTML4 = """
foo

hello

bar
""" BAD_HTML5 = """
foo {{#if foobar}} hello {{else}} bye {{/if}} bar
""" GOOD_HTML5 = """
foo {{#if foobar}} hello {{else}} bye {{/if}} bar
""" BAD_HTML6 = """

foobar

""" GOOD_HTML6 = """

foobar

""" BAD_HTML7 = """
{{dyn_name}} {{#if invite_only}}{{/if}}
""" GOOD_HTML7 = """
{{dyn_name}} {{#if invite_only}}{{/if}}
""" BAD_HTML8 = """ {{#each test}} {{#with this}} {{#if foobar}}
{{{test}}}
{{/if}} {{#if foobar2}} {{> teststuff}} {{/if}} {{/with}} {{/each}} """ GOOD_HTML8 = """ {{#each test}} {{#with this}} {{#if foobar}}
{{{test}}}
{{/if}} {{#if foobar2}} {{> teststuff}} {{/if}} {{/with}} {{/each}} """ BAD_HTML9 = """
{{!
}} {{!
}}
""" GOOD_HTML9 = """
{{!
}} {{!
}}
""" BAD_HTML10 = """ {% block portico_content %}
foobar
{% for row in data %}
{% for group in (row[0:2], row[2:4]) %}
{% endfor %}
{% endfor %}
{% endblock %} """ GOOD_HTML10 = """ {% block portico_content %}
foobar
{% for row in data %}
{% for group in (row[0:2], row[2:4]) %}
{% endfor %}
{% endfor %}
{% endblock %} """ BAD_HTML11 = """
foobar
""" GOOD_HTML11 = """
foobar
""" BAD_HTML12 = """
  
foobar
""" GOOD_HTML12 = """
  
foobar
""" BAD_HTML13 = """
{{#if this.code}}
 :{{this.name}}:
{{else}} {{#if this.is_realm_emoji}} {{else}}
{{/if}} {{/if}}
{{this.count}}
""" GOOD_HTML13 = """
{{#if this.code}}
 :{{this.name}}:
{{else}} {{#if this.is_realm_emoji}} {{else}}
{{/if}} {{/if}}
{{this.count}}
""" BAD_HTML14 = """
{{#if this.code}}
Here goes some cool code.
{{else}}
content of first div
content of second div.
{{/if}}
""" GOOD_HTML14 = """
{{#if this.code}}
Here goes some cool code.
{{else}}
content of first div
content of second div.
{{/if}}
""" BAD_HTML15 = """
:thumbs_up: :thumbs_up: :thumbs_up:
""" GOOD_HTML15 = """
:thumbs_up: :thumbs_up: :thumbs_up:
""" BAD_HTML16 = """
{{> settings_checkbox setting_name="realm_name_in_notifications" is_checked=user_settings.realm_name_in_notifications label=settings_label.realm_name_in_notifications}}
""" GOOD_HTML16 = """
{{> settings_checkbox setting_name="realm_name_in_notifications" is_checked=user_settings.realm_name_in_notifications label=settings_label.realm_name_in_notifications}}
""" BAD_HTML17 = """
{{ bla }}
""" GOOD_HTML17 = """
{{ bla }}
""" class TestPrettyPrinter(unittest.TestCase): def compare(self, a: str, b: str) -> None: self.assertEqual(a.split("\n"), b.split("\n")) 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) self.compare(pretty_print_html(BAD_HTML2), GOOD_HTML2) self.compare(pretty_print_html(BAD_HTML4), GOOD_HTML4) self.compare(pretty_print_html(BAD_HTML5), GOOD_HTML5) self.compare(pretty_print_html(BAD_HTML6), GOOD_HTML6) self.compare(pretty_print_html(BAD_HTML7), GOOD_HTML7) self.compare(pretty_print_html(BAD_HTML8), GOOD_HTML8) self.compare(pretty_print_html(BAD_HTML9), GOOD_HTML9) self.compare(pretty_print_html(BAD_HTML10), GOOD_HTML10) self.compare(pretty_print_html(BAD_HTML11), GOOD_HTML11) self.compare(pretty_print_html(BAD_HTML12), GOOD_HTML12) self.compare(pretty_print_html(BAD_HTML13), GOOD_HTML13) self.compare(pretty_print_html(BAD_HTML14), GOOD_HTML14) self.compare(pretty_print_html(BAD_HTML15), GOOD_HTML15) self.compare(pretty_print_html(BAD_HTML16), GOOD_HTML16) self.compare(pretty_print_html(BAD_HTML17), GOOD_HTML17)