|
2 | 2 | import os |
3 | 3 | from typer.testing import CliRunner |
4 | 4 | from cli.main import app |
| 5 | +from cli.commands.analyze import analyze_to_json |
5 | 6 |
|
6 | 7 | # Setup test runner |
7 | 8 | runner = CliRunner() |
8 | 9 |
|
9 | 10 | # Get the absolute path to the sample file |
10 | 11 | SAMPLE_FILE_PATH = os.path.join(os.path.dirname(__file__), "..", "sample-code", "example.py") |
11 | 12 |
|
| 13 | +# Get path to the sample files |
| 14 | +SAMPLES_DIR = os.path.join(os.path.dirname(__file__), "..", "sample-code") |
| 15 | +PY_SAMPLE = os.path.join(SAMPLES_DIR, "example.py") |
| 16 | + |
12 | 17 | def test_analyze_command_with_json_flag(): |
13 | 18 | """Test the analyze command with the --json flag for Python""" |
14 | 19 | # Run the command with --json flag |
@@ -61,3 +66,35 @@ def test_analyze_command_with_nonexistent_file(): |
61 | 66 |
|
62 | 67 | # Check if the output contains an error message |
63 | 68 | assert "error" in output |
| 69 | + |
| 70 | +def test_analyze_json_python(): |
| 71 | + result = analyze_to_json(PY_SAMPLE, ["line_count", "function_count", "comment_line_count", "blank_line_count", "inline_comment_count", "external_dependencies_count"]) |
| 72 | + |
| 73 | + json_obj = json.loads(result) |
| 74 | + assert isinstance(json_obj, dict) |
| 75 | + assert PY_SAMPLE in json_obj |
| 76 | + |
| 77 | + stats = json_obj[PY_SAMPLE] |
| 78 | + assert isinstance(stats, dict) |
| 79 | + assert "line_count" in stats |
| 80 | + assert "function_count" in stats |
| 81 | + assert "comment_line_count" in stats |
| 82 | + assert "blank_line_count" in stats |
| 83 | + assert "inline_comment_count" in stats |
| 84 | + assert "external_dependencies_count" in stats |
| 85 | + |
| 86 | + assert isinstance(stats["line_count"], int) |
| 87 | + assert isinstance(stats["function_count"], int) |
| 88 | + assert isinstance(stats["comment_line_count"], int) |
| 89 | + assert isinstance(stats["blank_line_count"], int) |
| 90 | + assert isinstance(stats["inline_comment_count"], int) |
| 91 | + assert isinstance(stats["external_dependencies_count"], int) |
| 92 | + |
| 93 | + assert stats["line_count"] > 0 |
| 94 | + assert stats["function_count"] >= 0 |
| 95 | + assert stats["comment_line_count"] >= 0 |
| 96 | + assert stats["blank_line_count"] >= 0 |
| 97 | + assert stats["inline_comment_count"] == 2 |
| 98 | + # This assertion depends on the content of example.py |
| 99 | + # If the sample file has external dependencies, this should be adjusted accordingly |
| 100 | + assert isinstance(stats["external_dependencies_count"], int) |
0 commit comments