Skip to content

Commit

Permalink
Merge pull request #520 from dodona-edu/feature/javascript-named-argu…
Browse files Browse the repository at this point in the history
…ments

Support named parameters in JavaScript
  • Loading branch information
niknetniko authored Jun 3, 2024
2 parents 60b43a8 + e269c18 commit d83baab
Show file tree
Hide file tree
Showing 6 changed files with 2,235 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Integration tests
on: [ pull_request ]

env:
EXERCISES_COMMIT: 4a2094135abe972eb38072be129171f685c18ec3
EXERCISES_COMMIT: 10ce1d2a5211514d219924a9bb26c9655daee464

jobs:
# Runs the test suite in a slightly modified Docker image used by Dodona.
Expand Down
1 change: 1 addition & 0 deletions tested/languages/javascript/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def supported_constructs(self) -> set[Construct]:
Construct.EVALUATION,
Construct.DEFAULT_PARAMETERS,
Construct.GLOBAL_VARIABLES,
Construct.NAMED_ARGUMENTS,
}

def datatype_support(self) -> dict[AllTypes, TypeSupport]:
Expand Down
18 changes: 12 additions & 6 deletions tested/languages/javascript/generators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from typing import cast

from tested.datatypes import (
AdvancedNothingTypes,
Expand All @@ -25,6 +24,7 @@
FunctionCall,
FunctionType,
Identifier,
NamedArgument,
ObjectType,
PropertyAssignment,
SequenceType,
Expand All @@ -38,8 +38,14 @@
from tested.testsuite import MainInput


def convert_arguments(arguments: list[Expression]) -> str:
return ", ".join(convert_statement(arg, True) for arg in arguments)
def convert_arguments(arguments: list[NamedArgument | Expression]) -> str:
results = []
for arg in arguments:
if isinstance(arg, NamedArgument):
results.append(f"{arg.name}={convert_statement(arg.value, True)}")
else:
results.append(convert_statement(arg, True))
return ", ".join(results)


def convert_value(value: Value) -> str:
Expand Down Expand Up @@ -86,10 +92,10 @@ def convert_value(value: Value) -> str:
return "null"
elif value.type == BasicSequenceTypes.SEQUENCE:
assert isinstance(value, SequenceType)
return f"[{convert_arguments(value.data)}]"
return f"[{convert_arguments(value.data)}]" # pyright: ignore
elif value.type == BasicSequenceTypes.SET:
assert isinstance(value, SequenceType)
return f"new Set([{convert_arguments(value.data)}])"
return f"new Set([{convert_arguments(value.data)}])" # pyright: ignore
elif value.type == BasicObjectTypes.MAP:
assert isinstance(value, ObjectType)
result = "new Map(["
Expand Down Expand Up @@ -119,7 +125,7 @@ def convert_function_call(call: FunctionCall, internal=False) -> str:
result += convert_statement(call.namespace, True) + "."
result += call.name
if call.type != FunctionType.PROPERTY:
result += f"({convert_arguments(cast(list[Expression], call.arguments))})"
result += f"({convert_arguments(call.arguments)})" # pyright: ignore
return result


Expand Down
Loading

0 comments on commit d83baab

Please sign in to comment.