From 6391f903d250373ff7df5cd225b6c891c3381580 Mon Sep 17 00:00:00 2001 From: Stephan Lukasczyk Date: Wed, 16 Mar 2022 10:16:27 +0100 Subject: [PATCH] Remove astor dependency Python 3.8 required us to use a library to print the generated Python AST into Python source files. Since we dropped the support for Python 3.8 (and 3.9) with 0bfb66aaa2aa0accc15789a3660ee66ba2ef01d7 we can now simply us the `unparse` function from Python's AST library that was introduced in Python 3.9. This change removes all `astor`-related code and makes the necessary changes to use Python's `ast.unparse`. --- docs/requirements.txt | 1 - poetry.lock | 14 +- pynguin/generation/export/abstractexporter.py | 5 +- pynguin/generation/export/pytestexporter.py | 2 +- pynguin/testcase/execution.py | 5 +- pyproject.toml | 1 - tests/analyses/test_ast_to_statement.py | 7 +- .../test_assertion_generation_integration.py | 13 +- tests/assertion/test_assertion_to_ast.py | 38 ++--- .../generation/export/test_pytestexporter.py | 5 +- tests/testcase/test_statement_to_ast.py | 131 ++++++++---------- .../test_testcase_to_ast_integration.py | 26 +++- 12 files changed, 115 insertions(+), 133 deletions(-) diff --git a/docs/requirements.txt b/docs/requirements.txt index d988d2de2..70ced4472 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,6 +1,5 @@ sphinx==4.4.0 sphinx-autodoc-typehints==1.16.0 -astor==0.8.1 simple-parsing==0.0.18 bytecode==0.13.0 typing_inspect==0.7.1 diff --git a/poetry.lock b/poetry.lock index 90f07b43e..579ba2b6a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -17,14 +17,6 @@ python-versions = "*" [package.dependencies] pydot = "*" -[[package]] -name = "astor" -version = "0.8.1" -description = "Read/rewrite/write Python ASTs" -category = "main" -optional = false -python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" - [[package]] name = "astroid" version = "2.9.3" @@ -1067,7 +1059,7 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" [metadata] lock-version = "1.1" python-versions = "^3.10" -content-hash = "7f5d2e4e8455425740d3ba24ca33467525c200b3eb7733178b21bc838ef7a205" +content-hash = "aa9e7a358391cd96ced14027e43df2958106c86dcb5b95dc536d77c3af7484d3" [metadata.files] alabaster = [ @@ -1077,10 +1069,6 @@ alabaster = [ astmonkey = [ {file = "astmonkey-0.3.6.tar.gz", hash = "sha256:f82dbdd18a2d1810ef43782d3a29743bacd2b09422b8193a72a572b118c8cfb5"}, ] -astor = [ - {file = "astor-0.8.1-py2.py3-none-any.whl", hash = "sha256:070a54e890cefb5b3739d19f30f5a5ec840ffc9c50ffa7d23cc9fc1a38ebbfc5"}, - {file = "astor-0.8.1.tar.gz", hash = "sha256:6a6effda93f4e1ce9f618779b2dd1d9d84f1e32812c23a29b3fff6fd7f63fa5e"}, -] astroid = [ {file = "astroid-2.9.3-py3-none-any.whl", hash = "sha256:506daabe5edffb7e696ad82483ad0228245a9742ed7d2d8c9cdb31537decf9f6"}, {file = "astroid-2.9.3.tar.gz", hash = "sha256:1efdf4e867d4d8ba4a9f6cf9ce07cd182c4c41de77f23814feb27ca93ca9d877"}, diff --git a/pynguin/generation/export/abstractexporter.py b/pynguin/generation/export/abstractexporter.py index 761b52e98..20c8b26d5 100644 --- a/pynguin/generation/export/abstractexporter.py +++ b/pynguin/generation/export/abstractexporter.py @@ -13,8 +13,6 @@ from pathlib import Path from typing import TYPE_CHECKING -import astor - import pynguin.testcase.testcase_to_ast as tc_to_ast if TYPE_CHECKING: @@ -97,6 +95,7 @@ def __create_function_node( defaults=[], vararg=None, kwarg=None, + posonlyargs=[], kwonlyargs=[], kw_defaults=[], ), @@ -118,4 +117,4 @@ def _save_ast_to_file(path: str | os.PathLike, module: ast.Module) -> None: target.parent.mkdir(parents=True, exist_ok=True) with target.open(mode="w", encoding="UTF-8") as file: file.write("# Automatically generated by Pynguin.\n") - file.write(astor.to_source(module)) + file.write(ast.unparse(ast.fix_missing_locations(module))) diff --git a/pynguin/generation/export/pytestexporter.py b/pynguin/generation/export/pytestexporter.py index d5b7c6c58..fdb935f93 100644 --- a/pynguin/generation/export/pytestexporter.py +++ b/pynguin/generation/export/pytestexporter.py @@ -31,5 +31,5 @@ def export_sequences(self, path: str | os.PathLike, test_cases: list[tc.TestCase module_aliases, common_modules ) functions = AbstractTestExporter._create_functions(asts, False) - module = ast.Module(body=import_nodes + functions) + module = ast.Module(body=import_nodes + functions, type_ignores=[]) AbstractTestExporter._save_ast_to_file(path, module) diff --git a/pynguin/testcase/execution.py b/pynguin/testcase/execution.py index d2997749d..25cd37952 100644 --- a/pynguin/testcase/execution.py +++ b/pynguin/testcase/execution.py @@ -21,7 +21,6 @@ from types import CodeType, ModuleType from typing import TYPE_CHECKING, Any, Callable -import astor from bytecode import Compare from jellyfish import levenshtein_distance from ordered_set import OrderedSet @@ -1114,13 +1113,13 @@ def _execute_statement( ) -> Exception | None: ast_node = exec_ctx.executable_node_for(statement) if self._logger.isEnabledFor(logging.DEBUG): - self._logger.debug("Executing %s", astor.to_source(ast_node)) + self._logger.debug("Executing %s", ast.unparse(ast_node)) code = compile(ast_node, "", "exec") try: # pylint: disable=exec-used exec(code, exec_ctx.global_namespace, exec_ctx.local_namespace) # nosec except Exception as err: # pylint: disable=broad-except - failed_stmt = astor.to_source(ast_node) + failed_stmt = ast.unparse(ast_node) TestCaseExecutor._logger.debug( "Failed to execute statement:\n%s%s", failed_stmt, err.args ) diff --git a/pyproject.toml b/pyproject.toml index c7a772227..e6cf2d641 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,7 +38,6 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.10" -astor = "^0.8.1" simple-parsing = "^0.0.18" bytecode = "^0" typing_inspect = "^0" diff --git a/tests/analyses/test_ast_to_statement.py b/tests/analyses/test_ast_to_statement.py index ebe71a2fd..b4e9e03b3 100644 --- a/tests/analyses/test_ast_to_statement.py +++ b/tests/analyses/test_ast_to_statement.py @@ -21,8 +21,7 @@ [ ( """ float_0 = 1.1 - var_0 = module_0.positional_only(float_0) -""" + var_0 = module_0.positional_only(float_0)""" ), ( """ float_0 = 1.1 @@ -32,8 +31,7 @@ str_1 = 'key' str_2 = 'value' str_3 = {str_1: str_2} - var_1 = module_0.all_params(float_0, int_0, *var_0, param4=str_0, **str_3) -""" + var_1 = module_0.all_params(float_0, int_0, *var_0, param4=str_0, **str_3)""" ), ], ) @@ -42,7 +40,6 @@ def test_parameter_mapping_roundtrip(testcase_seed, tmp_path): """# Automatically generated by Pynguin. import tests.fixtures.grammar.parameters as module_0 - def test_case_0(): """ + testcase_seed diff --git a/tests/assertion/test_assertion_generation_integration.py b/tests/assertion/test_assertion_generation_integration.py index 2664cfc66..f684526ca 100644 --- a/tests/assertion/test_assertion_generation_integration.py +++ b/tests/assertion/test_assertion_generation_integration.py @@ -7,7 +7,6 @@ import ast import importlib -import astor import pytest import pynguin.assertion.assertiongenerator as ag @@ -32,8 +31,7 @@ assert human_0 is not None assert module_0.static_state == 0 str_1 = human_0.get_name() -assert str_1 == 'foo bar' -""", +assert str_1 == 'foo bar'""", ), ( ag.MutationAnalysisAssertionGenerator, @@ -41,8 +39,7 @@ float_0 = 39.82 human_0 = module_0.Human(str_0, float_0) assert module_0.static_state == 0 -str_1 = human_0.get_name() -""", +str_1 = human_0.get_name()""", ), ], ) @@ -79,5 +76,9 @@ def test_generate_mutation_assertions(generator, expected_result): visitor = tc_to_ast.TestCaseToAstVisitor() test_case.accept(visitor) - source = astor.to_source(ast.Module(body=visitor.test_case_asts[0])) + source = ast.unparse( + ast.fix_missing_locations( + ast.Module(body=visitor.test_case_asts[0], type_ignores=[]) + ) + ) assert source == expected_result diff --git a/tests/assertion/test_assertion_to_ast.py b/tests/assertion/test_assertion_to_ast.py index 73afc2c11..65d451290 100644 --- a/tests/assertion/test_assertion_to_ast.py +++ b/tests/assertion/test_assertion_to_ast.py @@ -4,11 +4,10 @@ # # SPDX-License-Identifier: LGPL-3.0-or-later # +import ast import enum -from _ast import Module from unittest.mock import MagicMock -import astor import pytest import pynguin.assertion.assertion as ass @@ -24,34 +23,39 @@ def assertion_to_ast() -> ata.AssertionToAstVisitor: return ata.AssertionToAstVisitor(scope, module_aliases, set()) +def __create_source_from_ast(module_body: list[ast.stmt]) -> str: + return ast.unparse( + ast.fix_missing_locations(ast.Module(body=module_body, type_ignores=[])) + ) + + def test_none(assertion_to_ast): assertion = ass.NotNoneAssertion(source=vr.VariableReference(MagicMock(), None)) assertion.accept(assertion_to_ast) assert ( - astor.to_source(Module(body=assertion_to_ast.nodes)) - == "assert var_0 is not None\n" + __create_source_from_ast(assertion_to_ast.nodes) == "assert var_0 is not None" ) @pytest.mark.parametrize( "obj,output", [ - (True, "assert var_0 is True\n"), - (False, "assert var_0 is False\n"), - ((True, False), "assert var_0 == (True, False)\n"), - ([3, 8], "assert var_0 == [3, 8]\n"), - ([[3, 8], {"foo"}], "assert var_0 == [[3, 8], {'foo'}]\n"), + (True, "assert var_0 is True"), + (False, "assert var_0 is False"), + ((True, False), "assert var_0 == (True, False)"), + ([3, 8], "assert var_0 == [3, 8]"), + ([[3, 8], {"foo"}], "assert var_0 == [[3, 8], {'foo'}]"), ( {"foo": ["nope", 1, False, None]}, - "assert var_0 == {'foo': ['nope', 1, False, None]}\n", + "assert var_0 == {'foo': ['nope', 1, False, None]}", ), ( {"foo": "bar", "baz": "argh"}, - "assert var_0 == {'foo': 'bar', 'baz': 'argh'}\n", + "assert var_0 == {'foo': 'bar', 'baz': 'argh'}", ), ( {enum.Enum("Dummy", "a").a: False}, - "assert var_0 == {module_0.Dummy.a: False}\n", + "assert var_0 == {module_0.Dummy.a: False}", ), ], ) @@ -60,7 +64,7 @@ def test_object_assertion(assertion_to_ast, obj, output): source=vr.VariableReference(MagicMock(), None), value=obj ) assertion.accept(assertion_to_ast) - assert astor.to_source(Module(body=assertion_to_ast.nodes)) == output + assert __create_source_from_ast(assertion_to_ast.nodes) == output def test_float_assertion(assertion_to_ast): @@ -69,18 +73,18 @@ def test_float_assertion(assertion_to_ast): ) assertion.accept(assertion_to_ast) assert ( - astor.to_source(Module(body=assertion_to_ast.nodes)) - == "assert var_0 == pytest.approx(1.5, abs=0.01, rel=0.01)\n" + __create_source_from_ast(assertion_to_ast.nodes) + == "assert var_0 == pytest.approx(1.5, abs=0.01, rel=0.01)" ) @pytest.mark.parametrize( "length, output", - [(0, "assert len(var_0) == 0\n"), (42, "assert len(var_0) == 42\n")], + [(0, "assert len(var_0) == 0"), (42, "assert len(var_0) == 42")], ) def test_collection_length(assertion_to_ast, length, output): assertion = ass.CollectionLengthAssertion( source=vr.VariableReference(MagicMock(), None), length=length ) assertion.accept(assertion_to_ast) - assert astor.to_source(Module(body=assertion_to_ast.nodes)) == output + assert __create_source_from_ast(assertion_to_ast.nodes) == output diff --git a/tests/generation/export/test_pytestexporter.py b/tests/generation/export/test_pytestexporter.py index 8cf57ef85..a988aad61 100644 --- a/tests/generation/export/test_pytestexporter.py +++ b/tests/generation/export/test_pytestexporter.py @@ -17,7 +17,6 @@ def test_export_sequence(exportable_test_case, tmp_path): import pytest import tests.fixtures.accessibles.accessible as module_0 - def test_case_0(): int_0 = 5 some_type_0 = module_0.SomeType(int_0) @@ -26,13 +25,11 @@ def test_case_0(): float_1 = module_0.simple_function(float_0) assert float_1 == pytest.approx(42.23, abs=0.01, rel=0.01) - def test_case_1(): int_0 = 5 some_type_0 = module_0.SomeType(int_0) assert some_type_0 == 5 float_0 = 42.23 float_1 = module_0.simple_function(float_0) - assert float_1 == pytest.approx(42.23, abs=0.01, rel=0.01) -""" + assert float_1 == pytest.approx(42.23, abs=0.01, rel=0.01)""" ) diff --git a/tests/testcase/test_statement_to_ast.py b/tests/testcase/test_statement_to_ast.py index 5b5d84081..e6e11da4c 100644 --- a/tests/testcase/test_statement_to_ast.py +++ b/tests/testcase/test_statement_to_ast.py @@ -4,11 +4,10 @@ # # SPDX-License-Identifier: LGPL-3.0-or-later # +import ast import inspect -from ast import Module from unittest.mock import MagicMock -import astor import pytest import pynguin.testcase.statement as stmt @@ -31,30 +30,30 @@ def statement_to_ast_visitor() -> stmt_to_ast.StatementToAstVisitor: return stmt_to_ast.StatementToAstVisitor(module_aliases, var_names) +def __create_source_from_ast(module_body: list[ast.stmt]) -> str: + return ast.unparse( + ast.fix_missing_locations(ast.Module(body=module_body, type_ignores=[])) + ) + + def test_statement_to_ast_int(statement_to_ast_visitor, test_case_mock): int_stmt = stmt.IntPrimitiveStatement(test_case_mock, 5) statement_to_ast_visitor.visit_int_primitive_statement(int_stmt) - assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = 5\n" - ) + assert __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == "var_0 = 5" def test_statement_to_ast_float(statement_to_ast_visitor, test_case_mock): float_stmt = stmt.FloatPrimitiveStatement(test_case_mock, 5.5) statement_to_ast_visitor.visit_float_primitive_statement(float_stmt) - assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = 5.5\n" - ) + assert __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == "var_0 = 5.5" def test_statement_to_ast_str(statement_to_ast_visitor, test_case_mock): str_stmt = stmt.StringPrimitiveStatement(test_case_mock, "TestMe") statement_to_ast_visitor.visit_string_primitive_statement(str_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = 'TestMe'\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "var_0 = 'TestMe'" ) @@ -62,8 +61,8 @@ def test_statement_to_ast_bytes(statement_to_ast_visitor, test_case_mock): bytes_stmt = stmt.BytesPrimitiveStatement(test_case_mock, b"TestMe") statement_to_ast_visitor.visit_bytes_primitive_statement(bytes_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = b'TestMe'\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "var_0 = b'TestMe'" ) @@ -71,8 +70,7 @@ def test_statement_to_ast_bool(statement_to_ast_visitor, test_case_mock): bool_stmt = stmt.BooleanPrimitiveStatement(test_case_mock, True) statement_to_ast_visitor.visit_boolean_primitive_statement(bool_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = True\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == "var_0 = True" ) @@ -80,8 +78,7 @@ def test_statement_to_ast_none(statement_to_ast_visitor, test_case_mock): none_stmt = stmt.NoneStatement(test_case_mock, int) statement_to_ast_visitor.visit_none_statement(none_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = None\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == "var_0 = None" ) @@ -91,8 +88,8 @@ def test_statement_to_ast_enum(statement_to_ast_visitor, test_case_mock): ) statement_to_ast_visitor.visit_enum_statement(enum_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = module_0.Foo.BAR\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "var_0 = module_0.Foo.BAR" ) @@ -107,8 +104,8 @@ def test_statement_to_ast_assignment( ) statement_to_ast_visitor.visit_assignment_statement(assign_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0.foo = var_1\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "var_0.foo = var_1" ) @@ -118,8 +115,8 @@ def test_statement_to_ast_field(statement_to_ast_visitor, test_case_mock): field_stmt = stmt.FieldStatement(test_case_mock, field, string.ret_val) statement_to_ast_visitor.visit_field_statement(field_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = var_1.foo\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "var_0 = var_1.foo" ) @@ -187,26 +184,26 @@ def all_types_function(): @pytest.mark.parametrize( "args,expected", [ - ({}, "var_0 = module_0.Constructor()\n"), + ({}, "var_0 = module_0.Constructor()"), ( {"a": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_0 = module_0.Constructor(var_1)\n", + "var_0 = module_0.Constructor(var_1)", ), ( {"b": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_0 = module_0.Constructor(var_1)\n", + "var_0 = module_0.Constructor(var_1)", ), ( {"c": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_0 = module_0.Constructor(*var_1)\n", + "var_0 = module_0.Constructor(*var_1)", ), ( {"d": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_0 = module_0.Constructor(d=var_1)\n", + "var_0 = module_0.Constructor(d=var_1)", ), ( {"e": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_0 = module_0.Constructor(**var_1)\n", + "var_0 = module_0.Constructor(**var_1)", ), ( { @@ -216,7 +213,7 @@ def all_types_function(): "d": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val, "e": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val, }, - "var_0 = module_0.Constructor(var_1, var_2, *var_3, d=var_4, **var_5)\n", + "var_0 = module_0.Constructor(var_1, var_2, *var_3, d=var_4, **var_5)", ), ], ) @@ -225,32 +222,32 @@ def test_statement_to_ast_constructor_args( ): constr_stmt = stmt.ConstructorStatement(test_case_mock, all_types_constructor, args) statement_to_ast_visitor.visit_constructor_statement(constr_stmt) - assert astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) == expected + assert __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == expected @pytest.mark.parametrize( "args,expected", [ - ({}, "var_1 = var_0.method()\n"), + ({}, "var_1 = var_0.method()"), ( {"a": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_2 = var_0.method(var_1)\n", + "var_2 = var_0.method(var_1)", ), ( {"b": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_2 = var_0.method(var_1)\n", + "var_2 = var_0.method(var_1)", ), ( {"c": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_2 = var_0.method(*var_1)\n", + "var_2 = var_0.method(*var_1)", ), ( {"d": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_2 = var_0.method(d=var_1)\n", + "var_2 = var_0.method(d=var_1)", ), ( {"e": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_2 = var_0.method(**var_1)\n", + "var_2 = var_0.method(**var_1)", ), ( { @@ -260,7 +257,7 @@ def test_statement_to_ast_constructor_args( "d": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val, "e": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val, }, - "var_6 = var_0.method(var_1, var_2, *var_3, d=var_4, **var_5)\n", + "var_6 = var_0.method(var_1, var_2, *var_3, d=var_4, **var_5)", ), ], ) @@ -274,32 +271,32 @@ def test_statement_to_ast_method_args( args, ) statement_to_ast_visitor.visit_method_statement(method_stmt) - assert astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) == expected + assert __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == expected @pytest.mark.parametrize( "args,expected", [ - ({}, "var_0 = module_0.function()\n"), + ({}, "var_0 = module_0.function()"), ( {"a": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_1 = module_0.function(var_0)\n", + "var_1 = module_0.function(var_0)", ), ( {"b": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_1 = module_0.function(var_0)\n", + "var_1 = module_0.function(var_0)", ), ( {"c": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_1 = module_0.function(*var_0)\n", + "var_1 = module_0.function(*var_0)", ), ( {"d": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_1 = module_0.function(d=var_0)\n", + "var_1 = module_0.function(d=var_0)", ), ( {"e": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val}, - "var_1 = module_0.function(**var_0)\n", + "var_1 = module_0.function(**var_0)", ), ( { @@ -309,7 +306,7 @@ def test_statement_to_ast_method_args( "d": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val, "e": stmt.IntPrimitiveStatement(MagicMock(), 3).ret_val, }, - "var_5 = module_0.function(var_0, var_1, *var_2, d=var_3, **var_4)\n", + "var_5 = module_0.function(var_0, var_1, *var_2, d=var_3, **var_4)", ), ], ) @@ -318,7 +315,7 @@ def test_statement_to_ast_function_args( ): func_stmt = stmt.FunctionStatement(test_case_mock, all_types_function, args) statement_to_ast_visitor.visit_function_statement(func_stmt) - assert astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) == expected + assert __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == expected def test_statement_to_ast_with_wrap(test_case_mock): @@ -330,8 +327,8 @@ def test_statement_to_ast_with_wrap(test_case_mock): int_stmt = stmt.IntPrimitiveStatement(test_case_mock, 5) statement_to_ast_visitor.visit_int_primitive_statement(int_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "try:\n var_0 = 5\nexcept BaseException:\n pass\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "try:\n var_0 = 5\nexcept BaseException:\n pass" ) @@ -345,8 +342,8 @@ def test_statement_to_ast_list_single( ) statement_to_ast_visitor.visit_list_statement(list_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = [var_1]\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "var_0 = [var_1]" ) @@ -359,10 +356,7 @@ def test_statement_to_ast_list_empty( [], ) statement_to_ast_visitor.visit_list_statement(list_stmt) - assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = []\n" - ) + assert __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == "var_0 = []" def test_statement_to_ast_set_single( @@ -375,8 +369,8 @@ def test_statement_to_ast_set_single( ) statement_to_ast_visitor.visit_set_statement(set_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_1 = {var_0}\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "var_1 = {var_0}" ) @@ -386,8 +380,7 @@ def test_statement_to_ast_set_empty( set_stmt = stmt.SetStatement(test_case_mock, set[int], []) statement_to_ast_visitor.visit_set_statement(set_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = set()\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == "var_0 = set()" ) @@ -401,8 +394,8 @@ def test_statement_to_ast_tuple_single( ) statement_to_ast_visitor.visit_tuple_statement(tuple_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = var_1,\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "var_0 = (var_1,)" ) @@ -415,10 +408,7 @@ def test_statement_to_ast_tuple_empty( [], ) statement_to_ast_visitor.visit_tuple_statement(tuple_stmt) - assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = ()\n" - ) + assert __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == "var_0 = ()" def test_statement_to_ast_dict_single( @@ -436,8 +426,8 @@ def test_statement_to_ast_dict_single( ) statement_to_ast_visitor.visit_dict_statement(dict_stmt) assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = {var_1: var_2}\n" + __create_source_from_ast(statement_to_ast_visitor.ast_nodes) + == "var_0 = {var_1: var_2}" ) @@ -450,7 +440,4 @@ def test_statement_to_ast_dict_empty( [], ) statement_to_ast_visitor.visit_dict_statement(dict_stmt) - assert ( - astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) - == "var_0 = {}\n" - ) + assert __create_source_from_ast(statement_to_ast_visitor.ast_nodes) == "var_0 = {}" diff --git a/tests/testcase/test_testcase_to_ast_integration.py b/tests/testcase/test_testcase_to_ast_integration.py index 49d09769f..cf55fd589 100644 --- a/tests/testcase/test_testcase_to_ast_integration.py +++ b/tests/testcase/test_testcase_to_ast_integration.py @@ -4,9 +4,9 @@ # # SPDX-License-Identifier: LGPL-3.0-or-later # +import ast from ast import Module -import astor import pytest import pynguin.assertion.assertion as ass @@ -33,8 +33,12 @@ def test_test_case_to_ast_once(simple_test_case): simple_test_case.accept(visitor) simple_test_case.accept(visitor) assert ( - astor.to_source(Module(body=visitor.test_case_asts[0])) - == "int_0 = 5\nsome_type_0 = module_0.SomeType(int_0)\nassert some_type_0 == 3\n" + ast.unparse( + ast.fix_missing_locations( + Module(body=visitor.test_case_asts[0], type_ignores=[]) + ) + ) + == "int_0 = 5\nsome_type_0 = module_0.SomeType(int_0)\nassert some_type_0 == 3" ) @@ -43,12 +47,20 @@ def test_test_case_to_ast_twice(simple_test_case): simple_test_case.accept(visitor) simple_test_case.accept(visitor) assert ( - astor.to_source(Module(body=visitor.test_case_asts[0])) - == "int_0 = 5\nsome_type_0 = module_0.SomeType(int_0)\nassert some_type_0 == 3\n" + ast.unparse( + ast.fix_missing_locations( + Module(body=visitor.test_case_asts[0], type_ignores=[]) + ) + ) + == "int_0 = 5\nsome_type_0 = module_0.SomeType(int_0)\nassert some_type_0 == 3" ) assert ( - astor.to_source(Module(body=visitor.test_case_asts[1])) - == "int_0 = 5\nsome_type_0 = module_0.SomeType(int_0)\nassert some_type_0 == 3\n" + ast.unparse( + ast.fix_missing_locations( + Module(body=visitor.test_case_asts[1], type_ignores=[]) + ) + ) + == "int_0 = 5\nsome_type_0 = module_0.SomeType(int_0)\nassert some_type_0 == 3" )