Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
69 changes: 39 additions & 30 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from astToolkit import Be, Make
from collections.abc import Callable, Iterator
from functools import cache
from pathlib import Path
from tests.dataSamples.Make import allSubclasses
from typing import Any
import ast # pyright: ignore[reportUnusedImport]
Expand Down Expand Up @@ -91,7 +92,7 @@ def beNegativeTestData(request: pytest.FixtureRequest) -> tuple[str, str, str, d

def generateBeAttributeMethodTestData() -> Iterator[tuple[str, str, str, Any, Any, bool]]:
"""Generate test data for Be attribute methods.

Yields
------
identifierClass : str
Expand All @@ -112,181 +113,181 @@ def generateBeAttributeMethodTestData() -> Iterator[tuple[str, str, str, Any, An
# NOTE: For AST objects in positive tests, we use the same object instance (not separate Make calls)
# to ensure proper object identity comparison. For negative tests, we use different objects.
listTestCases: list[tuple[str, str, str, Any, Any, bool]] = []

# alias tests
listTestCases.extend([
("alias", "nameIs", "name", "moduleNorth", "moduleNorth", True),
("alias", "nameIs", "name", "moduleNorth", "moduleSouth", False),
("alias", "asnameIs", "asname", "aliasEast", "aliasEast", True),
("alias", "asnameIs", "asname", "aliasEast", "aliasWest", False),
])

# arg tests
listTestCases.extend([
("arg", "argIs", "arg", "parameterFibonacci", "parameterFibonacci", True),
("arg", "argIs", "arg", "parameterFibonacci", "parameterPrime", False),
])

# Constant tests (primitives can be compared directly)
listTestCases.extend([
("Constant", "valueIs", "value", 233, 233, True),
("Constant", "valueIs", "value", 233, 377, False),
("Constant", "kindIs", "kind", "u", "u", True),
("Constant", "kindIs", "kind", "u", "r", False),
])

# Name tests
listTestCases.extend([
("Name", "idIs", "id", "identifierNorth", "identifierNorth", True),
("Name", "idIs", "id", "identifierNorth", "identifierSouth", False),
])

# ClassDef tests
listTestCases.extend([
("ClassDef", "nameIs", "name", "ClassNorthEast", "ClassNorthEast", True),
("ClassDef", "nameIs", "name", "ClassNorthEast", "ClassSouthWest", False),
])

# FunctionDef tests
listTestCases.extend([
("FunctionDef", "nameIs", "name", "functionNorthward", "functionNorthward", True),
("FunctionDef", "nameIs", "name", "functionNorthward", "functionSouthward", False),
])

# keyword tests
listTestCases.extend([
("keyword", "argIs", "arg", "keywordPrime", "keywordPrime", True),
("keyword", "argIs", "arg", "keywordPrime", "keywordComposite", False),
])

# Attribute tests
listTestCases.extend([
("Attribute", "attrIs", "attr", "attributeEast", "attributeEast", True),
("Attribute", "attrIs", "attr", "attributeEast", "attributeWest", False),
])

# Global tests (list of strings)
listTestCases.extend([
("Global", "namesIs", "names", ["variableAlpha"], ["variableAlpha"], True),
("Global", "namesIs", "names", ["variableAlpha"], ["variableBeta"], False),
])

# Nonlocal tests (list of strings)
listTestCases.extend([
("Nonlocal", "namesIs", "names", ["variableGamma"], ["variableGamma"], True),
("Nonlocal", "namesIs", "names", ["variableGamma"], ["variableDelta"], False),
])

# For AST object attributes, we need to use the same object instance for positive tests
# Return tests
nodeReturnConstant = Make.Constant(89)
listTestCases.extend([
("Return", "valueIs", "value", nodeReturnConstant, nodeReturnConstant, True),
("Return", "valueIs", "value", nodeReturnConstant, Make.Constant(144), False),
])

# Expr tests
nodeExprConstant = Make.Constant(13)
listTestCases.extend([
("Expr", "valueIs", "value", nodeExprConstant, nodeExprConstant, True),
("Expr", "valueIs", "value", nodeExprConstant, Make.Constant(17), False),
])
# Delete tests

# Delete tests
nodeDeleteTarget = Make.Name("targetPrimary")
listTestCases.extend([
("Delete", "targetsIs", "targets", [nodeDeleteTarget], [nodeDeleteTarget], True),
("Delete", "targetsIs", "targets", [nodeDeleteTarget], [Make.Name("targetSecondary")], False),
])

# Import tests
nodeImportAlias = Make.alias("modulePi")
listTestCases.extend([
("Import", "namesIs", "names", [nodeImportAlias], [nodeImportAlias], True),
("Import", "namesIs", "names", [nodeImportAlias], [Make.alias("moduleEuler")], False),
])

# Lambda tests
nodeLambdaBody = Make.Constant(5)
listTestCases.extend([
("Lambda", "bodyIs", "body", nodeLambdaBody, nodeLambdaBody, True),
("Lambda", "bodyIs", "body", nodeLambdaBody, Make.Constant(8), False),
])

# Yield tests
nodeYieldValue = Make.Constant(21)
listTestCases.extend([
("Yield", "valueIs", "value", nodeYieldValue, nodeYieldValue, True),
("Yield", "valueIs", "value", nodeYieldValue, Make.Constant(34), False),
])

# YieldFrom tests
nodeYieldFromValue = Make.Name("generatorNorth")
listTestCases.extend([
("YieldFrom", "valueIs", "value", nodeYieldFromValue, nodeYieldFromValue, True),
("YieldFrom", "valueIs", "value", nodeYieldFromValue, Make.Name("generatorSouth"), False),
])

# NamedExpr tests
nodeNamedExprTarget = Make.Name("walrusAlpha")
listTestCases.extend([
("NamedExpr", "targetIs", "target", nodeNamedExprTarget, nodeNamedExprTarget, True),
("NamedExpr", "targetIs", "target", nodeNamedExprTarget, Make.Name("walrusBeta"), False),
])

# Starred tests
nodeStarredValue = Make.Name("argsCollection")
listTestCases.extend([
("Starred", "valueIs", "value", nodeStarredValue, nodeStarredValue, True),
("Starred", "valueIs", "value", nodeStarredValue, Make.Name("kwargsMapping"), False),
])

# List tests
nodeListElt = Make.Constant(3)
listTestCases.extend([
("List", "eltsIs", "elts", [nodeListElt], [nodeListElt], True),
("List", "eltsIs", "elts", [nodeListElt], [Make.Constant(5)], False),
])

# Set tests
nodeSetElt = Make.Constant(11)
listTestCases.extend([
("Set", "eltsIs", "elts", [nodeSetElt], [nodeSetElt], True),
("Set", "eltsIs", "elts", [nodeSetElt], [Make.Constant(13)], False),
])

# Tuple tests
nodeTupleElt = Make.Constant(7)
listTestCases.extend([
("Tuple", "eltsIs", "elts", [nodeTupleElt], [nodeTupleElt], True),
("Tuple", "eltsIs", "elts", [nodeTupleElt], [Make.Constant(11)], False),
])

# Dict tests
nodeDictKey = Make.Constant("keyAlpha")
listTestCases.extend([
("Dict", "keysIs", "keys", [nodeDictKey], [nodeDictKey], True),
("Dict", "keysIs", "keys", [nodeDictKey], [Make.Constant("keyBeta")], False),
])

yield from listTestCases

@pytest.fixture(
params=list(generateBeAttributeMethodTestData()),
params=list(generateBeAttributeMethodTestData()),
ids=lambda param: f"{param[0]}_{param[1]}_{param[5]}"
)
def beAttributeMethodTestData(request: pytest.FixtureRequest) -> tuple[str, str, str, Any, Any, bool]:
"""Fixture providing Be attribute method test data.

Parameters
----------
request : pytest.FixtureRequest
Pytest request object for the fixture.

Returns
-------
tuple[str, str, str, Any, Any, bool]
Tuple containing identifierClass, nameMethod, nameAttribute, valueAttributeNode,
Tuple containing identifierClass, nameMethod, nameAttribute, valueAttributeNode,
valueAttributeCheck, expectedResult.
"""
return request.param
Expand Down Expand Up @@ -417,3 +418,11 @@ def grabAttributeTestData(request: pytest.FixtureRequest) -> tuple[str, Callable
def grabIndexTestData(request: pytest.FixtureRequest) -> tuple[str, Callable[[], list[ast.AST]], int, Callable[[ast.AST], ast.AST | list[ast.AST] | None], list[str]]:
"""Fixture providing test data for Grab.index method."""
return request.param

# _toolkitAST test fixtures

@pytest.fixture
def pathFilenameSampleModule() -> Path:
"""Fixture providing path to sample module for parsePathFilename2astModule tests."""
pathFilename = Path(__file__).parent / "dataSamples" / "sampleModuleForParsing.py"
return pathFilename
22 changes: 22 additions & 0 deletions tests/dataSamples/sampleModuleForParsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Sample Python module for testing parsePathFilename2astModule."""

def functionAlpha(parameterFirst: int, parameterSecond: str) -> str:
"""A sample function for testing extractFunctionDef."""
resultComputed = f"{parameterFirst}: {parameterSecond}"
return resultComputed

def functionBeta() -> None:
"""Another sample function."""
pass

class ClassGamma:
"""A sample class for testing extractClassDef."""

def methodDelta(self) -> int:
"""A method within the class."""
return 13

class ClassEpsilon:
"""Another sample class."""

attributeZeta: str = "valueTest"
Loading