Skip to content

Commit 77b52c2

Browse files
authored
Merge pull request #145 from domaframework/fix/testdata-check
List type test data check and exclude else directive
2 parents b888fea + a2dadbb commit 77b52c2

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

src/main/kotlin/org/domaframework/doma/intellij/inspection/sql/visitor/SqlTestDataAfterBlockCommentVisitor.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import com.intellij.psi.PsiComment
2020
import com.intellij.psi.PsiElement
2121
import com.intellij.psi.PsiFile
2222
import com.intellij.psi.PsiLiteralExpression
23+
import com.intellij.psi.PsiWhiteSpace
2324
import com.intellij.psi.util.PsiTreeUtil
25+
import com.intellij.psi.util.nextLeafs
2426
import org.domaframework.doma.intellij.common.isInjectionSqlFile
2527
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
2628
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationTestDataResult
@@ -54,6 +56,7 @@ class SqlTestDataAfterBlockCommentVisitor(
5456

5557
val nextElement = element.nextSibling ?: return
5658
if (isSqlLiteral(nextElement)) return
59+
if (isMatchListTestData(element)) return
5760

5861
val result = ValidationTestDataResult(element, shortName)
5962
result.highlightElement(holder)
@@ -67,25 +70,47 @@ class SqlTestDataAfterBlockCommentVisitor(
6770
PsiTreeUtil.getChildOfType(element, SqlElForDirective::class.java)
6871
?: PsiTreeUtil.getChildOfType(element, SqlElIfDirective::class.java)
6972
?: PsiTreeUtil.getChildOfType(element, SqlElElseifDirective::class.java)
70-
val endDirective =
73+
val otherDirective =
7174
PsiTreeUtil
7275
.getChildrenOfType(element, PsiElement::class.java)
7376
?.find {
7477
it.elementType == SqlTypes.EL_END ||
7578
it.elementType == SqlTypes.HASH ||
76-
it.elementType == SqlTypes.EL_POPULATE
79+
it.elementType == SqlTypes.EL_POPULATE ||
80+
it.elementType == SqlTypes.EL_ELSE
7781
}
78-
if (directive != null || endDirective != null) return true
82+
if (directive != null || otherDirective != null) return true
7983

8084
val content = PsiTreeUtil.getChildOfType(element, PsiComment::class.java)
8185
return content != null
8286
}
8387

8488
private fun isSqlLiteral(element: PsiElement): Boolean =
8589
element.elementType == SqlTypes.STRING ||
86-
element.elementType == SqlTypes.BOOLEAN ||
87-
element.elementType == SqlTypes.NUMBER ||
88-
element.elementType == SqlTypes.NULL ||
8990
listOf("true", "false", "null").contains(element.text) ||
9091
element.text.matches(Regex("^\\d+$"))
92+
93+
/**
94+
* Determines if the given element matches the pattern for "List type test data."
95+
*
96+
* The function checks if the text of the element and its subsequent non-whitespace siblings
97+
* form a valid list enclosed in parentheses. The list can contain:
98+
* - Strings (double-quoted or single-quoted)
99+
* - Numbers (integers)
100+
* - Boolean values ("true" or "false")
101+
* - The "null" literal
102+
* These values can be separated by commas, and the entire list must be enclosed in parentheses.
103+
*/
104+
private fun isMatchListTestData(element: PsiElement): Boolean {
105+
val parenthesesListPattern =
106+
Regex(
107+
"""^\(\s*(?:(?:"[^"]*"|'[^']*'|\d+|true|false|null)\s*(?:,\s*(?:"[^"]*"|'[^']*'|\d+|true|false|null)\s*)*)?\)$""",
108+
)
109+
val testDataText =
110+
element.nextLeafs
111+
.takeWhile { it !is PsiWhiteSpace }
112+
.toList()
113+
.joinToString("") { it.text }
114+
return testDataText.matches(parenthesesListPattern)
115+
}
91116
}

src/test/testData/src/main/java/doma/example/dao/inspection/TestDataCheckDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface TestDataCheckDao {
2222
List<Employee> conditionAndLoopDirective(List<Project> projects,LocalDate referenceDate);
2323

2424
@Select
25-
Employee commentBlock(Integer id);
25+
Employee commentBlock(Integer id, List<Integer> subIds);
2626

2727
@Update(sqlFile=true)
2828
int populateDirective(Employee employee);

src/test/testData/src/main/resources/META-INF/doma/example/dao/inspection/TestDataCheckDao/commentBlock.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ SELECT e.employee_id AS employeeId
88
FROM employee e
99
/*%! This comment will be removed */
1010
WHERE e.employee_id = <error descr="Bind variables must be followed by test data">/*^ id */</error>
11-
AND e.age >= /*^ literalAge */99
11+
AND e.age >= /*^ literalAge */99
12+
AND e.sub_id IN /* subIds */(1,2,3)

src/test/testData/src/main/resources/META-INF/doma/example/dao/inspection/TestDataCheckDao/conditionAndLoopDirective.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ SELECT *
44
/*%for project : projects */
55
employee_name LIKE /* project.projectName */'hoge'
66
/*%if project_has_next */
7+
/*%if project.projectName.startsWith("A") */
78
/*# "or" */
9+
/*%else */
10+
/*! delete comment */
11+
and name = /* project.projectName */'testName'
12+
/*# "or" */
13+
/*%end */
814
/*%end */
915
/*%end*/
1016
OR salary > 1000

0 commit comments

Comments
 (0)