|
1 | | -# import pytest |
2 | | -# import os |
3 | | -# from spice.analyzers.count_inline_comments import count_inline_comments |
| 1 | +import pytest |
| 2 | +import os |
| 3 | +from spice.analyzers.count_inline_comments import count_inline_comments |
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_inline_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_inline_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_inline_comments |
16 | | -# @pytest.mark.parametrize( |
17 | | -# "filename, expected_inline_comments", |
18 | | -# [ |
19 | | -# # Based on the content of ratio_sample.* files |
20 | | -# ("ratio_sample.py", 2), # `import sys # ...`, `y = 2 # ...` |
21 | | -# ("ratio_sample.js", 2), # `const x = 1; // ...`, `let y = 2; // ...` |
22 | | -# ("ratio_sample.go", 2), # `package main // ...`, `func main() { // ...` |
23 | | -# ("ratio_sample.rb", 3), # `require ... # ...`, `x * 2 # ...`, `puts ... # ...` |
24 | | -# # Based on func_sample.* files |
25 | | -# ("func_sample.py", 0), # No inline comments in this specific sample |
26 | | -# ("func_sample.js", 0), # No inline comments in this specific sample |
27 | | -# ("func_sample.go", 0), # No inline comments in this specific sample |
28 | | -# ("func_sample.rb", 0), # No inline comments in this specific sample |
29 | | -# # Based on original example.* files |
30 | | -# ("example.py", 1), # `print("Hello, Python!") # Output greeting` |
31 | | -# ("example.js", 1), # `console.log("Hello, JavaScript!"); // Output greeting` |
32 | | -# ("example.go", 1), # `fmt.Println("Hello, Go!") // Output greeting` |
33 | | -# ("example.rb", 1), # `puts "Hello, Ruby!" # Output greeting` |
34 | | -# ] |
35 | | -# ) |
36 | | -# def test_count_inline_comments_sample_files(filename, expected_inline_comments): |
37 | | -# """Test count_inline_comments with various sample files.""" |
38 | | -# file_path = os.path.join(SAMPLE_CODE_DIR, filename) |
39 | | -# assert os.path.exists(file_path), f"Sample file not found: {file_path}" |
40 | | -# assert count_inline_comments(file_path) == expected_inline_comments |
| 15 | +# Test cases for count_inline_comments |
| 16 | +@pytest.mark.parametrize( |
| 17 | + "filename, expected_inline_comments", |
| 18 | + [ |
| 19 | + # Based on the content of ratio_sample.* files |
| 20 | + ("ratio_sample.py", 2), # `import sys # ...`, `y = 2 # ...` |
| 21 | + ("ratio_sample.js", 2), # `const x = 1; // ...`, `let y = 2; // ...` |
| 22 | + ("ratio_sample.go", 2), # `package main // ...`, `func main() { // ...` |
| 23 | + ("ratio_sample.rb", 3), # `require ... # ...`, `x * 2 # ...`, `puts ... # ...` |
| 24 | + # Based on func_sample.* files |
| 25 | + ("func_sample.py", 0), # No inline comments in this specific sample |
| 26 | + ("func_sample.js", 0), # No inline comments in this specific sample |
| 27 | + ("func_sample.go", 0), # No inline comments in this specific sample |
| 28 | + ("func_sample.rb", 0), # No inline comments in this specific sample |
| 29 | + # Based on original example.* files |
| 30 | + ("example.py", 1), # `print("Hello, Python!") # Output greeting` |
| 31 | + ("example.js", 1), # `console.log("Hello, JavaScript!"); // Output greeting` |
| 32 | + ("example.go", 1), # `fmt.Println("Hello, Go!") // Output greeting` |
| 33 | + ("example.rb", 1), # `puts "Hello, Ruby!" # Output greeting` |
| 34 | + ] |
| 35 | +) |
| 36 | +def test_count_inline_comments_sample_files(filename, expected_inline_comments): |
| 37 | + """Test count_inline_comments with various sample files.""" |
| 38 | + file_path = os.path.join(SAMPLE_CODE_DIR, filename) |
| 39 | + assert os.path.exists(file_path), f"Sample file not found: {file_path}" |
| 40 | + assert count_inline_comments(file_path) == expected_inline_comments |
41 | 41 |
|
42 | | -# def test_count_inline_comments_empty_file(): |
43 | | -# """Test count_inline_comments with an empty file.""" |
44 | | -# empty_file_path = create_temp_file("", "empty_inline.tmp") |
45 | | -# assert count_inline_comments(empty_file_path) == 0 |
46 | | -# os.remove(empty_file_path) |
| 42 | +def test_count_inline_comments_empty_file(): |
| 43 | + """Test count_inline_comments with an empty file.""" |
| 44 | + empty_file_path = create_temp_file("", "empty_inline.tmp") |
| 45 | + assert count_inline_comments(empty_file_path) == 0 |
| 46 | + os.remove(empty_file_path) |
47 | 47 |
|
48 | | -# def test_count_inline_comments_no_comments(): |
49 | | -# """Test count_inline_comments with a file containing no comments.""" |
50 | | -# no_comments_path = create_temp_file("print(\"Hello\")\nx = 1", "no_comments_inline.py") |
51 | | -# assert count_inline_comments(no_comments_path) == 0 |
52 | | -# os.remove(no_comments_path) |
| 48 | +def test_count_inline_comments_no_comments(): |
| 49 | + """Test count_inline_comments with a file containing no comments.""" |
| 50 | + no_comments_path = create_temp_file("print(\"Hello\")\nx = 1", "no_comments_inline.py") |
| 51 | + assert count_inline_comments(no_comments_path) == 0 |
| 52 | + os.remove(no_comments_path) |
53 | 53 |
|
54 | | -# def test_count_inline_comments_only_full_line(): |
55 | | -# """Test count_inline_comments with only full-line comments.""" |
56 | | -# full_line_comments_path = create_temp_file("# line 1\n# line 2", "full_line_inline.py") |
57 | | -# assert count_inline_comments(full_line_comments_path) == 0 |
58 | | -# os.remove(full_line_comments_path) |
| 54 | +def test_count_inline_comments_only_full_line(): |
| 55 | + """Test count_inline_comments with only full-line comments.""" |
| 56 | + full_line_comments_path = create_temp_file("# line 1\n# line 2", "full_line_inline.py") |
| 57 | + assert count_inline_comments(full_line_comments_path) == 0 |
| 58 | + os.remove(full_line_comments_path) |
59 | 59 |
|
60 | | -# def test_count_inline_comments_mixed(): |
61 | | -# """Test count_inline_comments with mixed comment types.""" |
62 | | -# mixed_path = create_temp_file("# full line\nx = 1 # inline\n# another full line\ny=2", "mixed_inline.py") |
63 | | -# assert count_inline_comments(mixed_path) == 1 |
64 | | -# os.remove(mixed_path) |
| 60 | +def test_count_inline_comments_mixed(): |
| 61 | + """Test count_inline_comments with mixed comment types.""" |
| 62 | + mixed_path = create_temp_file("# full line\nx = 1 # inline\n# another full line\ny=2", "mixed_inline.py") |
| 63 | + assert count_inline_comments(mixed_path) == 1 |
| 64 | + os.remove(mixed_path) |
65 | 65 |
|
66 | | -# def test_count_inline_comments_unsupported_extension(): |
67 | | -# """Test count_inline_comments with an unsupported file extension.""" |
68 | | -# unsupported_path = create_temp_file("code # inline comment", "unsupported_inline.txt") |
69 | | -# # Should raise ValueError because lexer cannot be found |
70 | | -# with pytest.raises(ValueError): |
71 | | -# count_inline_comments(unsupported_path) |
72 | | -# os.remove(unsupported_path) |
| 66 | +def test_count_inline_comments_unsupported_extension(): |
| 67 | + """Test count_inline_comments with an unsupported file extension.""" |
| 68 | + unsupported_path = create_temp_file("code # inline comment", "unsupported_inline.txt") |
| 69 | + # Should raise ValueError because lexer cannot be found |
| 70 | + with pytest.raises(ValueError): |
| 71 | + count_inline_comments(unsupported_path) |
| 72 | + os.remove(unsupported_path) |
73 | 73 |
|
74 | 74 |
|
0 commit comments