diff --git a/utbot-framework-test/src/test/kotlin/org/utbot/sarif/SarifReportTest.kt b/utbot-framework-test/src/test/kotlin/org/utbot/sarif/SarifReportTest.kt index 2de19835cd..888d02a816 100644 --- a/utbot-framework-test/src/test/kotlin/org/utbot/sarif/SarifReportTest.kt +++ b/utbot-framework-test/src/test/kotlin/org/utbot/sarif/SarifReportTest.kt @@ -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) } @@ -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) } @@ -330,6 +330,8 @@ 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 } @@ -337,6 +339,8 @@ class SarifReportTest { 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); diff --git a/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt b/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt index 960ae7aa90..2fdcbd1d5b 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt @@ -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` * ... @@ -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 ->