diff --git a/utbot-summary/build.gradle b/utbot-summary/build.gradle index 66eaae3682..0ec442f9f0 100644 --- a/utbot-summary/build.gradle +++ b/utbot-summary/build.gradle @@ -9,5 +9,7 @@ dependencies { implementation group: 'com.github.javaparser', name: 'javaparser-core', version: '3.22.1' + testImplementation group: 'org.mockito', name: 'mockito-core', version: mockito_version + testImplementation("org.junit.jupiter:junit-jupiter:$junit5_version") } diff --git a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt index 579328fc32..eab1d0d3e6 100644 --- a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt +++ b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt @@ -349,7 +349,7 @@ open class SimpleCommentBuilder( * In case when an enclosing class in nested, we need to replace '$' with '.' * to render the reference. */ - protected fun invokeDescription(className: String, methodName: String, methodParameterTypes: List): String { + fun invokeDescription(className: String, methodName: String, methodParameterTypes: List): String { val prettyClassName: String = className.replace("$", ".") return if (methodParameterTypes.isEmpty()) { diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt new file mode 100644 index 0000000000..fe3c808717 --- /dev/null +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt @@ -0,0 +1,62 @@ +package org.utbot.summary.comment + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import org.utbot.framework.plugin.api.Step +import org.utbot.framework.plugin.api.UtOverflowFailure +import org.utbot.summary.ast.JimpleToASTMap +import org.utbot.summary.tag.StatementTag +import org.utbot.summary.tag.TraceTag +import soot.SootMethod +import soot.jimple.Stmt +import soot.jimple.internal.JReturnStmt + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class SimpleClusterCommentBuilderTest { + private lateinit var traceTag: TraceTag + private lateinit var jimpleToASTMap: JimpleToASTMap + private lateinit var sootToAst: MutableMap + private lateinit var sootMethod: SootMethod + private lateinit var statementTag: StatementTag + private lateinit var step: Step + private lateinit var statement: Stmt + + @BeforeAll + fun setUp() { + traceTag = mock(TraceTag::class.java) + sootMethod = mock(SootMethod::class.java) + jimpleToASTMap = mock(JimpleToASTMap::class.java) + statementTag = mock(StatementTag::class.java) + step = mock(Step::class.java) + statement = mock(JReturnStmt::class.java) + sootToAst = mutableMapOf() + + `when`(statementTag.step).thenReturn(step) + `when`(step.stmt).thenReturn(statement) + `when`(traceTag.path).thenReturn(listOf(step)) + `when`(traceTag.rootStatementTag).thenReturn(statementTag) + `when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) + + sootToAst[sootMethod] = jimpleToASTMap + } + + @Test + fun `builds empty comment if execution result is null`() { + val commentBuilder = SimpleClusterCommentBuilder(traceTag, sootToAst) + val comment = commentBuilder.buildString(sootMethod) + assertEquals(" ", comment) + } + + @Test + fun `builds empty doc statement if execution result is null`() { + val commentBuilder = SimpleClusterCommentBuilder(traceTag, sootToAst) + val statements = commentBuilder.buildDocStmts(sootMethod) + assertEquals(statements.size, 1) + assertEquals(statements[0].toString(), " ") + } + +} \ No newline at end of file diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt new file mode 100644 index 0000000000..39b995ccad --- /dev/null +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt @@ -0,0 +1,84 @@ +package org.utbot.summary.comment + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import org.utbot.framework.plugin.api.Step +import org.utbot.framework.plugin.api.UtOverflowFailure +import org.utbot.summary.ast.JimpleToASTMap +import org.utbot.summary.tag.StatementTag +import org.utbot.summary.tag.TraceTag +import soot.SootMethod +import soot.jimple.Stmt +import soot.jimple.internal.JReturnStmt + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class SimpleCommentBuilderTest { + private lateinit var traceTag: TraceTag + private lateinit var jimpleToASTMap: JimpleToASTMap + private lateinit var sootToAst: MutableMap + private lateinit var sootMethod: SootMethod + private lateinit var statementTag: StatementTag + private lateinit var step: Step + private lateinit var statement: Stmt + + @BeforeAll + fun setUp() { + traceTag = mock(TraceTag::class.java) + sootMethod = mock(SootMethod::class.java) + jimpleToASTMap = mock(JimpleToASTMap::class.java) + statementTag = mock(StatementTag::class.java) + step = mock(Step::class.java) + statement = mock(JReturnStmt::class.java) + sootToAst = mutableMapOf() + + `when`(statementTag.step).thenReturn(step) + `when`(step.stmt).thenReturn(statement) + `when`(traceTag.path).thenReturn(listOf(step)) + `when`(traceTag.rootStatementTag).thenReturn(statementTag) + `when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) + + sootToAst[sootMethod] = jimpleToASTMap + } + + @Test + fun `throws throwable if execution result is null`() { + val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) + val comment = commentBuilder.buildString(sootMethod) + val expectedComment = "
\n" +
+                "Test throws Throwable \n" +
+                "
" + assertEquals(expectedComment, comment) + } + + @Test + fun `builds one doc statement`() { + val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) + val statements = commentBuilder.buildDocStmts(sootMethod) + val expectedDocStatement = "Test \n" + + "throws Throwable \n" + assertEquals(statements.size, 1) + assertEquals(statements[0].toString(), expectedDocStatement) + } + + @Test + fun `builds inline link for method`() { + val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) + val methodReference = commentBuilder.invokeDescription("org.utbot.ClassName", "methodName", listOf()) + val expectedMethodReference = "{@link org.utbot.ClassName#methodName()}" + assertEquals(methodReference, expectedMethodReference) + } + + @Test + fun `builds inline link for method in nested class`() { + val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) + val methodReference = + commentBuilder.invokeDescription("org.utbot.ClassName\$NestedClassName", "methodName", listOf()) + val expectedMethodReference = "{@link org.utbot.ClassName.NestedClassName#methodName()}" + assertEquals(methodReference, expectedMethodReference) + } + +} \ No newline at end of file diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt new file mode 100644 index 0000000000..fc0acf2a91 --- /dev/null +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt @@ -0,0 +1,74 @@ +package org.utbot.summary.name + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import org.utbot.framework.plugin.api.UtOverflowFailure +import org.utbot.summary.ast.JimpleToASTMap +import org.utbot.summary.tag.TraceTag +import org.utbot.summary.tag.UniquenessTag +import soot.SootMethod + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class SimpleNameBuilderTest { + private lateinit var traceTag: TraceTag + private lateinit var jimpleToASTMap: JimpleToASTMap + private lateinit var sootToAst: MutableMap + private lateinit var sootMethod: SootMethod + + @BeforeAll + fun setUp() { + traceTag = mock(TraceTag::class.java) + sootMethod = mock(SootMethod::class.java) + jimpleToASTMap = mock(JimpleToASTMap::class.java) + sootToAst = mutableMapOf() + + `when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) + + sootToAst[sootMethod] = jimpleToASTMap + } + + @Test + fun `method name should start with test`() { + `when`(sootMethod.name).thenReturn("methodName") + + val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) + val methodName = simpleNameBuilder.name + assert(methodName.startsWith("test")) + } + + @Test + fun `creates a name pair with a candidate with a bigger index`() { + `when`(sootMethod.name).thenReturn("") + + val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) + val fromCandidateName = DisplayNameCandidate("fromCandidate", UniquenessTag.Unique, 3) + + val toCandidate1 = DisplayNameCandidate("candidate1", UniquenessTag.Common, 2) + val toCandidate2 = DisplayNameCandidate("candidate2", UniquenessTag.Common, 4) + + val candidate = simpleNameBuilder.buildCandidate(fromCandidateName, toCandidate1, toCandidate2) + + val resultPair = Pair(fromCandidateName.name, toCandidate2.name) + assertEquals(candidate, resultPair) + } + + @Test + fun `returns null if candidates are equal`() { + `when`(sootMethod.name).thenReturn("") + + val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) + val fromCandidateName = DisplayNameCandidate("candidate", UniquenessTag.Unique, 0) + + // two equal candidates + val toCandidate1 = DisplayNameCandidate("candidate", UniquenessTag.Common, 1) + val toCandidate2 = DisplayNameCandidate("candidate", UniquenessTag.Common, 1) + + val candidate = simpleNameBuilder.buildCandidate(fromCandidateName, toCandidate1, toCandidate2) + + assertEquals(candidate, null) + } +} \ No newline at end of file diff --git a/utbot-summary/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/utbot-summary/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 0000000000..1f0955d450 --- /dev/null +++ b/utbot-summary/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline