From 57c3079a117adc8f4279d26e67c95ab352ccbf26 Mon Sep 17 00:00:00 2001 From: breblanc Date: Sat, 16 Nov 2024 16:14:49 +0100 Subject: [PATCH] added comments for descriptions --- tested/dsl/translate_parser.py | 5 ++ .../evaluation/comment_description_plan.yaml | 7 +++ .../evaluation/plan_no_description.yaml | 4 ++ tests/test_functionality.py | 59 ++++++++++++++++++- 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/exercises/global/evaluation/comment_description_plan.yaml create mode 100644 tests/exercises/global/evaluation/plan_no_description.yaml diff --git a/tested/dsl/translate_parser.py b/tested/dsl/translate_parser.py index 6f589799..29c03ba5 100644 --- a/tested/dsl/translate_parser.py +++ b/tested/dsl/translate_parser.py @@ -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") + print(f"testcase: {testcase}") line_comment = "" _validate_testcase_combinations(testcase) if (expr_stmt := testcase.get("statement", testcase.get("expression"))) is not None: @@ -639,6 +640,10 @@ def _convert_testcase(testcase: YamlDict, context: DslContext) -> Testcase: assert isinstance( dd, str ), f"The description.description field must be a string, got {dd!r}." + line_comment = extract_comment(dd) + if line_comment and dd.endswith(line_comment): + dd = dd[:-len(line_comment)].rstrip().rstrip("#").rstrip() + df = description.get("format", "text") assert isinstance( df, str diff --git a/tests/exercises/global/evaluation/comment_description_plan.yaml b/tests/exercises/global/evaluation/comment_description_plan.yaml new file mode 100644 index 00000000..244ebf45 --- /dev/null +++ b/tests/exercises/global/evaluation/comment_description_plan.yaml @@ -0,0 +1,7 @@ +- tab: "Global variable" + testcases: + - expression: "GLOBAL_VAR # The name of the global variable" + return: "GLOBAL" + description: + description: "Hallo # This is a greeting" + format: "code" diff --git a/tests/exercises/global/evaluation/plan_no_description.yaml b/tests/exercises/global/evaluation/plan_no_description.yaml new file mode 100644 index 00000000..5ed14985 --- /dev/null +++ b/tests/exercises/global/evaluation/plan_no_description.yaml @@ -0,0 +1,4 @@ +- tab: "Global variable" + testcases: + - expression: "GLOBAL_VAR # The name of the global variable" + return: "GLOBAL" diff --git a/tests/test_functionality.py b/tests/test_functionality.py index 73471182..9b5d3d7f 100644 --- a/tests/test_functionality.py +++ b/tests/test_functionality.py @@ -58,7 +58,6 @@ def test_global_variable_yaml( updates = assert_valid_output(result, pytestconfig) assert updates.find_status_enum() == ["correct"] - @pytest.mark.parametrize( "language, comment_start", [ @@ -76,7 +75,7 @@ def test_global_comment( language: str, comment_start: str, tmp_path: Path, pytestconfig: pytest.Config ): conf = configuration( - pytestconfig, "global", language, tmp_path, "plan.yaml", "correct" + pytestconfig, "global", language, tmp_path, "plan_no_description.yaml", "correct" ) result = execute_config(conf) updates = assert_valid_output(result, pytestconfig) @@ -87,6 +86,62 @@ def test_global_comment( f"{comment_start} The name of the global variable" ) +@pytest.mark.parametrize( + "language, comment_start", + [ + ("bash", "#"), + ("python", "#"), + ("kotlin", "//"), + ("csharp", "//"), + ("java", "//"), + ("c", "//"), + ("javascript", "//"), + ("haskell", "--"), + ], +) +def test_global_no_comment( + language: str, comment_start: str, tmp_path: Path, pytestconfig: pytest.Config +): + conf = configuration( + pytestconfig, "global", language, tmp_path, "plan.yaml", "correct" + ) + result = execute_config(conf) + updates = assert_valid_output(result, pytestconfig) + description = updates.find_next("start-testcase") + + assert "description" in description and "description" in description["description"] + assert not description["description"]["description"].endswith( + f"{comment_start} The name of the global variable" + ) + +@pytest.mark.parametrize( + "language, comment_start", + [ + ("bash", "#"), + ("python", "#"), + ("kotlin", "//"), + ("csharp", "//"), + ("java", "//"), + ("c", "//"), + ("javascript", "//"), + ("haskell", "--"), + ], +) +def test_global_comment_description( + language: str, comment_start: str, tmp_path: Path, pytestconfig: pytest.Config +): + conf = configuration( + pytestconfig, "global", language, tmp_path, "comment_description_plan.yaml", "correct" + ) + result = execute_config(conf) + updates = assert_valid_output(result, pytestconfig) + description = updates.find_next("start-testcase") + + assert "description" in description and "description" in description["description"] + assert description["description"]["description"].endswith( + f"{comment_start} This is a greeting" + ) + @pytest.mark.parametrize("lang", EXCEPTION_LANGUAGES) def test_generic_exception_wrong(