Skip to content

Commit

Permalink
Added types for TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentBlanckaert committed Dec 10, 2024
1 parent cb71451 commit b3f2e3c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
32 changes: 28 additions & 4 deletions tested/languages/typescript/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
from tested.datatypes import (
AdvancedNothingTypes,
AdvancedNumericTypes,
AdvancedStringTypes,
AllTypes,
BasicBooleanTypes,
BasicNothingTypes,
BasicNumericTypes,
BasicObjectTypes,
BasicSequenceTypes,
BasicStringTypes,
resolve_to_basic,
)
from tested.datatypes.advanced import AdvancedObjectTypes
from tested.languages.conventionalize import submission_file
Expand All @@ -33,7 +36,8 @@
StringType,
Value,
VariableAssignment,
as_basic_type,
VariableType,
as_basic_type, _get_type_for,
)
from tested.testsuite import MainInput

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

def convert_declaration(tp: AllTypes | VariableType) -> str:
def convert_declaration(statement: Statement) -> str:
tp = statement.type
if isinstance(tp, VariableType):
return tp.data
elif tp == AdvancedStringTypes.CHAR:
Expand All @@ -151,8 +156,27 @@ def convert_declaration(tp: AllTypes | VariableType) -> str:
return "number"
elif basic == BasicNothingTypes.NOTHING:
return "null"
elif basic == BasicSequenceTypes.MAP:
elif basic == BasicObjectTypes.MAP:
return "object"
elif basic == BasicSequenceTypes.SEQUENCE:
type_ = "Object"

expression = statement.expression
if isinstance(expression, SequenceType):
type_ = {convert_declaration(element) for element in expression.data}
type_ = '|'.join(type_)


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

expression = statement.expression
if isinstance(expression, SequenceType):
type_ = {convert_declaration(element) for element in expression.data}
type_ = '|'.join(type_)

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

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

import itertools
import shutil
import sys
from pathlib import Path
Expand All @@ -10,8 +10,10 @@

from tested.configs import create_bundle
from tested.datatypes import BasicBooleanTypes, BasicNumericTypes, BasicStringTypes
from tested.dsl import parse_string
from tested.languages.conventionalize import submission_name
from tested.languages.generation import generate_statement
from tested.languages.typescript.generators import convert_statement
from tested.serialisation import (
BooleanType,
FunctionCall,
Expand All @@ -22,6 +24,29 @@
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)
types = ["string", "boolean", "number", "object", "null"]
permutations = list(itertools.permutations(types))
valid_results = [
f'let test : Array<{"|".join(perm)}> = ["test", true, 10, 10.1, null, new Map([["wow", 10]])]'
for perm in permutations
]

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)
types = ["string", "boolean", "number", "object", "null"]
permutations = list(itertools.permutations(types))
valid_results = [
f'let test : Set<{"|".join(perm)}> = new Set(["test", true, 10, 10.1, null, new Map([["wow", 10]])])'
for perm in permutations
]

assert result in valid_results

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

0 comments on commit b3f2e3c

Please sign in to comment.