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