cleanup: Format using black

This commit is contained in:
James Westman 2022-02-23 14:09:18 -06:00
parent 4b42016837
commit af03c2ac0f
36 changed files with 928 additions and 616 deletions

View file

@ -18,7 +18,7 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import difflib # I love Python
import difflib # I love Python
from pathlib import Path
import traceback
import unittest
@ -48,20 +48,23 @@ class TestSamples(unittest.TestCase):
raise MultipleErrors(warnings)
actual = ast.generate()
if actual.strip() != expected.strip(): # pragma: no cover
if actual.strip() != expected.strip(): # pragma: no cover
diff = difflib.unified_diff(expected.splitlines(), actual.splitlines())
print("\n".join(diff))
raise AssertionError()
except PrintableError as e: # pragma: no cover
except PrintableError as e: # pragma: no cover
e.pretty_print(name + ".blp", blueprint)
raise AssertionError()
def assert_sample_error(self, name):
try:
with open((Path(__file__).parent / f"sample_errors/{name}.blp").resolve()) as f:
with open(
(Path(__file__).parent / f"sample_errors/{name}.blp").resolve()
) as f:
blueprint = f.read()
with open((Path(__file__).parent / f"sample_errors/{name}.err").resolve()) as f:
with open(
(Path(__file__).parent / f"sample_errors/{name}.err").resolve()
) as f:
expected = f.read()
tokens = tokenizer.tokenize(blueprint)
@ -74,6 +77,7 @@ class TestSamples(unittest.TestCase):
if len(warnings):
raise MultipleErrors(warnings)
except PrintableError as e:
def error_str(error):
line, col = utils.idx_to_pos(error.start + 1, blueprint)
len = error.end - error.start
@ -83,17 +87,16 @@ class TestSamples(unittest.TestCase):
actual = error_str(e)
elif isinstance(e, MultipleErrors):
actual = "\n".join([error_str(error) for error in e.errors])
else: # pragma: no cover
else: # pragma: no cover
raise AssertionError()
if actual.strip() != expected.strip(): # pragma: no cover
if actual.strip() != expected.strip(): # pragma: no cover
diff = difflib.unified_diff(expected.splitlines(), actual.splitlines())
print("\n".join(diff))
raise AssertionError()
else: # pragma: no cover
else: # pragma: no cover
raise AssertionError("Expected a compiler error, but none was emitted")
def assert_decompile(self, name):
try:
with open((Path(__file__).parent / f"samples/{name}.blp").resolve()) as f:
@ -104,15 +107,14 @@ class TestSamples(unittest.TestCase):
actual = decompiler.decompile(ui_path)
if actual.strip() != expected.strip(): # pragma: no cover
if actual.strip() != expected.strip(): # pragma: no cover
diff = difflib.unified_diff(expected.splitlines(), actual.splitlines())
print("\n".join(diff))
raise AssertionError()
except PrintableError as e: # pragma: no cover
except PrintableError as e: # pragma: no cover
e.pretty_print(name + ".blp", blueprint)
raise AssertionError()
def test_samples(self):
self.assert_sample("accessibility")
self.assert_sample("action_widgets")
@ -141,7 +143,6 @@ class TestSamples(unittest.TestCase):
self.assert_sample("uint")
self.assert_sample("using")
def test_sample_errors(self):
self.assert_sample_error("a11y_in_non_widget")
self.assert_sample_error("a11y_prop_dne")
@ -180,7 +181,6 @@ class TestSamples(unittest.TestCase):
self.assert_sample_error("using_invalid_namespace")
self.assert_sample_error("widgets_in_non_size_group")
def test_decompiler(self):
self.assert_decompile("accessibility_dec")
self.assert_decompile("binding")

View file

@ -32,42 +32,52 @@ class TestTokenizer(unittest.TestCase):
for token, (type, token_str) in zip(tokens, expect):
self.assertEqual(token.type, type)
self.assertEqual(str(token), token_str)
except PrintableError as e: # pragma: no cover
except PrintableError as e: # pragma: no cover
e.pretty_print("<test input>", string)
raise e
def test_basic(self):
self.assert_tokenize("ident(){}; \n <<+>>*/=", [
(TokenType.IDENT, "ident"),
(TokenType.PUNCTUATION, "("),
(TokenType.PUNCTUATION, ")"),
(TokenType.PUNCTUATION, "{"),
(TokenType.PUNCTUATION, "}"),
(TokenType.PUNCTUATION, ";"),
(TokenType.WHITESPACE, " \n "),
(TokenType.OP, "<<+>>*/="),
(TokenType.EOF, ""),
])
self.assert_tokenize(
"ident(){}; \n <<+>>*/=",
[
(TokenType.IDENT, "ident"),
(TokenType.PUNCTUATION, "("),
(TokenType.PUNCTUATION, ")"),
(TokenType.PUNCTUATION, "{"),
(TokenType.PUNCTUATION, "}"),
(TokenType.PUNCTUATION, ";"),
(TokenType.WHITESPACE, " \n "),
(TokenType.OP, "<<+>>*/="),
(TokenType.EOF, ""),
],
)
def test_quotes(self):
self.assert_tokenize(r'"this is a \n string""this is \\another \"string\""', [
(TokenType.QUOTED, r'"this is a \n string"'),
(TokenType.QUOTED, r'"this is \\another \"string\""'),
(TokenType.EOF, ""),
])
self.assert_tokenize(
r'"this is a \n string""this is \\another \"string\""',
[
(TokenType.QUOTED, r'"this is a \n string"'),
(TokenType.QUOTED, r'"this is \\another \"string\""'),
(TokenType.EOF, ""),
],
)
def test_comments(self):
self.assert_tokenize('/* \n \\n COMMENT /* */', [
(TokenType.COMMENT, '/* \n \\n COMMENT /* */'),
(TokenType.EOF, ""),
])
self.assert_tokenize('line // comment\nline', [
(TokenType.IDENT, 'line'),
(TokenType.WHITESPACE, ' '),
(TokenType.COMMENT, '// comment'),
(TokenType.WHITESPACE, '\n'),
(TokenType.IDENT, 'line'),
(TokenType.EOF, ""),
])
self.assert_tokenize(
"/* \n \\n COMMENT /* */",
[
(TokenType.COMMENT, "/* \n \\n COMMENT /* */"),
(TokenType.EOF, ""),
],
)
self.assert_tokenize(
"line // comment\nline",
[
(TokenType.IDENT, "line"),
(TokenType.WHITESPACE, " "),
(TokenType.COMMENT, "// comment"),
(TokenType.WHITESPACE, "\n"),
(TokenType.IDENT, "line"),
(TokenType.EOF, ""),
],
)