Skip to content

Commit c5c3955

Browse files
authored
Fixed the weird logic in the of error messages in SummaryTestCaseGeneratorTest #383 (#572)
* Handled the bad case with the empty paths * Improved test messages in the generated meta matchers
1 parent 4481574 commit c5c3955

File tree

2 files changed

+176
-16
lines changed

2 files changed

+176
-16
lines changed

utbot-summary-tests/src/test/kotlin/examples/SummaryTestCaseGeneratorTest.kt

Lines changed: 173 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import kotlin.reflect.KFunction3
2525
import kotlin.reflect.KFunction4
2626

2727

28+
private const val NEW_LINE = "\n"
29+
private const val POINT_IN_THE_LIST = " * "
30+
private const val COMMENT_SEPARATOR = "-------------------------------------------------------------"
31+
2832
@Disabled
2933
open class SummaryTestCaseGeneratorTest(
3034
testClass: KClass<*>,
@@ -120,18 +124,51 @@ open class SummaryTestCaseGeneratorTest(
120124
return result
121125
}
122126

123-
124127
fun List<UtExecution>.checkMatchersWithTextSummary(
125-
summaryTextKeys: List<String>,
128+
comments: List<String>,
126129
) {
127-
if (summaryTextKeys.isEmpty()) {
130+
if (comments.isEmpty()) {
128131
return
129132
}
130133
val notMatchedExecutions = this.filter { execution ->
131-
summaryTextKeys.none { summaryKey -> val normalize = execution.summary?.toString()?.normalize()
132-
normalize?.contains(summaryKey.normalize()) == true }
134+
comments.none { comment ->
135+
val normalize = execution.summary?.toString()?.normalize()
136+
normalize?.contains(comment.normalize()) == true
137+
}
138+
}
139+
140+
val notMatchedComments = comments.filter { comment ->
141+
this.none { execution ->
142+
val normalize = execution.summary?.toString()?.normalize()
143+
normalize?.contains(comment.normalize()) == true
144+
}
145+
}
146+
147+
Assertions.assertTrue(notMatchedExecutions.isEmpty() && notMatchedComments.isEmpty()) {
148+
buildString {
149+
if (notMatchedExecutions.isNotEmpty()) {
150+
append(
151+
"\nThe following comments were produced by the UTBot, " +
152+
"but were not found in the list of comments passed in the check() method:\n\n${
153+
commentsFromExecutions(
154+
notMatchedExecutions
155+
)
156+
}"
157+
)
158+
}
159+
160+
if (notMatchedComments.isNotEmpty()) {
161+
append(
162+
"\nThe following comments were passed in the check() method, " +
163+
"but were not found in the list of comments produced by the UTBot:\n\n${
164+
comments(
165+
notMatchedComments
166+
)
167+
}"
168+
)
169+
}
170+
}
133171
}
134-
Assertions.assertTrue(notMatchedExecutions.isEmpty()) { "Not matched comments ${summaries(notMatchedExecutions)}" }
135172
}
136173

137174
fun List<UtExecution>.checkMatchersWithMethodNames(
@@ -143,7 +180,36 @@ open class SummaryTestCaseGeneratorTest(
143180
val notMatchedExecutions = this.filter { execution ->
144181
methodNames.none { methodName -> execution.testMethodName?.equals(methodName) == true }
145182
}
146-
Assertions.assertTrue(notMatchedExecutions.isEmpty()) { "Not matched test names ${summaries(notMatchedExecutions)}" }
183+
184+
val notMatchedMethodNames = methodNames.filter { methodName ->
185+
this.none { execution -> execution.testMethodName?.equals(methodName) == true }
186+
}
187+
188+
Assertions.assertTrue(notMatchedExecutions.isEmpty() && notMatchedMethodNames.isEmpty()) {
189+
buildString {
190+
if (notMatchedExecutions.isNotEmpty()) {
191+
append(
192+
"\nThe following method names were produced by the UTBot, " +
193+
"but were not found in the list of method names passed in the check() method:\n\n${
194+
methodNamesFromExecutions(
195+
notMatchedExecutions
196+
)
197+
}"
198+
)
199+
}
200+
201+
if (notMatchedMethodNames.isNotEmpty()) {
202+
append(
203+
"\nThe following method names were passed in the check() method, " +
204+
"but were not found in the list of method names produced by the UTBot:\n\n${
205+
methodNames(
206+
notMatchedMethodNames
207+
)
208+
}"
209+
)
210+
}
211+
}
212+
}
147213
}
148214

149215
fun List<UtExecution>.checkMatchersWithDisplayNames(
@@ -155,14 +221,108 @@ open class SummaryTestCaseGeneratorTest(
155221
val notMatchedExecutions = this.filter { execution ->
156222
displayNames.none { displayName -> execution.displayName?.equals(displayName) == true }
157223
}
158-
Assertions.assertTrue(notMatchedExecutions.isEmpty()) { "Not matched display names ${summaries(notMatchedExecutions)}" }
224+
225+
val notMatchedDisplayNames = displayNames.filter { displayName ->
226+
this.none { execution -> execution.displayName?.equals(displayName) == true }
227+
}
228+
229+
Assertions.assertTrue(notMatchedExecutions.isEmpty() && notMatchedDisplayNames.isEmpty()) {
230+
buildString {
231+
if (notMatchedExecutions.isNotEmpty()) {
232+
append(
233+
"\nThe following display names were produced by the UTBot, " +
234+
"but were not found in the list of display names passed in the check() method:\n\n${
235+
displayNamesFromExecutions(
236+
notMatchedExecutions
237+
)
238+
}"
239+
)
240+
}
241+
242+
if (notMatchedDisplayNames.isNotEmpty()) {
243+
append(
244+
"\nThe following display names were passed in the check() method, " +
245+
"but were not found in the list of display names produced by the UTBot:\n\n${
246+
displayNames(
247+
notMatchedDisplayNames
248+
)
249+
}"
250+
)
251+
}
252+
}
253+
}
159254
}
160255

161-
private fun summaries(executions: List<UtExecution>): String {
162-
var result = ""
163-
executions.forEach {
164-
result += it.summary?.joinToString(separator = "", postfix = "\n")
256+
private fun commentsFromExecutions(executions: List<UtExecution>): String {
257+
return buildString {
258+
append(COMMENT_SEPARATOR)
259+
executions.forEach {
260+
append(NEW_LINE)
261+
append(NEW_LINE)
262+
append(it.summary?.joinToString(separator = "", postfix = NEW_LINE))
263+
append(COMMENT_SEPARATOR)
264+
append(NEW_LINE)
265+
}
266+
append(NEW_LINE)
267+
}
268+
}
269+
270+
private fun comments(comments: List<String>): String {
271+
return buildString {
272+
append(COMMENT_SEPARATOR)
273+
comments.forEach {
274+
append(NEW_LINE)
275+
append(NEW_LINE)
276+
append(it)
277+
append(NEW_LINE)
278+
append(COMMENT_SEPARATOR)
279+
append(NEW_LINE)
280+
}
281+
append(NEW_LINE)
282+
}
283+
}
284+
285+
private fun displayNamesFromExecutions(executions: List<UtExecution>): String {
286+
return buildString {
287+
executions.forEach {
288+
append(POINT_IN_THE_LIST)
289+
append(it.displayName)
290+
append(NEW_LINE)
291+
}
292+
append(NEW_LINE)
293+
}
294+
}
295+
296+
private fun displayNames(displayNames: List<String>): String {
297+
return buildString {
298+
displayNames.forEach {
299+
append(POINT_IN_THE_LIST)
300+
append(it)
301+
append(NEW_LINE)
302+
}
303+
append(NEW_LINE)
304+
}
305+
}
306+
307+
private fun methodNamesFromExecutions(executions: List<UtExecution>): String {
308+
return buildString {
309+
executions.forEach {
310+
append(POINT_IN_THE_LIST)
311+
append(it.testMethodName)
312+
append(NEW_LINE)
313+
}
314+
append(NEW_LINE)
315+
}
316+
}
317+
318+
private fun methodNames(methodNames: List<String>): String {
319+
return buildString {
320+
methodNames.forEach {
321+
append(POINT_IN_THE_LIST)
322+
append(it)
323+
append(NEW_LINE)
324+
}
325+
append(NEW_LINE)
165326
}
166-
return result
167327
}
168328
}

utbot-summary-tests/src/test/kotlin/math/SummaryOfMathTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ class SummaryOfMathTest : SummaryTestCaseGeneratorTest(
143143
"Test then returns from: return acummulator.snapshot();\n"
144144
val summary6 = "Test calls {@link guava.examples.math.StatsAccumulator#addAll(double[])},\n" +
145145
" there it iterates the loop for(double value: values) twice,\n" +
146-
" inside this loop, the test calls StatsAccumulator::add,\n" +
146+
" inside this loop, the test calls {@link guava.examples.math.StatsAccumulator#add(double)},\n" +
147147
" there it executes conditions:\n" +
148148
" (!isFinite(value)): True\n" +
149-
"Test afterwards calls {@link guava.examples.math.StatsAccumulator#snapshot()},\n" +
149+
"Test then calls {@link guava.examples.math.StatsAccumulator#snapshot()},\n" +
150150
" there it returns from: return new Stats(count, mean, sumOfSquaresOfDeltas, min, max);\n" +
151151
" \n" +
152-
"Test then returns from: return acummulator.snapshot();\n"
152+
"Test afterwards returns from: return acummulator.snapshot();\n"
153153
val summary7 = "Test calls {@link guava.examples.math.StatsAccumulator#addAll(double[])},\n" +
154154
" there it iterates the loop for(double value: values) twice,\n" +
155155
" inside this loop, the test calls {@link guava.examples.math.StatsAccumulator#add(double)},\n" +

0 commit comments

Comments
 (0)