Skip to content

Fix incorrect highlighting of the method call in sarif (#536) #876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class SarifReportTest {
it.location.physicalLocation
}
assert(codeFlowPhysicalLocations[0].artifactLocation.uri.contains("MainTest.java"))
assert(codeFlowPhysicalLocations[0].region.startLine == 3)
assert(codeFlowPhysicalLocations[0].region.startLine == 5)
assert(codeFlowPhysicalLocations[0].region.startColumn == 7)
}

Expand All @@ -183,7 +183,7 @@ class SarifReportTest {
it.location.physicalLocation
}
assert(codeFlowPhysicalLocations[0].artifactLocation.uri.contains("MainTest.java"))
assert(codeFlowPhysicalLocations[0].region.startLine == 4)
assert(codeFlowPhysicalLocations[0].region.startLine == 6)
assert(codeFlowPhysicalLocations[0].region.startColumn == 5)
}

Expand Down Expand Up @@ -330,13 +330,17 @@ class SarifReportTest {

private val generatedTestsCodeMain = """
public void testMain_ThrowArithmeticException() {
/* This test fails because method [Main.main] produces [java.lang.ArithmeticException: / by zero]
Main.main(Main.java:15) */
Main main = new Main();
main.main(0); // shift for `startColumn` == 7
}
""".trimIndent()

private val generatedTestsCodePrivateMain = """
public void testMain_ThrowArithmeticException() {
/* This test fails because method [Main.main] produces [java.lang.ArithmeticException: / by zero]
Main.main(Main.java:15) */
Main main = new Main();
// ...
mainMethod.invoke(main, mainMethodArguments);
Expand Down
13 changes: 9 additions & 4 deletions utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,21 @@ class SarifReport(
)
}

private val testsBodyLines by lazy {
generatedTestsCode.split('\n')
}

private fun findMethodCallInTestBody(testMethodName: String?, methodName: String): SarifPhysicalLocation? {
if (testMethodName == null)
return null

// searching needed test
val testsBodyLines = generatedTestsCode.split('\n')
val testMethodStartsAt = testsBodyLines.indexOfFirst { line ->
line.contains(testMethodName)
}
if (testMethodStartsAt == -1)
return null
/**
/*
* ...
* public void testMethodName() { // <- `testMethodStartsAt`
* ...
Expand All @@ -294,8 +297,10 @@ class SarifReport(
*/

// searching needed method call
val publicMethodCallPattern = "$methodName("
val privateMethodCallPattern = Regex("""$methodName.*\.invoke\(""") // using reflection
// Regex("[^:]*") satisfies every character except ':'
// It is necessary to avoid strings from the stacktrace, such as "className.methodName(FileName.java:10)"
val publicMethodCallPattern = Regex("""$methodName\([^:]*\)""")
val privateMethodCallPattern = Regex("""$methodName.*\.invoke\([^:]*\)""") // using reflection
val methodCallShiftInTestMethod = testsBodyLines
.drop(testMethodStartsAt + 1) // for search after it
.indexOfFirst { line ->
Expand Down