KOMP is a Kotlin library designed to generate instances of @Serializable
classes. It allows for easy setup of data sources for primitive and string types, as well as custom value generators for complex types such as enums.
Disclaimer
This project is a proof of concept and heavily built on the inner behavior of the kotlinx.serialization library; therefore, the correct behavior of this library is not guaranteed.
But even with its limitations, it can be useful for testing purposes.
- Generate instances of
@Serializable
classes. - Customize data sources for primitive and string types.
- Set up custom value generators for complex types like enums.
- Use annotations or direct method calls to generate data.
- Predefine values for specific properties when using direct method calls.
To use KOMP in your project, add the following dependency to your build.gradle.kts
file:
repositories {
maven("https://jitpack.io")
}
dependencies {
implementation("com.github.LasOri:komp:0.1.0")
}
You can set up KOMP by specifying data sources and custom value generators. Here's an example setup:
val testEnumGenerator = object : Generator<Convertible<*, *>> {
override fun generate(): Convertible<*, *> {
val testEnum = TestEnum.entries.toList().random()
return Convertible(testEnum, TestEnum.serializer())
}
}
Komp.setup(
host = this,
intType = IntType.prime,
doubleType = DoubleType.famousConstants,
stringType = StringType.movieQuote,
testEnumGenerator
)
In this setup:
intType
is set to generate prime numbers.
doubleType
is set to generate famous constants.
stringType
is set to generate movie quotes.
testEnumGenerator
is a custom generator for TestEnum values.
You can use the @Kompify
annotation to automatically generate data for a variable:
@Kompify
lateinit var testData: TestData
Alternatively, you can use a direct method call to generate data, with the option to set predefined values for specific properties:
val testData: TestData = komp.kompify(predefinedValues = mapOf(TestData::text to Convertible(expectedValue, String.serializer())))
In this example, the text property of TestData is predefined with the value expectedValue.
Here's a simple example demonstrating how to use KOMP:
// Define a serializable class
@Serializable
data class TestData(val text: String, val number: Int)
// Setup KOMP
Komp.setup(
host = this,
intType = IntType.prime,
stringType = StringType.movieQuote
)
// Generate data using annotation
@Kompify
lateinit var testData: TestData
// Generate data using direct method call
val testData: TestData = komp.kompify(predefinedValues = mapOf(TestData::text to Convertible("Hello, World!", String.serializer())))
This project is licensed under the MIT License - see the LICENSE file for details.