Skip to content

Commit 720eb68

Browse files
committed
feat: move count_test_cases
1 parent 4d9972a commit 720eb68

File tree

11 files changed

+246
-791
lines changed

11 files changed

+246
-791
lines changed

.amazonq/plans/cli-implementation.md

Lines changed: 0 additions & 543 deletions
This file was deleted.

.amazonq/plans/test-case-integration.md

Lines changed: 0 additions & 74 deletions
This file was deleted.

.amazonq/rules/test-case-enhancement.md renamed to .amazonq/rules/test-quality-assurance.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Test Case Enhancement Rules
1+
# Test Quality Assurance Rules
22

33
## Simple Enhancement Workflow
44

@@ -7,7 +7,7 @@ When user requests test case enhancement or **test reproducibility verification*
77
### 1. Problem Resolution
88

99
- Use active file context or user-provided problem name
10-
- If unclear, run: `poetry run python .templates/check_test_cases.py --threshold=10 --max=1`
10+
- If unclear, run: `poetry run python -m leetcode_py.tools.check_test_cases --threshold=10 --max=1`
1111

1212
### 2. Enhancement Process
1313

@@ -72,7 +72,13 @@ make p-lint PROBLEM={problem_name}
7272

7373
```bash
7474
# Find problems needing enhancement
75-
poetry run python .templates/check_test_cases.py --threshold=10
75+
poetry run python -m leetcode_py.tools.check_test_cases --threshold=10
76+
77+
# Check all problems (no limit)
78+
poetry run python -m leetcode_py.tools.check_test_cases --threshold=10 --max=none
79+
80+
# Check with custom threshold
81+
poetry run python -m leetcode_py.tools.check_test_cases --threshold=12
7682

7783
# Generate from JSON template (uses lcpy internally)
7884
make p-gen PROBLEM={problem_name} FORCE=1

.templates/check_test_cases.py

Lines changed: 0 additions & 95 deletions
This file was deleted.

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Use an LLM assistant (Cursor, GitHub Copilot Chat, Amazon Q) with the rule files
1313

1414
### 2. Enhance Test Cases
1515

16-
- Include `.amazonq/rules/test-case-enhancement.md` in your LLM context
16+
- Include `.amazonq/rules/test-quality-assurance.md` in your LLM context
1717
- Ask: "Enhance test cases for [problem_name] problem"
1818

1919
### 3. Improve Helper Classes

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ nb-to-py:
8484
@find leetcode -name "*.ipynb" -delete
8585
@echo "Conversion complete. All .ipynb files converted to .py and deleted."
8686

87+
# Find problems with few test cases
88+
check-test-cases:
89+
@echo "Checking test case coverage..."
90+
poetry run python leetcode_py/tools/check_test_cases.py --threshold=$(or $(THRESHOLD),10) --max=$(or $(MAX),1)
91+
8792
# Generate All Problems - useful for people who fork this repo
8893
gen-all-problems:
8994
@echo "This will DELETE all existing problems and regenerate from JSON templates."

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ If you need more problems beyond Grind 75, use an LLM assistant in your IDE (Cur
163163
**Required LLM Context**: Include these rule files in your LLM context for automated problem generation and test enhancement:
164164

165165
- [`.amazonq/rules/problem-creation.md`](.amazonq/rules/problem-creation.md) - Complete problem generation workflow
166-
- [`.amazonq/rules/test-case-enhancement.md`](.amazonq/rules/test-case-enhancement.md) - Test enhancement and reproducibility verification
166+
- [`.amazonq/rules/test-quality-assurance.md`](.amazonq/rules/test-quality-assurance.md) - Test enhancement and reproducibility verification
167167

168168
**Manual Check**: Find problems needing more test cases:
169169

leetcode_py/cli/resources/leetcode/gen.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

leetcode_py/cli/resources/leetcode/scrape.py

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
3+
import ast
4+
import json
5+
from typing import Any
6+
7+
import typer
8+
9+
from leetcode_py.cli.utils.resources import get_problems_json_path
10+
11+
12+
def count_test_cases_for_problem(json_data: dict[str, Any]) -> int:
13+
"""Count total test cases across all test methods for a problem."""
14+
total = 0
15+
16+
# Handle both direct test_methods and nested _test_methods.list
17+
test_methods = json_data.get("test_methods", [])
18+
if not test_methods and "_test_methods" in json_data:
19+
test_methods = json_data["_test_methods"].get("list", [])
20+
21+
for method in test_methods:
22+
test_cases = method.get("test_cases", "")
23+
if test_cases.strip():
24+
# Parse Python list literal using ast.literal_eval
25+
cases_list = ast.literal_eval(test_cases)
26+
total += len(cases_list)
27+
return total
28+
29+
30+
def check_test_cases(
31+
threshold: int = typer.Option(
32+
10, "--threshold", "-t", help="Show problems with test cases <= threshold"
33+
),
34+
max_results: str = typer.Option(
35+
"10", "--max", "-m", help="Maximum number of results to show ('none' for no limit)"
36+
),
37+
) -> None:
38+
"""Check test case counts in LeetCode problems."""
39+
problems_dir = get_problems_json_path()
40+
all_problems: list[tuple[str, int]] = []
41+
42+
for problem_file in problems_dir.glob("*.json"):
43+
try:
44+
with open(problem_file) as f:
45+
data = json.load(f)
46+
47+
test_count = count_test_cases_for_problem(data)
48+
all_problems.append((problem_file.name, test_count))
49+
except Exception as e:
50+
typer.echo(f"Error reading problem {problem_file.name}: {e}", err=True)
51+
52+
# Sort by test count
53+
all_problems.sort(key=lambda x: x[1])
54+
55+
# Filter by threshold
56+
filtered_problems = [p for p in all_problems if p[1] <= threshold]
57+
58+
# Apply max results limit
59+
if max_results.lower() not in ["none", "null", "-1"]:
60+
try:
61+
max_count = int(max_results)
62+
if max_count > 0:
63+
filtered_problems = filtered_problems[:max_count]
64+
except ValueError:
65+
typer.echo(f"Invalid max_results value: {max_results}", err=True)
66+
raise typer.Exit(1)
67+
68+
typer.echo(f"Problems with ≤{threshold} test cases ({len(filtered_problems)} total):")
69+
for problem_name, count in filtered_problems:
70+
typer.echo(f"{problem_name}: {count} test cases")
71+
72+
# Exit with non-zero code if any problems found
73+
if filtered_problems:
74+
raise typer.Exit(1)
75+
76+
77+
app = typer.Typer()
78+
app.command()(check_test_cases)
79+
80+
if __name__ == "__main__":
81+
app()

0 commit comments

Comments
 (0)