|
1 | | -# import pytest |
2 | | -# import os |
3 | | -# from spice.analyzers.count_comment_ratio import count_comment_ratio |
| 1 | +import pytest |
| 2 | +import os |
| 3 | +from spice.analyzers.count_comment_ratio import count_comment_ratio |
4 | 4 |
|
5 | | -# # Define the path to the sample code directory relative to the test file |
6 | | -# SAMPLE_CODE_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "sample-code") |
| 5 | +# Define the path to the sample code directory relative to the test file |
| 6 | +SAMPLE_CODE_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "sample-code") |
7 | 7 |
|
8 | | -# # Helper function to create a temporary file |
9 | | -# def create_temp_file(content, filename="temp_test_file"): |
10 | | -# file_path = os.path.join(SAMPLE_CODE_DIR, filename) |
11 | | -# with open(file_path, "w", encoding="utf-8") as f: |
12 | | -# f.write(content) |
13 | | -# return file_path |
| 8 | +# Helper function to create a temporary file |
| 9 | +def create_temp_file(content, filename="temp_test_file"): |
| 10 | + file_path = os.path.join(SAMPLE_CODE_DIR, filename) |
| 11 | + with open(file_path, "w", encoding="utf-8") as f: |
| 12 | + f.write(content) |
| 13 | + return file_path |
14 | 14 |
|
15 | | -# # Test cases for count_comment_ratio |
16 | | -# @pytest.mark.parametrize( |
17 | | -# "filename, expected_ratio_str", |
18 | | -# [ |
19 | | -# # Based on the content of sample files created earlier |
20 | | -# # ratio_sample.py: 5 comment lines (3 full, 2 inline) / 7 non-empty code lines = 71.43% |
21 | | -# ("ratio_sample.py", "71.43%"), |
22 | | -# # ratio_sample.js: 5 comment lines (2 full, 2 multi, 1 inline) / 6 non-empty code lines = 83.33% |
23 | | -# ("ratio_sample.js", "83.33%"), |
24 | | -# # ratio_sample.go: 5 comment lines (2 full, 2 multi, 1 inline) / 7 non-empty code lines = 71.43% |
25 | | -# ("ratio_sample.go", "71.43%"), |
26 | | -# # ratio_sample.rb: 4 comment lines (3 full, 1 inline) / 6 non-empty code lines = 66.67% (Note: =begin/=end ignored by current analyzer) |
27 | | -# ("ratio_sample.rb", "66.67%"), |
28 | | -# ] |
29 | | -# ) |
30 | | -# def test_count_comment_ratio_sample_files(filename, expected_ratio_str): |
31 | | -# """Test count_comment_ratio with various sample files.""" |
32 | | -# file_path = os.path.join(SAMPLE_CODE_DIR, filename) |
33 | | -# assert os.path.exists(file_path), f"Sample file not found: {file_path}" |
34 | | -# assert count_comment_ratio(file_path) == expected_ratio_str |
| 15 | +# Test cases for count_comment_ratio |
| 16 | +@pytest.mark.parametrize( |
| 17 | + "filename, expected_ratio_str", |
| 18 | + [ |
| 19 | + # Based on the content of sample files created earlier |
| 20 | + # ratio_sample.py: 5 comment lines (3 full, 2 inline) / 7 non-empty code lines = 71.43% |
| 21 | + ("ratio_sample.py", "71.43%"), |
| 22 | + # ratio_sample.js: 5 comment lines (2 full, 2 multi, 1 inline) / 6 non-empty code lines = 83.33% |
| 23 | + ("ratio_sample.js", "83.33%"), |
| 24 | + # ratio_sample.go: 5 comment lines (2 full, 2 multi, 1 inline) / 7 non-empty code lines = 71.43% |
| 25 | + ("ratio_sample.go", "71.43%"), |
| 26 | + # ratio_sample.rb: 4 comment lines (3 full, 1 inline) / 6 non-empty code lines = 66.67% (Note: =begin/=end ignored by current analyzer) |
| 27 | + ("ratio_sample.rb", "66.67%"), |
| 28 | + ] |
| 29 | +) |
| 30 | +def test_count_comment_ratio_sample_files(filename, expected_ratio_str): |
| 31 | + """Test count_comment_ratio with various sample files.""" |
| 32 | + file_path = os.path.join(SAMPLE_CODE_DIR, filename) |
| 33 | + assert os.path.exists(file_path), f"Sample file not found: {file_path}" |
| 34 | + assert count_comment_ratio(file_path) == expected_ratio_str |
35 | 35 |
|
36 | | -# def test_count_comment_ratio_empty_file(): |
37 | | -# """Test count_comment_ratio with an empty file.""" |
38 | | -# empty_file_path = create_temp_file("", "empty_ratio.tmp") |
39 | | -# assert count_comment_ratio(empty_file_path) == "0.00%" |
40 | | -# os.remove(empty_file_path) |
| 36 | +def test_count_comment_ratio_empty_file(): |
| 37 | + """Test count_comment_ratio with an empty file.""" |
| 38 | + empty_file_path = create_temp_file("", "empty_ratio.tmp") |
| 39 | + assert count_comment_ratio(empty_file_path) == "0.00%" |
| 40 | + os.remove(empty_file_path) |
41 | 41 |
|
42 | | -# def test_count_comment_ratio_no_comments(): |
43 | | -# """Test count_comment_ratio with a file containing no comments.""" |
44 | | -# no_comments_path = create_temp_file("print(\"Hello\")\nx = 1", "no_comments_ratio.py") |
45 | | -# assert count_comment_ratio(no_comments_path) == "0.00%" |
46 | | -# os.remove(no_comments_path) |
| 42 | +def test_count_comment_ratio_no_comments(): |
| 43 | + """Test count_comment_ratio with a file containing no comments.""" |
| 44 | + no_comments_path = create_temp_file("print(\"Hello\")\nx = 1", "no_comments_ratio.py") |
| 45 | + assert count_comment_ratio(no_comments_path) == "0.00%" |
| 46 | + os.remove(no_comments_path) |
47 | 47 |
|
48 | | -# def test_count_comment_ratio_all_comments(): |
49 | | -# """Test count_comment_ratio with a file containing only comments.""" |
50 | | -# all_comments_py = create_temp_file("# line 1\n# line 2", "all_comments_ratio.py") |
51 | | -# assert count_comment_ratio(all_comments_py) == "100.00%" |
52 | | -# os.remove(all_comments_py) |
| 48 | +def test_count_comment_ratio_all_comments(): |
| 49 | + """Test count_comment_ratio with a file containing only comments.""" |
| 50 | + all_comments_py = create_temp_file("# line 1\n# line 2", "all_comments_ratio.py") |
| 51 | + assert count_comment_ratio(all_comments_py) == "100.00%" |
| 52 | + os.remove(all_comments_py) |
53 | 53 |
|
54 | | -# all_comments_js = create_temp_file("// line 1\n/* line 2 */", "all_comments_ratio.js") |
55 | | -# assert count_comment_ratio(all_comments_js) == "100.00%" |
56 | | -# os.remove(all_comments_js) |
| 54 | + all_comments_js = create_temp_file("// line 1\n/* line 2 */", "all_comments_ratio.js") |
| 55 | + assert count_comment_ratio(all_comments_js) == "100.00%" |
| 56 | + os.remove(all_comments_js) |
57 | 57 |
|
58 | | -# def test_count_comment_ratio_unsupported_extension(): |
59 | | -# """Test count_comment_ratio with an unsupported file extension.""" |
60 | | -# unsupported_path = create_temp_file("# comment\ncode", "unsupported.txt") |
61 | | -# assert count_comment_ratio(unsupported_path) == "0.00%" # Should ignore the file |
62 | | -# os.remove(unsupported_path) |
| 58 | +def test_count_comment_ratio_unsupported_extension(): |
| 59 | + """Test count_comment_ratio with an unsupported file extension.""" |
| 60 | + unsupported_path = create_temp_file("# comment\ncode", "unsupported.txt") |
| 61 | + assert count_comment_ratio(unsupported_path) == "0.00%" # Should ignore the file |
| 62 | + os.remove(unsupported_path) |
63 | 63 |
|
64 | | -# def test_count_comment_ratio_directory(): |
65 | | -# """Test count_comment_ratio when given a directory path.""" |
66 | | -# # It should analyze all supported files within the directory |
67 | | -# # Using SAMPLE_CODE_DIR which contains ratio_sample.* files |
68 | | -# # Total comments = 5(py) + 5(js) + 5(go) + 4(rb) = 19 |
69 | | -# # Total lines = 7(py) + 6(js) + 7(go) + 6(rb) = 26 |
70 | | -# # Ratio = (19 / 26) * 100 = 73.08% |
71 | | -# # Note: This depends on the exact content and assumes no other supported files exist there |
72 | | -# # We might need a dedicated test directory for more reliable results |
73 | | -# # For now, let's test based on the known sample files |
74 | | -# # Re-calculate based ONLY on the ratio_sample files created: |
75 | | -# # Py: 5 comments / 7 lines |
76 | | -# # JS: 5 comments / 6 lines |
77 | | -# # Go: 5 comments / 7 lines |
78 | | -# # Rb: 4 comments / 6 lines |
79 | | -# # Total comments = 19, Total lines = 26 |
80 | | -# # Ratio = 19 / 26 * 100 = 73.076... => 73.08% |
81 | | -# assert count_comment_ratio(SAMPLE_CODE_DIR) == "73.08%" |
| 64 | +def test_count_comment_ratio_directory(): |
| 65 | + """Test count_comment_ratio when given a directory path.""" |
| 66 | + # It should analyze all supported files within the directory |
| 67 | + # Using SAMPLE_CODE_DIR which contains ratio_sample.* files |
| 68 | + # Total comments = 5(py) + 5(js) + 5(go) + 4(rb) = 19 |
| 69 | + # Total lines = 7(py) + 6(js) + 7(go) + 6(rb) = 26 |
| 70 | + # Ratio = (19 / 26) * 100 = 73.08% |
| 71 | + # Note: This depends on the exact content and assumes no other supported files exist there |
| 72 | + # We might need a dedicated test directory for more reliable results |
| 73 | + # For now, let's test based on the known sample files |
| 74 | + # Re-calculate based ONLY on the ratio_sample files created: |
| 75 | + # Py: 5 comments / 7 lines |
| 76 | + # JS: 5 comments / 6 lines |
| 77 | + # Go: 5 comments / 7 lines |
| 78 | + # Rb: 4 comments / 6 lines |
| 79 | + # Total comments = 19, Total lines = 26 |
| 80 | + # Ratio = 19 / 26 * 100 = 73.076... => 73.08% |
| 81 | + assert count_comment_ratio(SAMPLE_CODE_DIR) == "73.08%" |
82 | 82 |
|
83 | 83 |
|
0 commit comments