Skip to content

Commit 618c815

Browse files
committed
Fix samples errors after updated
1 parent 02d330b commit 618c815

File tree

37 files changed

+105
-24
lines changed

37 files changed

+105
-24
lines changed

src/test/kotlin/com/compiler/server/base/BaseResourceCompileTest.kt

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ package com.compiler.server
33
import com.compiler.server.model.ExecutionResult
44
import com.compiler.server.model.ProjectType
55
import java.io.File
6+
import java.security.MessageDigest
67
import kotlin.test.assertNotNull
78
import kotlin.test.assertTrue
89

10+
private val FILES_TO_USE_WORKAROUND = mapOf(
11+
"5ec261721162729866abc1e8f7293ef4.31" to "a2c8dd6ff3a6074c01c7bb813cc2cbfb",
12+
"5ec261721162729866abc1e8f7293ef4.32" to "69496b10b87ba1491dff96f2396c5b40",
13+
"8a72411880f54ee0a305b57ed6c6901d.2" to "82e5ecc6747c91c85b697bd43cf93c93",
14+
"4c8b9a374d2df52b3a804a9b8e54ee2b.20" to "e58b9c7e6996fcfd2a53da5373c683ba",
15+
)
16+
917
interface BaseResourceCompileTest {
1018
val testDirJVM: String
1119
get() = "src/test/resources/test-compile-data/jvm"
@@ -25,28 +33,20 @@ interface BaseResourceCompileTest {
2533
assertTrue(testFiles.isNotEmpty(), "No files in test directory")
2634

2735
testFiles.forEach { file ->
28-
val baseFileName = file.nameWithoutExtension
29-
val workaroundFileName = "$baseFileName-workaround.kt"
30-
val workaroundFile = File(file.parent, workaroundFileName)
31-
32-
// Some examples are needed to be rewritten,
33-
// but it leads to new pull requests from verifier bot.
34-
// That is why we added files `*-workaround.kt` to override the logic.
35-
if (workaroundFile.exists() ) {
36-
return@forEach
37-
}
38-
39-
val code = file.readText()
4036

4137
val platform: ProjectType = when (folder) {
4238
testDirJVM -> ProjectType.JAVA
4339
testDirJS -> ProjectType.JS_IR
4440
else -> throw IllegalArgumentException("Unknown type $folder")
4541
}
4642

43+
// Decide whether to use workaround expected output based on hash table
44+
val expectedResultFile = getExpectedResultFile(platform, file, folder, badMap) ?: return@forEach
45+
val code = expectedResultFile.readText()
46+
4747
val result = request(code, platform)
4848

49-
verifyResult(result, file)?.run {
49+
verifyResult(result, expectedResultFile)?.run {
5050
val key = "${platform.name}:${file.path.substring(folder.length + 1)}"
5151
badMap[key] = badMap[key].orEmpty() + this
5252
}
@@ -63,4 +63,34 @@ interface BaseResourceCompileTest {
6363
error("Compile tests failed.\nResults:\n$message")
6464
}
6565
}
66+
67+
fun getExpectedResultFile(
68+
platform: ProjectType,
69+
file: File,
70+
folder: String,
71+
badMap: MutableMap<String, String>,
72+
): File? {
73+
val fileName = file.nameWithoutExtension
74+
val expectedHash = FILES_TO_USE_WORKAROUND[fileName]
75+
return if (expectedHash != null) {
76+
val actualHash = md5Hex(file.readText())
77+
if (!actualHash.equals(expectedHash, ignoreCase = true)) {
78+
val key = "${platform.name}:${file.path.substring(folder.length + 1)}"
79+
val msg =
80+
"Workaround hash mismatch. Actual MD5: $actualHash, expected: $expectedHash. Please review the source and update FILES_TO_USE_WORKAROUND entry and/or the *-workaround.json accordingly.\n"
81+
badMap[key] = badMap[key].orEmpty() + msg
82+
null
83+
} else {
84+
File(file.parent.replace("kotlin-web-site", "workaround-examples"), "${fileName}-workaround.kt")
85+
}
86+
} else {
87+
file
88+
}
89+
}
90+
91+
private fun md5Hex(text: String): String {
92+
return MessageDigest.getInstance("MD5")
93+
.digest(text.toByteArray())
94+
.toHexString(HexFormat.Default)
95+
}
6696
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ fun main() {
1616
}
1717
printLength("Incomprehensibilities")
1818
printLength(1000)
19-
printLength(listOf(Any()))
19+
val any = object : Any() {
20+
override fun toString(): String {
21+
return "Any"
22+
}
23+
}
24+
printLength(listOf(any))
2025
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//sampleStart
22
fun getStringLength(obj: Any): Int? {
3-
// `obj` is automatically cast to `String` on the right-hand side of `&&`
4-
if (obj is String && obj.length > 0) {
5-
return obj.length
6-
}
3+
if (obj !is String) return null
74

8-
return null
5+
// `obj` is automatically cast to `String` in this branch
6+
return obj.length
97
}
108
//sampleEnd
119

@@ -14,6 +12,11 @@ fun main() {
1412
println("Getting the length of '$obj'. Result: ${getStringLength(obj) ?: "Error: The object is not a string"} ")
1513
}
1614
printLength("Incomprehensibilities")
17-
printLength("")
1815
printLength(1000)
16+
val any = object : Any() {
17+
override fun toString(): String {
18+
return "Any"
19+
}
20+
}
21+
printLength(listOf(any))
1922
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object MyObject {
2+
override fun toString(): String {
3+
val superString = super.toString()
4+
// MyObject@hashcode
5+
return superString.substringBefore('@')
6+
}
7+
}
8+
9+
fun main() {
10+
println(MyObject)
11+
// MyObject
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fun main(args: Array<String>) {
2+
//sampleStart
3+
val array = arrayOf("a", "b", "c")
4+
println(array.toString().substringBefore('@')) // JVM implementation: type-and-hash gibberish
5+
println(array.contentToString()) // nicely formatted as list
6+
//sampleEnd
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"errors":{"File.kt":[]},"exception":null,"jvmByteCode":null,"text":"<outStream>Getting the length of 'Incomprehensibilities'. Result: 21 \nGetting the length of '1000'. Result: Error: The object is not a string \nGetting the length of '[java.lang.Object@1f17ae12]'. Result: Error: The object is not a string \n</outStream>"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"errors":{"File.kt":[]},"exception":null,"jvmByteCode":null,"text":"<outStream>Getting the length of 'Incomprehensibilities'. Result: 21 \nGetting the length of '1000'. Result: Error: The object is not a string \nGetting the length of '[java.lang.Object@1f17ae12]'. Result: Error: The object is not a string \n</outStream>"}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"exception":null,"text":"<outStream>First property: hello\nFirst initializer block that prints hello\nSecond property: 5\nSecond initializer block that prints 5\n</outStream>","jvmByteCode":null,"errors":{"File.kt":[]}}
1+
{"errors":{"File.kt":[]},"exception":null,"jvmByteCode":null,"text":"<outStream>Alice\n0\n</outStream>"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"errors":{"File.kt":[]},"exception":null,"jvmByteCode":null,"text":"<outStream>Hello, my name is Alice.\nI am Alice, 20 years old, and I study at Engineering University.\n</outStream>"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"errors":{"File.kt":[]},"exception":null,"jvmByteCode":null,"text":"<outStream>Anonymous\n</outStream>"}

0 commit comments

Comments
 (0)