1 /* 2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines.tools 6 7 import java.io.* 8 import kotlin.test.* 9 10 private val OVERWRITE_EXPECTED_OUTPUT = 11 System.getProperty("overwrite.output")?.toBoolean() ?: false // use -Doverwrite.output=true 12 dumpAndCompareWithnull13fun List<ClassBinarySignature>.dumpAndCompareWith(to: File) { 14 if (!to.exists()) { 15 to.parentFile?.mkdirs() 16 to.bufferedWriter().use { dump(to = it) } 17 fail("Expected data file did not exist. Generated: $to") 18 } else { 19 val actual = dump(to = StringBuilder()) 20 assertEqualsToFile(to, actual) 21 } 22 } 23 assertEqualsToFilenull24private fun assertEqualsToFile(to: File, actual: CharSequence) { 25 val actualText = actual.trimTrailingWhitespacesAndAddNewlineAtEOF() 26 val expectedText = to.readText().trimTrailingWhitespacesAndAddNewlineAtEOF() 27 if (expectedText == actualText) return // Ok 28 // Difference 29 if (OVERWRITE_EXPECTED_OUTPUT) { 30 to.writeText(actualText) 31 println("Generated: $to") 32 return // make test pass when overwriting output 33 } 34 // Fail on difference 35 assertEquals( 36 expectedText, 37 actualText, 38 "Actual data differs from file content: ${to.name}\nTo overwrite the expected API rerun with -Doverwrite.output=true parameter\n" 39 ) 40 } 41 trimTrailingWhitespacesAndAddNewlineAtEOFnull42private fun CharSequence.trimTrailingWhitespacesAndAddNewlineAtEOF(): String = 43 this.lineSequence().map { it.trimEnd() }.joinToString(separator = "\n").let { 44 if (it.endsWith("\n")) it else it + "\n" 45 } 46 47 48 private val UPPER_CASE_CHARS = Regex("[A-Z]+") 49