Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add comments #555

Merged
merged 20 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tested/dsl/ast_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"""

import ast
import tokenize
from decimal import Decimal
from io import BytesIO
from typing import Literal, cast, overload

from attrs import evolve
Expand Down Expand Up @@ -333,6 +335,24 @@
return _convert_statement(statement_or_expression)


def extract_comment(code: str) -> str:
BrentBlanckaert marked this conversation as resolved.
Show resolved Hide resolved
"""
Extract the comment from the code.

:param code: The code to extract the comment from.
:return: The comment if it exists, otherwise an empty string.
"""
tokens = tokenize.tokenize(BytesIO(code.encode("utf-8")).readline)
BrentBlanckaert marked this conversation as resolved.
Show resolved Hide resolved
comments = list(
map(lambda t: t.string, filter(lambda t: t.type == tokenize.COMMENT, tokens))
)
if len(comments) == 0:
return ""
comment = comments[0][1:]
BrentBlanckaert marked this conversation as resolved.
Show resolved Hide resolved
assert isinstance(comment, str)
BrentBlanckaert marked this conversation as resolved.
Show resolved Hide resolved
return comment.strip()

Check warning on line 353 in tested/dsl/ast_translator.py

View check run for this annotation

Codecov / codecov/patch

tested/dsl/ast_translator.py#L351-L353

Added lines #L351 - L353 were not covered by tests


@overload
def parse_string(code: str, is_return: Literal[True]) -> Value: ...

Expand Down
5 changes: 4 additions & 1 deletion tested/dsl/translate_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
resolve_to_basic,
)
from tested.dodona import ExtendedMessage
from tested.dsl.ast_translator import InvalidDslError, parse_string
from tested.dsl.ast_translator import InvalidDslError, extract_comment, parse_string
Fixed Show fixed Hide fixed
from tested.parsing import get_converter, suite_to_json
from tested.serialisation import (
BooleanType,
Expand Down Expand Up @@ -567,6 +567,7 @@ def _convert_testcase(testcase: YamlDict, context: DslContext) -> Testcase:
if "statement" in testcase and "return" in testcase:
testcase["expression"] = testcase.pop("statement")

line_comment = ""
_validate_testcase_combinations(testcase)
if (expr_stmt := testcase.get("statement", testcase.get("expression"))) is not None:
if isinstance(expr_stmt, dict) or context.language != "tested":
Expand All @@ -583,6 +584,7 @@ def _convert_testcase(testcase: YamlDict, context: DslContext) -> Testcase:
the_input = LanguageLiterals(literals=the_dict, type=the_type)
else:
assert isinstance(expr_stmt, str)
line_comment = extract_comment(expr_stmt)
the_input = parse_string(expr_stmt)
return_channel = IgnoredChannel.IGNORED if "statement" in testcase else None
else:
Expand Down Expand Up @@ -653,6 +655,7 @@ def _convert_testcase(testcase: YamlDict, context: DslContext) -> Testcase:
input=the_input,
output=output,
link_files=context.files,
line_comment=line_comment,
)


Expand Down
3 changes: 3 additions & 0 deletions tested/languages/bash/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
def file_extension(self) -> str:
return "sh"

def comment(self, text: str) -> str:
return f"# {text}"

Check warning on line 42 in tested/languages/bash/config.py

View check run for this annotation

Codecov / codecov/patch

tested/languages/bash/config.py#L42

Added line #L42 was not covered by tests

def initial_dependencies(self) -> list[str]:
return []

Expand Down
3 changes: 3 additions & 0 deletions tested/languages/c/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
def file_extension(self) -> str:
return "c"

def comment(self, text: str) -> str:
return f"// {text}"

Check warning on line 42 in tested/languages/c/config.py

View check run for this annotation

Codecov / codecov/patch

tested/languages/c/config.py#L42

Added line #L42 was not covered by tests

def naming_conventions(self) -> dict[Conventionable, NamingConventions]:
return {"global_identifier": "macro_case"}

Expand Down
3 changes: 3 additions & 0 deletions tested/languages/csharp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
def file_extension(self) -> str:
return "cs"

def comment(self, text: str) -> str:
return f"// {text}"

Check warning on line 45 in tested/languages/csharp/config.py

View check run for this annotation

Codecov / codecov/patch

tested/languages/csharp/config.py#L45

Added line #L45 was not covered by tests

def naming_conventions(self) -> dict[Conventionable, NamingConventions]:
return {
"namespace": "pascal_case",
Expand Down
3 changes: 3 additions & 0 deletions tested/languages/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@
text = case.input.get_for(bundle.config.programming_language)
format_ = bundle.config.programming_language

if case.line_comment:
text = f"{text} {bundle.language.comment(case.line_comment)}"

Check warning on line 155 in tested/languages/generation.py

View check run for this annotation

Codecov / codecov/patch

tested/languages/generation.py#L155

Added line #L155 was not covered by tests

# If there are no files, return now. This means we don't need to do ugly stuff.
if not case.link_files:
return ExtendedMessage(description=text, format=format_), set()
Expand Down
3 changes: 3 additions & 0 deletions tested/languages/haskell/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
def file_extension(self) -> str:
return "hs"

def comment(self, text: str) -> str:
return f"-- {text}"

Check warning on line 41 in tested/languages/haskell/config.py

View check run for this annotation

Codecov / codecov/patch

tested/languages/haskell/config.py#L41

Added line #L41 was not covered by tests

def naming_conventions(self) -> dict[Conventionable, NamingConventions]:
return {
"namespace": "pascal_case",
Expand Down
3 changes: 3 additions & 0 deletions tested/languages/java/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
def file_extension(self) -> str:
return "java"

def comment(self, text: str) -> str:
return f"// {text}"

Check warning on line 47 in tested/languages/java/config.py

View check run for this annotation

Codecov / codecov/patch

tested/languages/java/config.py#L46-L47

Added lines #L46 - L47 were not covered by tests
def naming_conventions(self) -> dict[Conventionable, NamingConventions]:
return {
"namespace": "pascal_case",
Expand Down
3 changes: 3 additions & 0 deletions tested/languages/javascript/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def needs_selector(self):
def file_extension(self) -> str:
return "js"

def comment(self, text: str) -> str:
return f"// {text}"

def naming_conventions(self) -> dict[Conventionable, NamingConventions]:
return {
"namespace": "camel_case",
Expand Down
3 changes: 3 additions & 0 deletions tested/languages/kotlin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
def file_extension(self) -> str:
return "kt"

def comment(self, text: str) -> str:
return f"// {text}"

Check warning on line 54 in tested/languages/kotlin/config.py

View check run for this annotation

Codecov / codecov/patch

tested/languages/kotlin/config.py#L54

Added line #L54 was not covered by tests

def naming_conventions(self) -> dict[Conventionable, NamingConventions]:
return {
"namespace": "pascal_case",
Expand Down
10 changes: 10 additions & 0 deletions tested/languages/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@
"""
raise NotImplementedError

@abstractmethod
def comment(self, text: str) -> str:
"""
Generate a comment for the given text.

:param text: The text to comment.
:return: The comment.
"""
raise NotImplementedError

Check warning on line 203 in tested/languages/language.py

View check run for this annotation

Codecov / codecov/patch

tested/languages/language.py#L203

Added line #L203 was not covered by tests

def is_source_file(self, file: Path) -> bool:
"""
Check if the given file could be a source file for this programming language.
Expand Down
3 changes: 3 additions & 0 deletions tested/languages/python/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
def file_extension(self) -> str:
return "py"

def comment(self, text: str) -> str:
return f"# {text}"

Check warning on line 55 in tested/languages/python/config.py

View check run for this annotation

Codecov / codecov/patch

tested/languages/python/config.py#L55

Added line #L55 was not covered by tests

def naming_conventions(self) -> dict[Conventionable, NamingConventions]:
return {"class": "pascal_case", "global_identifier": "macro_case"}

Expand Down
1 change: 1 addition & 0 deletions tested/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ class Testcase(WithFeatures, WithFunctions):
description: Message | None = None
output: Output = field(factory=Output)
link_files: list[FileUrl] = field(factory=list)
line_comment: str = ""

def get_used_features(self) -> FeatureSet:
return combine_features(
Expand Down
Loading