From 5530e6b7d3e89a68da71c66085679c785430c889 Mon Sep 17 00:00:00 2001 From: Pierre-Marie de Rodat Date: Wed, 17 Jan 2024 13:00:38 +0000 Subject: [PATCH] Start a default unparsing configuration for Libadalang and test it --- extensions/default_unparsing_config.json | 10 +++ testsuite/drivers/unparser_driver.py | 67 +++++++++++++++++++ .../unparsing/dotted_name_0/doc-baseline.json | 61 +++++++++++++++++ .../tests/unparsing/dotted_name_0/input.ada | 1 + .../tests/unparsing/dotted_name_0/test.out | 1 + .../tests/unparsing/dotted_name_0/test.yaml | 2 + testsuite/testsuite.py | 3 +- 7 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 extensions/default_unparsing_config.json create mode 100644 testsuite/drivers/unparser_driver.py create mode 100644 testsuite/tests/unparsing/dotted_name_0/doc-baseline.json create mode 100644 testsuite/tests/unparsing/dotted_name_0/input.ada create mode 100644 testsuite/tests/unparsing/dotted_name_0/test.out create mode 100644 testsuite/tests/unparsing/dotted_name_0/test.yaml diff --git a/extensions/default_unparsing_config.json b/extensions/default_unparsing_config.json new file mode 100644 index 000000000..a872f9b0e --- /dev/null +++ b/extensions/default_unparsing_config.json @@ -0,0 +1,10 @@ +{ + "node_configs": { + "DottedName": { + "node": {"kind": "fill", "document": "recurse"}, + "fields": { + "f_prefix": ["recurse", "softline"] + } + } + } +} diff --git a/testsuite/drivers/unparser_driver.py b/testsuite/drivers/unparser_driver.py new file mode 100644 index 000000000..4159078b4 --- /dev/null +++ b/testsuite/drivers/unparser_driver.py @@ -0,0 +1,67 @@ +from __future__ import annotations + +import os.path + +from drivers.base_driver import BaseDriver + + +class UnparserDriver(BaseDriver): + """ + Parse Ada source code with a given grammar rule, then unparsing it with the + default unparsing configuration. Check both the Prettier document and the + formatted source code against baselines. + """ + + input_filename = "input.ada" + document_baseline_filename = "doc-baseline.json" + + @property + def unparsing_config_filename(self) -> str: + """ + Return the absolute path to the default unparsing configuration. + """ + return os.path.join( + self.env.root_dir, + "..", + "extensions", + "default_unparsing_config.json", + ) + + def run(self) -> None: + # Run the unparser on "input.ada" for the given grammar rule and + # unparse it with the default unparsing configuration, with a dump of + # the Prettier document. + argv = ["lal_unparse", "-d"] + + rule = self.test_env.get("rule") + if rule: + argv += ["-r", rule] + + argv += [self.unparsing_config_filename, self.input_filename] + + self.run_and_check(argv, memcheck=True) + + def compute_failures(self) -> list[str]: + # Check the formatted source against "test.out" + result = super().compute_failures() + + # Check the prettier document against "doc-baseline.json" + + def read_file(filename: str) -> str: + with open(filename, "r", encoding=self.default_encoding) as f: + return f.read() + + document_baseline_filename = self.test_dir( + self.document_baseline_filename + ) + document_baseline = read_file(document_baseline_filename) + document_actual = read_file(self.working_dir("doc.json")) + + result.extend(self.compute_diff( + baseline_file=document_baseline_filename, + baseline=document_baseline, + actual=document_actual, + failure_message="Prettier document mismatch", + )) + + return result diff --git a/testsuite/tests/unparsing/dotted_name_0/doc-baseline.json b/testsuite/tests/unparsing/dotted_name_0/doc-baseline.json new file mode 100644 index 000000000..9a1192cdf --- /dev/null +++ b/testsuite/tests/unparsing/dotted_name_0/doc-baseline.json @@ -0,0 +1,61 @@ +{ + "id": 8, + "kind": "command", + "command": { + "command": "fill", + "parts": { + "id": 7, + "kind": "list", + "list": [ + { + "id": 4, + "kind": "command", + "command": { + "command": "fill", + "parts": { + "id": 3, + "kind": "list", + "list": [ + { + "id": 0, + "kind": "text", + "text": "A" + }, + { + "id": 1, + "kind": "command", + "command": { + "command": "line", + "literal": false, + "soft": true, + "hard": false + } + }, + { + "id": 2, + "kind": "text", + "text": ".B" + } + ] + } + } + }, + { + "id": 5, + "kind": "command", + "command": { + "command": "line", + "literal": false, + "soft": true, + "hard": false + } + }, + { + "id": 6, + "kind": "text", + "text": ".C" + } + ] + } + } +} diff --git a/testsuite/tests/unparsing/dotted_name_0/input.ada b/testsuite/tests/unparsing/dotted_name_0/input.ada new file mode 100644 index 000000000..f69c35896 --- /dev/null +++ b/testsuite/tests/unparsing/dotted_name_0/input.ada @@ -0,0 +1 @@ +A . B . C diff --git a/testsuite/tests/unparsing/dotted_name_0/test.out b/testsuite/tests/unparsing/dotted_name_0/test.out new file mode 100644 index 000000000..86562312c --- /dev/null +++ b/testsuite/tests/unparsing/dotted_name_0/test.out @@ -0,0 +1 @@ +A.B.C diff --git a/testsuite/tests/unparsing/dotted_name_0/test.yaml b/testsuite/tests/unparsing/dotted_name_0/test.yaml new file mode 100644 index 000000000..26e692904 --- /dev/null +++ b/testsuite/tests/unparsing/dotted_name_0/test.yaml @@ -0,0 +1,2 @@ +driver: unparser +rule: expr diff --git a/testsuite/testsuite.py b/testsuite/testsuite.py index debc9fcff..f8110125f 100755 --- a/testsuite/testsuite.py +++ b/testsuite/testsuite.py @@ -25,7 +25,7 @@ from drivers import ( adaapi_driver, capi_driver, dda_driver, inline_pg_driver, java_driver, name_resolution_driver, navigation_driver, ocaml_driver, parser_driver, - prep_driver, python_driver + prep_driver, python_driver, unparser_driver, ) @@ -71,6 +71,7 @@ class LALTestsuite(Testsuite): 'parser': parser_driver.ParserDriver, 'prep': prep_driver.PrepDriver, 'python': python_driver.PythonDriver, + 'unparser': unparser_driver.UnparserDriver, } # Even though we test Libadalang only in native configurations, this allows