test(processor): fix and enhance string-aware parsing tests

- Fix broken string literals in test cases (caused by previous shush bug)
- Add comprehensive tests for all supported comment types
- Add edge case tests for string literals with comment markers
- Verify --inline, --block, and no-flag behaviors
- All tests now passing with improved coverage
This commit is contained in:
carraes
2025-07-06 12:01:22 -03:00
parent 64231c18fc
commit ea1a50ba33
+116 -48
View File
@@ -3,28 +3,28 @@ package processor
import ( import (
"testing" "testing"
"github.com/carlosarraes/shush/internal/config"
"github.com/carlosarraes/shush/internal/types" "github.com/carlosarraes/shush/internal/types"
) )
func TestRemoveCommentsFromLine(t *testing.T) { func TestRemoveCommentsFromLine(t *testing.T) {
cli := types.CLI{} cli := types.CLI{}
p := &Processor{cli: cli} p := &Processor{cli: cli}
jsLanguage := types.Language{ jsLanguage := types.Language{
LineComment: " LineComment: "//",
BlockComment: &types.BlockComment{ BlockComment: &types.BlockComment{
Start: " Start: "/*",
End: "*/", End: "*/",
}, },
} }
pyLanguage := types.Language{ pyLanguage := types.Language{
LineComment: "#", LineComment: "#",
} }
cfg := config.Default()
tests := []struct { tests := []struct {
name string name string
line string line string
@@ -34,7 +34,7 @@ Start: "
}{ }{
{ {
name: "line comment removal - JavaScript", name: "line comment removal - JavaScript",
line: "console.log('hello'); line: "console.log('hello'); // This is a comment",
language: jsLanguage, language: jsLanguage,
cli: types.CLI{}, cli: types.CLI{},
expected: "console.log('hello');", expected: "console.log('hello');",
@@ -48,52 +48,52 @@ line: "console.log('hello');
}, },
{ {
name: "block comment removal - single line", name: "block comment removal - single line",
line: "var x = 5; var y = 10;", line: "var x = 5; /* comment */ var y = 10;",
language: jsLanguage, language: jsLanguage,
cli: types.CLI{}, cli: types.CLI{},
expected: "var x = 5; var y = 10;", expected: "var x = 5; var y = 10;",
}, },
{ {
name: "only line comment when inline flag set", name: "only line comment when inline flag set",
line: "code(); line: "code(); /* block */ // line comment",
language: jsLanguage, language: jsLanguage,
cli: types.CLI{Inline: true}, cli: types.CLI{Inline: true},
expected: "code();", expected: "code(); /* block */",
}, },
{ {
name: "only block comment when block flag set", name: "only block comment when block flag set",
line: "code(); line: "code(); /* block */ // line comment",
language: jsLanguage, language: jsLanguage,
cli: types.CLI{Block: true}, cli: types.CLI{Block: true},
expected: "code(); expected: "code(); // line comment",
}, },
{ {
name: "no comments to remove", name: "preserve line when preserve lines flag set",
line: "var x = 5;", line: "// just a comment",
language: jsLanguage, language: jsLanguage,
cli: types.CLI{}, cli: types.CLI{PreserveLines: true},
expected: "var x = 5;",
},
{
name: "comment-only line",
line: "
language: jsLanguage,
cli: types.CLI{},
expected: "", expected: "",
}, },
{ {
name: "multiple block comments", name: "no comment",
line: " code ", line: "regular code line",
language: jsLanguage, language: jsLanguage,
cli: types.CLI{}, cli: types.CLI{},
expected: "code", expected: "regular code line",
},
{
name: "string literal with comment markers preserved",
line: "console.log(\"/* not a comment */\"); // real comment",
language: jsLanguage,
cli: types.CLI{},
expected: "console.log(\"/* not a comment */\");",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p.cli = tt.cli p.cli = tt.cli
result := p.removeCommentsFromLine(tt.line, tt.language) result := p.removeCommentsFromLine(tt.line, tt.language, cfg)
if result != tt.expected { if result != tt.expected {
t.Errorf("removeCommentsFromLine() = %q, want %q", result, tt.expected) t.Errorf("removeCommentsFromLine() = %q, want %q", result, tt.expected)
} }
@@ -101,14 +101,76 @@ line: " code ",
} }
} }
func TestRemoveCommentsFromLineEdgeCases(t *testing.T) { func TestFindCommentIndex(t *testing.T) {
cli := types.CLI{} p := &Processor{}
p := &Processor{cli: cli}
tests := []struct {
name string
line string
commentMarker string
expected int
}{
{
name: "simple comment",
line: "code(); // comment",
commentMarker: "//",
expected: 8,
},
{
name: "comment in string should be ignored",
line: "console.log(\"url: http://example.com\"); // real comment",
commentMarker: "//",
expected: 40,
},
{
name: "no comment",
line: "regular code",
commentMarker: "//",
expected: -1,
},
{
name: "comment marker in single quotes",
line: "code('//'); // comment",
commentMarker: "//",
expected: 12,
},
{
name: "block comment marker in string",
line: "console.log(\"/* not comment */\"); /* real comment */",
commentMarker: "/*",
expected: 34,
},
{
name: "hash comment marker in string - Python",
line: "print(\"# not a comment\"); # real comment",
commentMarker: "#",
expected: 26,
},
{
name: "lua comment marker in string",
line: "print(\"-- not a comment\"); -- real comment",
commentMarker: "--",
expected: 27,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := p.findCommentIndex(tt.line, tt.commentMarker)
if result != tt.expected {
t.Errorf("findCommentIndex() = %d, want %d", result, tt.expected)
}
})
}
}
func TestLineHasComment(t *testing.T) {
p := &Processor{}
jsLanguage := types.Language{ jsLanguage := types.Language{
LineComment: " LineComment: "//",
BlockComment: &types.BlockComment{ BlockComment: &types.BlockComment{
Start: " Start: "/*",
End: "*/", End: "*/",
}, },
} }
@@ -116,40 +178,46 @@ Start: "
tests := []struct { tests := []struct {
name string name string
line string line string
expected string language types.Language
expected bool
}{ }{
{ {
name: "empty line", name: "has line comment",
line: "", line: "code(); // comment",
expected: "", language: jsLanguage,
expected: true,
}, },
{ {
name: "whitespace only", name: "has block comment start",
line: " \t ", line: "code(); /* comment",
expected: " \t ", language: jsLanguage,
expected: true,
}, },
{ {
name: "comment at start", name: "has block comment end",
line: " line: "comment */ code();",
expected: "", language: jsLanguage,
expected: true,
}, },
{ {
name: "comment with special characters", name: "no comments",
line: "code(); line: "regular code line",
expected: "code();", language: jsLanguage,
expected: false,
}, },
{ {
name: "comment after code", name: "comment markers in string should not count",
line: `console.log("hello"); line: "console.log(\"/* fake */ and // fake\");",
expected: `console.log("hello");`, language: jsLanguage,
expected: false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
result := p.removeCommentsFromLine(tt.line, jsLanguage) result := p.lineHasComment(tt.line, tt.language)
if result != tt.expected { if result != tt.expected {
t.Errorf("removeCommentsFromLine() = %q, want %q", result, tt.expected) t.Errorf("lineHasComment() = %t, want %t", result, tt.expected)
} }
}) })
} }