Skip to content

Commit 23dc414

Browse files
authored
Merge pull request #20 from hunterhogan:copilot/add-tests-for-toolkitast
Add test coverage for _toolkitAST module
2 parents 1ff4074 + 13a66f7 commit 23dc414

3 files changed

Lines changed: 278 additions & 30 deletions

File tree

tests/conftest.py

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from astToolkit import Be, Make
44
from collections.abc import Callable, Iterator
55
from functools import cache
6+
from pathlib import Path
67
from tests.dataSamples.Make import allSubclasses
78
from typing import Any
89
import ast # pyright: ignore[reportUnusedImport]
@@ -91,7 +92,7 @@ def beNegativeTestData(request: pytest.FixtureRequest) -> tuple[str, str, str, d
9192

9293
def generateBeAttributeMethodTestData() -> Iterator[tuple[str, str, str, Any, Any, bool]]:
9394
"""Generate test data for Be attribute methods.
94-
95+
9596
Yields
9697
------
9798
identifierClass : str
@@ -112,181 +113,181 @@ def generateBeAttributeMethodTestData() -> Iterator[tuple[str, str, str, Any, An
112113
# NOTE: For AST objects in positive tests, we use the same object instance (not separate Make calls)
113114
# to ensure proper object identity comparison. For negative tests, we use different objects.
114115
listTestCases: list[tuple[str, str, str, Any, Any, bool]] = []
115-
116+
116117
# alias tests
117118
listTestCases.extend([
118119
("alias", "nameIs", "name", "moduleNorth", "moduleNorth", True),
119120
("alias", "nameIs", "name", "moduleNorth", "moduleSouth", False),
120121
("alias", "asnameIs", "asname", "aliasEast", "aliasEast", True),
121122
("alias", "asnameIs", "asname", "aliasEast", "aliasWest", False),
122123
])
123-
124+
124125
# arg tests
125126
listTestCases.extend([
126127
("arg", "argIs", "arg", "parameterFibonacci", "parameterFibonacci", True),
127128
("arg", "argIs", "arg", "parameterFibonacci", "parameterPrime", False),
128129
])
129-
130+
130131
# Constant tests (primitives can be compared directly)
131132
listTestCases.extend([
132133
("Constant", "valueIs", "value", 233, 233, True),
133134
("Constant", "valueIs", "value", 233, 377, False),
134135
("Constant", "kindIs", "kind", "u", "u", True),
135136
("Constant", "kindIs", "kind", "u", "r", False),
136137
])
137-
138+
138139
# Name tests
139140
listTestCases.extend([
140141
("Name", "idIs", "id", "identifierNorth", "identifierNorth", True),
141142
("Name", "idIs", "id", "identifierNorth", "identifierSouth", False),
142143
])
143-
144+
144145
# ClassDef tests
145146
listTestCases.extend([
146147
("ClassDef", "nameIs", "name", "ClassNorthEast", "ClassNorthEast", True),
147148
("ClassDef", "nameIs", "name", "ClassNorthEast", "ClassSouthWest", False),
148149
])
149-
150+
150151
# FunctionDef tests
151152
listTestCases.extend([
152153
("FunctionDef", "nameIs", "name", "functionNorthward", "functionNorthward", True),
153154
("FunctionDef", "nameIs", "name", "functionNorthward", "functionSouthward", False),
154155
])
155-
156+
156157
# keyword tests
157158
listTestCases.extend([
158159
("keyword", "argIs", "arg", "keywordPrime", "keywordPrime", True),
159160
("keyword", "argIs", "arg", "keywordPrime", "keywordComposite", False),
160161
])
161-
162+
162163
# Attribute tests
163164
listTestCases.extend([
164165
("Attribute", "attrIs", "attr", "attributeEast", "attributeEast", True),
165166
("Attribute", "attrIs", "attr", "attributeEast", "attributeWest", False),
166167
])
167-
168+
168169
# Global tests (list of strings)
169170
listTestCases.extend([
170171
("Global", "namesIs", "names", ["variableAlpha"], ["variableAlpha"], True),
171172
("Global", "namesIs", "names", ["variableAlpha"], ["variableBeta"], False),
172173
])
173-
174+
174175
# Nonlocal tests (list of strings)
175176
listTestCases.extend([
176177
("Nonlocal", "namesIs", "names", ["variableGamma"], ["variableGamma"], True),
177178
("Nonlocal", "namesIs", "names", ["variableGamma"], ["variableDelta"], False),
178179
])
179-
180+
180181
# For AST object attributes, we need to use the same object instance for positive tests
181182
# Return tests
182183
nodeReturnConstant = Make.Constant(89)
183184
listTestCases.extend([
184185
("Return", "valueIs", "value", nodeReturnConstant, nodeReturnConstant, True),
185186
("Return", "valueIs", "value", nodeReturnConstant, Make.Constant(144), False),
186187
])
187-
188+
188189
# Expr tests
189190
nodeExprConstant = Make.Constant(13)
190191
listTestCases.extend([
191192
("Expr", "valueIs", "value", nodeExprConstant, nodeExprConstant, True),
192193
("Expr", "valueIs", "value", nodeExprConstant, Make.Constant(17), False),
193194
])
194-
195-
# Delete tests
195+
196+
# Delete tests
196197
nodeDeleteTarget = Make.Name("targetPrimary")
197198
listTestCases.extend([
198199
("Delete", "targetsIs", "targets", [nodeDeleteTarget], [nodeDeleteTarget], True),
199200
("Delete", "targetsIs", "targets", [nodeDeleteTarget], [Make.Name("targetSecondary")], False),
200201
])
201-
202+
202203
# Import tests
203204
nodeImportAlias = Make.alias("modulePi")
204205
listTestCases.extend([
205206
("Import", "namesIs", "names", [nodeImportAlias], [nodeImportAlias], True),
206207
("Import", "namesIs", "names", [nodeImportAlias], [Make.alias("moduleEuler")], False),
207208
])
208-
209+
209210
# Lambda tests
210211
nodeLambdaBody = Make.Constant(5)
211212
listTestCases.extend([
212213
("Lambda", "bodyIs", "body", nodeLambdaBody, nodeLambdaBody, True),
213214
("Lambda", "bodyIs", "body", nodeLambdaBody, Make.Constant(8), False),
214215
])
215-
216+
216217
# Yield tests
217218
nodeYieldValue = Make.Constant(21)
218219
listTestCases.extend([
219220
("Yield", "valueIs", "value", nodeYieldValue, nodeYieldValue, True),
220221
("Yield", "valueIs", "value", nodeYieldValue, Make.Constant(34), False),
221222
])
222-
223+
223224
# YieldFrom tests
224225
nodeYieldFromValue = Make.Name("generatorNorth")
225226
listTestCases.extend([
226227
("YieldFrom", "valueIs", "value", nodeYieldFromValue, nodeYieldFromValue, True),
227228
("YieldFrom", "valueIs", "value", nodeYieldFromValue, Make.Name("generatorSouth"), False),
228229
])
229-
230+
230231
# NamedExpr tests
231232
nodeNamedExprTarget = Make.Name("walrusAlpha")
232233
listTestCases.extend([
233234
("NamedExpr", "targetIs", "target", nodeNamedExprTarget, nodeNamedExprTarget, True),
234235
("NamedExpr", "targetIs", "target", nodeNamedExprTarget, Make.Name("walrusBeta"), False),
235236
])
236-
237+
237238
# Starred tests
238239
nodeStarredValue = Make.Name("argsCollection")
239240
listTestCases.extend([
240241
("Starred", "valueIs", "value", nodeStarredValue, nodeStarredValue, True),
241242
("Starred", "valueIs", "value", nodeStarredValue, Make.Name("kwargsMapping"), False),
242243
])
243-
244+
244245
# List tests
245246
nodeListElt = Make.Constant(3)
246247
listTestCases.extend([
247248
("List", "eltsIs", "elts", [nodeListElt], [nodeListElt], True),
248249
("List", "eltsIs", "elts", [nodeListElt], [Make.Constant(5)], False),
249250
])
250-
251+
251252
# Set tests
252253
nodeSetElt = Make.Constant(11)
253254
listTestCases.extend([
254255
("Set", "eltsIs", "elts", [nodeSetElt], [nodeSetElt], True),
255256
("Set", "eltsIs", "elts", [nodeSetElt], [Make.Constant(13)], False),
256257
])
257-
258+
258259
# Tuple tests
259260
nodeTupleElt = Make.Constant(7)
260261
listTestCases.extend([
261262
("Tuple", "eltsIs", "elts", [nodeTupleElt], [nodeTupleElt], True),
262263
("Tuple", "eltsIs", "elts", [nodeTupleElt], [Make.Constant(11)], False),
263264
])
264-
265+
265266
# Dict tests
266267
nodeDictKey = Make.Constant("keyAlpha")
267268
listTestCases.extend([
268269
("Dict", "keysIs", "keys", [nodeDictKey], [nodeDictKey], True),
269270
("Dict", "keysIs", "keys", [nodeDictKey], [Make.Constant("keyBeta")], False),
270271
])
271-
272+
272273
yield from listTestCases
273274

274275
@pytest.fixture(
275-
params=list(generateBeAttributeMethodTestData()),
276+
params=list(generateBeAttributeMethodTestData()),
276277
ids=lambda param: f"{param[0]}_{param[1]}_{param[5]}"
277278
)
278279
def beAttributeMethodTestData(request: pytest.FixtureRequest) -> tuple[str, str, str, Any, Any, bool]:
279280
"""Fixture providing Be attribute method test data.
280-
281+
281282
Parameters
282283
----------
283284
request : pytest.FixtureRequest
284285
Pytest request object for the fixture.
285-
286+
286287
Returns
287288
-------
288289
tuple[str, str, str, Any, Any, bool]
289-
Tuple containing identifierClass, nameMethod, nameAttribute, valueAttributeNode,
290+
Tuple containing identifierClass, nameMethod, nameAttribute, valueAttributeNode,
290291
valueAttributeCheck, expectedResult.
291292
"""
292293
return request.param
@@ -417,3 +418,11 @@ def grabAttributeTestData(request: pytest.FixtureRequest) -> tuple[str, Callable
417418
def grabIndexTestData(request: pytest.FixtureRequest) -> tuple[str, Callable[[], list[ast.AST]], int, Callable[[ast.AST], ast.AST | list[ast.AST] | None], list[str]]:
418419
"""Fixture providing test data for Grab.index method."""
419420
return request.param
421+
422+
# _toolkitAST test fixtures
423+
424+
@pytest.fixture
425+
def pathFilenameSampleModule() -> Path:
426+
"""Fixture providing path to sample module for parsePathFilename2astModule tests."""
427+
pathFilename = Path(__file__).parent / "dataSamples" / "sampleModuleForParsing.py"
428+
return pathFilename
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Sample Python module for testing parsePathFilename2astModule."""
2+
3+
def functionAlpha(parameterFirst: int, parameterSecond: str) -> str:
4+
"""A sample function for testing extractFunctionDef."""
5+
resultComputed = f"{parameterFirst}: {parameterSecond}"
6+
return resultComputed
7+
8+
def functionBeta() -> None:
9+
"""Another sample function."""
10+
pass
11+
12+
class ClassGamma:
13+
"""A sample class for testing extractClassDef."""
14+
15+
def methodDelta(self) -> int:
16+
"""A method within the class."""
17+
return 13
18+
19+
class ClassEpsilon:
20+
"""Another sample class."""
21+
22+
attributeZeta: str = "valueTest"

0 commit comments

Comments
 (0)