Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions embedding/commentfilter/c_style_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,31 @@ var _ = Describe("C and C++", func() {

assertFiltered("sample.cpp", RetainNone, lines, expected)
})

It("should preserve escaped and unterminated quoted literals", func() {
lines := []string{
`const char* escaped = "quote: \\""; // comment`,
`const char* open = "unterminated`,
`const char* invalid = R"bad delimiter(text)bad delimiter";`,
`const char* missing = R"delimiter`,
}
expected := []string{
`const char* escaped = "quote: \\""; // comment`,
`const char* open = "unterminated`,
`const char* invalid = R"bad delimiter(text)bad delimiter";`,
`const char* missing = R"delimiter`,
}

assertFiltered("sample.cpp", RetainNone, lines, expected)
})

It("should keep an open block comment across lines", func() {
lines := []string{
"/* open block",
"continued",
"closed */ int value = 1;",
}

assertFiltered("sample.cpp", RetainBlock, lines, lines)
})
})
27 changes: 27 additions & 0 deletions embedding/commentfilter/csharp_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,31 @@ var _ = Describe("C#", func() {

assertFiltered("Api.cs", RetainNone, lines, expected)
})

It("should preserve C# string variants inside interpolation expressions", func() {
cases := [][]string{
{`var value = "quote: \\"";`},
{`var value = $"{Format("a\\\"b", @"a "" b", """raw""")}";`},
{`var value = $"{Format("""raw`},
{`var value = $"{Format("text`},
{`var value = $"{number:000`},
{`var value = $"""Keep {{ and }} {number}""";`},
{`var value = @"a "" b";`},
{`var value = "unterminated`},
}

for _, lines := range cases {
assertFiltered("Api.cs", RetainNone, lines, lines)
}
})

It("should keep an open block comment across lines", func() {
lines := []string{
"/* open block",
"continued",
"closed */ var value = 1;",
}

assertFiltered("Api.cs", RetainBlock, lines, lines)
})
})
13 changes: 13 additions & 0 deletions embedding/commentfilter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ func TestCommentFilter(t *testing.T) {
}

var _ = Describe("Comment filter", func() {
It("should parse supported comment modes", func() {
mode, err := ParseMode("")
Expect(err).ShouldNot(HaveOccurred())
Expect(mode).Should(Equal(RetainAll))

mode, err = ParseMode(string(RetainBlock))
Expect(err).ShouldNot(HaveOccurred())
Expect(mode).Should(Equal(RetainBlock))

_, err = ParseMode("unsupported")
Expect(err).Should(HaveOccurred())
})

Describe("unsupported extensions", func() {
It("should return unsupported files unchanged", func() {
lines := []string{
Expand Down
12 changes: 12 additions & 0 deletions embedding/commentfilter/java_style_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
. "embed-code/embed-code-go/embedding/commentfilter"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Java-style languages", func() {
Expand Down Expand Up @@ -120,4 +121,15 @@ var _ = Describe("Java-style languages", func() {

assertFiltered("Api.java", RetainNone, lines, expected)
})

It("should support configured documentation line markers", func() {
filter := MarkerCommentFilter{Syntax: CommentMarker{
Documentation: DocumentationMarker{Inline: []string{"///"}},
}}
lines := []string{"/// docs", "code"}

result := filter.Filter(lines, RetainDocumentation)

Expect(result).Should(Equal(lines))
})
})
27 changes: 27 additions & 0 deletions embedding/commentfilter/javascript_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,31 @@ var _ = Describe("JavaScript and TypeScript", func() {

assertFiltered("sample.ts", RetainNone, lines, expected)
})

It("should preserve JavaScript regex, string, and interpolation variants", func() {
lines := []string{
`const text = "escaped \\" // literal"; // comment`,
`const classPattern = /[\\/]/gi; // comment`,
`/start/.test(value); // comment`,
`const open = /unterminated`,
`const division = identifier / 2; // comment`,
"const template = `escaped \\` text ${value}`; // comment",
"const nested = `${{ value: /[}]/g, text: \"}\" }}`; // comment",
`const unusual = */ /regex/; // comment`,
`*/ /regex/; // comment`,
}
expected := []string{
`const text = "escaped \\" `,
`const classPattern = /[\\/]/gi; `,
`/start/.test(value); `,
`const open = /unterminated`,
`const division = identifier / 2; `,
"const template = `escaped \\` text ${value}`; ",
"const nested = `${{ value: /[}]/g, text: \"}\" }}`; ",
`const unusual = */ /regex/; `,
`*/ /regex/; `,
}

assertFiltered("sample.ts", RetainNone, lines, expected)
})
})
19 changes: 19 additions & 0 deletions embedding/commentfilter/kotlin_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,23 @@ var _ = Describe("Kotlin", func() {

assertFiltered("Sample.kt", RetainNone, lines, expected)
})

It("should preserve escaped strings, characters, and nested interpolation braces", func() {
lines := []string{
`val quote = '\'' // comment`,
`val text = "escaped \\" // literal" // comment`,
`val nested = "${if (ready) { "// literal" } else { value }}" // comment`,
`val raw = "${"""raw"""}" // comment`,
`val quoted = "${"text"}" // comment`,
}
expected := []string{
`val quote = '\'' `,
`val text = "escaped \\" `,
`val nested = "${if (ready) { "// literal" } else { value }}" `,
`val raw = "${"""raw"""}" `,
`val quoted = "${"text"}" `,
}

assertFiltered("Sample.kt", RetainNone, lines, expected)
})
})
38 changes: 38 additions & 0 deletions embedding/commentfilter/python_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,42 @@ var _ = Describe("Python", func() {

assertFiltered("module.py", RetainNone, lines, expected)
})

It("should preserve strings nested inside f-string expressions", func() {
cases := []struct {
lines []string
expected []string
}{
{
lines: []string{`value = f"{format('escaped \\' quote')}" # comment`},
expected: []string{`value = f"{format('escaped \\' quote')}" # comment`},
},
{
lines: []string{`value = f"{format("""raw`, `text`, `""")}" # comment`},
expected: []string{
`value = f"{format("""raw`, `text`, `""")}" `,
},
},
{
lines: []string{`value = f"{number:04`},
expected: []string{`value = f"{number:04`},
},
{
lines: []string{`value = f"{format('unterminated`},
expected: []string{`value = f"{format('unterminated`},
},
{
lines: []string{`value = f"{ {'nested': value} }" # comment`},
expected: []string{`value = f"{ {'nested': value} }" `},
},
{
lines: []string{`value = "unterminated`},
expected: []string{`value = "unterminated`},
},
}

for _, tc := range cases {
assertFiltered("module.py", RetainNone, tc.lines, tc.expected)
}
})
})
6 changes: 6 additions & 0 deletions embedding/commentfilter/visual_basic_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ var _ = Describe("Visual Basic", func() {

assertFiltered("Module.vb", RetainDocumentation, lines, expected)
})

It("should preserve an unterminated quoted string", func() {
lines := []string{`Dim value = "unterminated`}

assertFiltered("Module.vb", RetainNone, lines, lines)
})
})
Loading
Loading