Skip to content

Commit

Permalink
fixed linting and types
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentBlanckaert committed Dec 10, 2024
1 parent b3f2e3c commit a791d6a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
51 changes: 40 additions & 11 deletions tested/languages/typescript/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
Value,
VariableAssignment,
VariableType,
as_basic_type, _get_type_for,
as_basic_type,
_get_type_for,
)
from tested.testsuite import MainInput

Expand Down Expand Up @@ -132,8 +133,8 @@ def convert_function_call(call: FunctionCall, internal=False) -> str:
result += f"({convert_arguments(call.arguments)})" # pyright: ignore
return result

def convert_declaration(statement: Statement) -> str:
tp = statement.type

def convert_declaration(statement: Statement, tp: AllTypes | VariableType) -> str:
if isinstance(tp, VariableType):
return tp.data
elif tp == AdvancedStringTypes.CHAR:
Expand Down Expand Up @@ -161,24 +162,52 @@ def convert_declaration(statement: Statement) -> str:
elif basic == BasicSequenceTypes.SEQUENCE:
type_ = "Object"

expression = statement.expression
if isinstance(statement, VariableAssignment):
expression = statement.expression
else:
expression = statement
if isinstance(expression, SequenceType):
type_ = {convert_declaration(element) for element in expression.data}
type_ = '|'.join(type_)
type_ = {
(
convert_declaration(element, element.type)
if not isinstance(element, Identifier)
and not isinstance(element, FunctionCall)
else "id"
)
for element in expression.data
}
if "id" in type_:
type_ = "any"
else:
type_ = "|".join(type_)


return f"Array<{type_}>"
elif basic == BasicSequenceTypes.SET:
type_ = "Object"

expression = statement.expression
if isinstance(statement, VariableAssignment):
expression = statement.expression
else:
expression = statement
if isinstance(expression, SequenceType):
type_ = {convert_declaration(element) for element in expression.data}
type_ = '|'.join(type_)
type_ = {
(
convert_declaration(element, element.type)
if not isinstance(element, Identifier)
and not isinstance(element, FunctionCall)
else "id"
)
for element in expression.data
}
if "id" in type_:
type_ = "any"
else:
type_ = "|".join(type_)

return f"Set<{type_}>"
raise AssertionError(f"Unknown type: {tp!r}")


def convert_statement(statement: Statement, internal=False, full=False) -> str:
if isinstance(statement, Identifier):
return statement
Expand All @@ -197,7 +226,7 @@ def convert_statement(statement: Statement, internal=False, full=False) -> str:
else:
prefix = ""
return (
f"{prefix}{statement.variable} : {convert_declaration(statement)} = "
f"{prefix}{statement.variable} : {convert_declaration(statement, statement.type)} = "
f"{convert_statement(statement.expression, True)}"
)
raise AssertionError(f"Unknown statement: {statement!r}")
Expand Down
4 changes: 4 additions & 0 deletions tests/test_language_quircks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for specific aspects of certain language implementations.
"""

import itertools
import shutil
import sys
Expand All @@ -24,6 +25,7 @@
from tested.testsuite import Suite
from tests.manual_utils import assert_valid_output, configuration, execute_config


def test_typescript_array_typing(tmp_path: Path, pytestconfig: pytest.Config):
statement_string = "test = ['test', True, 10, 10.1, None, {'wow': 10}]"
result = convert_statement(parse_string(statement_string), full=True)
Expand All @@ -36,6 +38,7 @@ def test_typescript_array_typing(tmp_path: Path, pytestconfig: pytest.Config):

assert result in valid_results


def test_typescript_set_typing(tmp_path: Path, pytestconfig: pytest.Config):
statement_string = "test = {'test', True, 10, 10.1, None, {'wow': 10}}"
result = convert_statement(parse_string(statement_string), full=True)
Expand All @@ -48,6 +51,7 @@ def test_typescript_set_typing(tmp_path: Path, pytestconfig: pytest.Config):

assert result in valid_results


def test_javascript_vanilla_object(tmp_path: Path, pytestconfig: pytest.Config):
conf = configuration(
pytestconfig,
Expand Down

0 comments on commit a791d6a

Please sign in to comment.