Skip to content

Commit 1741b70

Browse files
committed
Fix after review (#452)
1 parent ff2d2d1 commit 1741b70

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,11 @@ data class SarifRegion(
154154
* Makes [startColumn] the first non-whitespace character in [startLine] in the [text].
155155
* If the [text] contains less than [startLine] lines, [startColumn] == null.
156156
*/
157-
fun fromStartLine(startLine: Int, text: String): SarifRegion {
157+
fun withStartLine(text: String, startLine: Int): SarifRegion {
158158
val neededLine = text.split('\n').getOrNull(startLine - 1) // to zero-based
159-
val startColumnZeroBased = neededLine?.takeWhile { it.toString().isBlank() }?.length
160-
val startColumn = startColumnZeroBased?.let { it + 1 }
159+
val startColumn = neededLine?.let {
160+
neededLine.takeWhile { it.toString().isBlank() }.length + 1 // to one-based
161+
}
161162
return SarifRegion(startLine = startLine, startColumn = startColumn)
162163
}
163164
}

utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class SarifReport(
146146
val sourceRelativePath = sourceFinding.getSourceRelativePath(classFqn)
147147
val startLine = extractLineNumber(utExecution) ?: defaultLineNumber
148148
val sourceCode = sourceFinding.getSourceFile(classFqn)?.readText() ?: ""
149-
val sourceRegion = SarifRegion.fromStartLine(startLine, sourceCode)
149+
val sourceRegion = SarifRegion.withStartLine(sourceCode, startLine)
150150
return listOf(
151151
SarifPhysicalLocationWrapper(
152152
SarifPhysicalLocation(SarifArtifact(sourceRelativePath), sourceRegion)
@@ -157,11 +157,11 @@ class SarifReport(
157157
private fun getRelatedLocations(utExecution: UtExecution): List<SarifRelatedPhysicalLocationWrapper> {
158158
val startLine = utExecution.testMethodName?.let { testMethodName ->
159159
val neededLine = generatedTestsCode.split('\n').indexOfFirst { line ->
160-
line.contains(testMethodName)
160+
line.contains("$testMethodName(")
161161
}
162162
if (neededLine == -1) null else neededLine + 1 // to one-based
163163
} ?: defaultLineNumber
164-
val sourceRegion = SarifRegion.fromStartLine(startLine, generatedTestsCode)
164+
val sourceRegion = SarifRegion.withStartLine(generatedTestsCode, startLine)
165165
return listOf(
166166
SarifRelatedPhysicalLocationWrapper(
167167
relatedLocationId,
@@ -235,7 +235,7 @@ class SarifReport(
235235
),
236236
physicalLocation = SarifPhysicalLocation(
237237
SarifArtifact(relativePath),
238-
SarifRegion.fromStartLine(lineNumber, sourceCode)
238+
SarifRegion.withStartLine(sourceCode, lineNumber)
239239
)
240240
)
241241
)
@@ -256,18 +256,23 @@ class SarifReport(
256256
// searching needed method call
257257
val publicMethodCallPattern = "$methodName("
258258
val privateMethodCallPattern = Regex("""$methodName.*\.invoke\(""") // using reflection
259-
val methodCallLineNumber = testsBodyLines
259+
val methodCallShiftInTestMethod = testsBodyLines
260260
.drop(testMethodStartsAt + 1) // for search after it
261261
.indexOfFirst { line ->
262262
line.contains(publicMethodCallPattern) || line.contains(privateMethodCallPattern)
263263
}
264-
if (methodCallLineNumber == -1)
264+
if (methodCallShiftInTestMethod == -1)
265265
return null
266266

267-
val startLine = methodCallLineNumber + 1 + testMethodStartsAt + 1
267+
// `startLine` consists of:
268+
// shift to the testMethod call (+ testMethodStartsAt)
269+
// the line with testMethodName (+ 1)
270+
// shift to the method call (+ methodCallShiftInTestMethod)
271+
// to one-based (+ 1)
272+
val startLine = testMethodStartsAt + 1 + methodCallShiftInTestMethod + 1
268273
return SarifPhysicalLocation(
269274
SarifArtifact(sourceFinding.testsRelativePath),
270-
SarifRegion.fromStartLine(startLine, generatedTestsCode)
275+
SarifRegion.withStartLine(generatedTestsCode, startLine)
271276
)
272277
}
273278

0 commit comments

Comments
 (0)