Skip to content

Commit 9730501

Browse files
authored
Add unit tests for builders #543 (#734)
* Configured unit tests with mockito * Add unit tests for SimpleNameBuilderTest #543 * Add unit tests for SimpleClusterCommentBuilderTest.kt and SimpleCommentBuilderTest.kt #543 * change mockito version, fix broken test, add tests for buildDocStmts * add tests for invokeDescription method
1 parent 0ee76e2 commit 9730501

File tree

6 files changed

+224
-1
lines changed

6 files changed

+224
-1
lines changed

utbot-summary/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ dependencies {
1010

1111
implementation group: 'com.github.javaparser', name: 'javaparser-core', version: '3.22.1'
1212

13+
testImplementation group: 'org.mockito', name: 'mockito-core', version: mockito_version
14+
1315
testImplementation("org.junit.jupiter:junit-jupiter:$junit5_version")
1416
}

utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ open class SimpleCommentBuilder(
349349
* In case when an enclosing class in nested, we need to replace '$' with '.'
350350
* to render the reference.
351351
*/
352-
protected fun invokeDescription(className: String, methodName: String, methodParameterTypes: List<Type>): String {
352+
fun invokeDescription(className: String, methodName: String, methodParameterTypes: List<Type>): String {
353353
val prettyClassName: String = className.replace("$", ".")
354354

355355
return if (methodParameterTypes.isEmpty()) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.utbot.summary.comment
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.BeforeAll
5+
import org.junit.jupiter.api.Test
6+
import org.junit.jupiter.api.TestInstance
7+
import org.mockito.Mockito.mock
8+
import org.mockito.Mockito.`when`
9+
import org.utbot.framework.plugin.api.Step
10+
import org.utbot.framework.plugin.api.UtOverflowFailure
11+
import org.utbot.summary.ast.JimpleToASTMap
12+
import org.utbot.summary.tag.StatementTag
13+
import org.utbot.summary.tag.TraceTag
14+
import soot.SootMethod
15+
import soot.jimple.Stmt
16+
import soot.jimple.internal.JReturnStmt
17+
18+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
19+
class SimpleClusterCommentBuilderTest {
20+
private lateinit var traceTag: TraceTag
21+
private lateinit var jimpleToASTMap: JimpleToASTMap
22+
private lateinit var sootToAst: MutableMap<SootMethod, JimpleToASTMap>
23+
private lateinit var sootMethod: SootMethod
24+
private lateinit var statementTag: StatementTag
25+
private lateinit var step: Step
26+
private lateinit var statement: Stmt
27+
28+
@BeforeAll
29+
fun setUp() {
30+
traceTag = mock(TraceTag::class.java)
31+
sootMethod = mock(SootMethod::class.java)
32+
jimpleToASTMap = mock(JimpleToASTMap::class.java)
33+
statementTag = mock(StatementTag::class.java)
34+
step = mock(Step::class.java)
35+
statement = mock(JReturnStmt::class.java)
36+
sootToAst = mutableMapOf()
37+
38+
`when`(statementTag.step).thenReturn(step)
39+
`when`(step.stmt).thenReturn(statement)
40+
`when`(traceTag.path).thenReturn(listOf(step))
41+
`when`(traceTag.rootStatementTag).thenReturn(statementTag)
42+
`when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable()))
43+
44+
sootToAst[sootMethod] = jimpleToASTMap
45+
}
46+
47+
@Test
48+
fun `builds empty comment if execution result is null`() {
49+
val commentBuilder = SimpleClusterCommentBuilder(traceTag, sootToAst)
50+
val comment = commentBuilder.buildString(sootMethod)
51+
assertEquals(" ", comment)
52+
}
53+
54+
@Test
55+
fun `builds empty doc statement if execution result is null`() {
56+
val commentBuilder = SimpleClusterCommentBuilder(traceTag, sootToAst)
57+
val statements = commentBuilder.buildDocStmts(sootMethod)
58+
assertEquals(statements.size, 1)
59+
assertEquals(statements[0].toString(), " ")
60+
}
61+
62+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.utbot.summary.comment
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.BeforeAll
5+
import org.junit.jupiter.api.Test
6+
import org.junit.jupiter.api.TestInstance
7+
import org.mockito.Mockito.mock
8+
import org.mockito.Mockito.`when`
9+
import org.utbot.framework.plugin.api.Step
10+
import org.utbot.framework.plugin.api.UtOverflowFailure
11+
import org.utbot.summary.ast.JimpleToASTMap
12+
import org.utbot.summary.tag.StatementTag
13+
import org.utbot.summary.tag.TraceTag
14+
import soot.SootMethod
15+
import soot.jimple.Stmt
16+
import soot.jimple.internal.JReturnStmt
17+
18+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
19+
class SimpleCommentBuilderTest {
20+
private lateinit var traceTag: TraceTag
21+
private lateinit var jimpleToASTMap: JimpleToASTMap
22+
private lateinit var sootToAst: MutableMap<SootMethod, JimpleToASTMap>
23+
private lateinit var sootMethod: SootMethod
24+
private lateinit var statementTag: StatementTag
25+
private lateinit var step: Step
26+
private lateinit var statement: Stmt
27+
28+
@BeforeAll
29+
fun setUp() {
30+
traceTag = mock(TraceTag::class.java)
31+
sootMethod = mock(SootMethod::class.java)
32+
jimpleToASTMap = mock(JimpleToASTMap::class.java)
33+
statementTag = mock(StatementTag::class.java)
34+
step = mock(Step::class.java)
35+
statement = mock(JReturnStmt::class.java)
36+
sootToAst = mutableMapOf()
37+
38+
`when`(statementTag.step).thenReturn(step)
39+
`when`(step.stmt).thenReturn(statement)
40+
`when`(traceTag.path).thenReturn(listOf(step))
41+
`when`(traceTag.rootStatementTag).thenReturn(statementTag)
42+
`when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable()))
43+
44+
sootToAst[sootMethod] = jimpleToASTMap
45+
}
46+
47+
@Test
48+
fun `throws throwable if execution result is null`() {
49+
val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst)
50+
val comment = commentBuilder.buildString(sootMethod)
51+
val expectedComment = "<pre>\n" +
52+
"Test throws Throwable \n" +
53+
"</pre>"
54+
assertEquals(expectedComment, comment)
55+
}
56+
57+
@Test
58+
fun `builds one doc statement`() {
59+
val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst)
60+
val statements = commentBuilder.buildDocStmts(sootMethod)
61+
val expectedDocStatement = "Test \n" +
62+
"throws Throwable \n"
63+
assertEquals(statements.size, 1)
64+
assertEquals(statements[0].toString(), expectedDocStatement)
65+
}
66+
67+
@Test
68+
fun `builds inline link for method`() {
69+
val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst)
70+
val methodReference = commentBuilder.invokeDescription("org.utbot.ClassName", "methodName", listOf())
71+
val expectedMethodReference = "{@link org.utbot.ClassName#methodName()}"
72+
assertEquals(methodReference, expectedMethodReference)
73+
}
74+
75+
@Test
76+
fun `builds inline link for method in nested class`() {
77+
val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst)
78+
val methodReference =
79+
commentBuilder.invokeDescription("org.utbot.ClassName\$NestedClassName", "methodName", listOf())
80+
val expectedMethodReference = "{@link org.utbot.ClassName.NestedClassName#methodName()}"
81+
assertEquals(methodReference, expectedMethodReference)
82+
}
83+
84+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.utbot.summary.name
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.BeforeAll
5+
import org.junit.jupiter.api.Test
6+
import org.junit.jupiter.api.TestInstance
7+
import org.mockito.Mockito.mock
8+
import org.mockito.Mockito.`when`
9+
import org.utbot.framework.plugin.api.UtOverflowFailure
10+
import org.utbot.summary.ast.JimpleToASTMap
11+
import org.utbot.summary.tag.TraceTag
12+
import org.utbot.summary.tag.UniquenessTag
13+
import soot.SootMethod
14+
15+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
16+
class SimpleNameBuilderTest {
17+
private lateinit var traceTag: TraceTag
18+
private lateinit var jimpleToASTMap: JimpleToASTMap
19+
private lateinit var sootToAst: MutableMap<SootMethod, JimpleToASTMap>
20+
private lateinit var sootMethod: SootMethod
21+
22+
@BeforeAll
23+
fun setUp() {
24+
traceTag = mock(TraceTag::class.java)
25+
sootMethod = mock(SootMethod::class.java)
26+
jimpleToASTMap = mock(JimpleToASTMap::class.java)
27+
sootToAst = mutableMapOf()
28+
29+
`when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable()))
30+
31+
sootToAst[sootMethod] = jimpleToASTMap
32+
}
33+
34+
@Test
35+
fun `method name should start with test`() {
36+
`when`(sootMethod.name).thenReturn("methodName")
37+
38+
val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod)
39+
val methodName = simpleNameBuilder.name
40+
assert(methodName.startsWith("test"))
41+
}
42+
43+
@Test
44+
fun `creates a name pair with a candidate with a bigger index`() {
45+
`when`(sootMethod.name).thenReturn("")
46+
47+
val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod)
48+
val fromCandidateName = DisplayNameCandidate("fromCandidate", UniquenessTag.Unique, 3)
49+
50+
val toCandidate1 = DisplayNameCandidate("candidate1", UniquenessTag.Common, 2)
51+
val toCandidate2 = DisplayNameCandidate("candidate2", UniquenessTag.Common, 4)
52+
53+
val candidate = simpleNameBuilder.buildCandidate(fromCandidateName, toCandidate1, toCandidate2)
54+
55+
val resultPair = Pair(fromCandidateName.name, toCandidate2.name)
56+
assertEquals(candidate, resultPair)
57+
}
58+
59+
@Test
60+
fun `returns null if candidates are equal`() {
61+
`when`(sootMethod.name).thenReturn("")
62+
63+
val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod)
64+
val fromCandidateName = DisplayNameCandidate("candidate", UniquenessTag.Unique, 0)
65+
66+
// two equal candidates
67+
val toCandidate1 = DisplayNameCandidate("candidate", UniquenessTag.Common, 1)
68+
val toCandidate2 = DisplayNameCandidate("candidate", UniquenessTag.Common, 1)
69+
70+
val candidate = simpleNameBuilder.buildCandidate(fromCandidateName, toCandidate1, toCandidate2)
71+
72+
assertEquals(candidate, null)
73+
}
74+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock-maker-inline

0 commit comments

Comments
 (0)