Skip to content

Add unit tests for builders #543 #734

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 5 commits into from
Aug 18, 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
2 changes: 2 additions & 0 deletions utbot-summary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type>): String {
fun invokeDescription(className: String, methodName: String, methodParameterTypes: List<Type>): String {
val prettyClassName: String = className.replace("$", ".")

return if (methodParameterTypes.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SootMethod, JimpleToASTMap>
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(), " ")
}

}
Original file line number Diff line number Diff line change
@@ -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<SootMethod, JimpleToASTMap>
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 = "<pre>\n" +
"Test throws Throwable \n" +
"</pre>"
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)
}

}
Original file line number Diff line number Diff line change
@@ -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<SootMethod, JimpleToASTMap>
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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline