Skip to content

Commit 1e9e3a1

Browse files
authored
Merge pull request #139 from spicecode-cli/inline-comment-tests
Inline comment tests
2 parents bdef661 + 0788767 commit 1e9e3a1

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

tests/analyze/test_analyze_json_go.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ def test_analyze_command_with_json_flag():
2525
assert "line_count" in output
2626
assert "comment_line_count" in output
2727
assert "function_count" in output
28+
assert "inline_comment_count" in output
2829

2930
# Verify the values match expected results
3031
assert output["file_name"] == os.path.basename(SAMPLE_FILE_PATH)
3132
assert output["line_count"] == 195
3233
assert output["comment_line_count"] == 33
3334
assert output["function_count"] == 15
35+
assert output["inline_comment_count"] == 1
3436

3537
def test_analyze_command_with_all_and_json_flags():
3638
"""Test the analyze command with both --all and --json flags for Go"""
@@ -47,6 +49,7 @@ def test_analyze_command_with_all_and_json_flags():
4749
assert output["line_count"] == 195
4850
assert output["comment_line_count"] == 33
4951
assert output["function_count"] == 15
52+
assert output["inline_comment_count"] == 1
5053

5154
def test_analyze_command_with_nonexistent_file():
5255
"""Test the analyze command with a nonexistent file"""

tests/analyze/test_analyze_json_javascript.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ def test_analyze_command_with_json_flag():
2525
assert "line_count" in output
2626
assert "comment_line_count" in output
2727
assert "function_count" in output
28+
assert "inline_comment_count" in output
2829

2930
# Verify the values match expected results
3031
assert output["file_name"] == os.path.basename(SAMPLE_FILE_PATH)
3132
assert output["line_count"] == 153
3233
assert output["comment_line_count"] == 21
3334
assert output["function_count"] == 18
35+
assert output["inline_comment_count"] == 2
3436

3537
def test_analyze_command_with_all_and_json_flags():
3638
"""Test the analyze command with both --all and --json flags for JavaScript"""
@@ -47,6 +49,7 @@ def test_analyze_command_with_all_and_json_flags():
4749
assert output["line_count"] == 153
4850
assert output["comment_line_count"] == 21
4951
assert output["function_count"] == 18
52+
assert output["inline_comment_count"] == 2
5053

5154
def test_analyze_command_with_nonexistent_file():
5255
"""Test the analyze command with a nonexistent file"""

tests/analyze/test_analyze_json_python.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ def test_analyze_command_with_json_flag():
2525
assert "line_count" in output
2626
assert "comment_line_count" in output
2727
assert "function_count" in output
28+
assert "inline_comment_count" in output
2829

2930
# Verify the values match expected results
3031
assert output["file_name"] == os.path.basename(SAMPLE_FILE_PATH)
3132
assert output["line_count"] == 161
3233
assert output["comment_line_count"] == 25
3334
assert output["function_count"] == 17
35+
assert output["inline_comment_count"] == 2
3436

3537
def test_analyze_command_with_all_and_json_flags():
3638
"""Test the analyze command with both --all and --json flags for Python"""
@@ -47,6 +49,7 @@ def test_analyze_command_with_all_and_json_flags():
4749
assert output["line_count"] == 161
4850
assert output["comment_line_count"] == 25
4951
assert output["function_count"] == 17
52+
assert output["inline_comment_count"] == 2
5053

5154
def test_analyze_command_with_nonexistent_file():
5255
"""Test the analyze command with a nonexistent file"""

tests/analyze/test_analyze_json_ruby.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ def test_analyze_command_with_json_flag():
2525
assert "line_count" in output
2626
assert "comment_line_count" in output
2727
assert "function_count" in output
28+
assert "inline_comment_count" in output
2829

2930
# Verify the values match expected results
3031
assert output["file_name"] == os.path.basename(SAMPLE_FILE_PATH)
3132
assert output["line_count"] == 226
3233
assert output["comment_line_count"] == 31
3334
assert output["function_count"] == 29
35+
assert output["inline_comment_count"] == 1
3436

3537
def test_analyze_command_with_all_and_json_flags():
3638
"""Test the analyze command with both --all and --json flags for Ruby"""
@@ -47,6 +49,7 @@ def test_analyze_command_with_all_and_json_flags():
4749
assert output["line_count"] == 226
4850
assert output["comment_line_count"] == 31
4951
assert output["function_count"] == 29
52+
assert output["inline_comment_count"] == 1
5053

5154
def test_analyze_command_with_nonexistent_file():
5255
"""Test the analyze command with a nonexistent file"""
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import os
2+
from spice.analyzers.count_inline_comments import count_inline_comments
3+
4+
# Get path to the sample files
5+
SAMPLES_DIR = os.path.join(os.path.dirname(__file__), "..", "sample-code")
6+
PY_SAMPLE = os.path.join(SAMPLES_DIR, "example.py")
7+
JS_SAMPLE = os.path.join(SAMPLES_DIR, "example.js")
8+
GO_SAMPLE = os.path.join(SAMPLES_DIR, "example.go")
9+
RB_SAMPLE = os.path.join(SAMPLES_DIR, "example.rb")
10+
11+
def test_count_inline_comments_python():
12+
"""Test counting inline comments in Python code."""
13+
count = count_inline_comments(PY_SAMPLE)
14+
assert count == 2, f"Expected 2 inline comments in Python sample, got {count}"
15+
16+
def test_count_inline_comments_javascript():
17+
"""Test counting inline comments in JavaScript code."""
18+
count = count_inline_comments(JS_SAMPLE)
19+
assert count == 2, f"Expected 2 inline comments in JavaScript sample, got {count}"
20+
21+
def test_count_inline_comments_go():
22+
"""Test counting inline comments in Go code."""
23+
count = count_inline_comments(GO_SAMPLE)
24+
assert count == 1, f"Expected 1 inline comment in Go sample, got {count}"
25+
26+
def test_count_inline_comments_ruby():
27+
"""Test counting inline comments in Ruby code."""
28+
count = count_inline_comments(RB_SAMPLE)
29+
assert count == 1, f"Expected 1 inline comment in Ruby sample, got {count}"
30+
31+
def test_count_inline_comments_nonexistent_file():
32+
"""Test counting inline comments in a nonexistent file."""
33+
try:
34+
count_inline_comments("nonexistent_file.py")
35+
assert False, "Expected FileNotFoundError for nonexistent file"
36+
except FileNotFoundError:
37+
# Expected behavior
38+
pass
39+
40+
def test_count_inline_comments_empty_string():
41+
"""Test counting inline comments in an empty file."""
42+
# Create a temporary empty file
43+
import tempfile
44+
with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as temp:
45+
temp_name = temp.name
46+
47+
try:
48+
count = count_inline_comments(temp_name)
49+
assert count == 0, f"Expected 0 inline comments in empty file, got {count}"
50+
finally:
51+
# Clean up the temporary file
52+
os.unlink(temp_name)
53+
54+
def test_count_inline_comments_with_only_inline_comments():
55+
"""Test counting inline comments in a file with only inline comments."""
56+
# Create a temporary file with only inline comments
57+
import tempfile
58+
with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as temp:
59+
temp.write(b"x = 5 # This is an inline comment\n")
60+
temp.write(b"y = 10 # This is another inline comment\n")
61+
temp.write(b"z = 15 # This is yet another inline comment\n")
62+
temp_name = temp.name
63+
64+
try:
65+
count = count_inline_comments(temp_name)
66+
assert count == 3, f"Expected 3 inline comments in file with only inline comments, got {count}"
67+
finally:
68+
# Clean up the temporary file
69+
os.unlink(temp_name)
70+
71+
def test_count_inline_comments_with_mixed_comments():
72+
"""Test counting inline comments in a file with both regular and inline comments."""
73+
# Create a temporary file with mixed comments
74+
import tempfile
75+
with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as temp:
76+
temp.write(b"# This is a regular comment\n")
77+
temp.write(b"x = 5 # This is an inline comment\n")
78+
temp.write(b"# This is another regular comment\n")
79+
temp.write(b"y = 10 # This is another inline comment\n")
80+
temp_name = temp.name
81+
82+
try:
83+
count = count_inline_comments(temp_name)
84+
assert count == 2, f"Expected 2 inline comments in file with mixed comments, got {count}"
85+
finally:
86+
# Clean up the temporary file
87+
os.unlink(temp_name)

0 commit comments

Comments
 (0)