2021-07-17 08:37:58 +00:00
|
|
|
package prog8tests.helpers
|
|
|
|
|
2021-10-29 00:42:10 +00:00
|
|
|
import prog8.compiler.*
|
2021-10-29 03:00:30 +00:00
|
|
|
import prog8.compilerinterface.ErrorReporter
|
2021-10-29 00:42:10 +00:00
|
|
|
import prog8.compilerinterface.ICompilationTarget
|
|
|
|
import prog8.compilerinterface.IErrorReporter
|
2021-10-29 03:00:30 +00:00
|
|
|
import prog8tests.ast.helpers.assumeReadableFile
|
|
|
|
import prog8tests.ast.helpers.outputDir
|
2021-10-10 22:22:04 +00:00
|
|
|
import java.nio.file.Path
|
|
|
|
import kotlin.io.path.name
|
|
|
|
import kotlin.test.assertFalse
|
|
|
|
import kotlin.test.assertTrue
|
2021-07-17 08:37:58 +00:00
|
|
|
|
2021-07-30 15:39:43 +00:00
|
|
|
|
2021-07-17 08:37:58 +00:00
|
|
|
internal fun CompilationResult.assertSuccess(description: String = ""): CompilationResult {
|
|
|
|
assertTrue(success, "expected successful compilation but failed $description")
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
internal fun CompilationResult.assertFailure(description: String = ""): CompilationResult {
|
|
|
|
assertFalse(success, "expected failure to compile but succeeded $description")
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see CompilationResult.assertSuccess
|
|
|
|
* @see CompilationResult.assertFailure
|
|
|
|
*/
|
|
|
|
internal fun compileFile(
|
|
|
|
platform: ICompilationTarget,
|
|
|
|
optimize: Boolean,
|
|
|
|
fileDir: Path,
|
|
|
|
fileName: String,
|
2021-10-29 03:00:30 +00:00
|
|
|
outputDir: Path = prog8tests.ast.helpers.outputDir,
|
2021-10-21 23:25:26 +00:00
|
|
|
errors: IErrorReporter? = null,
|
|
|
|
writeAssembly: Boolean = true
|
2021-07-17 08:37:58 +00:00
|
|
|
) : CompilationResult {
|
|
|
|
val filepath = fileDir.resolve(fileName)
|
|
|
|
assumeReadableFile(filepath)
|
|
|
|
return compileProgram(
|
|
|
|
filepath,
|
|
|
|
optimize,
|
2021-10-21 23:25:26 +00:00
|
|
|
writeAssembly = writeAssembly,
|
2021-07-17 08:37:58 +00:00
|
|
|
slowCodegenWarnings = false,
|
|
|
|
platform.name,
|
2021-10-13 16:16:51 +00:00
|
|
|
sourceDirs = listOf(),
|
2021-10-21 23:25:26 +00:00
|
|
|
outputDir,
|
|
|
|
errors = errors ?: ErrorReporter()
|
2021-07-17 08:37:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a [sourceText] as a String, writes it to a temporary
|
|
|
|
* file and then runs the compiler on that.
|
|
|
|
* @see compileFile
|
|
|
|
*/
|
|
|
|
internal fun compileText(
|
|
|
|
platform: ICompilationTarget,
|
|
|
|
optimize: Boolean,
|
2021-10-21 23:25:26 +00:00
|
|
|
sourceText: String,
|
|
|
|
errors: IErrorReporter? = null,
|
|
|
|
writeAssembly: Boolean = true
|
2021-07-17 08:37:58 +00:00
|
|
|
) : CompilationResult {
|
|
|
|
val filePath = outputDir.resolve("on_the_fly_test_" + sourceText.hashCode().toUInt().toString(16) + ".p8")
|
|
|
|
// we don't assumeNotExists(filePath) - should be ok to just overwrite it
|
|
|
|
filePath.toFile().writeText(sourceText)
|
2021-10-21 23:25:26 +00:00
|
|
|
return compileFile(platform, optimize, filePath.parent, filePath.name, errors=errors, writeAssembly=writeAssembly)
|
2021-07-17 08:37:58 +00:00
|
|
|
}
|