mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-07-15 03:24:37 -04:00
fix: Improve support for code blocks without language (#108)
This makes it possible to represent code blocks in models that emit their suggestion immediately after the ``` characters.
This commit is contained in:
committed by
GitHub
parent
9a5047618d
commit
f94c79a5ff
@ -33,18 +33,66 @@ class CodeHandlerTest : public QObject, public testing::Test
|
||||
Q_OBJECT
|
||||
};
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithCodeBlock)
|
||||
TEST_F(CodeHandlerTest, testProcessTextEmpty)
|
||||
{
|
||||
EXPECT_EQ(CodeHandler::processText("", "/file.py"), "\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithLanguageCodeBlock)
|
||||
{
|
||||
QString input = "This is a comment\n"
|
||||
"```python\nprint('Hello, world!')\n```\n"
|
||||
"Another comment";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input),
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# This is a comment\n\nprint('Hello, world!')\n# Another comment\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithMultipleCodeBlocks)
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithPlainCodeBlockNoNewline)
|
||||
{
|
||||
QString input = "This is a comment\n"
|
||||
"```print('Hello, world!')\n```\n"
|
||||
"Another comment";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# This is a comment\n\nprint('Hello, world!')\n# Another comment\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithPlainCodeBlockWithNewline)
|
||||
{
|
||||
QString input = "This is a comment\n"
|
||||
"```\nprint('Hello, world!')\n```\n"
|
||||
"Another comment";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# This is a comment\n\n\nprint('Hello, world!')\n# Another comment\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextNoCommentsWithLanguageCodeBlock)
|
||||
{
|
||||
QString input = "```python\nprint('Hello, world!')\n```";
|
||||
|
||||
EXPECT_EQ(CodeHandler::processText(input, "/file.py"), "print('Hello, world!')\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextNoCommentsWithPlainCodeBlockNoNewline)
|
||||
{
|
||||
QString input = "```print('Hello, world!')\n```";
|
||||
|
||||
EXPECT_EQ(CodeHandler::processText(input, "/file.py"), "print('Hello, world!')\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextNoCommentsWithPlainCodeBlockWithNewline)
|
||||
{
|
||||
QString input = "```\nprint('Hello, world!')\n```";
|
||||
|
||||
EXPECT_EQ(CodeHandler::processText(input, "/file.py"), "\nprint('Hello, world!')\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithMultipleCodeBlocksDifferentLanguages)
|
||||
{
|
||||
QString input = "First comment\n```python\nprint('Block 1')\n"
|
||||
"```\nMiddle comment\n"
|
||||
@ -52,7 +100,7 @@ TEST_F(CodeHandlerTest, testProcessTextWithMultipleCodeBlocks)
|
||||
"Last comment";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input),
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# First comment\n\n"
|
||||
"print('Block 1')\n"
|
||||
"// Middle comment\n\n"
|
||||
@ -60,13 +108,74 @@ TEST_F(CodeHandlerTest, testProcessTextWithMultipleCodeBlocks)
|
||||
"// Last comment\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithMultipleCodeBlocksSameLanguage)
|
||||
{
|
||||
QString input = "First comment\n```python\nprint('Block 1')\n"
|
||||
"```\nMiddle comment\n"
|
||||
"```python\nprint('Block 2')\n```\n"
|
||||
"Last comment";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# First comment\n\n"
|
||||
"print('Block 1')\n"
|
||||
"# Middle comment\n\n"
|
||||
"print('Block 2')\n"
|
||||
"# Last comment\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithMultiplePlainCodeBlocksWithNewline)
|
||||
{
|
||||
QString input = "First comment\n```\nprint('Block 1')\n"
|
||||
"```\nMiddle comment\n"
|
||||
"```\ncout << \"Block 2\";\n```\n"
|
||||
"Last comment";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# First comment\n\n\n"
|
||||
"print('Block 1')\n"
|
||||
"# Middle comment\n\n\n"
|
||||
"cout << \"Block 2\";\n"
|
||||
"# Last comment\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithMultiplePlainCodeBlocksWithoutNewline)
|
||||
{
|
||||
QString input = "First comment\n```print('Block 1')\n"
|
||||
"```\nMiddle comment\n"
|
||||
"```cout << \"Block 2\";\n```\n"
|
||||
"Last comment";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# First comment\n\n"
|
||||
"print('Block 1')\n"
|
||||
"# Middle comment\n\n"
|
||||
"cout << \"Block 2\";\n"
|
||||
"# Last comment\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithEmptyLines)
|
||||
{
|
||||
QString input = "Comment with empty line\n\n```python\nprint('Hello')\n```\n\nAnother comment";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input),
|
||||
"# Comment with empty line\n\n\nprint('Hello')\n\n# Another comment\n\n");
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# Comment with empty line\n\n\n"
|
||||
"print('Hello')\n\n"
|
||||
"# Another comment\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextPlainCodeBlockWithNewlineWithEmptyLines)
|
||||
{
|
||||
QString input = "Comment with empty line\n\n```\nprint('Hello')\n```\n\nAnother comment";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# Comment with empty line\n\n\n\n"
|
||||
"print('Hello')\n\n"
|
||||
"# Another comment\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithoutCodeBlock)
|
||||
@ -74,31 +183,32 @@ TEST_F(CodeHandlerTest, testProcessTextWithoutCodeBlock)
|
||||
QString input = "This is just a comment\nwith multiple lines";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input), "// This is just a comment\n// with multiple lines\n\n");
|
||||
CodeHandler::processText(input, "/file.py"),
|
||||
"# This is just a comment\n# with multiple lines\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testProcessTextWithDifferentLanguages)
|
||||
TEST_F(CodeHandlerTest, testDetectLanguageFromLine)
|
||||
{
|
||||
QString input = "Python code:\n"
|
||||
"```python\nprint('Hello')\n```\n"
|
||||
"JavaScript code:\n"
|
||||
"```javascript\nconsole.log('Hello');\n```";
|
||||
|
||||
EXPECT_EQ(
|
||||
CodeHandler::processText(input),
|
||||
"# Python code:\n\nprint('Hello')\n"
|
||||
"// JavaScript code:\n\nconsole.log('Hello');\n");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromLine("```python"), "python");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromLine("```javascript"), "js");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromLine("```cpp"), "c-like");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromLine("``` ruby "), "ruby");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromLine("```"), "");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromLine("``` "), "");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testDetectLanguage)
|
||||
TEST_F(CodeHandlerTest, testDetectLanguageFromExtension)
|
||||
{
|
||||
EXPECT_EQ(CodeHandler::detectLanguage("```python"), "python");
|
||||
EXPECT_EQ(CodeHandler::detectLanguage("```javascript"), "javascript");
|
||||
EXPECT_EQ(CodeHandler::detectLanguage("```cpp"), "cpp");
|
||||
EXPECT_EQ(CodeHandler::detectLanguage("``` ruby "), "ruby");
|
||||
EXPECT_EQ(CodeHandler::detectLanguage("```"), "");
|
||||
EXPECT_EQ(CodeHandler::detectLanguage("``` "), "");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromExtension("py"), "python");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromExtension("js"), "js");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromExtension("cpp"), "c-like");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromExtension("hpp"), "c-like");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromExtension("rb"), "ruby");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromExtension("sh"), "shell");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromExtension("unknown"), "");
|
||||
EXPECT_EQ(CodeHandler::detectLanguageFromExtension(""), "");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testCommentPrefixForDifferentLanguages)
|
||||
{
|
||||
struct TestCase
|
||||
@ -115,21 +225,9 @@ TEST_F(CodeHandlerTest, testCommentPrefixForDifferentLanguages)
|
||||
{"lua", "Comment\n```lua\ncode\n```", "-- Comment\n\ncode\n"}};
|
||||
|
||||
for (const auto &testCase : testCases) {
|
||||
EXPECT_EQ(CodeHandler::processText(testCase.input), testCase.expected)
|
||||
EXPECT_EQ(CodeHandler::processText(testCase.input, ""), testCase.expected)
|
||||
<< "Failed for language: " << testCase.language;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testEmptyInput)
|
||||
{
|
||||
EXPECT_EQ(CodeHandler::processText(""), "\n\n");
|
||||
}
|
||||
|
||||
TEST_F(CodeHandlerTest, testCodeBlockWithoutLanguage)
|
||||
{
|
||||
QString input = "Comment\n```\ncode\n```";
|
||||
|
||||
EXPECT_EQ(CodeHandler::processText(input), "// Comment\n\ncode\n");
|
||||
}
|
||||
|
||||
#include "CodeHandlerTest.moc"
|
||||
|
Reference in New Issue
Block a user