forked from JetBrains/kotlin-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit-tests: Create a simple test runner implementation
- Loading branch information
1 parent
77195e8
commit f26e5b5
Showing
18 changed files
with
550 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
.../compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/TestProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package org.jetbrains.kotlin.backend.konan.lower | ||
|
||
import org.jetbrains.kotlin.backend.common.FileLoweringPass | ||
import org.jetbrains.kotlin.backend.common.IrElementVisitorVoidWithContext | ||
import org.jetbrains.kotlin.backend.konan.KonanBackendContext | ||
import org.jetbrains.kotlin.backend.konan.descriptors.contributedMethods | ||
import org.jetbrains.kotlin.backend.konan.llvm.symbolName | ||
import org.jetbrains.kotlin.descriptors.ClassDescriptor | ||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor | ||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor | ||
import org.jetbrains.kotlin.ir.IrElement | ||
import org.jetbrains.kotlin.ir.declarations.IrClass | ||
import org.jetbrains.kotlin.ir.declarations.IrFile | ||
import org.jetbrains.kotlin.ir.declarations.IrFunction | ||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment | ||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol | ||
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol | ||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid | ||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid | ||
import org.jetbrains.kotlin.name.FqName | ||
|
||
internal class TestProcessor (val context: KonanBackendContext): FileLoweringPass { | ||
|
||
// TODO: Replace with symbols | ||
companion object { | ||
val fqNameTestAnnotation = FqName("kotlin.test.Test") | ||
val fqNameBeforeAnnotation = FqName("kotlin.test.Before") | ||
val fqNameAfterAnnotation = FqName("kotlin.test.After") | ||
val fqNameBeforeClassAnnotation = FqName("kotlin.test.BeforeClass") | ||
val fqNameAfterClassAnnotation = FqName("kotlin.test.AfterClass") | ||
val fqNameIgnoreAnnotation = FqName("kotlin.test.Ignore") | ||
} | ||
|
||
private inner class TestClass(val clazz: IrClassSymbol) { | ||
val testFunctions = mutableListOf<IrFunctionSymbol>() | ||
val beforeFunctions = mutableListOf<IrFunctionSymbol>() | ||
val afterFunctions = mutableListOf<IrFunctionSymbol>() | ||
} | ||
|
||
private inner class TopLevelTestSuite() { | ||
val testFunctions = mutableListOf<IrFunctionSymbol>() | ||
val beforeFunctions = mutableListOf<IrFunctionSymbol>() | ||
val afterFunctions = mutableListOf<IrFunctionSymbol>() | ||
val beforeClassFunctions = mutableListOf<IrFunctionSymbol>() | ||
val afterClassFunctions = mutableListOf<IrFunctionSymbol>() | ||
} | ||
|
||
private inner class AnnotationCollector : IrElementVisitorVoid { | ||
val testClasses = mutableMapOf<IrClassSymbol, TestClass>() | ||
val topLevelTestSuite = TopLevelTestSuite() | ||
|
||
override fun visitElement(element: IrElement) { | ||
element.acceptChildrenVoid(this) | ||
} | ||
|
||
|
||
|
||
// override fun visitClass(declaration: IrClass) { | ||
// val doNotprocess = declaration.descriptor.staticScope.contributedMethods.none { it.testFunction() } | ||
// if (!doNotprocess) { | ||
// testClasses.add(declaration.symbol) | ||
// | ||
// | ||
// | ||
// declaration.acceptChildrenVoid(object:IrElementVisitorVoid{ | ||
// override fun visitElement(element: IrElement) { | ||
// element.acceptChildrenVoid(this) | ||
// } | ||
// | ||
// override fun visitFunction(declaration: IrFunction) { | ||
// processFunction(declaration.descriptor) | ||
// } | ||
// }) | ||
// } | ||
// } | ||
|
||
override fun visitFunction(declaration: IrFunction) { | ||
val descriptor = declaration.descriptor | ||
val symbol = declaration.symbol | ||
|
||
// TODO: Make it smarter | ||
//if (descriptor.test()) testFunctions.add(symbol) | ||
//if (descriptor.before()) beforeFunctions.add(symbol) | ||
//if (descriptor.after()) afterFunctions.add(symbol) | ||
//if (descriptor.beforeClass()) beforeClassFunctions.add(symbol) | ||
//if (descriptor.afterClass()) afterClassFunctions.add(symbol) | ||
|
||
|
||
} | ||
|
||
} | ||
|
||
|
||
override fun lower(irFile: IrFile) { | ||
irFile.acceptChildrenVoid(AnnotationCollector()) | ||
/** | ||
* TODO: for all test classes generate registration... | ||
*/ | ||
} | ||
|
||
private fun FunctionDescriptor.isTestFunction() = | ||
annotations.any { annotation -> | ||
hasTestAnnotation(annotation) | ||
} | ||
|
||
|
||
private fun hasTestAnnotation(annotation: AnnotationDescriptor): Boolean { | ||
return annotation.fqName == fqNameTestAnnotation || | ||
annotation.fqName == fqNameBeforeAnnotation || | ||
annotation.fqName == fqNameAfterAnnotation || | ||
annotation.fqName == fqNameBeforeClassAnnotation || | ||
annotation.fqName == fqNameAfterClassAnnotation | ||
} | ||
|
||
fun FunctionDescriptor.isAfter() = testAnnotationFqName(fqNameAfterAnnotation) | ||
fun FunctionDescriptor.isBefore() = testAnnotationFqName(fqNameBeforeAnnotation) | ||
fun FunctionDescriptor.isAfterClass() = testAnnotationFqName(fqNameAfterClassAnnotation) | ||
fun FunctionDescriptor.isBeforeClass() = testAnnotationFqName(fqNameBeforeClassAnnotation) | ||
fun FunctionDescriptor.isTest() = testAnnotationFqName(fqNameTestAnnotation) | ||
|
||
fun FunctionDescriptor.testAnnotationFqName(fqName:FqName) = annotations.any { it.fqName == fqNameTestAnnotation } | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
.../compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/UnitProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.jetbrains.kotlin.backend.konan.lower | ||
|
||
import org.jetbrains.kotlin.backend.common.FileLoweringPass | ||
import org.jetbrains.kotlin.backend.konan.KonanBackendContext | ||
import org.jetbrains.kotlin.backend.konan.descriptors.contributedMethods | ||
import org.jetbrains.kotlin.descriptors.ClassDescriptor | ||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor | ||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor | ||
import org.jetbrains.kotlin.ir.IrElement | ||
import org.jetbrains.kotlin.ir.declarations.IrClass | ||
import org.jetbrains.kotlin.ir.declarations.IrFile | ||
import org.jetbrains.kotlin.ir.declarations.IrFunction | ||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid | ||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid | ||
import org.jetbrains.kotlin.name.FqName | ||
|
||
internal class UnitProcessor (val context: KonanBackendContext):FileLoweringPass { | ||
val fqNameTestAnnotation = FqName("org.junit.Test") | ||
val fqNameBeforeAnnotation = FqName("org.junit.Before") | ||
val fqNameAfterAnnotation = FqName("org.junit.After") | ||
val fqNameBeforeClassAnnotation = FqName("org.junit.BeforeClass") | ||
val fqNameAfterClassAnnotation = FqName("org.junit.AfterClass") | ||
|
||
override fun lower(irFile: IrFile) { | ||
val testFunction = mutableListOf<FunctionDescriptor>() | ||
val beforeFunction = mutableListOf<FunctionDescriptor>() | ||
val afterFunction = mutableListOf<FunctionDescriptor>() | ||
val beforeClassFunction = mutableListOf<FunctionDescriptor>() | ||
val afterClassFunction = mutableListOf<FunctionDescriptor>() | ||
val testClasses = mutableListOf<ClassDescriptor>() | ||
irFile.acceptChildrenVoid(object: IrElementVisitorVoid { | ||
override fun visitElement(element: IrElement) { | ||
element.acceptChildrenVoid(this) | ||
} | ||
|
||
override fun visitClass(declaration: IrClass) { | ||
val classDescriptor = declaration.descriptor | ||
val doNotprocess = declaration.descriptor.staticScope.contributedMethods.none{it.junitFunction()} | ||
if (!doNotprocess) { | ||
testClasses.add(declaration.descriptor) | ||
declaration.acceptChildrenVoid(object:IrElementVisitorVoid{ | ||
override fun visitElement(element: IrElement) { | ||
element.acceptChildrenVoid(this) | ||
} | ||
|
||
override fun visitFunction(declaration: IrFunction) { | ||
processFunction(declaration.descriptor) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
override fun visitFunction(declaration: IrFunction) { | ||
processFunction(declaration.descriptor) | ||
} | ||
|
||
private fun processFunction(descriptor: FunctionDescriptor) { | ||
when { | ||
descriptor.test() -> testFunction.add(descriptor) | ||
descriptor.before() -> beforeFunction.add(descriptor) | ||
descriptor.after() -> afterFunction.add(descriptor) | ||
descriptor.beforeClass() -> beforeClassFunction.add(descriptor) | ||
descriptor.afterClass() -> afterClassFunction.add(descriptor) | ||
} | ||
} | ||
}) | ||
/** | ||
* TODO: for all test classes generate registration... | ||
*/ | ||
|
||
} | ||
|
||
private fun FunctionDescriptor.junitFunction() = | ||
annotations.any { annotation -> | ||
hasJunitAnnotation(annotation) | ||
} | ||
|
||
|
||
private fun hasJunitAnnotation(annotation: AnnotationDescriptor): Boolean { | ||
return annotation.fqName == fqNameTestAnnotation && | ||
annotation.fqName == fqNameBeforeAnnotation && | ||
annotation.fqName == fqNameAfterAnnotation && | ||
annotation.fqName == fqNameBeforeClassAnnotation && | ||
annotation.fqName == fqNameAfterClassAnnotation | ||
} | ||
|
||
fun FunctionDescriptor.after() = testAnnotationFqName(fqNameAfterAnnotation) | ||
fun FunctionDescriptor.before() = testAnnotationFqName(fqNameBeforeAnnotation) | ||
fun FunctionDescriptor.afterClass() = testAnnotationFqName(fqNameAfterClassAnnotation) | ||
fun FunctionDescriptor.beforeClass() = testAnnotationFqName(fqNameBeforeClassAnnotation) | ||
fun FunctionDescriptor.test() = testAnnotationFqName(fqNameTestAnnotation) | ||
|
||
fun FunctionDescriptor.testAnnotationFqName(fqName:FqName) = annotations.any { it.fqName == fqNameTestAnnotation } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.